00001
00002
00003 #include "orbsvcs/Notify/MonitorControl/MonitorManager.h"
00004
00005 #if defined (TAO_HAS_MONITOR_FRAMEWORK) && (TAO_HAS_MONITOR_FRAMEWORK == 1)
00006
00007 #include "orbsvcs/Notify/MonitorControl/NotificationServiceMonitor_i.h"
00008
00009 #include "orbsvcs/Naming/Naming_Client.h"
00010
00011 #include "tao/ORB.h"
00012 #include "tao/IORTable/IORTable.h"
00013
00014 #include "ace/Service_Config.h"
00015 #include "ace/Get_Opt.h"
00016
00017 TAO_BEGIN_VERSIONED_NAMESPACE_DECL
00018
00019 TAO_MonitorManager::TAO_MonitorManager (void)
00020 : run_ (false)
00021 , initialized_ (false)
00022 {
00023 }
00024
00025 int
00026 TAO_MonitorManager::init (int argc, ACE_TCHAR* argv[])
00027 {
00028 ACE_GUARD_RETURN (TAO_SYNCH_MUTEX, guard, this->task_.mutex_, -1);
00029
00030 this->task_.argv_.add (ACE_TEXT("fake_process_name"));
00031
00032 ACE_Get_Opt opts (argc,
00033 argv,
00034 ACE_TEXT ("o:"),
00035 0,
00036 0,
00037 ACE_Get_Opt::PERMUTE_ARGS,
00038 1);
00039
00040 static const ACE_TCHAR* orbarg = ACE_TEXT ("ORBArg");
00041 static const ACE_TCHAR* nonamesvc = ACE_TEXT ("NoNameSvc");
00042 opts.long_option (orbarg, ACE_Get_Opt::ARG_REQUIRED);
00043 opts.long_option (nonamesvc, ACE_Get_Opt::NO_ARG);
00044
00045 int c;
00046 while ((c = opts ()) != -1)
00047 switch (c)
00048 {
00049 case 'o':
00050 this->task_.ior_output_ = opts.opt_arg ();
00051 if (TAO_debug_level > 7)
00052 {
00053 ACE_DEBUG((LM_INFO,
00054 ACE_TEXT("(%P|%t) TAO_MonitorManager: Setting IOR output file to: %s"),
00055 this->task_.ior_output_.c_str ()));
00056 }
00057 break;
00058 case 0:
00059 if (ACE_OS::strcmp (opts.long_option (), orbarg) == 0)
00060 {
00061 ACE_TCHAR * orbArgs = opts.opt_arg ();
00062 if (TAO_debug_level > 7)
00063 {
00064 ACE_DEBUG((LM_INFO,
00065 ACE_TEXT("(%P|%t) TAO_MonitorManager: Setting Orb arguments to: %s"),
00066 orbArgs));
00067 }
00068 this->task_.argv_.add (orbArgs);
00069 }
00070 else if (ACE_OS::strcmp (opts.long_option (), nonamesvc) == 0)
00071 {
00072 if (TAO_debug_level > 7)
00073 {
00074 ACE_DEBUG((LM_INFO,
00075 ACE_TEXT("(%P|%t) TAO_MonitorManager: Not using naming service")));
00076 }
00077 this->task_.use_name_svc_ = false;
00078 }
00079 break;
00080 case ':':
00081 ACE_ERROR_RETURN ((LM_ERROR,
00082 ACE_TEXT ("(%P|%t) TAO_MonitorManager: %s requires an argument\n"),
00083 opts.last_option ()),
00084 -1);
00085 }
00086
00087
00088 this->task_.argv_.argv ();
00089
00090
00091 this->initialized_ = true;
00092 return 0;
00093 }
00094
00095 int
00096 TAO_MonitorManager::fini (void)
00097 {
00098 if (!CORBA::is_nil (this->task_.orb_.in ()))
00099 {
00100 ACE_GUARD_RETURN (TAO_SYNCH_MUTEX, guard, this->task_.mutex_, -1);
00101
00102 if (!CORBA::is_nil (this->task_.orb_.in ()))
00103 {
00104 this->task_.orb_->shutdown (true);
00105 }
00106 }
00107
00108 this->task_.wait ();
00109 return 0;
00110 }
00111
00112 int
00113 TAO_MonitorManager::run (void)
00114 {
00115 bool activate = false;
00116
00117 {
00118 ACE_GUARD_RETURN (TAO_SYNCH_MUTEX, guard, this->task_.mutex_, -1);
00119
00120
00121
00122
00123 int argc = task_.argv_.argc ();
00124 task_.orb_ = CORBA::ORB_init (argc,
00125 task_.argv_.argv (),
00126 task_.mc_orb_name_.c_str ());
00127
00128 if (!this->run_ && this->initialized_)
00129 {
00130 this->run_ = true;
00131 activate = true;
00132 }
00133 }
00134
00135 int status = 0;
00136
00137 if (activate)
00138 {
00139 status = this->task_.activate ();
00140
00141 if (status == 0)
00142 {
00143
00144
00145
00146
00147 this->task_.startup_barrier_.wait ();
00148 }
00149 }
00150
00151 return status;
00152 }
00153
00154 int
00155 TAO_MonitorManager::resume (void)
00156 {
00157 return this->run ();
00158 }
00159
00160 void
00161 TAO_MonitorManager::shutdown (void)
00162 {
00163 TAO_MonitorManager* monitor =
00164 ACE_Dynamic_Service<TAO_MonitorManager>::instance (
00165 TAO_NOTIFY_MONITOR_CONTROL_MANAGER);
00166
00167 if (monitor != 0)
00168 {
00169 monitor->fini ();
00170 }
00171 }
00172
00173 TAO_MonitorManager::ORBTask::ORBTask (void)
00174 : use_name_svc_ (true)
00175 , startup_barrier_ (2)
00176 , mc_orb_name_ (ACE_TEXT("TAO_MonitorAndControl"))
00177 {
00178 }
00179
00180 int
00181 TAO_MonitorManager::ORBTask::svc (void)
00182 {
00183 try
00184 {
00185 if (CORBA::is_nil (this->orb_.in ()))
00186 {
00187 ACE_ERROR_RETURN ((LM_ERROR,
00188 "(%P|%t) TAO_MonitorManager: Unable to "
00189 "initialize the ORB\n"),
00190 1);
00191 }
00192
00193 PortableServer::POA_var poa;
00194
00195 {
00196 ACE_GUARD_RETURN (TAO_SYNCH_MUTEX, guard, this->mutex_, -1);
00197
00198 CORBA::Object_var obj =
00199 this->orb_->resolve_initial_references ("RootPOA");
00200
00201 poa = PortableServer::POA::_narrow (obj.in ());
00202
00203 if (CORBA::is_nil (poa.in ()))
00204 {
00205 ACE_ERROR_RETURN ((LM_ERROR,
00206 "(%P|%t) TAO_MonitorManager: Unable to "
00207 "resolve the RootPOA\n"),
00208 1);
00209 }
00210
00211 PortableServer::POAManager_var poa_manager = poa->the_POAManager ();
00212 poa_manager->activate ();
00213
00214
00215 NotificationServiceMonitor_i* servant;
00216 ACE_NEW_RETURN (servant,
00217 NotificationServiceMonitor_i (this->orb_.in ()), 1);
00218 PortableServer::ServantBase_var owner_transfer(servant);
00219 PortableServer::ObjectId_var id = poa->activate_object (servant);
00220
00221
00222 obj = poa->id_to_reference (id.in ());
00223 CosNotification::NotificationServiceMonitorControl_var monitor =
00224 CosNotification::NotificationServiceMonitorControl::_narrow (obj.in ());
00225 CORBA::String_var ior = this->orb_->object_to_string (monitor.in ());
00226 obj = this->orb_->resolve_initial_references ("IORTable");
00227 IORTable::Table_var iortable = IORTable::Table::_narrow (obj.in ());
00228
00229 if (CORBA::is_nil (iortable.in ()))
00230 {
00231 ACE_ERROR_RETURN ((LM_ERROR,
00232 "(%P|%t) TAO_MonitorManager: Unable to "
00233 "resolve the IORTable\n"),
00234 1);
00235 }
00236
00237 iortable->bind(ACE_TEXT_ALWAYS_CHAR(mc_orb_name_.c_str()), ior.in ());
00238
00239 if (this->use_name_svc_)
00240 {
00241 TAO_Naming_Client nc;
00242 nc.init (this->orb_.in ());
00243 CosNaming::Name name (1);
00244 name.length (1);
00245 name[0].id = CORBA::string_dup (ACE_TEXT_ALWAYS_CHAR(mc_orb_name_.c_str()));
00246 nc->rebind (name, monitor.in ());
00247 }
00248
00249 if (this->ior_output_.length () > 0)
00250 {
00251 FILE* fp = ACE_OS::fopen (this->ior_output_.c_str (), "w");
00252
00253 if (fp == 0)
00254 {
00255 ACE_ERROR_RETURN ((LM_ERROR,
00256 ACE_TEXT ("(%P|%t) TAO_MonitorManager: "
00257 ACE_TEXT ("Unable to write to %s\n")),
00258 this->ior_output_.c_str ()),
00259 1);
00260 }
00261 else
00262 {
00263 ACE_OS::fprintf (fp, "%s", ior.in ());
00264 ACE_OS::fclose (fp);
00265 }
00266 }
00267 }
00268
00269
00270
00271
00272
00273 startup_barrier_.wait ();
00274 this->orb_->run ();
00275
00276 ACE_GUARD_RETURN (TAO_SYNCH_MUTEX, guard, this->mutex_, -1);
00277
00278
00279 if (!CORBA::is_nil (poa.in ()))
00280 {
00281 poa->destroy (true, true);
00282 }
00283
00284 this->orb_->destroy ();
00285
00286
00287 this->orb_ = CORBA::ORB::_nil ();
00288 }
00289 catch (const CORBA::Exception& ex)
00290 {
00291 if (!CORBA::is_nil (this->orb_.in ()))
00292 {
00293 try
00294 {
00295 this->orb_->shutdown ();
00296 }
00297 catch (...)
00298 {
00299 }
00300
00301 this->orb_ = CORBA::ORB::_nil ();
00302 }
00303
00304 ex._tao_print_exception ("Caught in "
00305 "TAO_MonitorManager::ORBTask::svc");
00306 }
00307 catch (...)
00308 {
00309 if (!CORBA::is_nil (this->orb_.in ()))
00310 {
00311 try
00312 {
00313 this->orb_->shutdown ();
00314 }
00315 catch (...)
00316 {
00317 }
00318 this->orb_ = CORBA::ORB::_nil ();
00319 }
00320
00321 ACE_ERROR ((LM_ERROR,
00322 "Unexpected exception type caught "
00323 "in TAO_MonitorManager::ORBTask::svc"));
00324 }
00325
00326 return 0;
00327 }
00328
00329 TAO_END_VERSIONED_NAMESPACE_DECL
00330
00331 typedef TAO_MonitorManager TAO_MonitorAndControl;
00332 ACE_STATIC_SVC_DEFINE (TAO_MonitorAndControl,
00333 ACE_TEXT (TAO_NOTIFY_MONITOR_CONTROL_MANAGER),
00334 ACE_Service_Type::SERVICE_OBJECT,
00335 &ACE_SVC_NAME (TAO_MonitorAndControl),
00336 ACE_Service_Type::DELETE_THIS | ACE_Service_Type::DELETE_OBJ,
00337 0)
00338 ACE_FACTORY_DEFINE (TAO_Notify_MC, TAO_MonitorAndControl)
00339
00340 #endif