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

Orb.cc

Go to the documentation of this file.
00001 //                            Package   : omniEvents
00002 // Orb.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 "Orb.h"
00025 
00026 #ifdef HAVE_IOSTREAM
00027 #  include <iostream>
00028 #else
00029 #  include <iostream.h>
00030 #endif
00031 
00032 #include <stdlib.h>
00033 #include <assert.h>
00034 
00035 #include "Callback.h"
00036 
00037 namespace OmniEvents {
00038 
00039 Orb Orb::_inst;
00040 
00041 
00042 Orb::~Orb()
00043 {
00044   omni_mutex_lock l(_deferredRequestsLock);
00045   list<RequestCallback_t>::iterator curr, next=_deferredRequests.begin();
00046   while(next!=_deferredRequests.end())
00047   {
00048     curr=next++;
00049     CORBA::release(curr->first);
00050     _deferredRequests.erase(curr);
00051   }
00052 }
00053 
00054 
00055 void Orb::resolveInitialReferences()
00056 {
00057   assert(!CORBA::is_nil(_orb));
00058 
00059   const char* action=""; // Use this variable to help report errors.
00060   try
00061   {
00062     CORBA::Object_var obj;
00063 
00064     action="resolve initial reference 'RootPOA'";
00065     obj=_orb->resolve_initial_references("RootPOA");
00066     _RootPOA=PortableServer::POA::_narrow(obj);
00067     if(CORBA::is_nil(_RootPOA))
00068         throw CORBA::OBJECT_NOT_EXIST();
00069 
00070     action="resolve initial reference 'omniINSPOA'";
00071     obj=_orb->resolve_initial_references("omniINSPOA");
00072     _omniINSPOA=PortableServer::POA::_narrow(obj);
00073     if(CORBA::is_nil(_omniINSPOA))
00074         throw CORBA::OBJECT_NOT_EXIST();
00075 
00076     // The naming service is optional.
00077     try
00078     {
00079       action="resolve initial reference 'NameService'";
00080       obj=_orb->resolve_initial_references("NameService");
00081       _NameService=CosNaming::NamingContext::_narrow(obj);
00082     }
00083     catch(CORBA::Exception& ex)
00084     {
00085       DB(1,"Warning - failed to "<<action<<
00086          IFELSE_OMNIORB4(". Exception: "<<ex._name(),"."))
00087     }
00088   
00089 #ifdef HAVE_OMNIORB4
00090     action="resolve initial reference 'POACurrent'";
00091     obj=_orb->resolve_initial_references("POACurrent");
00092     _POACurrent=PortableServer::Current::_narrow(obj);
00093     if(CORBA::is_nil(_POACurrent))
00094         throw CORBA::OBJECT_NOT_EXIST();
00095 #endif
00096 
00097     return;
00098   }
00099   catch(CORBA::ORB::InvalidName& ex) // resolve_initial_references
00100   {
00101     DB(0,"Failed to "<<action<<". InvalidName")
00102   }
00103   catch(CORBA::TRANSIENT& ex) // _narrow()
00104   {
00105     DB(0,"Failed to "<<action<<". TRANSIENT")
00106   }
00107   catch(CORBA::OBJECT_NOT_EXIST& ex) // _narrow()
00108   {
00109     DB(0,"Failed to "<<action<<". OBJECT_NOT_EXIST")
00110   }
00111   catch(CORBA::SystemException& ex)
00112   {
00113     DB(0,"Failed to "<<action<<"."
00114       IF_OMNIORB4(" "<<ex._name()<<" ("<<NP_MINORSTRING(ex)<<")") )
00115   }
00116   catch(CORBA::Exception& ex)
00117   {
00118     DB(0,"Failed to "<<action<<"." IF_OMNIORB4(" "<<ex._name()) )
00119   }
00120   exit(1);
00121 }
00122 
00123 
00124 void Orb::run()
00125 {
00126   while(!_shutdownRequested)
00127   {
00128     omni_thread::sleep(5);
00129     omni_mutex_lock l(_deferredRequestsLock);
00130     DB(20,"Polling "<<_deferredRequests.size()<<" deferred requests.")
00131     list<RequestCallback_t>::iterator curr, next=_deferredRequests.begin();
00132     while(next!=_deferredRequests.end())
00133     {
00134       curr=next++;
00135       if(curr->first->poll_response())
00136       {
00137         CORBA::Environment_ptr env=curr->first->env();// No need to release env.
00138         if(!CORBA::is_nil(env) && env->exception())
00139         {
00140           CORBA::Exception* ex =env->exception(); // No need to free exception.
00141           DB(10,"Deferred call to "<<curr->first->operation()
00142             <<"() got exception" IF_OMNIORB4(<<": "<<ex->_name()))
00143         }
00144         else if(curr->second && curr->second->isValid())
00145         {
00146           DB(15,"Deferred call to "<<curr->first->operation()<<"() returned.")
00147           curr->second->callback(curr->first);
00148         }
00149         else
00150         {
00151           DB(15,"Orphan call to "<<curr->first->operation()<<"() returned.")
00152         }
00153         CORBA::release(curr->first);
00154         _deferredRequests.erase(curr);
00155       }
00156     }
00157   }
00158 }
00159 
00160 
00161 void Orb::deferredRequest(CORBA::Request_ptr req, Callback* callback)
00162 {
00163   omni_mutex_lock l(_deferredRequestsLock);
00164   if(callback)
00165       _deferredRequests.push_front(RequestCallback_t(req,callback));
00166   else
00167       _deferredRequests.push_back(RequestCallback_t(req,NULL));
00168 }
00169 
00170 
00171 void Orb::cancelCallback(const Callback* callback)
00172 {
00173   omni_mutex_lock l(_deferredRequestsLock);
00174   list<RequestCallback_t>::iterator i=_deferredRequests.begin();
00175   // All the callback are at the front, so we can stop when i->second==NULL.
00176   while(i!=_deferredRequests.end() && i->second)
00177   {
00178     if(i->second==callback)
00179        i->second=NULL;
00180     ++i;
00181   }
00182 }
00183 
00184 
00185 void Orb::reportObjectFailure(
00186   const char*       here,
00187   CORBA::Object_ptr obj,
00188   CORBA::Exception* ex
00189 )
00190 {
00191   assert(!CORBA::is_nil(obj));
00192 #ifdef HAVE_OMNIORB4
00193   {
00194     // Hack! The '!' signals object failure.
00195     // See DaemonImpl::log() in daemon_unix.cc.
00196     omniORB::logger log("omniEvents! Object failure: ");
00197     omniIOR* ior =obj->_PR_getobj()->_getIOR();
00198     // Log Repository ID.
00199     log<<ior->repositoryID();
00200     // Log Object ID. (Limitation: only display the first TAG_INTERNET_IOP)
00201     for(CORBA::ULong i=0; i<ior->iopProfiles().length(); i++)
00202     {
00203       if (ior->iopProfiles()[i].tag == IOP::TAG_INTERNET_IOP)
00204       {
00205         IIOP::ProfileBody pBody;
00206         IIOP::unmarshalProfile(ior->iopProfiles()[i],pBody);
00207         log<<" \"";
00208         for(CORBA::ULong j=0; j<pBody.object_key.length(); ++j)
00209         {
00210           char c=(char)pBody.object_key[j];
00211           log<<( (c>=' '&&c<='~')? c: '.' ); // Log object key as text
00212         }
00213         log<<"\" at "<<(const char*)pBody.address.host<<":"<<pBody.address.port;
00214         break; // ONLY DISPLAY FIRST!
00215       }
00216     }
00217     // Log exception.
00218     if(!ex)
00219     {
00220       log<<" threw unknown exception\n";
00221     }
00222     else
00223     {
00224       log<<" threw "<<ex->_name();
00225       CORBA::SystemException* sysex =CORBA::SystemException::_downcast(ex);
00226       if(sysex)
00227           log<<" ("<<NP_MINORSTRING(*sysex)<<")";
00228       log<<"\n";
00229     }
00230   }
00231 #endif
00232   {
00233     omniORB::logger log("omniEvents! Object failure detail: ");
00234     CORBA::String_var sior( Orb::inst()._orb->object_to_string(obj) );
00235     log<<sior<<" at "<<here<<"\n";
00236   }
00237 }
00238 
00239 
00240 //
00241 // Callback
00242 //
00243 
00244 const long Callback::_goodMagic =0xCa11ab1e;
00245 
00246 Callback::~Callback()
00247 {
00248   _magic=0;
00249   // This call can be blocked waiting for a mutex. In the meantime, Orb::run()
00250   // might come along and try to call callback(). If that happens.. BANG!
00251   // This objects virtual call table has already been destroyed, so
00252   // any attempt to call a virtual function will cause a core dump.
00253 
00254   // To prevent this, here we set _magic=0 BEFORE calling cancelCallback(),
00255   // and in Orb::run() we check the value of _magic (by calling isValid())
00256   // BEFORE we try to call callback().
00257   Orb::inst().cancelCallback(this);
00258 }
00259 
00260 
00261 }; // end namespace OmniEvents

Generated on Fri Nov 19 17:42:20 2004 for OmniEvents by doxygen1.2.15