Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members | File Members

IRObject.cc

Go to the documentation of this file.
00001 //                            Package   : omniIFR
00002 //  IRObject.cc               Created   : 2004/02/22
00003 //                            Author    : Alex Tingle
00004 //
00005 //    Copyright (C) 2004 Alex Tingle.
00006 //
00007 //    This file is part of the omniIFR application.
00008 //
00009 //    omniIFR 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 //    omniIFR 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 #include "IRObject.h"
00024 
00025 #include "Repository.h"
00026 #include "PersistNode.h"
00027 
00028 #include <algorithm>
00029 
00030 #ifdef HAVE_SYS_TYPES_H
00031 #  include <sys/types.h> // getpid
00032 #endif
00033 
00034 #ifdef HAVE_UNISTD_H
00035 #  include <unistd.h>    // getpid
00036 #elif defined(HAVE_PROCESS_H)
00037 # include <process.h>
00038 #endif
00039 
00040 #include <stdio.h>     // sprintf
00041 
00042 namespace Omniifr {
00043 
00046 void getPoaAndOid(
00047   IRObject_impl*                 servant,
00048   PortableServer::POA_var&       poa,
00049   CORBA::String_var&             poaName,
00050   PortableServer::ObjectId_var&  oid,
00051   CORBA::String_var&             oidStr
00052 )
00053 {
00054   using namespace PortableServer;
00055   poa=servant->_default_POA();
00056   poaName=poa->the_name();
00057   try
00058   {
00059     oid=poa->servant_to_id(servant);
00060   }
00061   catch(POA::ServantNotActive& ex)
00062   {
00063     cerr<<"Can't get ObjectID.\nPOA '"<<poaName.in()
00064         <<"' says it is not active."<<endl;
00065   }
00066   catch(POA::WrongPolicy& ex)
00067   {
00068     cerr<<"Can't get ObjectID.\nPOA '"<<poaName.in()
00069         <<"' has wrong policy for servant_to_id()."<<endl;
00070     ::exit(1); // Programming error - so quit.
00071   }
00072 
00073   try
00074   {
00075     oidStr=ObjectId_to_string(oid.in());
00076   }
00077   catch(CORBA::BAD_PARAM& ex)
00078   {
00079     cerr<<"Can't convert ObjectID to string.\n"
00080       "BAD_PARAM" IFELSE_OMNIORB4(": "<<ex.NP_minorString(),) <<endl;
00081   }
00082 }
00083 
00084 //
00085 //
00086 
00087 void IRObject_impl::destroy()
00088 {
00089   bool dependencyPreventsDestruction =true;
00090 
00091   set<const IRObject_impl*> localDeps;
00092   this->dependentObjectSet(localDeps);
00093 
00094   if(localDeps.empty())
00095   {
00096     dependencyPreventsDestruction=false;
00097   }
00098   else
00099   {
00100     set<const IRObject_impl*> localObjs;
00101     this->containedObjectSet(localObjs);
00102 
00103     set<const IRObject_impl*> externalDeps;
00104     set_difference(
00105       localDeps.begin(),localDeps.end(),
00106       localObjs.begin(),localObjs.end(),
00107       inserter(externalDeps,externalDeps.end())
00108     );
00109     if(externalDeps.empty())
00110         dependencyPreventsDestruction=false;
00111   }
00112 
00113   if(dependencyPreventsDestruction)
00114   {
00115     // Specification dictates the following exception:
00116     throw CORBA::BAD_INV_ORDER(
00117       IFELSE_OMNIORB4(omni::BAD_INV_ORDER_DependencyPreventsDestruction,1),
00118       CORBA::COMPLETED_NO
00119     );
00120   }
00121   else
00122   {
00123     this->uncheckedDestroy();
00124     this->deactivateObject();
00125     _remove_ref(); // Kill the constructor's reference to this.
00126   }
00127 }
00128 
00129 PortableServer::POA_ptr IRObject_impl::_default_POA()
00130 {
00131   return PortableServer::POA::_duplicate(Repository_impl::inst()._poa.in());
00132 }
00133 
00134 void IRObject_impl::activateObject()
00135 {
00136   // Generate a new unique ID.
00137   static long  count=0;
00138   static omni_mutex  mutex;
00139   int  mypid =getpid(); // MS VC++6 doesn't have type pid_t!
00140   unsigned long  sec,nsec;
00141   omni_thread::get_time(&sec,&nsec); // More portable than just time().
00142   char buf[128];
00143   {
00144     omni_mutex_lock l(mutex);
00145     sprintf(buf,"%lx.%d.%lx",++count,mypid,sec);
00146   }
00147   activateObjectWithId(buf);
00148 }
00149 
00150 void IRObject_impl::activateObjectWithId(const char* oidStr)
00151 {
00152   using namespace PortableServer;
00153   PortableServer::POA_var poa     =_default_POA();
00154   CORBA::String_var       poaName =poa->the_name();
00155   DB(9,"Activating object "<<poaName.in()<<"/"<<oidStr);
00156   try
00157   {
00158     ObjectId_var oid =string_to_ObjectId(oidStr);
00159     poa->activate_object_with_id(oid.in(),this);
00160     _activated=true;
00161   }
00162   catch(CORBA::BAD_PARAM& ex)
00163   {
00164     cerr<<"Can't activate "<<oidStr<<".\n"
00165       "BAD_PARAM" IFELSE_OMNIORB4(": "<<ex.NP_minorString(),) <<endl;
00166   }
00167   catch(POA::ServantAlreadyActive& ex)
00168   {
00169     cerr<<"Can't activate "<<oidStr<<".\nServant is already active."<<endl;
00170   }
00171   catch(POA::ObjectAlreadyActive& ex)
00172   {
00173     cerr<<"Can't activate "<<oidStr<<".\nObject is already active."<<endl;
00174   }
00175   catch(POA::WrongPolicy& ex)
00176   {
00177     cerr<<"Can't activate "<<oidStr<<".\nPOA '"<<poaName.in()
00178         <<"' has wrong policy for activate_object_with_id()."<<endl;
00179     exit(1); // Programming error - so quit.
00180   }
00181 }
00182 
00183 
00184 void IRObject_impl::deactivateObject()
00185 {
00186   using namespace PortableServer;
00187   POA_var            poa;
00188   CORBA::String_var  poaName;
00189   ObjectId_var       oid;
00190   CORBA::String_var  oidStr;
00191   getPoaAndOid(this,poa,poaName,oid,oidStr);
00192 
00193   try
00194   {
00195     DB(10,"Deactivating object "<<poaName<<"/"<<oidStr.in());
00196     poa->deactivate_object(oid.in());
00197   }
00198   catch(POA::ObjectNotActive& ex)
00199   {
00200     cerr<<"Can't deactivate "<<oidStr<<".\nObject is not active."<<endl;
00201   }
00202   catch(POA::WrongPolicy& ex)
00203   {
00204     cerr<<"Can't deactivate "<<oidStr<<".\nPOA '"<<poaName.in()
00205         <<"' has wrong policy for deactivate_object()."<<endl;
00206     exit(1); // Programming error - so quit.
00207   }
00208 }
00209 
00210 
00211 void IRObject_impl::reincarnate(const PersistNode& node)
00212 {
00213   cerr<<"Failed attempt to reincarnate. Wrong class."<<endl;
00214   node.output(cerr,"Error Node");
00215 }
00216 
00217 
00218 void IRObject_impl::outputOid(ostream &os)
00219 {
00220   using namespace PortableServer;
00221   POA_var            poa;
00222   CORBA::String_var  poaName;
00223   ObjectId_var       oid;
00224   CORBA::String_var  oidStr;
00225   getPoaAndOid(this,poa,poaName,oid,oidStr);
00226 
00227   os<<oidStr.in();
00228 }
00229 
00230 
00231 bool IRObject_impl::checkReadonly() const
00232 {
00233   if(_activated && Repository_impl::inst().readonly())
00234   {
00235     DB(20,"rejected attempt to change readonly repository.")
00236     throw CORBA::NO_PERMISSION();
00237   }
00238 }
00239 
00240 
00241 } // end namespace Omniifr

Generated on Fri Mar 4 13:03:23 2005 for OmniIFR by  doxygen 1.4.1