Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   File Members  

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 #define DB(l,x) ((omniORB::traceLevel >= l) && (cerr << x << endl))
00041 
00042 namespace OmniEvents {
00043 
00044 CORBA::Object_ptr createReference(
00045   PortableServer::POA_ptr poa,          // POA to own new object
00046   const char*             repositoryId  // e.g. _tc_ProxyPushSupplier->id()
00047 )
00048 {
00049   CORBA::String_var oidStr =newUniqueId();
00050 
00051   PortableServer::ObjectId_var oid =
00052     PortableServer::string_to_ObjectId(oidStr.in());
00053 
00054   CORBA::Object_var obj =
00055     poa->create_reference_with_id(oid.in(),repositoryId);
00056 
00057   assert(!CORBA::is_nil(obj));
00058   return obj._retn();
00059 }
00060 
00061 char* newUniqueId()
00062 {
00063   static long  count=0;
00064   static omni_mutex  mutex;
00065   int  mypid =getpid(); // MS VC++6 doesn't have type pid_t!
00066   unsigned long  sec,nsec;
00067   omni_thread::get_time(&sec,&nsec); // More portable than just time().
00068   char buf[128];
00069   {
00070     omni_mutex_lock l(mutex);
00071     sprintf(buf,"%x.%x.%x",mypid,sec,++count);
00072   }
00073   return CORBA::string_dup(buf);
00074 }
00075 
00076 
00077 //
00078 //  Servant
00079 //
00080 
00081 Servant::Servant(PortableServer::POA_ptr poa)
00082 : _poa(PortableServer::POA::_duplicate(poa))
00083 {
00084   // pass
00085 }
00086 
00087 
00088 Servant::~Servant()
00089 {
00090   // pass
00091 }
00092 
00093 
00094 PortableServer::POA_ptr Servant::_default_POA()
00095 {
00096   return PortableServer::POA::_duplicate(_poa.in());
00097 }
00098 
00099 
00100 void Servant::activateObjectWithId(const char* oidStr)
00101 {
00102   using namespace PortableServer;
00103   CORBA::String_var poaName =_poa->the_name();
00104   DB(5,"Activating object "<<poaName.in()<<"/"<<oidStr);
00105   try
00106   {
00107     ObjectId_var oid =string_to_ObjectId(oidStr);
00108     _poa->activate_object_with_id(oid.in(),this);
00109   }
00110   catch(CORBA::BAD_PARAM& ex)
00111   {
00112     cerr<<"Can't activate "<<oidStr<<".\n"
00113       "BAD_PARAM: "<<ex.NP_minorString()<<endl;
00114   }
00115   catch(POA::ServantAlreadyActive& ex)
00116   {
00117     cerr<<"Can't activate "<<oidStr<<".\nServant is already active."<<endl;
00118   }
00119   catch(POA::ObjectAlreadyActive& ex)
00120   {
00121     cerr<<"Can't activate "<<oidStr<<".\nObject is already active."<<endl;
00122   }
00123   catch(POA::WrongPolicy& ex)
00124   {
00125     cerr<<"Can't activate "<<oidStr<<".\nPOA '"<<poaName.in()
00126         <<"' has wrong policy for activate_object_with_id()."<<endl;
00127     exit(1); // Programming error - so quit.
00128   }
00129 }
00130 
00131 
00132 void Servant::deactivateObject()
00133 {
00134   using namespace PortableServer;
00135   CORBA::String_var poaName =_poa->the_name();
00136 
00137   ObjectId_var oid;
00138   try
00139   {
00140     oid=_poa->servant_to_id(this);
00141   }
00142   catch(POA::ServantNotActive& ex)
00143   {
00144     cerr<<"Can't deactivate servant.\nPOA '"<<poaName.in()
00145         <<"' says it is not active."<<endl;
00146   }
00147   catch(POA::WrongPolicy& ex)
00148   {
00149     cerr<<"Can't deactivate servant.\nPOA '"<<poaName.in()
00150         <<"' has wrong policy for servant_to_id()."<<endl;
00151     exit(1); // Programming error - so quit.
00152   }
00153 
00154   CORBA::String_var oidStr;
00155   try
00156   {
00157     oidStr=ObjectId_to_string(oid.in());
00158   }
00159   catch(CORBA::BAD_PARAM& ex)
00160   {
00161     cerr<<"Can't deactivate servant. ObjectId looks bad.\n"
00162       "BAD_PARAM: "<<ex.NP_minorString()<<endl;
00163   }
00164 
00165   try
00166   {
00167     DB(5,"Deactivating object "<<poaName<<"/"<<oidStr.in());
00168     _poa->deactivate_object(oid.in());
00169   }
00170   catch(POA::ObjectNotActive& ex)
00171   {
00172     cerr<<"Can't deactivate "<<oidStr<<".\nObject is not active."<<endl;
00173   }
00174   catch(POA::WrongPolicy& ex)
00175   {
00176     cerr<<"Can't deactivate "<<oidStr<<".\nPOA '"<<poaName.in()
00177         <<"' has wrong policy for deactivate_object()."<<endl;
00178     exit(1); // Programming error - so quit.
00179   }
00180 }
00181 
00182 Servant::Output Servant::basicOutput(
00183   ostream&                     os,
00184   const char*                  name,
00185   int                          indent,
00186   PortableServer::ServantBase* servant,
00187   CORBA::Object_ptr            target
00188 )
00189 {
00190   string indentStr(indent,'\t');
00191   Output result(os,indentStr);
00192 
00193   PortableServer::ObjectId_var oid=_poa->servant_to_id(servant);
00194   CORBA::String_var oidStr =PortableServer::ObjectId_to_string(oid.in());
00195 
00196   os<<indentStr<<name<<"\n"
00197     <<indentStr<<"{\n"
00198     <<indentStr<<"\tKEY\t" << oidStr.in() << "\n";
00199 
00200   if(!CORBA::is_nil(target))
00201   {
00202     os<<indentStr<<"\tIOR\t";
00203     CORBA::String_var iorstr =Orb::inst()._orb->object_to_string(target);
00204     os << iorstr.in() << "\n";
00205   }
00206   return result;
00207 }
00208 
00209 }; // end namespace OmniEvents

Generated on Fri Dec 12 10:53:03 2003 for OmniEvents by doxygen1.2.15