00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040 #include "TaskContextServer.hpp"
00041 #include "TaskContextProxy.hpp"
00042 #include "corba.h"
00043 #ifdef CORBA_IS_TAO
00044 #include "TaskContextS.h"
00045 #include <orbsvcs/CosNamingC.h>
00046
00047 #include <ace/SString.h>
00048 #include "tao/TimeBaseC.h"
00049 #include "tao/Messaging/Messaging.h"
00050 #include "tao/Messaging/Messaging_RT_PolicyC.h"
00051 #else
00052 #include <omniORB4/Naming.hh>
00053 #endif
00054 #include "TaskContextC.h"
00055 #include "TaskContextI.h"
00056 #include "DataFlowI.h"
00057 #include "POAUtility.h"
00058 #include <iostream>
00059 #include <fstream>
00060
00061 #include "../../os/threads.hpp"
00062 #include "../../Activity.hpp"
00063
00064 namespace RTT
00065 {namespace corba
00066 {
00067 using namespace std;
00068
00069 std::map<TaskContext*, TaskContextServer*> TaskContextServer::servers;
00070
00071 base::ActivityInterface* TaskContextServer::orbrunner = 0;
00072
00073 bool TaskContextServer::is_shutdown = false;
00074
00075 std::map<TaskContext*, std::string> TaskContextServer::iors;
00076
00077 TaskContextServer::~TaskContextServer()
00078 {
00079 Logger::In in("~TaskContextServer()");
00080 servers.erase(mtaskcontext);
00081
00082
00083 iors.erase(mtaskcontext);
00084
00085 PortableServer::ObjectId_var oid = mpoa->servant_to_id(mtask_i.in());
00086 mpoa->deactivate_object(oid);
00087
00088 if (muse_naming) {
00089 try {
00090 CORBA::Object_var rootObj = orb->resolve_initial_references("NameService");
00091 CosNaming::NamingContext_var rootNC = CosNaming::NamingContext::_narrow(rootObj.in());
00092
00093 if (CORBA::is_nil( rootNC.in() ) ) {
00094 log(Warning) << "CTaskContext '"<< mtaskcontext->getName() << "' could not find CORBA Naming Service."<<endlog();
00095 } else {
00096
00097 CosNaming::Name name;
00098 name.length(2);
00099 name[0].id = CORBA::string_dup("TaskContexts");
00100 name[1].id = CORBA::string_dup( mtaskcontext->getName().c_str() );
00101 try {
00102 rootNC->unbind(name);
00103 log(Info) << "Successfully removed CTaskContext '"<<mtaskcontext->getName()<<"' from CORBA Naming Service."<<endlog();
00104 }
00105 catch( ... ) {
00106 log(Warning) << "CTaskContext '"<< mtaskcontext->getName() << "' unbinding failed."<<endlog();
00107 }
00108 }
00109 } catch (...) {
00110 log(Warning) << "CTaskContext '"<< mtaskcontext->getName() << "' unbinding failed from CORBA Naming Service."<<endlog();
00111 }
00112 }
00113 }
00114
00115
00116
00117
00118 TaskContextServer::TaskContextServer(TaskContext* taskc, bool use_naming, bool require_name_service)
00119 : mtaskcontext(taskc), muse_naming(use_naming)
00120 {
00121 Logger::In in("TaskContextServer()");
00122 servers[taskc] = this;
00123 try {
00124
00125
00126 CORBA::Object_var poa_object =
00127 orb->resolve_initial_references ("RootPOA");
00128 mpoa = PortableServer::POA::_narrow(poa_object);
00129 PortableServer::POAManager_var poa_manager =
00130 mpoa->the_POAManager ();
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144 RTT_corba_CTaskContext_i* serv;
00145 mtask_i = serv = new RTT_corba_CTaskContext_i( taskc, mpoa );
00146 mtask = serv->activate_this();
00147
00148
00149 CORBA::String_var ior = orb->object_to_string( mtask.in() );
00150 iors[taskc] = std::string( ior.in() );
00151
00152 if ( use_naming ) {
00153 CORBA::Object_var rootObj;
00154 CosNaming::NamingContext_var rootNC;
00155 try {
00156 rootObj = orb->resolve_initial_references("NameService");
00157 rootNC = CosNaming::NamingContext::_narrow(rootObj);
00158 } catch (...) {}
00159
00160 if (CORBA::is_nil( rootNC ) ) {
00161 std::string err("CTaskContext '" + taskc->getName() + "' could not find CORBA Naming Service.");
00162 if (require_name_service) {
00163 servers.erase(taskc);
00164 log(Error) << err << endlog();
00165 servers.erase(taskc);
00166 throw IllegalServer(err);
00167 }
00168 else
00169 {
00170 log(Warning) << err << endlog();
00171 log() <<"Writing IOR to 'std::cerr' and file '" << taskc->getName() <<".ior'"<<endlog();
00172
00173
00174 CORBA::String_var ior = orb->object_to_string( mtask.in() );
00175 std::cerr << ior.in() <<std::endl;
00176 {
00177
00178 std::string iorname( taskc->getName());
00179 iorname += ".ior";
00180 std::ofstream file_ior( iorname.c_str() );
00181 file_ior << ior.in() <<std::endl;
00182 }
00183 return;
00184 }
00185 }
00186 log(Info) << "CTaskContext '"<< taskc->getName() << "' found CORBA Naming Service."<<endlog();
00187
00188 CosNaming::Name name;
00189 name.length(1);
00190 name[0].id = CORBA::string_dup("TaskContexts");
00191 CosNaming::NamingContext_var controlNC;
00192 try {
00193 controlNC = rootNC->bind_new_context(name);
00194 }
00195 catch( CosNaming::NamingContext::AlreadyBound&) {
00196 log(Debug) << "NamingContext 'TaskContexts' already bound to CORBA Naming Service."<<endlog();
00197
00198 }
00199
00200 name.length(2);
00201 name[1].id = CORBA::string_dup( taskc->getName().c_str() );
00202 try {
00203 rootNC->bind(name, mtask );
00204 log(Info) << "Successfully added CTaskContext '"<<taskc->getName()<<"' to CORBA Naming Service."<<endlog();
00205 }
00206 catch( CosNaming::NamingContext::AlreadyBound&) {
00207 log(Warning) << "CTaskContext '"<< taskc->getName() << "' already bound to CORBA Naming Service."<<endlog();
00208 log() <<"Trying to rebind...";
00209 try {
00210 rootNC->rebind(name, mtask);
00211 } catch( ... ) {
00212 log() << " failed!"<<endlog();
00213 return;
00214 }
00215 log() << " done. New CTaskContext bound to Naming Service."<<endlog();
00216 }
00217 }
00218 else {
00219 log(Info) <<"CTaskContext '"<< taskc->getName() << "' is not using the CORBA Naming Service."<<endlog();
00220 log() <<"Writing IOR to 'std::cerr' and file '" << taskc->getName() <<".ior'"<<endlog();
00221
00222
00223 CORBA::String_var ior = orb->object_to_string( mtask.in() );
00224 std::cerr << ior.in() <<std::endl;
00225 {
00226
00227 std::string iorname( taskc->getName());
00228 iorname += ".ior";
00229 std::ofstream file_ior( iorname.c_str() );
00230 file_ior << ior.in() <<std::endl;
00231 }
00232 return;
00233 }
00234 }
00235 catch (CORBA::Exception &e) {
00236 log(Error) << "CORBA exception raised!" << endlog();
00237 log() << CORBA_EXCEPTION_INFO(e) << endlog();
00238 }
00239
00240 }
00241
00242 void TaskContextServer::CleanupServers() {
00243 if ( !CORBA::is_nil(orb) && !is_shutdown) {
00244 log(Info) << "Cleaning up TaskContextServers..."<<endlog();
00245 ServerMap::iterator it = servers.begin();
00246 while ( !servers.empty() ){
00247 delete servers.begin()->second;
00248
00249 }
00250 CDataFlowInterface_i::clearServants();
00251 log() << "Cleanup done."<<endlog();
00252 }
00253 }
00254
00255 void TaskContextServer::CleanupServer(TaskContext* c) {
00256 if ( !CORBA::is_nil(orb) ) {
00257 ServerMap::iterator it = servers.find(c);
00258 if ( it != servers.end() ){
00259 log(Info) << "Cleaning up TaskContextServer for "<< c->getName()<<endlog();
00260 delete it->second;
00261
00262 }
00263 }
00264 }
00265
00266 void TaskContextServer::ShutdownOrb(bool wait_for_completion)
00267 {
00268 Logger::In in("ShutdownOrb");
00269 DoShutdownOrb(wait_for_completion);
00270 }
00271
00272 void TaskContextServer::DoShutdownOrb(bool wait_for_completion)
00273 {
00274 if (is_shutdown) {
00275 log(Info) << "Orb already down..."<<endlog();
00276 return;
00277 }
00278 if ( CORBA::is_nil(orb) ) {
00279 log(Error) << "Orb Shutdown...failed! Orb is nil." << endlog();
00280 return;
00281 }
00282
00283 try {
00284 CleanupServers();
00285 log(Info) << "Orb Shutdown...";
00286 is_shutdown = true;
00287 if (wait_for_completion)
00288 log(Info)<<"waiting..."<<endlog();
00289 orb->shutdown( wait_for_completion );
00290 log(Info) << "done." << endlog();
00291 }
00292 catch (CORBA::Exception &e) {
00293 log(Error) << "Orb Shutdown...failed! CORBA exception raised." << endlog();
00294 log() << CORBA_EXCEPTION_INFO(e) << endlog();
00295 return;
00296 }
00297 }
00298
00299
00300 void TaskContextServer::RunOrb()
00301 {
00302 if ( CORBA::is_nil(orb) ) {
00303 log(Error) << "RunOrb...failed! Orb is nil." << endlog();
00304 return;
00305 }
00306 try {
00307 log(Info) <<"Entering orb->run()."<<endlog();
00308 orb->run();
00309 log(Info) <<"Breaking out of orb->run()."<<endlog();
00310 }
00311 catch (CORBA::Exception &e) {
00312 log(Error) << "Orb Run : CORBA exception raised!" << endlog();
00313 log() << CORBA_EXCEPTION_INFO(e) << endlog();
00314 }
00315 }
00316
00320 class OrbRunner
00321 : public Activity
00322 {
00323 public:
00324 OrbRunner()
00325 : Activity(RTT::os::LowestPriority)
00326 {}
00327 void loop()
00328 {
00329 Logger::In in("OrbRunner");
00330 TaskContextServer::RunOrb();
00331 }
00332
00333 bool breakLoop()
00334 {
00335 return true;
00336 }
00337
00338 void finalize()
00339 {
00340 Logger::In in("OrbRunner");
00341 log(Info) <<"Safely stopped."<<endlog();
00342 }
00343 };
00344
00345 void TaskContextServer::ThreadOrb()
00346 {
00347 Logger::In in("ThreadOrb");
00348 if ( CORBA::is_nil(orb) ) {
00349 log(Error) << "ThreadOrb...failed! Orb is nil." << endlog();
00350 return;
00351 }
00352 if (orbrunner != 0) {
00353 log(Error) <<"Orb already running in a thread."<<endlog();
00354 } else {
00355 log(Info) <<"Starting Orb in a thread."<<endlog();
00356 orbrunner = new OrbRunner();
00357
00358 orbrunner->start();
00359 }
00360 }
00361
00362 void TaskContextServer::DestroyOrb()
00363 {
00364 Logger::In in("DestroyOrb");
00365 if ( CORBA::is_nil(orb) ) {
00366 log(Error) << "DestroyOrb...failed! Orb is nil." << endlog();
00367 return;
00368 }
00369
00370 if (orbrunner) {
00371 orbrunner->stop();
00372 delete orbrunner;
00373 orbrunner = 0;
00374 }
00375
00376 try {
00377
00378
00379 CleanupServers();
00380 orb->destroy();
00381 rootPOA = 0;
00382 orb = 0;
00383 log(Info) <<"Orb destroyed."<<endlog();
00384 }
00385 catch (CORBA::Exception &e) {
00386 log(Error) << "Orb Destroy : CORBA exception raised!" << endlog();
00387 log() << CORBA_EXCEPTION_INFO(e) << endlog();
00388 }
00389
00390 }
00391
00392 TaskContextServer* TaskContextServer::Create(TaskContext* tc, bool use_naming, bool require_name_service) {
00393 if ( CORBA::is_nil(orb) )
00394 return 0;
00395
00396 if ( servers.count(tc) ) {
00397 log(Debug) << "Returning existing TaskContextServer for "<<tc->getName()<<endlog();
00398 return servers.find(tc)->second;
00399 }
00400
00401
00402 log(Info) << "Creating new TaskContextServer for "<<tc->getName()<<endlog();
00403 try {
00404 TaskContextServer* cts = new TaskContextServer(tc, use_naming, require_name_service);
00405 return cts;
00406 }
00407 catch( IllegalServer& is ) {
00408 cerr << is.what() << endl;
00409 }
00410 return 0;
00411 }
00412
00413 CTaskContext_ptr TaskContextServer::CreateServer(TaskContext* tc, bool use_naming, bool require_name_service) {
00414 if ( CORBA::is_nil(orb) )
00415 return CTaskContext::_nil();
00416
00417 if ( servers.count(tc) ) {
00418 log(Debug) << "Returning existing TaskContextServer for "<<tc->getName()<<endlog();
00419 return CTaskContext::_duplicate( servers.find(tc)->second->server() );
00420 }
00421
00422 for (TaskContextProxy::PMap::iterator it = TaskContextProxy::proxies.begin(); it != TaskContextProxy::proxies.end(); ++it)
00423 if ( (it->first) == tc ) {
00424 log(Debug) << "Returning server of Proxy for "<<tc->getName()<<endlog();
00425 return CTaskContext::_duplicate(it->second);
00426 }
00427
00428
00429 log(Info) << "Creating new TaskContextServer for "<<tc->getName()<<endlog();
00430 try {
00431 TaskContextServer* cts = new TaskContextServer(tc, use_naming, require_name_service);
00432 return CTaskContext::_duplicate( cts->server() );
00433 }
00434 catch( IllegalServer& is ) {
00435 cerr << is.what() << endl;
00436 }
00437 return CTaskContext::_nil();
00438 }
00439
00440
00441 corba::CTaskContext_ptr TaskContextServer::server() const
00442 {
00443
00444 return mtask.in();
00445 }
00446
00447 std::string TaskContextServer::getIOR(TaskContext* tc)
00448 {
00449 IorMap::const_iterator it = iors.find(tc);
00450 if (it != iors.end())
00451 return it->second;
00452
00453 return std::string("");
00454 }
00455
00456 }}