00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
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
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
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
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) );
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);
00140 }
00141 _poaManager->deactivate(
00142 CORBA::Boolean(0) ,
00143 CORBA::Boolean(1)
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
00157
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
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
00186
00187
00188
00189
00190
00191
00192
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
00201
00202 _poa=p->create_POA(channelName,POAManager::_nil(),policies);
00203 _poaManager=_poa->the_POAManager();
00204 }
00205 catch(POA::AdapterAlreadyExists& ex)
00206 {
00207 cerr<<"POA::AdapterAlreadyExists"<<endl;
00208 }
00209 catch(POA::InvalidPolicy& ex)
00210 {
00211 cerr<<"POA::InvalidPolicy: "<<ex.index<<endl;
00212 }
00213 }
00214
00215
00216 };
00217