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

ProxyPushConsumer.cc

Go to the documentation of this file.
00001 //                            Package   : omniEvents
00002 // ProxyPushConsumer.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 "ProxyPushConsumer.h"
00025 #include "Orb.h"
00026 #include "omniEventsLog.h"
00027 
00028 #include <assert.h>
00029 
00030 #define DB(l,x) ((omniORB::traceLevel >= l) && (cerr << x << endl))
00031 
00032 namespace OmniEvents {
00033 
00034 void ProxyPushConsumer_i::connect_push_supplier(
00035   CosEventComm::PushSupplier_ptr pushSupplier)
00036 {
00037   // pushSupplier is permitted to be nil.
00038   if(CORBA::is_nil(pushSupplier))
00039       return;
00040 
00041   ObjectId oid =currentObjectId();
00042   Connections_t::iterator pos =_connections.find(oid);
00043 
00044   if(pos!=_connections.end())
00045       throw CosEventChannelAdmin::AlreadyConnected();
00046 
00047   _connections.insert(Connections_t::value_type(
00048       oid,
00049       CosEventComm::PushSupplier::_duplicate(pushSupplier)
00050   ));
00051 
00052   omniEventsLog::persist();
00053 }
00054 
00055 
00056 void ProxyPushConsumer_i::disconnect_push_consumer()
00057 {
00058   DB(5,"ProxyPushConsumer_i::disconnect_push_consumer()");
00059   ObjectId oid =currentObjectId();
00060   Connections_t::iterator pos =_connections.find(oid);
00061 
00062   if(pos!=_connections.end())
00063   {
00064     CORBA::Request_var req =
00065       pos->second->_request("disconnect_push_supplier");
00066     req->send_oneway();
00067     _connections.erase(pos);
00068   }
00069 }
00070 
00071 
00072 void ProxyPushConsumer_i::push(const CORBA::Any& event)
00073 {
00074   _queue.push_back(new CORBA::Any(event));
00075 }
00076 
00077 
00078 ProxyPushConsumer_i::ProxyPushConsumer_i(
00079   PortableServer::POA_ptr p,
00080   list<CORBA::Any*>& q
00081 )
00082 : Servant(PortableServer::POA::_nil()),
00083   _connections(),
00084   _queue(q)
00085 {
00086   using namespace PortableServer;
00087   try
00088   {  
00089     // POLICIES:
00090     //  Lifespan          =PERSISTENT             // we can persist
00091     //  Assignment        =USER_ID                // write our own oid
00092     //  Uniqueness        =MULTIPLE_ID            // only one servant
00093     //  ImplicitActivation=NO_IMPLICIT_ACTIVATION // disable auto activation
00094     //  RequestProcessing =USE_DEFAULT_SERVANT    // only one servant
00095     //  ServantRetention  =NON_RETAIN             // stateless POA
00096     //  Thread            =SINGLE_THREAD_MODEL    // keep it simple
00097 
00098     CORBA::PolicyList policies;
00099     policies.length(7);
00100     policies[0]=p->create_lifespan_policy(PERSISTENT);
00101     policies[1]=p->create_id_assignment_policy(USER_ID);
00102     policies[2]=p->create_id_uniqueness_policy(MULTIPLE_ID);
00103     policies[3]=p->create_implicit_activation_policy(NO_IMPLICIT_ACTIVATION);
00104     policies[4]=p->create_request_processing_policy(USE_DEFAULT_SERVANT);
00105     policies[5]=p->create_servant_retention_policy(NON_RETAIN);
00106     policies[6]=p->create_thread_policy(SINGLE_THREAD_MODEL);
00107 
00108     // Create a POA for this proxy type in this channel.
00109     CORBA::String_var parentName  =p->the_name();
00110     string          poaName       =string(parentName.in())+".ProxyPushConsumer";
00111     POAManager_var  parentManager =p->the_POAManager();
00112     _poa=p->create_POA(poaName.c_str(),parentManager.in(),policies);
00113   }
00114   catch(POA::AdapterAlreadyExists& ex) // create_POA
00115   {
00116     cerr<<"POA::AdapterAlreadyExists"<<endl;
00117   }
00118   catch(POA::InvalidPolicy& ex) // create_POA
00119   {
00120     cerr<<"POA::InvalidPolicy: "<<ex.index<<endl;
00121   }
00122   // This object is the POA's default servant.
00123   _poa->set_servant(this);
00124 }
00125 
00126 
00127 ProxyPushConsumer_i::~ProxyPushConsumer_i()
00128 {
00129   // pass (_poa is destroyed by its parent.)
00130 }
00131 
00132 
00133 CosEventChannelAdmin::ProxyPushConsumer_ptr
00134 ProxyPushConsumer_i::createObject()
00135 {
00136   return createNarrowedReference<CosEventChannelAdmin::ProxyPushConsumer>(
00137            _poa,
00138            CosEventChannelAdmin::_tc_ProxyPushConsumer->id()
00139          );
00140 }
00141 
00142 
00143 void ProxyPushConsumer_i::reincarnate(
00144   const char* thisOid,
00145   const char* supplierIOR
00146 )
00147 {
00148   ObjectId oid(thisOid);
00149 
00150   Connections_t::iterator pos =_connections.find(oid);
00151   if(pos!=_connections.end())
00152       throw CosEventChannelAdmin::AlreadyConnected();
00153 
00154   CosEventComm::PushSupplier_var pushSupplier;
00155   try
00156   {
00157     CORBA::Object_ptr obj =Orb::inst()._orb->string_to_object(supplierIOR);
00158     pushSupplier=CosEventComm::PushSupplier::_narrow(obj);
00159   }
00160   catch(...)//??
00161   {
00162   }
00163 
00164   if(CORBA::is_nil(pushSupplier))
00165       throw CORBA::BAD_PARAM();
00166 
00167   _connections.insert(Connections_t::value_type(
00168       oid,
00169       pushSupplier._retn()
00170   ));
00171 }
00172 
00173 
00174 void ProxyPushConsumer_i::output(ostream& os) const
00175 {
00176   for(Connections_t::const_iterator i=_connections.begin();
00177       i!=_connections.end();
00178       ++i)
00179   {
00180     os << "\t\t\tpxyPushConsumer\n"
00181        << "\t\t\t{\n"
00182        << "\t\t\t\tKEY\t" << i->first._str << "\n";
00183     if(!CORBA::is_nil(i->second.in()))
00184     {
00185       CORBA::String_var iorstr;
00186       iorstr = Orb::inst()._orb->object_to_string(i->second.in());
00187       os << "\t\t\t\tIOR\t" << iorstr << "\n";
00188     }
00189     os << "\t\t\t}\n";
00190   }
00191 }
00192 
00193 
00194 ProxyPushConsumer_i::ObjectId
00195 ProxyPushConsumer_i::currentObjectId() const
00196 {
00197   ProxyPushConsumer_i::ObjectId result;
00198   try
00199   {
00200     using namespace PortableServer;
00201     result._oid=Orb::inst()._POACurrent->get_object_id();
00202     CORBA::String_var oidStr =ObjectId_to_string(result._oid.in());
00203     result._str=string(oidStr.in());
00204   }
00205   catch(PortableServer::Current::NoContext ex) // get_object_id()
00206   {
00207     cerr<<"No context!!"<<endl;
00208   }
00209   catch(CORBA::BAD_PARAM ex) // ObjectId_to_string()
00210   {
00211     // Should never get here in omniORB, because ObjectID is a char*.
00212     assert(0);
00213   }
00214   return result;
00215 }
00216 
00217 
00218 //
00219 //  ProxyPushConsumer_i::ObjectId
00220 //
00221 
00222 ProxyPushConsumer_i::ObjectId::ObjectId(const char* str)
00223 : _str(str)
00224 {
00225   _oid=PortableServer::string_to_ObjectId(_str.c_str());
00226 }
00227 
00228 
00229 }; // end namespace OmniEvents

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