[omniORB] WG: How to return an object reference?
Andreas Groch
groch at promessmontage.de
Thu Jul 7 15:32:08 BST 2005
Skipped content of type multipart/alternative-------------- next part --------------
interface DefaultApi {
void hello();
};
interface ObjectFactory {
DefaultApi getDefaultApi();
void hello();
};
-------------- next part --------------
#include <iostream>
#include "../orb_objfac.hh"
using namespace std;
int main (int argc, char *argv[])
{
try {
// Initialize orb
CORBA::ORB_var orb = CORBA::ORB_init(argc, argv);
// Check arguments
// if (argc != 2) {
// cerr << "Usage: ch3cli IOR_string" << endl;
// throw 0;
// }
// Destringify argv[1]
// CORBA::Object_var obj = orb->string_to_object(argv[1]);
CORBA::Object_var obj = orb->string_to_object("IOR:010000001600000049444c3a4f626a656374466163746f72793a312e30000000010000000000000064000000010102000e0000003139322e3136382e302e3130350038800e000000fed506cd420000168c000000000100000200000000000000080000000100000000545441010000001c00000001000000010001000100000001000105090101000100000009010100");
if (CORBA::is_nil(obj)) {
cerr << "Nil Time reference" << endl;
throw 0;
}
// Try to narrow to Narrow to ObjectFactory
ObjectFactory_var of = ObjectFactory::_narrow(obj);
if (CORBA::is_nil(of)) {
cerr << "Argument is not a ObjectFactory reference" << endl;
throw 0;
}
of->hello();
// getDefaultApi for the 1st time
DefaultApi_var da = of->getDefaultApi();
if (CORBA::is_nil(da)) {
cerr << "Argument is not a DefaultApi" << endl;
throw 0;
}
da->hello();
// getDefaultApi for the 2nd time
DefaultApi_var da2 = of->getDefaultApi();
if (CORBA::is_nil(da2)) {
cerr << "Argument is not a DefaultApi" << endl;
throw 0;
}
da2->hello();
}
catch (const CORBA::Exception & ex) {
cerr << "Uncaught CORBA exception" << endl;
return 1;
}
catch (...) {
return 1;
}
return(0);
}
-------------- next part --------------
#include <iostream>
#include "../orb_objfac.hh"
using namespace std;
//
// Example class implementing IDL interface DefaultApi
//
class DefaultApi_impl: public POA_DefaultApi,
public PortableServer::RefCountServantBase {
private:
// Make sure all instances are built on the heap by making the
// destructor non-public
//virtual ~DefaultApi_i();
public:
// standard constructor
DefaultApi_impl();
virtual ~DefaultApi_impl();
// methods corresponding to defined IDL attributes and operations
void hello();
};
//
// Example implementational code for IDL interface DefaultApi
//
DefaultApi_impl::DefaultApi_impl(){
// add extra constructor code here
}
DefaultApi_impl::~DefaultApi_impl(){
// add extra destructor code here
}
// Methods corresponding to IDL attributes and operations
void DefaultApi_impl::hello(){
cerr << "DefaultApi_impl::hello()" << endl;
}
// End of example implementational code
//
// Example class implementing IDL interface ObjectFactory
//
class ObjectFactory_impl: public POA_ObjectFactory,
public PortableServer::RefCountServantBase {
private:
// Make sure all instances are built on the heap by making the
// destructor non-public
//virtual ~ObjectFactory_impl();
// POA
PortableServer::POA_var m_poa;
public:
// standard constructor
ObjectFactory_impl();
virtual ~ObjectFactory_impl();
// methods corresponding to defined IDL attributes and operations
DefaultApi_ptr getDefaultApi();
void hello();
};
DefaultApi_var defaultApi_ref;
//
// Example implementational code for IDL interface ObjectFactory
//
ObjectFactory_impl::ObjectFactory_impl()
// : m_poa(PortableServer::POA::_duplicate(poa))
{
// add extra constructor code here
}
ObjectFactory_impl::~ObjectFactory_impl(){
// add extra destructor code here
}
// Methods corresponding to IDL attributes and operations
DefaultApi_ptr ObjectFactory_impl::getDefaultApi(){
cerr << "ObjectFactory_impl::getDefaultApi()" << endl;
return defaultApi_ref;
}
void ObjectFactory_impl::hello(){
cerr << "ObjectFactory_impl::hello()" << endl;
}
// End of example implementational code
int main (int argc, char *argv[])
{
try {
// Initalize ORB
CORBA::ORB_var orb = CORBA::ORB_init(argc, argv);
orb = CORBA::ORB_init(argc, argv);
// Get reference to root POA
CORBA::Object_var obj = orb->resolve_initial_references("RootPOA");
PortableServer::POA_var poa = PortableServer::POA::_narrow(obj);
// Activate POA manager
PortableServer::POAManager_var mgr = poa->the_POAManager();
mgr->activate();
// Create the DefaultApi object
DefaultApi_impl* defaultApi_servant = new DefaultApi_impl();
// Create and return object reference
defaultApi_ref = defaultApi_servant->_this();
// Create the ObjectFactory object
ObjectFactory_impl objectFactory_servant;
// Write its stringified reference to stdout
ObjectFactory_var of = objectFactory_servant._this();
CORBA::String_var str = orb->object_to_string(of);
cout << str << endl;
// Accept requests
orb->run();
}
catch (const CORBA::Exception &) {
cerr << "Uncaught CORBA exception" << endl;
return 1;
}
return 0;
}
More information about the omniORB-list
mailing list