OmniEvents

Servant.cc

Go to the documentation of this file.
00001 //                            Package   : omniEvents
00002 // Servant.cc                 Created   : 2003/12/04
00003 //                            Author    : Alex Tingle
00004 //
00005 //    Copyright (C) 2003-2005 Alex Tingle.
00006 //
00007 //    This file is part of the omniEvents application.
00008 //
00009 //    omniEvents is free software; you can redistribute it and/or
00010 //    modify it under the terms of the GNU Lesser General Public
00011 //    License as published by the Free Software Foundation; either
00012 //    version 2.1 of the License, or (at your option) any later version.
00013 //
00014 //    omniEvents is distributed in the hope that it will be useful,
00015 //    but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017 //    Lesser General Public License for more details.
00018 //
00019 //    You should have received a copy of the GNU Lesser General Public
00020 //    License along with this library; if not, write to the Free Software
00021 //    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00022 //
00023 
00024 #include "Servant.h"
00025 #include "Orb.h"
00026 
00027 #ifdef HAVE_SYS_TYPES_H
00028 #  include <sys/types.h> // getpid
00029 #endif
00030 
00031 #ifdef HAVE_UNISTD_H
00032 #  include <unistd.h>    // getpid
00033 #elif defined(HAVE_PROCESS_H)
00034 # include <process.h>
00035 #endif
00036 
00037 #include <stdio.h>     // sprintf
00038 #include <cstdlib>
00039 #include <assert.h>
00040 
00041 #ifdef HAVE_IOSTREAM
00042 #  include <iostream>
00043 #else
00044 #  include <iostream.h>
00045 #endif
00046 
00047 #ifdef HAVE_STD_IOSTREAM
00048 using namespace std;
00049 #endif
00050 
00051 namespace OmniEvents {
00052 
00053 
00054 CORBA::Object_ptr createReference(
00055   PortableServer::POA_ptr poa,          // POA to own new object
00056   const char*             repositoryId  // e.g. _tc_ProxyPushSupplier->id()
00057 )
00058 {
00059   CORBA::String_var oidStr =newUniqueId();
00060 
00061   PortableServer::ObjectId_var oid =
00062     PortableServer::string_to_ObjectId(oidStr.in());
00063 
00064   CORBA::Object_var obj =
00065     poa->create_reference_with_id(oid.in(),repositoryId);
00066 
00067   assert(!CORBA::is_nil(obj));
00068   return obj._retn();
00069 }
00070 
00071 char* newUniqueId()
00072 {
00073   static long  count=0;
00074   static omni_mutex  mutex;
00075   int  mypid =getpid(); // MS VC++6 doesn't have type pid_t!
00076   unsigned long  sec,nsec;
00077   omni_thread::get_time(&sec,&nsec); // More portable than just time().
00078   char buf[128];
00079   {
00080     omni_mutex_lock l(mutex);
00081     sprintf(buf,"%lx.%d.%lx",++count,mypid,sec);
00082   }
00083   return CORBA::string_dup(buf);
00084 }
00085 
00086 
00087 //
00088 //  Servant
00089 //
00090 
00091 
00092 #if OMNIEVENTS__DEBUG_SERVANT
00093 #  define OMNIEVENTS__ADDR "["<<long(this)<<"] "
00094 int Servant::_objectCount =0;
00095 #else
00096 #  define OMNIEVENTS__ADDR
00097 #endif
00098 
00099 
00100 Servant::Servant(PortableServer::POA_ptr poa)
00101 : _poa(PortableServer::POA::_duplicate(poa))
00102 {
00103 #if OMNIEVENTS__DEBUG_SERVANT
00104   ++_objectCount;
00105   DB(21,OMNIEVENTS__ADDR "Servant::Servant() count="<<_objectCount)
00106 #endif
00107 }
00108 
00109 
00110 Servant::~Servant()
00111 {
00112 #if OMNIEVENTS__DEBUG_SERVANT
00113   --_objectCount;
00114   DB(20,OMNIEVENTS__ADDR "Servant::~Servant() count="<<_objectCount)
00115 #endif
00116 }
00117 
00118 
00119 PortableServer::POA_ptr Servant::_default_POA()
00120 {
00121   return PortableServer::POA::_duplicate(_poa.in());
00122 }
00123 
00124 
00125 void Servant::activateObjectWithId(const char* oidStr)
00126 {
00127   using namespace PortableServer;
00128   CORBA::String_var poaName =_poa->the_name();
00129   DB(5,OMNIEVENTS__ADDR "Activating object "<<poaName.in()<<"/"<<oidStr);
00130   try
00131   {
00132     ObjectId_var oid =string_to_ObjectId(oidStr);
00133     _poa->activate_object_with_id(oid.in(),this);
00134   }
00135   catch(CORBA::BAD_PARAM& ex)
00136   {
00137     DB(0,"Can't activate "<<oidStr<<": "
00138       "BAD_PARAM" IF_OMNIORB4(" ("<<NP_MINORSTRING(ex)<<")") )
00139     throw;
00140   }
00141   catch(POA::ServantAlreadyActive& ex)
00142   {
00143     DB(0,"Can't activate "<<oidStr<<": Servant is already active.")
00144     throw;
00145   }
00146   catch(POA::ObjectAlreadyActive& ex)
00147   {
00148     DB(0,"Can't activate "<<oidStr<<": Object is already active.")
00149     throw;
00150   }
00151   catch(POA::WrongPolicy& ex)
00152   {
00153     DB(0,"Can't activate "<<oidStr<<": POA '"<<poaName.in()
00154         <<"' has wrong policy for activate_object_with_id().")
00155     exit(1); // Programming error - so quit.
00156   }
00157 }
00158 
00159 
00160 void Servant::deactivateObject()
00161 {
00162   using namespace PortableServer;
00163   CORBA::String_var poaName =_poa->the_name();
00164 
00165   ObjectId_var oid;
00166   try
00167   {
00168     oid=_poa->servant_to_id(this);
00169   }
00170   catch(POA::ServantNotActive& ex)
00171   {
00172     DB(0,"Can't deactivate servant: POA '"<<poaName.in()
00173         <<"' says it is not active.")
00174     return;
00175   }
00176   catch(POA::WrongPolicy& ex)
00177   {
00178     DB(0,"Can't deactivate servant: POA '"<<poaName.in()
00179         <<"' has wrong policy for servant_to_id().")
00180     exit(1); // Programming error - so quit.
00181   }
00182 
00183   CORBA::String_var oidStr;
00184   try
00185   {
00186     oidStr=ObjectId_to_string(oid.in());
00187   }
00188   catch(CORBA::BAD_PARAM& ex)
00189   {
00190     DB(0,"Can't deactivate servant. ObjectId looks bad: "
00191       "BAD_PARAM" IF_OMNIORB4(" ("<<NP_MINORSTRING(ex)<<")") )
00192     return;
00193   }
00194 
00195   try
00196   {
00197     DB(7,OMNIEVENTS__ADDR "Deactivating object "<<poaName<<"/"<<oidStr.in());
00198     _poa->deactivate_object(oid.in());
00199   }
00200   catch(POA::ObjectNotActive& ex)
00201   {
00202     DB(0,"Can't deactivate "<<oidStr<<": Object is not active.")
00203     return;
00204   }
00205   catch(POA::WrongPolicy& ex)
00206   {
00207     DB(0,"Can't deactivate "<<oidStr<<": POA '"<<poaName.in()
00208         <<"' has wrong policy for deactivate_object().")
00209     exit(1); // Programming error - so quit.
00210   }
00211 }
00212 
00213 }; // end namespace OmniEvents