44 #include "TaskContextS.h" 45 #include <orbsvcs/CosNamingC.h> 47 #include <ace/SString.h> 48 #include "tao/TimeBaseC.h" 49 #include "tao/Messaging/Messaging.h" 50 #include "tao/Messaging/Messaging_RT_PolicyC.h" 52 #include <omniORB4/Naming.hh> 54 #include "TaskContextC.h" 61 #include "../../os/threads.hpp" 62 #include "../../Activity.hpp" 80 servers.erase(mtaskcontext);
83 iors.erase(mtaskcontext);
85 PortableServer::ObjectId_var oid = mpoa->servant_to_id(mtask_i.in());
86 mpoa->deactivate_object(oid);
90 CORBA::Object_var rootObj = orb->resolve_initial_references(
"NameService");
91 CosNaming::NamingContext_var rootNC = CosNaming::NamingContext::_narrow(rootObj.in());
93 if (CORBA::is_nil( rootNC.in() ) ) {
94 log(
Warning) <<
"CTaskContext '"<< mregistered_name <<
"' could not find CORBA Naming Service."<<endlog();
99 name[0].id = CORBA::string_dup(
"TaskContexts");
100 name[1].id = CORBA::string_dup( mregistered_name.c_str() );
102 rootNC->unbind(name);
103 log(
Info) <<
"Successfully removed CTaskContext '"<< mregistered_name <<
"' from CORBA Naming Service."<<endlog();
105 catch( CosNaming::NamingContext::NotFound ) {
106 log(
Info) <<
"CTaskContext '"<< mregistered_name <<
"' task was already unbound."<<endlog();
109 log(
Warning) <<
"CTaskContext '"<< mregistered_name <<
"' unbinding failed."<<endlog();
113 log(
Warning) <<
"CTaskContext '"<< mregistered_name <<
"' unbinding failed from CORBA Naming Service."<<endlog();
122 servers[mtaskcontext] =
this;
126 CORBA::Object_var poa_object =
127 orb->resolve_initial_references (
"RootPOA");
128 mpoa = PortableServer::POA::_narrow(poa_object);
129 PortableServer::POAManager_var poa_manager =
130 mpoa->the_POAManager ();
149 CORBA::String_var ior = orb->object_to_string( mtask.in() );
150 iors[mtaskcontext] = std::string( ior.in() );
153 CORBA::Object_var rootObj;
154 CosNaming::NamingContext_var rootNC;
156 rootObj = orb->resolve_initial_references(
"NameService");
157 rootNC = CosNaming::NamingContext::_narrow(rootObj);
160 if (CORBA::is_nil( rootNC ) ) {
161 std::string err(
"CTaskContext '" + mregistered_name +
"' could not find CORBA Naming Service.");
162 if (require_name_service) {
163 log(
Error) << err << endlog();
164 servers.erase(mtaskcontext);
169 log(
Warning) << err << endlog();
170 #ifndef ORO_NO_EMIT_CORBA_IOR 171 log() <<
"Writing IOR to 'std::cerr' and file '" << mregistered_name <<
".ior'"<<endlog();
174 CORBA::String_var ior = orb->object_to_string( mtask.in() );
178 std::string iorname( mregistered_name );
180 std::ofstream file_ior( iorname.c_str() );
187 log(
Info) <<
"CTaskContext '"<< mregistered_name <<
"' found CORBA Naming Service."<<endlog();
189 CosNaming::Name name;
191 name[0].id = CORBA::string_dup(
"TaskContexts");
192 CosNaming::NamingContext_var controlNC;
194 controlNC = rootNC->bind_new_context(name);
196 catch( CosNaming::NamingContext::AlreadyBound&) {
197 log(
Debug) <<
"NamingContext 'TaskContexts' already bound to CORBA Naming Service."<<endlog();
202 name[1].id = CORBA::string_dup( mregistered_name.c_str() );
204 rootNC->bind(name, mtask );
205 log(
Info) <<
"Successfully added CTaskContext '"<< mregistered_name <<
"' to CORBA Naming Service."<<endlog();
207 catch( CosNaming::NamingContext::AlreadyBound&) {
208 log(
Warning) <<
"CTaskContext '"<< mregistered_name <<
"' already bound to CORBA Naming Service."<<endlog();
209 log() <<
"Trying to rebind...";
211 rootNC->rebind(name, mtask);
213 log() <<
" failed!"<<endlog();
216 log() <<
" done. New CTaskContext bound to Naming Service."<<endlog();
220 log(
Info) <<
"CTaskContext '"<< mregistered_name <<
"' is not using the CORBA Naming Service."<<endlog();
221 #ifndef ORO_NO_EMIT_CORBA_IOR 222 log() <<
"Writing IOR to 'std::cerr' and file '" << mregistered_name <<
".ior'"<<endlog();
225 CORBA::String_var ior = orb->object_to_string( mtask.in() );
229 std::string iorname( mregistered_name );
231 std::ofstream file_ior( iorname.c_str() );
238 catch (CORBA::Exception &e) {
239 log(
Error) <<
"CORBA exception raised!" << endlog();
246 : mtaskcontext(taskc), muse_naming(use_naming), mregistered_name(alias)
260 log(
Info) <<
"Cleaning up TaskContextServers..."<<endlog();
262 delete servers.begin()->second;
266 log() <<
"Cleanup done."<<endlog();
271 if ( !CORBA::is_nil(
orb) ) {
272 ServerMap::iterator it =
servers.find(c);
274 log(
Info) <<
"Cleaning up TaskContextServer for "<< c->
getName()<<endlog();
291 log(
Info) <<
"Orb already down..."<<endlog();
294 if ( CORBA::is_nil(
orb) ) {
295 log(
Error) <<
"Orb Shutdown...failed! Orb is nil." << endlog();
301 log(
Info) <<
"Orb Shutdown...";
303 if (wait_for_completion)
304 log(
Info)<<
"waiting..."<<endlog();
305 orb->shutdown( wait_for_completion );
306 log(
Info) <<
"done." << endlog();
308 catch (CORBA::Exception &e) {
309 log(
Error) <<
"Orb Shutdown...failed! CORBA exception raised." << endlog();
318 if ( CORBA::is_nil(
orb) ) {
319 log(
Error) <<
"RunOrb...failed! Orb is nil." << endlog();
323 log(
Info) <<
"Entering orb->run()."<<endlog();
325 log(
Info) <<
"Breaking out of orb->run()."<<endlog();
327 catch (CORBA::Exception &e) {
328 log(
Error) <<
"Orb Run : CORBA exception raised!" << endlog();
340 OrbRunner(
int scheduler,
int priority,
unsigned cpu_affinity)
341 :
Activity(scheduler, priority, cpu_affinity)
357 log(
Info) <<
"Safely stopped."<<endlog();
365 if ( CORBA::is_nil(
orb) ) {
366 log(
Error) <<
"ThreadOrb...failed! Orb is nil." << endlog();
370 log(
Error) <<
"Orb already running in a thread."<<endlog();
372 log(
Info) <<
"Starting Orb in a thread."<<endlog();
381 if ( CORBA::is_nil(
orb) ) {
382 log(
Error) <<
"DestroyOrb...failed! Orb is nil." << endlog();
399 log(
Info) <<
"Orb destroyed."<<endlog();
401 catch (CORBA::Exception &e) {
402 log(
Error) <<
"Orb Destroy : CORBA exception raised!" << endlog();
413 if ( CORBA::is_nil(
orb) )
417 log(
Debug) <<
"Returning existing TaskContextServer for "<< alias <<endlog();
418 return servers.find(tc)->second;
422 log(
Info) <<
"Creating new TaskContextServer for "<< alias <<endlog();
438 if ( CORBA::is_nil(
orb) )
439 return CTaskContext::_nil();
442 log(
Debug) <<
"Returning existing TaskContextServer for "<< alias <<endlog();
443 return CTaskContext::_duplicate(
servers.find(tc)->second->server() );
447 if ( (it->first) == tc ) {
448 log(
Debug) <<
"Returning server of Proxy for "<< alias <<endlog();
449 return CTaskContext::_duplicate(it->second);
453 log(
Info) <<
"Creating new TaskContextServer for "<< alias <<endlog();
456 return CTaskContext::_duplicate( cts->
server() );
461 return CTaskContext::_nil();
473 IorMap::const_iterator it =
iors.find(tc);
474 if (it !=
iors.end())
477 return std::string(
"");
This class manages the creation of TaskContext Corba Servers and a Corba Object Request Broker (Orb) ...
Service::shared_ptr provides()
Returns this Service.
~TaskContextServer()
When a TaskContextServer is destroyed, the object reference is removed from the Naming Service and th...
virtual RTT::corba::CTaskContext_ptr activate_this()
#define CORBA_EXCEPTION_INFO(x)
static CTaskContext_ptr CreateServer(TaskContext *tc, bool use_naming=true, bool require_name_service=false)
Factory method: create a CORBA server for an existing TaskContext.
static std::string getIOR(TaskContext *tc)
Get the IOR of a given TaskContext.
TaskContextServer(TaskContext *taskcontext, bool use_naming, bool require_name_service)
Private constructor which creates a new servant.
basic_ostreams & endl(basic_ostreams &s)
Flush and newline.
static void clearServants()
void initTaskContextServer(bool require_name_service)
OrbRunner(int scheduler, int priority, unsigned cpu_affinity)
Thrown if a server does not exist or has the wrong type.
static void CleanupServer(TaskContext *tc)
Deletes a TaskContext server for a given taskcontext.
const char * what() const
static base::ActivityInterface * orbrunner
static PortableServer::POA_var rootPOA
The root POA of this process.
static TaskContextServer * Create(TaskContext *tc, bool use_naming=true, bool require_name_service=false)
Factory method: create a CORBA server for an existing TaskContext.
static void ThreadOrb()
This is an overloaded member function, provided for convenience. It differs from the above function o...
An Activity executes a RunnableInterface object in a (periodic) thread.
Class which runs an orb in an Orocos thread.
corba::CTaskContext_var mtask
static void deregisterServant(DataFlowInterface *obj)
Notify the Logger in which 'module' the message occured.
static void CleanupServers()
Destroys all TaskContextServer objects.
The TaskContext is the C++ representation of an Orocos component.
virtual bool start()=0
Start the activity.
TaskContext * mtaskcontext
Contains TaskContext, Activity, OperationCaller, Operation, Property, InputPort, OutputPort, Attribute.
static void ShutdownOrb(bool wait_for_completion=true)
Invoke this method once to shutdown the Orb which is running the task servers in RunOrb().
static void RunOrb()
Invoke this method to run the orb and accept client requests.
static void DestroyOrb()
Invoke this method once to cleanup the orb.
static void DoShutdownOrb(bool wait_for_completion=true)
Internal shutdown function, used by both thread and ShutdownOrb.
CTaskContext_ptr server() const
Get the Corba Object of this TaskContext.
static CORBA::ORB_var orb
The orb of this process.
virtual const std::string & getName() const
Returns the name of this TaskContext.
std::string mregistered_name
virtual bool stop()=0
Stop the activity This will stop the activity by removing it from the 'run-queue' of a thread or call...