ProxyManager.cc

Go to the documentation of this file.
00001 //                            Package   : omniEvents
00002 // ProxyManager.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 "ProxyManager.h"
00025 #include "PersistNode.h"
00026 #include "Orb.h"
00027 #include "omniEventsLog.h"
00028 
00029 #include <string>
00030 #include <map>
00031 #include <assert.h>
00032 #include <memory>
00033 
00034 namespace OmniEvents {
00035 
00036 //
00037 //  ProxyManager
00038 //
00039 
00040 void
00041 ProxyManager::etherealize(
00042   const PortableServer::ObjectId& oid,
00043   PortableServer::POA_ptr         adapter,
00044   PortableServer::Servant         serv,
00045   CORBA::Boolean                  cleanup_in_progress,
00046   CORBA::Boolean                  remaining_activations
00047 )
00048 {
00049   auto_ptr<Proxy> narrowed( dynamic_cast<Proxy*>(serv) );
00050   assert(narrowed.get()!=NULL);
00051   set<Proxy*>::iterator pos =_servants.find(narrowed.get());
00052   if(pos!=_servants.end())
00053       _servants.erase(pos);
00054   else
00055       DB(1,"\t\teh? - POA attempted to etherealize unknown servant.");
00056   // memory freed when narrowed goes out of scope.
00057 }
00058 
00059 
00060 void ProxyManager::reincarnate(const PersistNode& node)
00061 {
00062   // Reincarnate all connections from node's children.
00063   for(map<string,PersistNode*>::const_iterator i=node._child.begin();
00064       i!=node._child.end();
00065       ++i)
00066   {
00067     assert(i->second!=NULL);
00068     PortableServer::Servant serv =
00069       this->incarnate(PortableServer::ObjectId(),_managedPoa);
00070     Proxy* proxy =dynamic_cast<Proxy*>(serv);
00071     assert(proxy!=NULL);
00072     try
00073     {
00074       proxy->reincarnate(i->first,*(i->second));
00075     }
00076     catch(CORBA::BAD_PARAM& ex)
00077     {
00078       // This will happen when IOR fails to narrow.
00079       DB(5,"Failed to reincarnate proxy: "<<i->first.c_str());
00080       _servants.erase(proxy);
00081       delete proxy;
00082     }
00083   }
00084 }
00085 
00086 
00087 void ProxyManager::output(ostream& os)
00088 {
00089   for(set<Proxy*>::iterator i =_servants.begin(); i!=_servants.end(); ++i)
00090   {
00091     (*i)->output(os);
00092   }
00093 }
00094 
00095 
00096 ProxyManager::ProxyManager(PortableServer::POA_ptr p, const char* name)
00097 : Servant(p),
00098   _servants(),
00099   _managedPoa(PortableServer::POA::_nil())
00100 {
00101   using namespace PortableServer;
00102 
00103   // POLICIES:
00104   //  Lifespan          =PERSISTENT             // we can persist
00105   //  Assignment        =USER_ID                // write our own oid
00106   //  Uniqueness        =[default] UNIQUE_ID    // one servant per object
00107   //  ImplicitActivation=NO_IMPLICIT_ACTIVATION // disable auto activation
00108   //  RequestProcessing =USE_SERVANT_MANAGER
00109   //  ServantRetention  =[default] RETAIN
00110   //  Thread            =SINGLE_THREAD_MODEL    // keep it simple
00111 
00112   CORBA::PolicyList policies;
00113   policies.length(5);
00114   policies[0]=p->create_lifespan_policy(PERSISTENT);
00115   policies[1]=p->create_id_assignment_policy(USER_ID);
00116   policies[2]=p->create_implicit_activation_policy(NO_IMPLICIT_ACTIVATION);
00117   policies[3]=p->create_request_processing_policy(USE_SERVANT_MANAGER);
00118   policies[4]=p->create_thread_policy(SINGLE_THREAD_MODEL);
00119 
00120   try
00121   {  
00122     // Create a POA for this proxy type in this channel.
00123     CORBA::String_var parentName    =p->the_name();
00124     string            poaName       =string(parentName.in())+"."+name;
00125     POAManager_var    parentManager =p->the_POAManager();
00126     _managedPoa=p->create_POA(poaName.c_str(),parentManager.in(),policies);
00127   }
00128   catch(POA::AdapterAlreadyExists& ex) // create_POA
00129   {
00130     DB(0,"ProxyManager::ProxyManager() - POA::AdapterAlreadyExists")
00131   }
00132   catch(POA::InvalidPolicy& ex) // create_POA
00133   {
00134     DB(0,"ProxyManager::ProxyManager() - POA::InvalidPolicy: "<<ex.index)
00135   }
00136 
00137   // Destroy the policy objects (Not strictly necessary in omniORB)
00138   for(CORBA::ULong i=0; i<policies.length(); ++i)
00139       policies[i]->destroy();
00140 
00141   string oidStr =string(name)+"Manager";
00142   activateObjectWithId(oidStr.c_str());
00143   _managedPoa->set_servant_manager(_this());
00144 }
00145 
00146 
00147 ProxyManager::~ProxyManager()
00148 {
00149   // pass
00150 }
00151 
00152 
00153 //
00154 // Proxy
00155 //
00156 
00157 
00158 Proxy::~Proxy()
00159 {
00160   if(!CORBA::is_nil(_req))
00161   {
00162     Orb::inst().deferredRequest(_req._retn());
00163     _req=CORBA::Request::_nil();
00164   }
00165 }
00166 
00167 Proxy::Proxy(PortableServer::POA_ptr poa)
00168 : Servant(poa),
00169   _req(CORBA::Request::_nil())
00170 {
00171   // pass
00172 }
00173 
00174 void Proxy::keyOutput(ostream& os, const char* name)
00175 {
00176   PortableServer::POA_var parentPoa=_poa->the_parent();
00177   CORBA::String_var channelName=parentPoa->the_name();
00178 
00179   PortableServer::ObjectId_var oid=_poa->servant_to_id(this);
00180   CORBA::String_var oidStr =PortableServer::ObjectId_to_string(oid.in());
00181   os<<"ecf/"<<channelName.in()<<"/"<<name<<"/"<<oidStr.in();
00182 }
00183 
00184 void Proxy::eraseKey(const char* name)
00185 {
00186   if(omniEventsLog::exists())
00187   {
00188     // Remove this key from the persistency logfile.
00189     WriteLock log;
00190     log.os<<"-";
00191     keyOutput(log.os,name);
00192     log.os<<'\n';
00193   }
00194 }
00195 
00196 void Proxy::basicOutput(
00197   ostream&          os,
00198   const char*       name,
00199   CORBA::Object_ptr target,
00200   const char*       extraAttributes
00201 )
00202 {
00203   keyOutput(os,name);
00204   if(!CORBA::is_nil(target))
00205   {
00206     CORBA::String_var iorstr =Orb::inst()._orb->object_to_string(target);
00207     os<<" IOR="<<iorstr.in();
00208     if(extraAttributes)
00209         os<<extraAttributes;
00210   }
00211   os<<" ;;\n";
00212 }
00213 
00214 
00215 }; // end namespace OmniEvents

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