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

EventChannel.cc

Go to the documentation of this file.
00001 //                            Package   : omniEvents
00002 // EventChannel.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 "EventChannel.h"
00025 #include "ConsumerAdmin.h"
00026 #include "SupplierAdmin.h"
00027 #include "Orb.h"
00028 #include "oep_types.h"
00029 #include "defaults.h"
00030 
00031 #include <list>
00032 #include <assert.h>
00033 
00034 namespace OmniEvents {
00035 
00036 // CORBA interface methods
00037 CosEventChannelAdmin::ConsumerAdmin_ptr EventChannel_i::for_consumers()
00038 {
00039   if(!_consumerAdmin)
00040       _consumerAdmin=new ConsumerAdmin_i(*this,_poa);
00041   return _consumerAdmin->_this();
00042 }
00043 
00044 
00045 CosEventChannelAdmin::SupplierAdmin_ptr EventChannel_i::for_suppliers()
00046 {
00047   if(!_supplierAdmin)
00048       _supplierAdmin=new SupplierAdmin_i(*this,_poa);
00049   return _supplierAdmin->_this();
00050 }
00051 
00052 
00053 void EventChannel_i::destroy()
00054 {
00055   _shutdownRequested=true;
00056 }
00057 
00058 
00059 EventChannel_i::EventChannel_i(const char* channelName)
00060 : Servant(PortableServer::POA::_nil()),
00061   _consumerAdmin(NULL),
00062   _supplierAdmin(NULL),
00063   _poaManager(),
00064   _shutdownRequested(false),
00065   _pullRetryPeriod(PULL_RETRY_PERIOD),
00066   _maxQueueLength(MAX_QUEUE_LENGTH)
00067 {
00068   createPoa(channelName);
00069   activateObjectWithId("EventChannel");
00070 }
00071 
00072 
00073 EventChannel_i::EventChannel_i(const char* channelName, const OEP_ecps& ecps)
00074 : Servant(PortableServer::POA::_nil()),
00075   _consumerAdmin(NULL),
00076   _supplierAdmin(NULL),
00077   _poaManager(),
00078   _shutdownRequested(false),
00079   _pullRetryPeriod(ecps.getPullRetryPeriod()),
00080   _maxQueueLength(ecps.getMaxQueueLength())
00081 {
00082   createPoa(channelName);
00083 
00084   // Build Supplier Admin Object
00085   if(!ecps.getSupplierAdmins().empty())
00086   {
00087     OepSapsList::const_iterator sai =ecps.getSupplierAdmins().begin();
00088     _supplierAdmin=new SupplierAdmin_i(*this,_poa,**sai);
00089   }
00090 
00091   // Build Consumer Admin Object
00092   if(!ecps.getConsumerAdmins().empty())
00093   {
00094     OepCapsList::const_iterator cai =ecps.getConsumerAdmins().begin();
00095     _consumerAdmin=new ConsumerAdmin_i(*this,_poa,**cai);
00096   }
00097 
00098   activateObjectWithId("EventChannel");
00099 }
00100 
00101 
00102 EventChannel_i::~EventChannel_i()
00103 {
00104   if(_consumerAdmin)
00105   {
00106     delete _consumerAdmin;
00107     _consumerAdmin=NULL;
00108   }
00109   if(_supplierAdmin)
00110   {
00111     delete _supplierAdmin;
00112     _supplierAdmin=NULL;
00113   }
00114 }
00115 
00116 
00117 void EventChannel_i::run(void* arg)
00118 {
00119   try
00120   {
00121 
00122     _poaManager->activate();
00123     while(!_shutdownRequested)
00124     {
00125       if(_supplierAdmin)
00126       {
00127         _poaManager->hold_requests(CORBA::Boolean(1) /* wait_for_completion */);
00128 
00129         list<CORBA::Any*> events;
00130         _supplierAdmin->collect(events);
00131         if(_consumerAdmin)
00132             _consumerAdmin->send(events);
00133         else
00134             ConsumerAdmin_i::discard(events);
00135         assert(events.empty());
00136 
00137         _poaManager->activate();
00138       }
00139       omni_thread::sleep(0,10000000); // 0.1 second
00140     }
00141     _poaManager->deactivate(
00142       CORBA::Boolean(0) /* etherealize_objects */,
00143       CORBA::Boolean(1) /* wait_for_completion */
00144     );
00145 
00146   }
00147   catch(PortableServer::POAManager::AdapterInactive& ex)
00148   {
00149     cerr<<"POA deactivated from the outside."<<endl;
00150   }
00151   catch(...)
00152   {
00153     cerr<<"Unknown exception."<<endl;
00154   }
00155 
00156   // Thread now exits, and this object is deleted.
00157   // Contents are cleaned up by the destructor.
00158 }
00159 
00160 
00161 void EventChannel_i::output(ostream& os)
00162 {
00163   CORBA::String_var poaName =_poa->the_name();
00164 
00165   os << "\teventChannel\n"
00166      << "\t{\n"
00167      << "\t\tKEY\t" << poaName.in() << "\n"
00168      << "\t\tMAXQUEUELENGTH\t" << _maxQueueLength << "\n"
00169   //??   << "\t\tMAXEVENTSPERCONSUMER\t" << _maxEventsPerConsumer << "\n"
00170      << "\t\tPULLRETRYPERIOD\t" << _pullRetryPeriod << "\n";
00171   if(_supplierAdmin)
00172      _supplierAdmin->output(os);
00173   if(_consumerAdmin)
00174      _consumerAdmin->output(os);
00175   os << "\t}\n";
00176 }
00177 
00178 
00179 void EventChannel_i::createPoa(const char* channelName)
00180 {
00181   using namespace PortableServer;
00182   POA_ptr p=Orb::inst()._RootPOA.in();
00183   try
00184   {
00185     // POLICIES:
00186     //  Lifespan          =PERSISTENT             // we can persist
00187     //  Assignment        =USER_ID                // write our own oid
00188     //  Uniqueness        =[default] UNIQUE_ID    // one servant per object
00189     //  ImplicitActivation=[default] IMPLICIT_ACTIVATION // auto activation
00190     //  RequestProcessing =[default] USE_ACTIVE_OBJECT_MAP_ONLY
00191     //  ServantRetention  =[default] RETAIN       // stateless POA
00192     //  Thread            =SINGLE_THREAD_MODEL    // keep it simple
00193 
00194     CORBA::PolicyList policies;
00195     policies.length(3);
00196     policies[0]=p->create_lifespan_policy(PERSISTENT);
00197     policies[1]=p->create_id_assignment_policy(USER_ID);
00198     policies[2]=p->create_thread_policy(SINGLE_THREAD_MODEL);
00199 
00200     // Create a new POA (and new POAManager) for this channel.
00201     // The POAManager will be used for all of this channel's POAs.
00202     _poa=p->create_POA(channelName,POAManager::_nil(),policies);
00203     _poaManager=_poa->the_POAManager();
00204   }
00205   catch(POA::AdapterAlreadyExists& ex) // create_POA
00206   {
00207     cerr<<"POA::AdapterAlreadyExists"<<endl;
00208   }
00209   catch(POA::InvalidPolicy& ex) // create_POA
00210   {
00211     cerr<<"POA::InvalidPolicy: "<<ex.index<<endl;
00212   }
00213 }
00214 
00215 
00216 }; // end namespace OmniEvents
00217 

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