[omniORB] Object loading on demand
Swayamjit Das
swayam@krdl.org.sg
Fri, 23 Apr 1999 19:28:47 +0800
hi,
I was trying to write a loader that would work for arbitary type of objects.
I came across the following behaviour. I wish to know if this is expected
behaviour. I have attached a piece of code showing the essence of what I am trying
to do. The is very similar to what was posted as an example of loading feature. But
with the following differences.
a. In the server I instantiate a Dummy object instead of an Echo_i object. The IOR
of this dummy object is printed out.
b. When the loader method is called by the ORB, the object that is created is does
not use the key that is passed as a parameter. That is
Echo_i* obj = new Echo_i();
instead of
Echo_i* obj = new Echo_i(key);
I try to do this because in my application the object pointer is obtained by
calling a factory object that is running in another address space.
I observe that the loader method on the server is called "twice" for every run of
the client. Once during the _narrow call , and again during the call to the
echostring method at the client. Should it not be only once ? Is this expected
behaviour ?
Regards
SDas.
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Server.cpp
// Testing code: implementation of echo objects
//
// File : Echo.idl
//
// interface Echo {
// string echoString(in string mesg);
// };
//
// File : Dummy.idl
//
// interface Dummy {
// };
#include <iostream.h>
#include <string.h>
#include <echo.h>
#include <dummy.h>
static CORBA::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) {
return CORBA::string_dup(mesg);
}
static
CORBA::Object_ptr
loader(const omniORB::objectKey& key)
{
cerr << "Loader: create a new echo object." << endl;
Echo_i* obj = new Echo_i();
obj->_obj_is_ready(boa);
return obj->_this();
}
int
main(int argc, char **argv)
{
CORBA::ORB_var orb = CORBA::ORB_init(argc,argv,"omniORB2");
CORBA::BOA_var boa = orb->BOA_init(argc,argv,"omniORB2_BOA");
omniORB::loader::set(loader);
_sk_Dummy *myobj = new _sk_Dummy();
myobj->_obj_is_ready(boa);
{
Dummy_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.cpp
// Testing code: client of echo objects
//
// File : Echo.idl
//
// interface Echo {
// string echoString(in string mesg);
// };
//
#include <iostream.h>
#include <echo.h>
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;
}
try
{
CORBA::String_var echostr;
echostr = e->echoString((const char *)"abcde");
cout << "reply = "<< echostr << endl;
}
catch (const CORBA::COMM_FAILURE&)
{
cerr << "Caught COMM_FAILURE exception. Exit." << endl;
}
return 0;
}