Write the First Corba
Program in C#
|
Create a IDL script first |
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.cs) |
using Org.Omg.CORBA;
using System;
namespace demo.hello
{
public class GoodDayImpl : GoodDayPOA
{
private string location;
public GoodDayImpl( string location )
{
this.location = location;
}
public override string Hello_Simple()
{
return "Hello World, from " + location;
}
public override string Hello_Wide(string wide_msg)
{
Console.WriteLine("The message is: " + wide_msg );
return "Hello W?rld, from ?1 2 3 0 *&^%$#@!@";
}
}
}
|
Create a corba server |
Write the server program: (Server.cs) |
using Org.Omg.CORBA;
using Org.Omg.PortableServer;
using System;
using System.IO;
using Org.Omg.CosNaming;
namespace demo.hello
{
public class Server
{
public static void Main(string[] args)
{
try
{
//init
ORB ORB orb = ORB.Init( args, null); //init
POA POA poa
= POAHelper.Narrow( orb.Resolve_Initial_References( "RootPOA"));
poa.the_POAManager.Activate(); // create a GoodDay
object GoodDayImpl goodDayImpl = new GoodDayImpl( "Somewhere"); // create the object
reference Org.Omg.CORBA.Object obj = poa.Servant_To_Reference( goodDayImpl);
if ( args.Length = = 1)
{
// write the object reference to args[0]
StreamWriter stmWriter = new StreamWriter(File.Open(
args[0], FileMode.Create)); stmWriter.WriteLine( orb.Object_To_String(
obj));
stmWriter.Close();
}
else
{
naming context NamingContextExt nc
= NamingContextExtHelper.Narrow(
orb.Resolve_Initial_References("NameService")); nc.Bind(
nc.To_Name("HelloServer.example"), obj); } // wait for requests
orb.Run();
}
catch ( Exception e)
{
Console.WriteLine("Msg : " + e.Message);
Console.WriteLine(e.StackTrace);
}
}
}
}
}
|
Create a corba client |
Write the client program: (Client.cs)
|
using Org.Omg.CORBA;
using System;
using System.IO;
using Org.Omg.CosNaming;
namespace demo.hello
{
public class Client
{
public static void Main( string[] args )
{
try
{
// initialize the ORB.
ORB orb = ORB.Init( args, null );
// and narrow it to HelloWorld.GoodDay
// if this fails, a BAD_PARAM will be thrown
GoodDay goodDay;
if( args.Length == 1 )
{
StreamReader stmReader = new StreamReader(File.Open(args[0], FileMode.Open));
string reference = stmReader.ReadLine();
stmReader.Close();
goodDay = GoodDayHelper.Narrow( orb.String_To_Object(reference) );
}
else
{
// get hold of the naming service
NamingContextExt nc = NamingContextExtHelper.Narrow(orb.Resolve_Initial_References("NameService"));
goodDay = GoodDayHelper.Narrow(nc.Resolve(nc.To_Name("HelloServer.example")));
}
// 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 *&^%$#@!@"));
}
catch( Exception ex )
{
Console.WriteLine("Msg : " + ex.Message);
Console.WriteLine( ex.StackTrace );
}
}
}
}
|
How to test |
|
Program Setup
Suppose you are using Win32 platform and have installed .Net Framework v1.1
1. create a working directory
2. copy the <tomorb_home>\config\csharp\tomorb_config.xml
into the working directory and edit the name service reference as
follow:
ORBInitRef.NameService=file://c:/NS_Ref
3. Put all the source codes into this directory
4. Make sure to copy the runtime libraries from the
distribution directory (<tomorb_home>\dist\net-1.1\bin) or register all
TomORB in GAC (a script located in <tomorb_home>\config can help you to
register all required libraries)
5. copy <tomorb home>\dist\net-1.1\bin\TomORB*.* .
or
fire the following command to register strongname libraries into GAC
<tomorb_home>\config\install-net-library.bat
6. Compile the Idl file (server.idl) or use NAnt to fire the build script
Type the following command to compile the IDL script manually:
<tomorb_home>\dist\net-1.1\js-bin\idl2c.exe -lang CS -I . -d Generated server.idl
or
<tomorb_home>\dist\net-1.1\bin\idl2c.exe -lang CS -I . -d Generated server.idl
Compile and test the programs by hand
csc target:exe /out:hello-server.exe Server.cs GoodDayImpl.cs recurse:Generated\*.cs
r:TomORB_Based,TomORB,TomORB_CosNaming,System,System.Xml
csc target:exe out:hello-client.exe Client.cs GoodDayImpl.cs recurse:Generated\*.cs
r:TomORB_Based,TomORB,TomORB_CosNaming,System,System.Xml
run server program in one console with the following command
hello-server.exe
tomorb_home>\dist\net-1.1\bin\namesvc c:\NS_Ref
run client program in another console to test against the server program
hello-client.exe
Compile and test the programs by running the NAnt script
|