00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include "Servant.h"
00025 #include "Orb.h"
00026
00027 #ifdef HAVE_SYS_TYPES_H
00028 # include <sys/types.h>
00029 #endif
00030
00031 #ifdef HAVE_UNISTD_H
00032 # include <unistd.h>
00033 #elif defined(HAVE_PROCESS_H)
00034 # include <process.h>
00035 #endif
00036
00037 #include <stdio.h>
00038 #include <assert.h>
00039
00040 #ifdef HAVE_IOSTREAM
00041 # include <iostream>
00042 #else
00043 # include <iostream.h>
00044 #endif
00045
00046 #ifdef HAVE_STD_IOSTREAM
00047 using namespace std;
00048 #endif
00049
00050 namespace OmniEvents {
00051
00052
00053 CORBA::Object_ptr createReference(
00054 PortableServer::POA_ptr poa,
00055 const char* repositoryId
00056 )
00057 {
00058 CORBA::String_var oidStr =newUniqueId();
00059
00060 PortableServer::ObjectId_var oid =
00061 PortableServer::string_to_ObjectId(oidStr.in());
00062
00063 CORBA::Object_var obj =
00064 poa->create_reference_with_id(oid.in(),repositoryId);
00065
00066 assert(!CORBA::is_nil(obj));
00067 return obj._retn();
00068 }
00069
00070 char* newUniqueId()
00071 {
00072 static long count=0;
00073 static omni_mutex mutex;
00074 int mypid =getpid();
00075 unsigned long sec,nsec;
00076 omni_thread::get_time(&sec,&nsec);
00077 char buf[128];
00078 {
00079 omni_mutex_lock l(mutex);
00080 sprintf(buf,"%lx.%d.%lx",++count,mypid,sec);
00081 }
00082 return CORBA::string_dup(buf);
00083 }
00084
00085
00086
00087
00088
00089
00090
00091 #if OMNIEVENTS__DEBUG_SERVANT
00092 # define OMNIEVENTS__ADDR "["<<long(this)<<"] "
00093 int Servant::_objectCount =0;
00094 #else
00095 # define OMNIEVENTS__ADDR
00096 #endif
00097
00098
00099 Servant::Servant(PortableServer::POA_ptr poa)
00100 : _poa(PortableServer::POA::_duplicate(poa))
00101 {
00102 #if OMNIEVENTS__DEBUG_SERVANT
00103 ++_objectCount;
00104 DB(21,OMNIEVENTS__ADDR "Servant::Servant() count="<<_objectCount)
00105 #endif
00106 }
00107
00108
00109 Servant::~Servant()
00110 {
00111 #if OMNIEVENTS__DEBUG_SERVANT
00112 --_objectCount;
00113 DB(20,OMNIEVENTS__ADDR "Servant::~Servant() count="<<_objectCount)
00114 #endif
00115 }
00116
00117
00118 PortableServer::POA_ptr Servant::_default_POA()
00119 {
00120 return PortableServer::POA::_duplicate(_poa.in());
00121 }
00122
00123
00124 void Servant::activateObjectWithId(const char* oidStr)
00125 {
00126 using namespace PortableServer;
00127 CORBA::String_var poaName =_poa->the_name();
00128 DB(5,OMNIEVENTS__ADDR "Activating object "<<poaName.in()<<"/"<<oidStr);
00129 try
00130 {
00131 ObjectId_var oid =string_to_ObjectId(oidStr);
00132 _poa->activate_object_with_id(oid.in(),this);
00133 }
00134 catch(CORBA::BAD_PARAM& ex)
00135 {
00136 DB(0,"Can't activate "<<oidStr<<": "
00137 "BAD_PARAM" IF_OMNIORB4(" ("<<NP_MINORSTRING(ex)<<")") )
00138 throw;
00139 }
00140 catch(POA::ServantAlreadyActive& ex)
00141 {
00142 DB(0,"Can't activate "<<oidStr<<": Servant is already active.")
00143 throw;
00144 }
00145 catch(POA::ObjectAlreadyActive& ex)
00146 {
00147 DB(0,"Can't activate "<<oidStr<<": Object is already active.")
00148 throw;
00149 }
00150 catch(POA::WrongPolicy& ex)
00151 {
00152 DB(0,"Can't activate "<<oidStr<<": POA '"<<poaName.in()
00153 <<"' has wrong policy for activate_object_with_id().")
00154 exit(1);
00155 }
00156 }
00157
00158
00159 void Servant::deactivateObject()
00160 {
00161 using namespace PortableServer;
00162 CORBA::String_var poaName =_poa->the_name();
00163
00164 ObjectId_var oid;
00165 try
00166 {
00167 oid=_poa->servant_to_id(this);
00168 }
00169 catch(POA::ServantNotActive& ex)
00170 {
00171 DB(0,"Can't deactivate servant: POA '"<<poaName.in()
00172 <<"' says it is not active.")
00173 return;
00174 }
00175 catch(POA::WrongPolicy& ex)
00176 {
00177 DB(0,"Can't deactivate servant: POA '"<<poaName.in()
00178 <<"' has wrong policy for servant_to_id().")
00179 exit(1);
00180 }
00181
00182 CORBA::String_var oidStr;
00183 try
00184 {
00185 oidStr=ObjectId_to_string(oid.in());
00186 }
00187 catch(CORBA::BAD_PARAM& ex)
00188 {
00189 DB(0,"Can't deactivate servant. ObjectId looks bad: "
00190 "BAD_PARAM" IF_OMNIORB4(" ("<<NP_MINORSTRING(ex)<<")") )
00191 return;
00192 }
00193
00194 try
00195 {
00196 DB(7,OMNIEVENTS__ADDR "Deactivating object "<<poaName<<"/"<<oidStr.in());
00197 _poa->deactivate_object(oid.in());
00198 }
00199 catch(POA::ObjectNotActive& ex)
00200 {
00201 DB(0,"Can't deactivate "<<oidStr<<": Object is not active.")
00202 return;
00203 }
00204 catch(POA::WrongPolicy& ex)
00205 {
00206 DB(0,"Can't deactivate "<<oidStr<<": POA '"<<poaName.in()
00207 <<"' has wrong policy for deactivate_object().")
00208 exit(1);
00209 }
00210 }
00211
00212 };