Her er et HelloWorld-eksempel, som virker i VC++. Den ORB, jeg bruger, er MICO, som kan findes på
www.mico.org. Der kan sourcen downloades, tillige kan dokumentationen. Deri står bl.a., hvordan man sætter MICO op til VC++.
Jeg har følgende filer:
Fil: hello.idl
---------------------------------------------------
interface HelloWorld
{
string hello();
};
Denne IDL-fil kompileres vha. IDL-compileren til hello.h og hello.cpp
Fil: server.cpp
---------------------------------------------------
#include <fstream.h>
#include "hello.h"
#include <mico/CosNaming.h>
class HelloWorldImpl : virtual public POA_HelloWorld
{
public:
char* hello();
};
// This is the implementation of the method specified in the IDL file
char* HelloWorldImpl::hello()
{
cout << "Received a request from the client" << endl;
return CORBA::string_dup( "Hello World from server" );
}
class Server
{
private:
CORBA::ORB_var orb;
PortableServer::POA_var poa;
PortableServer::POAManager_var mgr;
public:
Server( int argc, char *argv[] );
~Server();
void attach( PortableServer::StaticImplementation* theServantObj );
void run();
};
Server::Server ( int argc, char *argv[] )
{
// Initialize the ORB
orb = CORBA::ORB_init (argc, argv);
// Obtain a reference to the RootPOA and its Manager
CORBA::Object_var poaobj = orb->resolve_initial_references( "RootPOA" );
poa = PortableServer::POA::_narrow( poaobj );
mgr = poa->the_POAManager();
mgr->activate();
}
void Server::attach( PortableServer::StaticImplementation* theServantObj )
{
// Activate the Servant
PortableServer::ObjectId_var oid = poa->activate_object( theServantObj );
// Initialize the Naming Service
CORBA::Object_var nsobj = orb->resolve_initial_references( "NameService" );
CosNaming::NamingContextExt_var nc = CosNaming::NamingContextExt::_narrow( nsobj );
// Initialize the name of the service, "hello.service"
CosNaming::Name name;
name.length( 1 );
name[0].id = CORBA::string_dup( "hello" );
name[0].kind = CORBA::string_dup( "service" );
// Bind the name and object to the Naming Service
nc->bind( name, theServantObj->_this() );
}
void Server::run()
{
cout << "Running...\n" << endl;
orb->run();
}
Server::~Server()
{
// Shutdown (never reached)
poa->destroy( TRUE, TRUE );
}
void main( int argc, char *argv[] )
{
Server* s = new Server( argc, argv );
// Create a Servant object
PortableServer::StaticImplementation* servant = new HelloWorldImpl();
s->attach( servant );
s->run();
// Shutdown (never reached)
delete servant;
}
Fil: client.cpp
---------------------------------------------------
#include "hello.h"
#include <mico/CosNaming.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
void main( int argc, char *argv[] )
{
// Initialize the ORB.
CORBA::ORB_var orb = CORBA::ORB_init( argc, argv );
// Initialize the Naming Service
CORBA::Object_var nsobj = orb->resolve_initial_references( "NameService" );
CosNaming::NamingContext_var nc = CosNaming::NamingContext::_narrow( nsobj );
// Initialize the name of the service, "hello.service"
CosNaming::Name name;
name.length( 1 );
name[0].id = CORBA::string_dup( "hello" );
name[0].kind = CORBA::string_dup( "service" );
// Get the object reference from the Naming Service
HelloWorld_var hello = HelloWorld::_narrow( nc->resolve( name ) );
// Call the remote method
char* result = hello->hello();
cout << "Received result: " << result << endl;
}
Der bliver brugt NamingService, hvorfor NameServeren skal startes. Til det har jeg lavet en bat-fil, som ser således ud (sikkert specifikt for MICO)
nsd -ORBIIOPAddr inet:127.0.0.1:12456
Til start af serveren ser bat-filen sådan ud:
CppServer.exe -ORBNamingAddr inet:127.0.0.1:12456
Til start af klienten ser bat-filen sådan ud:
CppClient.exe -ORBNamingAddr inet:127.0.0.1:12456