Write the First Corba
Program in VB.Net |
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.vb) |
Imports Org.Omg.CORBA
Namespace demo.hello
Public Class GoodDayImpl
Inherits GoodDayPOA
Private location As String
Public Sub New(ByVal location As String)
Me.location = location
End Sub
Public Overrides Function hello_simple() As String
Return "Hello World, from " + location
End Function
Public Overrides Function hello_wide(ByVal wide_msg As String) As String
Console.WriteLine("The message is: " + wide_msg)
Return "Hello W?rld, from ?1 2 3 0 *&^%$#@!@"
End Function
End Class
End Namespace
|
Create a corba server |
Write the server program: (Server.vb) |
Imports System.IO
Imports Org.Omg.CORBA
Imports Org.Omg.PortableServer
Imports System
Imports Org.Omg.CosNaming
Namespace demo.hello
Public Class Server
Public Shared Sub Main(ByVal args() As String)
Try
' init ORB
Dim orb As orb = orb.Init(args, Nothing)
' init POA
Dim poa As Org.Omg.PortableServer.POA = POAHelper.Narrow(orb.Resolve_Initial_References("RootPOA"))
poa.the_POAManager.Activate()
' create a GoodDay object
Dim goodDayImpl As goodDayImpl = New goodDayImpl("Somewhere")
' create the object reference
Dim obj As Org.Omg.CORBA.Object = poa.Servant_To_Reference(goodDayImpl)
If args.Length = 1 Then
Dim stmWriter As StreamWriter = New StreamWriter(File.Open(args(0), FileMode.Create))
stmWriter.WriteLine(orb.Object_To_String(obj))
stmWriter.Close()
Else
' register server with naming context
Dim nc As NamingContextExt = NamingContextExtHelper.Narrow(orb.Resolve_Initial_References("NameService"))
nc.Bind(nc.To_Name("HelloServer.service"), obj)
End If
' wait for requests
orb.Run()
Catch e As Exception
Console.Error.WriteLine("Msg : " + e.Message)
Console.Error.WriteLine(e.StackTrace)
End Try
End Sub
End Class
End Namespace
|
Create a corba client |
Write the client program: (Client.vb) |
Imports System.IO
Imports Org.Omg.CORBA
Imports Org.Omg.CosNaming
Namespace demo.hello
Public Class Client
Public Shared Sub Main(ByVal args() As String)
' initialize the ORB.
Dim orb As Org.Omg.CORBA.ORB = Org.Omg.CORBA.ORB.Init(args, Nothing)
Try
Dim goodDay As GoodDay
If args.Length = 1 Then
Dim stmReader As StreamReader = New StreamReader(File.Open(args(0), FileMode.Open))
Dim reference As String = stmReader.ReadLine()
stmReader.Close()
' and narrow it to HelloWorld.GoodDay
' if this fails, a BAD_PARAM will be thrown
goodDay = GoodDayHelper.Narrow(orb.String_To_Object(reference))
Else
' get hold of the naming service
Dim nc As NamingContextExt = NamingContextExtHelper.Narrow(orb.Resolve_Initial_References("NameService"))
goodDay = GoodDayHelper.Narrow(nc.Resolve(nc.To_Name("HelloServer.service")))
End If
' 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 ex As Exception
Console.Error.WriteLine("Msg : " + ex.Message)
Console.Error.WriteLine(ex.StackTrace)
End Try
End Sub
End Class
End Namespace
|
How to test |
|
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)
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
5. Compile the Idl file (server.idl)
6. type the following command to compile the IDL script into VB.Net:
<tomorb_home>\dist\net-1.1\js-bin\idl2c.exe -lang VB -I . -d Generated server.idl
or
<tomorb_home>\dist\net-1.1\bin\idl2c.exe -lang VB -I . -d Generated server.idl
7. Compile the source files
vbc /target:exe /out:hello-server.exe Server.vb GoodDayImpl.vb /recurse:Generated\*.vb
/r:TomORB_Based,TomORB,TomORB_CosNaming,System,System.Xml
vbc /target:exe /out:hello-client.exe Client.vb GoodDayImpl.vb /recurse:Generated\*.vb
/r:TomORB_Based,TomORB,TomORB_CosNaming,System,System.Xml
8. Start name service
<tomorb_home>\dist\net-1.1\bin\namesvc.exe c:\NS_Ref
9. run server program in one console with the following command
hello-server.exe
10. 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
|