Write
the First Corba Program in Chrome
|
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: (GoodDayImpl.pas)
|
namespace demo.hello;
interface
uses
demo.hello;
type
GoodDayImpl = class(GoodDayPOA)
private
location : String;
public
constructor(location : String);
method Hello_Simple() : String; override;
method Hello_Wide( wide_msg : String ) : String; override;
end;
implementation
constructor GoodDayImpl( location : String );
begin
inherited constructor;
self.location := location;
end;
method GoodDayImpl.Hello_Simple() : String;
begin
Result := 'Hello World, from ' + self.location;
end;
method GoodDayImpl.Hello_Wide( wide_msg : String ) : String;
begin
System.Console.WriteLine('The message is: ' + wide_msg );
Result := 'Hello W?rld, from ?1 2 3 0 *&^%$#@!@';
end;
end.
|
Create a corba server
|
Write the server program: (server.pas)
|
namespace demo.hello;
interface
uses
System.IO,Org.Omg.CORBA,Org.Omg.PortableServer,Org.Omg.CosNaming;
type
Server = public class
class method Main();
end;
implementation
class method Server.Main();
var
a : Org.Omg.CORBA.Any;
orb : Org.Omg.CORBA.ORB;
poa : Org.Omg.PortableServer.POA;
goodDayImpl : demo.hello.GoodDayImpl;
obj : Org.Omg.CORBA.Object;
args : array of String;
stmWriter : StreamWriter;
nc : Org.Omg.Cosnaming.NamingContextExt;
begin
args := System.Environment.GetCommandLineArgs();
// initialize the ORB.
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 := new demo.hello.GoodDayImpl('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 := new System.IO.StreamWriter( System.IO.Stream(System.IO.File.Open( args[1], 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;
end.
|
Create a clorba client
|
Write the client program: (Client.pas)
|
namespace demo.hello;
interface
uses
System.IO, Org.Omg.CORBA,Org.Omg.CosNaming,System.Reflection;
type
Client = public class
class method Main();
end;
implementation
class method Client.Main();
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
try
args := System.Environment.GetCommandLineArgs();
// initialize the ORB.
orb := Org.Omg.CORBA.ORB.Init(args, nil);
if high(args) = 1 then
begin
stmReader := new StreamReader(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
Console.WriteLine( goodDay.Hello_Simple() );
// invoke the operation again and print the wide string result
Console.WriteLine( 'wide string: ' + goodDay.Hello_Wide( 'Hello W?rld, from ??1 2 3 0 *&^%$#@!@'));
except
on ex : System.Exception do
begin
Console.WriteLine('Msg : ' + ex.Message);
Console.WriteLine( ex.StackTrace );
end;
end;
end;
end.
|