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   DB(1,"Shutdown requested.")
00159   _orb->shutdown(1); // Synchronous shutdown
00160   _orb->destroy(); // clean up
00161 }
00162 
00163 
00164 void Orb::deferredRequest(CORBA::Request_ptr req, Callback* callback)
00165 {
00166   omni_mutex_lock l(_deferredRequestsLock);
00167   if(callback)
00168       _deferredRequests.push_front(RequestCallback_t(req,callback));
00169   else
00170       _deferredRequests.push_back(RequestCallback_t(req,NULL));
00171 }
00172 
00173 
00174 void Orb::cancelCallback(const Callback* callback)
00175 {
00176   omni_mutex_lock l(_deferredRequestsLock);
00177   list<RequestCallback_t>::iterator i=_deferredRequests.begin();
00178   // All the callback are at the front, so we can stop when i->second==NULL.
00179   while(i!=_deferredRequests.end() && i->second)
00180   {
00181     if(i->second==callback)
00182        i->second=NULL;
00183     ++i;
00184   }
00185 }
00186 
00187 
00188 void Orb::reportObjectFailure(
00189   const char*       here,
00190   CORBA::Object_ptr obj,
00191   CORBA::Exception* ex
00192 )
00193 {
00194   assert(!CORBA::is_nil(obj));
00195 #ifdef HAVE_OMNIORB4
00196   {
00197     // Hack! The '!' signals object failure.
00198     // See DaemonImpl::log() in daemon_unix.cc.
00199     omniORB::logger log("omniEvents! Object failure: ");
00200     omniIOR* ior =obj->_PR_getobj()->_getIOR();
00201     // Log Repository ID.
00202     log<<ior->repositoryID();
00203     // Log Object ID. (Limitation: only display the first TAG_INTERNET_IOP)
00204     for(CORBA::ULong i=0; i<ior->iopProfiles().length(); i++)
00205     {
00206       if (ior->iopProfiles()[i].tag == IOP::TAG_INTERNET_IOP)
00207       {
00208         IIOP::ProfileBody pBody;
00209         IIOP::unmarshalProfile(ior->iopProfiles()[i],pBody);
00210         log<<" \"";
00211         for(CORBA::ULong j=0; j<pBody.object_key.length(); ++j)
00212         {
00213           char c=(char)pBody.object_key[j];
00214           log<<( (c>=' '&&c<='~')? c: '.' ); // Log object key as text
00215         }
00216         log<<"\" at "<<(const char*)pBody.address.host<<":"<<pBody.address.port;
00217         break; // ONLY DISPLAY FIRST!
00218       }
00219     }
00220     // Log exception.
00221     if(!ex)
00222     {
00223       log<<" threw unknown exception\n";
00224     }
00225     else
00226     {
00227       log<<" threw "<<ex->_name();
00228       CORBA::SystemException* sysex =CORBA::SystemException::_downcast(ex);
00229       if(sysex)
00230           log<<" ("<<NP_MINORSTRING(*sysex)<<")";
00231       log<<"\n";
00232     }
00233   }
00234 #endif
00235   {
00236     omniORB::logger log("omniEvents! Object failure detail: ");
00237     CORBA::String_var sior( Orb::inst()._orb->object_to_string(obj) );
00238     log<<sior<<" at "<<here<<"\n";
00239   }
00240 }
00241 
00242 
00243 //
00244 // Callback
00245 //
00246 
00247 const long Callback::_goodMagic =0xCa11ab1e;
00248 
00249 Callback::~Callback()
00250 {
00251   _magic=0;
00252   // This call can be blocked waiting for a mutex. In the meantime, Orb::run()
00253   // might come along and try to call callback(). If that happens.. BANG!
00254   // This objects virtual call table has already been destroyed, so
00255   // any attempt to call a virtual function will cause a core dump.
00256 
00257   // To prevent this, here we set _magic=0 BEFORE calling cancelCallback(),
00258   // and in Orb::run() we check the value of _magic (by calling isValid())
00259   // BEFORE we try to call callback().
00260   Orb::inst().cancelCallback(this);
00261 }
00262 
00263 
00264 }; // end namespace OmniEvents

Generated on Fri Oct 8 15:52:51 2004 for OmniEvents by doxygen1.2.15