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-2008, Free Software Foundation, Inc.          --
     --                                                                          --
     -- PolyORB is free software; you  can  redistribute  it and/or modify it    --
     -- under terms of the  GNU General Public License as published by the  Free --
     -- Software Foundation;  either version 2,  or (at your option)  any  later --
     -- version. PolyORB 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 distributed with PolyORB; see file COPYING. If    --
     -- not, write to the Free Software Foundation, 59 Temple Place - Suite 330, --
     -- Boston, MA 02111-1307, USA.                                              --
     --                                                                          --
     --                                                                          --
     --                PolyORB is maintained by ACT Europe.                      --
     --                    (email: sales@act-europe.fr)                          --
     --                                                                          --
     ------------------------------------------------------------------------------
     
     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-2008, Free Software Foundation, Inc.          --
     --                                                                          --
     -- PolyORB is free software; you  can  redistribute  it and/or modify it    --
     -- under terms of the  GNU General Public License as published by the  Free --
     -- Software Foundation;  either version 2,  or (at your option)  any  later --
     -- version. PolyORB 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 distributed with PolyORB; see file COPYING. If    --
     -- not, write to the Free Software Foundation, 59 Temple Place - Suite 330, --
     -- Boston, MA 02111-1307, USA.                                              --
     --                                                                          --
     --                                                                          --
     --                PolyORB is maintained by ACT Europe.                      --
     --                    (email: sales@act-europe.fr)                          --
     --                                                                          --
     ------------------------------------------------------------------------------
     
     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);
        begin
           Ada.Text_IO.Put_Line
             ("Echoing string: « " & CORBA.To_Standard_String (Mesg)
              & " »");
           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: