Next: , Up: Building a CORBA application with PolyORB


6.6.1 echo example

We consider building a simple “Echo” CORBA server and client. This application echoes a string. The source code for this example is located in the examples/corba/echo directory in the PolyORB distribution. This applications uses only basic elements of CORBA.

To build this application, you need the following pieces of code:

  1. IDL definition of an echo object
  2. Implementation code for the echo object
  3. Code for client and server nodes
6.6.1.1 IDL definition of an echo object

This interface defines an echo object with a unique method echoString. Per construction, this method returns its argument.

     

interface Echo { string echoString (in string Mesg); };

6.6.1.2 Implementation code for the echo object

Package Echo.Impl is an implementation of this interface. This implementation follows the IDL-to-Ada mapping.

     
     ------------------------------------------------------------------------------
     --                                                                          --
     --                           POLYORB COMPONENTS                             --
     --                                                                          --
     --                            E C H O . I M P L                             --
     --                                                                          --
     --                                 S p e c                                  --
     --                                                                          --
     --         Copyright (C) 2002-2012, Free Software Foundation, Inc.          --
     --                                                                          --
     -- This is free software;  you can redistribute it  and/or modify it  under --
     -- terms of the  GNU General Public License as published  by the Free Soft- --
     -- ware  Foundation;  either version 3,  or (at your option) any later ver- --
     -- sion.  This software is distributed in the hope  that it will be useful, --
     -- but WITHOUT ANY WARRANTY;  without even the implied warranty of MERCHAN- --
     -- TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public --
     -- License for  more details.                                               --
     --                                                                          --
     -- You should have received a copy of the GNU General Public License and    --
     -- a copy of the GCC Runtime Library Exception along with this program;     --
     -- see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see    --
     -- <http://www.gnu.org/licenses/>.                                          --
     --                                                                          --
     --                  PolyORB is maintained by AdaCore                        --
     --                     (email: sales@adacore.com)                           --
     --                                                                          --
     ------------------------------------------------------------------------------
     
     with CORBA;
     with PortableServer;
     
     package Echo.Impl is
     
        --  My own implementation of echo object.
        --  This is simply used to define the operations.
     
        type Object is new PortableServer.Servant_Base with null record;
     
        type Object_Acc is access Object;
     
        function EchoString
          (Self : access Object;
           Mesg : CORBA.String)
          return CORBA.String;
     
     end Echo.Impl;
     
     
     ------------------------------------------------------------------------------
     --                                                                          --
     --                           POLYORB COMPONENTS                             --
     --                                                                          --
     --                            E C H O . I M P L                             --
     --                                                                          --
     --                                 B o d y                                  --
     --                                                                          --
     --         Copyright (C) 2002-2012, Free Software Foundation, Inc.          --
     --                                                                          --
     -- This is free software;  you can redistribute it  and/or modify it  under --
     -- terms of the  GNU General Public License as published  by the Free Soft- --
     -- ware  Foundation;  either version 3,  or (at your option) any later ver- --
     -- sion.  This software is distributed in the hope  that it will be useful, --
     -- but WITHOUT ANY WARRANTY;  without even the implied warranty of MERCHAN- --
     -- TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public --
     -- License for  more details.                                               --
     --                                                                          --
     -- You should have received a copy of the GNU General Public License and    --
     -- a copy of the GCC Runtime Library Exception along with this program;     --
     -- see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see    --
     -- <http://www.gnu.org/licenses/>.                                          --
     --                                                                          --
     --                  PolyORB is maintained by AdaCore                        --
     --                     (email: sales@adacore.com)                           --
     --                                                                          --
     ------------------------------------------------------------------------------
     
     with Ada.Text_IO;
     
     with Echo.Skel;
     pragma Warnings (Off, Echo.Skel);
     --  No entity from Echo.Skel is referenced.
     
     package body Echo.Impl is
     
        ----------------
        -- EchoString --
        ----------------
     
        function EchoString
          (Self : access Object;
           Mesg : CORBA.String)
          return CORBA.String
        is
           pragma Warnings (Off);
           pragma Unreferenced (Self);
           pragma Warnings (On);
     
           S : String := CORBA.To_Standard_String (Mesg);
           L : Natural := S'Last;
        begin
           if S'Length > 13 then
              L := S'First + 12;
              S (L - 2 .. L) := (others => '.');
           end if;
           Ada.Text_IO.Put_Line
             ("Echoing string: « " & S (S'First .. L) & " »");
           return Mesg;
        end EchoString;
     
     end Echo.Impl;
     

Note: Echo.Impl body requires a dependency on Echo.Skel to ensure the elaboration of skeleton code and the correct setup of PolyORB's internals.

6.6.1.3 Test code for client and server nodes

Client and server code demonstrate how to make a remote invocation on a CORBA object, and how to set up an object on a server node.

Note: the dependency on PolyORB.Setup.Client or PolyORB.Setup.No_Tasking_Server enforces compile-time configuration, see Sample files.

6.6.1.4 Compilation and execution

To compile this demo,

  1. Process the IDL file with idlac (or iac)
               $ idlac echo.idl
    
  2. Compile the client node
               $ gnatmake client.adb `polyorb-config`
    
  3. Compile the server node
               $ gnatmake server.adb `polyorb-config`
    

Note the use of backticks (`). This means that polyorb-config is first executed, and then the command line is replaced with the output of the script, setting up library and include paths and library names.

To run this demo: