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 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 <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,          // POA to own new object
00054   const char*             repositoryId  // e.g. _tc_ProxyPushSupplier->id()
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(); // MS VC++6 doesn't have type pid_t!
00074   unsigned long  sec,nsec;
00075   omni_thread::get_time(&sec,&nsec); // More portable than just time().
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 //  Servant
00087 //
00088 
00089 Servant::Servant(PortableServer::POA_ptr poa)
00090 : _poa(PortableServer::POA::_duplicate(poa))
00091 {
00092   // pass
00093 }
00094 
00095 
00096 Servant::~Servant()
00097 {
00098   // pass
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); // Programming error - so quit.
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); // Programming error - so quit.
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); // Programming error - so quit.
00190   }
00191 }
00192 
00193 }; // end namespace OmniEvents

Generated on Mon Jan 9 03:52:14 2006 for OmniEvents by  doxygen 1.4.6