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 CORBA::Object_ptr createReference(
00053 PortableServer::POA_ptr poa,
00054 const char* repositoryId
00055 )
00056 {
00057 CORBA::String_var oidStr =newUniqueId();
00058
00059 PortableServer::ObjectId_var oid =
00060 PortableServer::string_to_ObjectId(oidStr.in());
00061
00062 CORBA::Object_var obj =
00063 poa->create_reference_with_id(oid.in(),repositoryId);
00064
00065 assert(!CORBA::is_nil(obj));
00066 return obj._retn();
00067 }
00068
00069 char* newUniqueId()
00070 {
00071 static long count=0;
00072 static omni_mutex mutex;
00073 int mypid =getpid();
00074 unsigned long sec,nsec;
00075 omni_thread::get_time(&sec,&nsec);
00076 char buf[128];
00077 {
00078 omni_mutex_lock l(mutex);
00079 sprintf(buf,"%lx.%d.%lx",++count,mypid,sec);
00080 }
00081 return CORBA::string_dup(buf);
00082 }
00083
00084
00085
00086
00087
00088
00089 Servant::Servant(PortableServer::POA_ptr poa)
00090 : _poa(PortableServer::POA::_duplicate(poa))
00091 {
00092
00093 }
00094
00095
00096 Servant::~Servant()
00097 {
00098
00099 }
00100
00101
00102 PortableServer::POA_ptr Servant::_default_POA()
00103 {
00104 return PortableServer::POA::_duplicate(_poa.in());
00105 }
00106
00107
00108 void Servant::activateObjectWithId(const char* oidStr)
00109 {
00110 using namespace PortableServer;
00111 CORBA::String_var poaName =_poa->the_name();
00112 DB(5,"Activating object "<<poaName.in()<<"/"<<oidStr);
00113 try
00114 {
00115 ObjectId_var oid =string_to_ObjectId(oidStr);
00116 _poa->activate_object_with_id(oid.in(),this);
00117 }
00118 catch(CORBA::BAD_PARAM& ex)
00119 {
00120 DB(0,"Can't activate "<<oidStr<<": "
00121 "BAD_PARAM" IF_OMNIORB4(" ("<<NP_MINORSTRING(ex)<<")") )
00122 throw;
00123 }
00124 catch(POA::ServantAlreadyActive& ex)
00125 {
00126 DB(0,"Can't activate "<<oidStr<<": Servant is already active.")
00127 throw;
00128 }
00129 catch(POA::ObjectAlreadyActive& ex)
00130 {
00131 DB(0,"Can't activate "<<oidStr<<": Object is already active.")
00132 throw;
00133 }
00134 catch(POA::WrongPolicy& ex)
00135 {
00136 DB(0,"Can't activate "<<oidStr<<": POA '"<<poaName.in()
00137 <<"' has wrong policy for activate_object_with_id().")
00138 exit(1);
00139 }
00140 }
00141
00142
00143 void Servant::deactivateObject()
00144 {
00145 using namespace PortableServer;
00146 CORBA::String_var poaName =_poa->the_name();
00147
00148 ObjectId_var oid;
00149 try
00150 {
00151 oid=_poa->servant_to_id(this);
00152 }
00153 catch(POA::ServantNotActive& ex)
00154 {
00155 DB(0,"Can't deactivate servant: POA '"<<poaName.in()
00156 <<"' says it is not active.")
00157 }
00158 catch(POA::WrongPolicy& ex)
00159 {
00160 DB(0,"Can't deactivate servant: POA '"<<poaName.in()
00161 <<"' has wrong policy for servant_to_id().")
00162 exit(1);
00163 }
00164
00165 CORBA::String_var oidStr;
00166 try
00167 {
00168 oidStr=ObjectId_to_string(oid.in());
00169 }
00170 catch(CORBA::BAD_PARAM& ex)
00171 {
00172 DB(0,"Can't deactivate servant. ObjectId looks bad: "
00173 "BAD_PARAM" IF_OMNIORB4(" ("<<NP_MINORSTRING(ex)<<")") )
00174 }
00175
00176 try
00177 {
00178 DB(7,"Deactivating object "<<poaName<<"/"<<oidStr.in());
00179 _poa->deactivate_object(oid.in());
00180 }
00181 catch(POA::ObjectNotActive& ex)
00182 {
00183 DB(0,"Can't deactivate "<<oidStr<<": Object is not active.")
00184 }
00185 catch(POA::WrongPolicy& ex)
00186 {
00187 DB(0,"Can't deactivate "<<oidStr<<": POA '"<<poaName.in()
00188 <<"' has wrong policy for deactivate_object().")
00189 exit(1);
00190 }
00191 }
00192
00193 };