00001
00002
00003 #include "orbsvcs/Notify/MonitorControlExt/MonitorEventChannelFactory.h"
00004 #include "orbsvcs/Notify/MonitorControlExt/NotifyMonitoringExtC.h"
00005 #include "orbsvcs/Notify/MonitorControlExt/MonitorEventChannel.h"
00006 #include "orbsvcs/Notify/MonitorControl/Dynamic_Statistic.h"
00007 #include "orbsvcs/Notify/Properties.h"
00008 #include "orbsvcs/Notify/Builder.h"
00009
00010 #include "ace/Monitor_Point_Registry.h"
00011
00012 #if defined (TAO_HAS_MONITOR_FRAMEWORK) && (TAO_HAS_MONITOR_FRAMEWORK == 1)
00013
00014 TAO_BEGIN_VERSIONED_NAMESPACE_DECL
00015
00016
00017
00018
00019
00020 class EventChannels
00021 : public TAO_Dynamic_Statistic<TAO_MonitorEventChannelFactory>
00022 {
00023 public:
00024 EventChannels (TAO_MonitorEventChannelFactory* ecf,
00025 const ACE_CString& name,
00026 Monitor_Control_Types::Information_Type type,
00027 bool active)
00028 : TAO_Dynamic_Statistic<TAO_MonitorEventChannelFactory> (ecf,
00029 name.c_str (),
00030 type),
00031 active_ (active)
00032 {
00033 }
00034
00035 virtual void update (void)
00036 {
00037 if (this->type () == Monitor_Control_Types::MC_LIST)
00038 {
00039 Monitor_Control_Types::NameList names;
00040 this->interf_->get_ecs (&names, this->active_);
00041 this->receive (names);
00042 }
00043 else
00044 {
00045 this->receive (this->interf_->get_ecs (0, this->active_));
00046 }
00047 }
00048
00049 private:
00050 bool active_;
00051 };
00052
00053
00054
00055
00056
00057 TAO_MonitorEventChannelFactory::TAO_MonitorEventChannelFactory (
00058 const char* name)
00059 : name_ (name)
00060 {
00061 if (name != 0)
00062 {
00063 ACE_CString dir_name (this->name_ + "/");
00064 ACE_CString stat_name (dir_name +
00065 NotifyMonitoringExt::ActiveEventChannelCount);
00066
00067 EventChannels* event_channels = 0;
00068 ACE_NEW (event_channels,
00069 EventChannels (this,
00070 stat_name,
00071 Monitor_Control_Types::MC_NUMBER,
00072 true));
00073
00074 event_channels->add_to_registry ();
00075 event_channels->remove_ref ();
00076
00077 this->stat_names_.push_back (stat_name);
00078
00079 stat_name = dir_name + NotifyMonitoringExt::InactiveEventChannelCount;
00080 event_channels = 0;
00081 ACE_NEW (event_channels,
00082 EventChannels (this,
00083 stat_name,
00084 Monitor_Control_Types::MC_NUMBER,
00085 false));
00086
00087 event_channels->add_to_registry ();
00088 event_channels->remove_ref ();
00089
00090 this->stat_names_.push_back (stat_name);
00091
00092 stat_name = dir_name + NotifyMonitoringExt::ActiveEventChannelNames;
00093 event_channels = 0;
00094 ACE_NEW (event_channels,
00095 EventChannels (this,
00096 stat_name,
00097 Monitor_Control_Types::MC_LIST,
00098 true));
00099
00100 event_channels->add_to_registry ();
00101 event_channels->remove_ref ();
00102
00103 this->stat_names_.push_back (stat_name);
00104
00105 stat_name = dir_name + NotifyMonitoringExt::InactiveEventChannelNames;
00106 event_channels = 0;
00107 ACE_NEW (event_channels,
00108 EventChannels (this,
00109 stat_name,
00110 Monitor_Control_Types::MC_LIST,
00111 false));
00112
00113 event_channels->add_to_registry ();
00114 event_channels->remove_ref ();
00115
00116 this->stat_names_.push_back (stat_name);
00117
00118 stat_name = dir_name + NotifyMonitoringExt::EventChannelCreationTime;
00119 Monitor_Base* timestamp = 0;
00120 ACE_NEW (timestamp,
00121 Timestamp_Monitor (stat_name.c_str ()));
00122
00123 ACE_Time_Value tv (ACE_OS::gettimeofday());
00124 timestamp->receive (tv.sec () + (tv.usec () / 1000000.0));
00125
00126 timestamp->add_to_registry ();
00127 timestamp->remove_ref ();
00128
00129 this->stat_names_.push_back (stat_name);
00130
00131 Monitor_Point_Registry* instance =
00132 Monitor_Point_Registry::instance ();
00133
00134 ACE_WRITE_GUARD (TAO_SYNCH_RW_MUTEX, guard, this->mutex_);
00135
00136 Monitor_Base* names =
00137 instance->get (NotifyMonitoringExt::EventChannelFactoryNames);
00138
00139 if (names == 0)
00140 {
00141 stat_name = NotifyMonitoringExt::EventChannelFactoryNames;
00142 ACE_NEW_THROW_EX (names,
00143 Monitor_Base (stat_name.c_str (),
00144 Monitor_Control_Types::MC_LIST),
00145 CORBA::NO_MEMORY ());
00146 names->add_to_registry ();
00147 }
00148
00149 Monitor_Control_Types::NameList list = names->get_list ();
00150 list.push_back (this->name_);
00151 names->receive (list);
00152 names->remove_ref ();
00153 }
00154 }
00155
00156 TAO_MonitorEventChannelFactory::~TAO_MonitorEventChannelFactory (void)
00157 {
00158 Monitor_Point_Registry* instance = Monitor_Point_Registry::instance ();
00159 size_t size = this->stat_names_.size ();
00160
00161 for (size_t i = 0; i < size; ++i)
00162 {
00163 instance->remove (this->stat_names_[i].c_str ());
00164 }
00165 }
00166
00167 CosNotifyChannelAdmin::EventChannel_ptr
00168 TAO_MonitorEventChannelFactory::create_named_channel (
00169 const CosNotification::QoSProperties& initial_qos,
00170 const CosNotification::AdminProperties& initial_admin,
00171 CosNotifyChannelAdmin::ChannelID_out id,
00172 const char* name)
00173 {
00174 if (ACE_OS::strlen (name) == 0)
00175 {
00176 throw NotifyMonitoringExt::NameMapError ();
00177 }
00178
00179 ACE_CString sname (this->name_ + "/");
00180 sname += name;
00181
00182 ACE_WRITE_GUARD_RETURN (TAO_SYNCH_RW_MUTEX, guard, this->mutex_, 0);
00183
00184
00185 if (this->map_.find (sname) == 0)
00186 {
00187 throw NotifyMonitoringExt::NameAlreadyUsed ();
00188 }
00189
00190 CosNotifyChannelAdmin::EventChannel_var ec =
00191 TAO_Notify_Properties::instance ()->builder ()->build_event_channel (
00192 this,
00193 initial_qos,
00194 initial_admin,
00195 id,
00196 sname.c_str ());
00197
00198 if (CORBA::is_nil (ec.in ()))
00199 {
00200 return CosNotifyChannelAdmin::EventChannel::_nil ();
00201 }
00202
00203
00204 if (this->map_.bind (sname, id) != 0)
00205 {
00206 throw NotifyMonitoringExt::NameMapError ();
00207 }
00208
00209
00210
00211 Unbinder unbinder (this->map_, sname);
00212
00213 this->self_change ();
00214
00215 unbinder.release ();
00216 return ec._retn ();
00217 }
00218
00219 CosNotifyChannelAdmin::EventChannel_ptr
00220 TAO_MonitorEventChannelFactory::create_channel (
00221 const CosNotification::QoSProperties& initial_qos,
00222 const CosNotification::AdminProperties& initial_admin,
00223 CosNotifyChannelAdmin::ChannelID_out id)
00224 {
00225 CosNotifyChannelAdmin::EventChannel_var ec =
00226 this->TAO_Notify_EventChannelFactory::create_channel (initial_qos,
00227 initial_admin,
00228 id);
00229
00230 if (CORBA::is_nil (ec.in ()))
00231 {
00232 return CosNotifyChannelAdmin::EventChannel::_nil ();
00233 }
00234
00235
00236 TAO_MonitorEventChannel* mec =
00237 dynamic_cast<TAO_MonitorEventChannel*> (ec->_servant ());
00238
00239 if (mec == 0)
00240 {
00241 throw CORBA::INTERNAL ();
00242 }
00243
00244
00245 ACE_CString sname (this->name_ + "/");
00246 char name[64];
00247 ACE_OS::sprintf(name, "%d", id);
00248 sname += name;
00249
00250 ACE_WRITE_GUARD_RETURN (TAO_SYNCH_RW_MUTEX, guard, this->mutex_, 0);
00251
00252
00253 if (this->map_.find (sname) == 0)
00254 throw NotifyMonitoringExt::NameAlreadyUsed ();
00255
00256
00257 if (this->map_.bind (sname, id) != 0)
00258 {
00259 throw NotifyMonitoringExt::NameMapError ();
00260 }
00261
00262
00263 mec->add_stats (sname.c_str ());
00264
00265 return ec._retn ();
00266 }
00267
00268 void
00269 TAO_MonitorEventChannelFactory::remove (TAO_Notify_EventChannel* channel)
00270 {
00271 TAO_MonitorEventChannel* mec =
00272 dynamic_cast<TAO_MonitorEventChannel*> (channel);
00273
00274 if (mec != 0)
00275 {
00276 ACE_WRITE_GUARD (TAO_SYNCH_RW_MUTEX, guard, this->mutex_);
00277 this->map_.unbind (mec->name ());
00278 }
00279
00280 this->TAO_Notify_EventChannelFactory::remove (channel);
00281 }
00282
00283 size_t
00284 TAO_MonitorEventChannelFactory::get_suppliers (
00285 CosNotifyChannelAdmin::ChannelID id)
00286 {
00287 size_t count = 0;
00288 CosNotifyChannelAdmin::EventChannel_var ec =
00289 this->get_event_channel (id);
00290
00291 if (!CORBA::is_nil (ec.in ()))
00292 {
00293 CosNotifyChannelAdmin::AdminIDSeq_var supadmin_ids =
00294 ec->get_all_supplieradmins ();
00295 CORBA::ULong length = supadmin_ids->length ();
00296
00297 for (CORBA::ULong j = 0; j < length; ++j)
00298 {
00299 CosNotifyChannelAdmin::SupplierAdmin_var admin =
00300 ec->get_supplieradmin (supadmin_ids[j]);
00301
00302 if (!CORBA::is_nil (admin.in ()))
00303 {
00304 CosNotifyChannelAdmin::ProxyIDSeq_var proxys =
00305 admin->push_consumers ();
00306 count += proxys->length ();
00307 }
00308 }
00309 }
00310
00311 return count;
00312 }
00313
00314 size_t
00315 TAO_MonitorEventChannelFactory::get_consumers (
00316 CosNotifyChannelAdmin::ChannelID id)
00317 {
00318 size_t count = 0;
00319 CosNotifyChannelAdmin::EventChannel_var ec =
00320 this->get_event_channel (id);
00321
00322 if (!CORBA::is_nil (ec.in ()))
00323 {
00324 CosNotifyChannelAdmin::AdminIDSeq_var conadmin_ids =
00325 ec->get_all_consumeradmins ();
00326 CORBA::ULong length = conadmin_ids->length ();
00327
00328 for (CORBA::ULong j = 0; j < length; ++j)
00329 {
00330 CosNotifyChannelAdmin::ConsumerAdmin_var admin =
00331 ec->get_consumeradmin (conadmin_ids[j]);
00332
00333 if (!CORBA::is_nil (admin.in ()))
00334 {
00335 CosNotifyChannelAdmin::ProxyIDSeq_var proxys =
00336 admin->push_suppliers ();
00337 count += proxys->length ();
00338 }
00339 }
00340 }
00341
00342 return count;
00343 }
00344
00345 size_t
00346 TAO_MonitorEventChannelFactory::get_ecs (
00347 Monitor_Control_Types::NameList* names,
00348 bool active)
00349 {
00350 size_t count = 0;
00351 CosNotifyChannelAdmin::ChannelIDSeq_var ids = this->get_all_channels ();
00352
00353 CORBA::ULong total = ids->length ();
00354
00355 for (CORBA::ULong i = 0; i < total; ++i)
00356 {
00357 CosNotifyChannelAdmin::ChannelID id = ids[i];
00358 bool want_event_channel = !active;
00359
00360
00361 size_t consumers = this->get_consumers (id);
00362
00363 if (consumers > 0)
00364 {
00365 want_event_channel = active;
00366 }
00367
00368 if ((!active && want_event_channel)
00369 || (active && !want_event_channel))
00370 {
00371
00372 size_t suppliers = this->get_suppliers (id);
00373
00374 if (suppliers > 0)
00375 {
00376 want_event_channel = active;
00377 }
00378 }
00379
00380 if (want_event_channel)
00381 {
00382 count++;
00383
00384 if (names != 0)
00385 {
00386 ACE_READ_GUARD_RETURN (TAO_SYNCH_RW_MUTEX,
00387 guard,
00388 this->mutex_,
00389 0);
00390
00391 Map::iterator itr (this->map_);
00392 Map::value_type* entry = 0;
00393
00394 while (itr.next (entry))
00395 {
00396 if (id == entry->item ())
00397 {
00398 names->push_back (entry->key ());
00399 }
00400
00401 itr.advance ();
00402 }
00403 }
00404 }
00405 }
00406
00407 return count;
00408 }
00409
00410
00411
00412
00413
00414 TAO_MonitorEventChannelFactory::Unbinder::Unbinder (
00415 TAO_MonitorEventChannelFactory::Map& map,
00416 const ACE_CString& name)
00417 : map_ (map),
00418 name_ (name),
00419 released_ (false)
00420 {
00421 }
00422
00423 TAO_MonitorEventChannelFactory::Unbinder::~Unbinder (void)
00424 {
00425 if (!this->released_)
00426 {
00427 this->map_.unbind (this->name_);
00428 }
00429 }
00430
00431 void
00432 TAO_MonitorEventChannelFactory::Unbinder::release (void)
00433 {
00434 this->released_ = true;
00435 }
00436
00437 TAO_END_VERSIONED_NAMESPACE_DECL
00438
00439 #endif
00440