HelloCpp - A 'Hello' using Visual C++ with CORBA
Description:
Build a small 'hello world' console application:
- Server written in Visual C++
- Client written in Visual C++
Function:
The client calls a function provided by a remote server, with the following
signature 'string hello(string)' and displays the result.
Goal:
Write the CORBA IDL, generate C# code from IDL, and write server & client
application in C++ accessing the generated C# code.
Source:
\Demo\HelloCpp
Example
Step 1. The CORBA IDL
a.) Write the IDL for the remote server (Hello.idl):
module Hello
{
interface Greetings
{
string hello( in string a_strName);
};
};
|
b.) Compile the IDL file to an assembly:
> BrewerCmd.exe -o Hello Hello.idl
|
This creates a C# Shared DLL named Hello.dll.
Step 3. Write the C++ Server
a.) Create a console application.
b.) Add the path '$ESPRESSO_INSTALL_DIR$\bin' to the using directives in the
C++ settings of your project.
c.) Add the location of the shared library to the using directives in the C++
settings of your project.
d.) Write an implementation for the IDL.
1.) Header (HelloSrv.h):
Include the J-Integra® Espresso runtime:
#using <Jintegra.Espresso.dll>
|
Include the new DLL (from Hello.cs):
Declare the implementation class:
__gc class GreetingsImpl: public Hello::GreetingsPOA
{
public:
String* hello( String* a_strName );
};
|
2.) Implementation (HelloSrv.cpp):
Include your header file:
Include the J-Integra® Espresso runtime:
#using <Jintegra.Espresso.dll>
|
Include the new DLL (from Hello.cs):
Implement the 'hello()' member function:
String* GreetingsImpl::hello( String* a_strName )
{
Console::WriteLine("\nFunction 'Hello', Parameter '{0}'\n", a_strName);
return a_strName;
}
|
e.) Write the server.
1.) Header (HelloSrv.h): Include the
J-Integra® Espresso runtime:
#using <Jintegra.Espresso.dll>
|
Declare the server class:
__gc class Server
{
public:
Server();
public:
void run( void );
private:
Ics::CORBA::ORB *m_oOrb;
Ics::PortableServer::POA *m_oRootPOA;
};
|
2.) Implementation (HelloSrv.cpp):
Include your header file:
Include the J-Integra® Espresso runtime:
#using <Jintegra.Espresso.dll>
|
Include the new DLL (from Hello.cs):
Initialize the CORBA ORB using RuntimeConfiguration:
Ics::Runtime::RuntimeConfiguration *rc = new Ics::Runtime::RuntimeConfiguration();
rc->SetApplicationLog(Ics::Runtime::OrbErrorLevel::ERRORS, "SrvLog.log", false);
Ics::CORBA::ORB *m_oOrb = Ics::CORBA::_ORB::init(rc);
|
Retrieve the Object Adaptor (POA) from the ORB:
m_oRootPOA = Ics::PortableServer::POAHelper::narrow( m_oOrb->resolve_initial_references( "RootPOA" ));
|
Activate the POA:
m_oRootPOA->the_POAManager->activate();
|
Instantiate the implementation of the IDL and generate a CORBA object reference
from the implementation object:
Ics::CORBA::Object *obj = m_oRootPOA->servant_to_reference( new GreetingsImpl());
|
Publish the CORBA object reference using a flat file:
Ics::CORBA::_ORB::wrIORtoFile( "c:\\hello.ior", obj);
|
Finally, start the ORBs main event loop:
Step 4. Write the C++ Client
a.) Create a console application.
b.) Add the path '$ESPRESSO_INSTALL_DIR$\bin' to the using directives in the
C++ settings of your project.
c.) Add the location of the shared library to the using directives in the C++
settings of your project.
d.) Write the client (HelloClt.cpp):
Include the J-Integra® Espresso runtime:
#using <Jintegra.Espresso.dll>
|
Include the new DLL (from Hello.cs):
Initialize the CORBA ORB:
Ics::Runtime::RuntimeConfiguration *rc = new Ics::Runtime::RuntimeConfiguration();
rc->SetApplicationLog(Ics::Runtime::OrbErrorLevel::ERRORS, "CltLog.log", false);
Ics::CORBA::ORB *oOrb = Ics::CORBA::_ORB::init(rc);
|
Retrieve the CORBA server object reference from the flat file and downcast
("narrow") the CORBA object to the 'Greetings' interface:
Hello::Greetings *oRefGreetings = Hello::GreetingsHelper::narrow( oOrb->string_to_object( "file://c:\\hello.ior" ) );
|
Finally, call the remote server object:
Console::WriteLine("\nCall server, returns {0}", oRefGreetings->hello("Santa Claus") );
|
Step 5. Run the example
a.) Start the Server.
b.) Start the Client.