CORBA Programming/Echo/Object
The echo object is a simple object with only one method. This method takes a single string parameter and returns that string unchanged.
CORBA IDL definition
editAny CORBA object needs to be defined in CORBA's Interface Definition Language (IDL). The IDL syntax is based on C/C++ including the use of the C preprocessor. Hence we need the usual protection against a double include:
#ifndef __TEST_ECHO_DEFINED
#define __TEST_ECHO_DEFINED
Next we define a module name. While not strictly needed for such a small example it is a good practice never to pollute the global namespace with classes and to use modules instead.
module Test
{
A class is called an "interface" in CORBA IDL. This should not be mixed up with interfaces in Ada or Java. A CORBA interface is the public part of the class — the part of the class you can interface with.
interface Echo
{
As said, we define just one method. It takes a string called Message as input parameter and returns a string as result.
string
Echo_String (
in string Message
);
};
// end interface Echo
};
// end module Test
#endif
Ada Specification
editBefore you download the specification below you should try to call the following PolyORB command. It will generate an implementation template for you.
idlac -i test-echo.idl
Most CORBA implementations will generate specification and implementation template files for the chosen implementation language. Which implementation languages you can choose from depends on your ORB. In our case it is Ada.
with CORBA;
with PortableServer;
The CORBA module (Test) and class (Echo) is mapped to an Ada package hierarchy — this is the normal way to do object oriented programming in Ada. The CORBA language mapping always tries to map close the implementation language. The Echo hierarchy contains several child packages which are fully generated and need no attention. Only the child package "Impl" contains the actual implementation of our Echo class and needs editing.
package Test.Echo.Impl
is
The type Object contains any data our class might want to store. However, all data stored is private, in fact all data in CORBA objects are private.
type Object
is new
PortableServer.Servant_Base
with private;
type Object_Ptr is access Object'Class;
The Echo function is specified as a normal primitive operation to a normal Ada class. If you are interested to know how Ada classes are declared you can read Ada Programming/Object Orientation.
function Echo_String (
-- The Object itself
Self : access Object;
-- The string which should be echoed.
Message : in CORBA.String)
return
-- The returned string - which is the same as Message
CORBA.String;
private
Here, in the private section of the package, we could now declare data members for our class. However our simple echo class needs no data, so we just declare a "null record". If you are interested to know what it would look like with data members you can read Ada Programming/Types/record.
type Object
is new
PortableServer.Servant_Base
with null record;
end Test.Echo.Impl;
Ada Implementation
editNow we know what the CORBA object looks like in Ada we have to actually implement the object. The implementation is a little more extensive than needed — using PolyORB logging facility to produce diagnostic output.
with PolyORB.Log;
with Test.Echo.Skel;
--
-- The following packages are only initialized but not used otherwise.
--
pragma Warnings (Off, Test.Echo.Skel);
--
-- Test.Echo.Skel is only initialized but not used.
--
pragma Elaborate (Test.Echo.Skel);
package body Test.Echo.Impl
is
--
-- a Test Module to test the basic PolyORB functions
--
--------------------------------------------------------------------------------
--
-- Initialize logging from configuration file.
--
package Log is new PolyORB.Log.Facility_Log ("test.echo");
--------------------------------------------------------------------------------
--
-- Log Message when Level is at least equal to the user-requested
-- level for Facility.
--
procedure Put_Line (
Message : in Standard.String;
Level : in PolyORB.Log.Log_Level := PolyORB.Log.Info)
renames
Log.Output;
--------------------------------------------------------------------------------
--
-- Echo the given string back to the client.
--
function Echo_String (
-- The Object itself
Self : access Object;
-- The string which should be echoed.
Message : in CORBA.String)
return
-- The returned string - which is the same as Message
CORBA.String
is
pragma Unreferenced (Self);
begin
Put_Line (
"Echoing string: « " &
CORBA.To_Standard_String (Message) &
" »");
return Message;
end Echo_String;
--------------------------------------------------------------------------------
end Test.Echo.Impl;