Demand-driven object loading (example)
Sai-Lai Lo
S.Lo@orl.co.uk
Tue, 17 Feb 1998 15:57:15 GMT
Here is an example on how to use the dynamic object loading scheme I just
posted.
------------------- server.cc -------------------------------------
// Testing code: implementation of echo objects
//
// interface Echo {
// string echoString(in string mesg);
// };
#include <iostream.h>
#include <string.h>
#include <echo.hh>
static BOA_var boa;
// implementation using inheritance skeleton
class Echo_i : public virtual _sk_Echo {
public:
Echo_i() {}
Echo_i(const omniORB::objectKey& key) : _sk_Echo(key) {}
virtual char * echoString(const char *mesg);
};
char *
Echo_i::echoString(const char *mesg) {
char *p = CORBA::string_alloc(strlen((const char*)mesg));
strcpy((char *)p,(const char *)mesg);
return p;
}
static
CORBA::Object_ptr
loader(const omniORB::objectKey& key)
{
cerr << "Loader: create a new echo object." << endl;
Echo_i* obj = new Echo_i(key);
obj->_obj_is_ready(boa);
return obj->_this();
}
int
main(int argc, char **argv)
{
CORBA::ORB_var orb = CORBA::ORB_init(argc,argv,"omniORB2");
boa = orb->BOA_init(argc,argv,"omniORB2_BOA");
omniORB::loader::set(loader);
CosNaming::Name use_name;
Echo_i *myobj = new Echo_i();
myobj->_obj_is_ready(boa);
{
Echo_var myobjRef = myobj->_this();
CORBA::String_var p = orb->object_to_string(myobjRef);
cerr << (char*)p << endl;
}
// Delete the object, let the loader reinstantiate the object when the
// client calls it.
myobj->_dispose();
boa->impl_is_ready();
return 0;
}
-------------------------------------------------------------------------
------------------- client.cc -------------------------------------------
// Testing code: client of echo objects
//
// interface Echo {
// string echoString(in string mesg);
// };
//
#include <iostream.h>
#include <echo.hh>
int
main (int argc, char **argv)
{
CORBA::ORB_ptr orb = CORBA::ORB_init(argc,argv,"omniORB2");
CORBA::Object_var obj;
if (argc == 2) {
obj = orb->string_to_object(argv[1]);
if (CORBA::is_nil(obj)) {
cerr << "Cannot convert stringified IOR to an object reference."
<< endl;
return 1;
}
}
else {
cerr << "usage: client <stringified IOR>" << endl;
return 1;
}
Echo_var e = Echo::_narrow(obj);
if (CORBA::is_nil(e)) {
cerr << "Can't create proxy.\n" << endl;
return 1;
}
if (e->_non_existent()) {
cerr << "Object does not exist.\n" << endl;
return 1;
}
try {
int loopcount = 10;
while (loopcount--) {
char * echostr;
echostr = e->echoString((char *)"abcde");
cerr << "reply " << echostr << endl;
if (strcmp((const char *)echostr,"abcde")) {
cerr << loopcount << " : echo string differs ('" << "abcde', '"
<< (char *)echostr << "')" << endl;
break;
}
CORBA::string_free(echostr);
}
}
catch (const CORBA::COMM_FAILURE&) {
cerr << "Caught COMM_FAILURE exception. Exit." << endl;
}
return 0;
}
----------------------------------------------------------