Write
the First Corba Program in Delphi
|
Create a IDL script firs
|
Write the IDL Script: (server.idl)
|
module demo
{
module hello {
interface GoodDay {
string hello_simple();
wstring hello_wide( in wstring msg );
};
};
};
|
Complete the implementation of the corba function calls
|
Write the Operation Implementation: (demo.hello.Impl.pas)
|
unit demo.hello.Impl;
interface
uses
demo.hello;
type GoodDayImpl = class(GoodDayPOA)
private
location : String;
public
constructor Create(location : String);
function Hello_Simple() : String; override;
function Hello_Wide( wide_msg : String ) : String; override;
end;
implementation
constructor GoodDayImpl.Create( location : String );
begin
inherited Create();
self.location := location;
end;
function GoodDayImpl.Hello_Simple() : String;
begin
Result := 'Hello World, from ' + self.location;
end;
function GoodDayImpl.Hello_Wide( wide_msg : String ) : String;
begin
writeln('The message is: ' + wide_msg );
Result := 'Hello W?rld, from ?1 2 3 0 *&^%$#@!@';
end;
end.
|
Create a corba server
|
Write the server program: (hello-server.dpr)
|
program helloserver;
{$APPTYPE CONSOLE}
uses
SysUtils,
System.IO,
Org.Omg.CORBA,
Org.Omg.PortableServer,
Org.Omg.CosNaming,
demo.hello,
demo.hello.impl;
var
a : Org.Omg.CORBA.Any;
orb : Org.Omg.CORBA.ORB;
poa : Org.Omg.PortableServer.POA;
goodDayImpl : demo.hello.impl.GoodDayImpl;
obj : Org.Omg.CORBA.Object;
args : array of String;
stmWriter : StreamWriter;
nc : Org.Omg.Cosnaming.NamingContextExt;
begin
{ TBD -oUser -cConsole Main : Insert code here }
//init ORB
SetLength(args, 0);
orb := Org.Omg.CORBA.ORB.Init( args, nil );
//init POA
poa := Org.Omg.PortableServer.POAHelper.Narrow( orb.Resolve_Initial_References('RootPOA' ));
poa.the_POAManager.Activate();
// create a GoodDay object
goodDayImpl := demo.hello.impl.GoodDayImpl.Create('Somewhere' );
// create the object reference
obj := poa.Servant_To_Reference( goodDayImpl );
if high(args) = 1 then
begin
// write the object reference to args[0]
stmWriter := System.IO.StreamWriter.Create( System.IO.Stream(System.IO.File.Open( args[0], FileMode.Create)) );
stmWriter.WriteLine( orb.Object_To_String( obj ));
stmWriter.Close();
end
else
begin
// register server with naming context
nc := NamingContextExtHelper.Narrow(orb.Resolve_Initial_References('NameService'));
nc.Bind( nc.To_Name('HelloServer.example'), obj);
end;
// wait for requests
orb.Run();
end.
|
Create a clorba client
|
Write the client program: (hello-client.dpr)
|
program helloclient;
{$APPTYPE CONSOLE}
uses
SysUtils,
System.IO,
Org.Omg.CORBA,
demo.hello,
Org.Omg.CosNaming,
Org.CorbaTech.TomORB.Util,
System.Reflection;
var
orb : Org.Omg.CORBA.ORB;
goodDay : demo.hello.GoodDay;
stmReader : StreamReader;
reference : String;
nc : Org.Omg.CosNaming.NamingContextExt;
args : array of String;
obj : Org.Omg.CORBA.Object;
begin
{ TBD -oUser -cConsole Main : Insert code here }
try
// initialize the ORB.
SetLength(args, 0);
orb := Org.Omg.CORBA.ORB.Init(args, nil);
if( high(args) = 1 ) then
begin
stmReader := StreamReader.Create(System.IO.File.Open(args[1], FileMode.Open));
reference := stmReader.ReadLine();
stmReader.Close();
goodDay := demo.hello.GoodDayHelper.Narrow( orb.String_To_Object(reference) );
end
else
begin
// get hold of the naming service
obj := orb.Resolve_Initial_References( 'NameService');
nc := Org.Omg.CosNaming.NamingContextExtHelper.Narrow( obj );
obj := nc.Resolve(nc.To_Name('HelloServer.example'));
goodDay := demo.hello.GoodDayHelper.Narrow(obj);
end;
// invoke the operation and print the result
writeln( goodDay.Hello_Simple() );
// invoke the operation again and print the wide string result
writeln( 'wide string: ' + goodDay.Hello_Wide( 'Hello W?rld, from ??1 2 3 0 *&^%$#@!@'));
except
on ex : System.Exception do
begin
writeln('Msg : ' + ex.Message);
writeln( ex.StackTrace );
end;
end;
end.
|