CORBAManager.cpp
Go to the documentation of this file.
1 // -*- C++ -*-
18 #include <assert.h>
19 #include <iostream>
20 #include <coil/stringutil.h>
22 #include <doil/ORBManager.h>
23 
24 #define UNUSED_ARG(a) do {} while (&a == 0)
25 
26 namespace doil
27 {
28 namespace CORBA
29 {
30  // singleton pointer initializer
33 
34 
43  throw()
44  {
45  if (!manager)
46  {
47  coil::Guard<coil::Mutex> guard(mutex);
48  if (!manager)
49  {
50  manager = new CORBAManager();
51  manager->initOrb(prop);
52  }
53  }
54  return manager;
55  };
56 
65  throw()
66  {
68  return *CORBAManager::init(prop);
69  }
70 
78  const char* CORBAManager::name()
79  throw()
80  {
81  return "corba";
82  }
84  throw()
85  {
86  // Do nothing
87  return;
88  }
89 
90 
92  throw()
93  {
94  while (m_orb->work_pending())
95  {
96  // RTC_PARANOID(("Pending work still exists."));
97  if (m_orb->work_pending())
98  m_orb->perform_work();
99  }
100 
101  if (!::CORBA::is_nil(m_poa))
102  {
103  try
104  {
105  if (!::CORBA::is_nil(m_poaManager))
106  m_poaManager->deactivate(false, true);
107  m_poa->destroy(false, true);
108  m_poa = PortableServer::POA::_nil();
109  }
110  catch (::CORBA::SystemException& ex)
111  {
112  }
113  catch (...)
114  {
115  }
116  }
117 
118  if (!::CORBA::is_nil(m_orb))
119  {
120  try
121  {
122  m_orb->shutdown(true);
123  m_orb = ::CORBA::ORB::_nil();
124  }
125  catch (::CORBA::SystemException& ex)
126  {
127  UNUSED_ARG(ex);
128  }
129  catch (...)
130  {
131  }
132  }
133  {
135  delete manager;
136  manager = NULL;
137  }
138  }
139 
140 
149  doil::ServantNewFunc new_func,
150  doil::ServantDeleteFunc delete_func)
151  throw()
152  {
153  if (id == NULL) return INVALID_ARGS;
154  if (new_func == NULL) return INVALID_ARGS;
155  if (delete_func == NULL) return INVALID_ARGS;
156 
157  bool ret;
158  ServantFactory* factory = new ServantFactory(id, new_func, delete_func);
159  ret = m_factory.registerObject(factory);
160  if (ret) return OK;
161  else return ALREADY_EXISTS;
162  }
163 
164 
166  doil::ProxyNewFunc new_func,
167  doil::ProxyDeleteFunc delete_func)
168  throw()
169  {
170  if (id == NULL) return INVALID_ARGS;
171  if (new_func == NULL) return INVALID_ARGS;
172  if (delete_func == NULL) return INVALID_ARGS;
173 
174  bool ret;
175  ProxyFactory* factory = new ProxyFactory(id, new_func, delete_func);
176  ret = m_factory_proxy.registerObject(factory);
177  if (ret) return OK;
178  else return ALREADY_EXISTS;
179  }
180 
189  throw()
190  {
191  if (impl == NULL) return INVALID_ARGS;
192 
193  const char* id = impl->id();
194  std::cout << "CORBAManager::activateObject: id " << id << std::endl;
195  std::cout << "CORBAManager::activateObject: name " << impl->name() << std::endl;
196  ServantFactory* factory = m_factory.find(id);
197 
198 #if 0
199  if (factory != NULL)
200  {
201 #else
202  // Factory NOT_FOUND
203  if (factory == NULL) return NOT_FOUND;
204 #endif
205  // INVALID_ARGS
206  try
207  {
208  doil::ServantBase* svt = factory->create(impl);
209  CORBAServantBase* csvt = dynamic_cast<CORBAServantBase*>(svt);
210  if (csvt == NULL)
211  {
212  std::cout << "dynamic_cast<CORBAServantBase*> failed" << std::endl;
213  delete svt;
214  return INVALID_ARGS;
215  }
216  ReturnCode_t ret = activateObject(impl, csvt);
217  if (ret != OK)
218  delete svt;
219  return ret;
220  }
221  catch (std::bad_alloc& e)
222  {
223  UNUSED_ARG(e);
224  return INVALID_ARGS;
225  }
226  catch (...)
227  {
228  return UNKNOWN;
229  }
230 #if 0
231 //
232 //for unit tests
233 //
234  }
235  else
236  {
237  ProxyFactory* factory_proxy = m_factory_proxy.find(id);
238  if (factory_proxy == NULL) return NOT_FOUND;
239  doil::ServantBase* svt = factory->create(impl);
240  }
241 #endif
242  return UNKNOWN;
243  }
244 
253  doil::ServantBase* servant)
254  throw()
255  {
256  if (impl == NULL) return INVALID_ARGS;
257  if (servant == NULL) return INVALID_ARGS;
258 
259  // check ID
260  if (strcmp(impl->id(), servant->id()) != 0)
261  return INVALID_ARGS;
262 
264  svt = dynamic_cast<doil::CORBA::CORBAServantBase*>(servant);
265 
266  if (svt == NULL) return INVALID_ARGS;
267 
268  // activate CORBA object
269  ::PortableServer::ObjectId_var id = m_poa->activate_object(svt);
270  ::CORBA::Object_ptr obj = m_poa->id_to_reference(id);
271 
272  Entry* entry = new Entry(impl, svt, obj);
273  if (m_map.registerObject(entry)) return OK;
274  return UNKNOWN;
275  }
276 
285  throw()
286  {
287  if (impl == NULL) return INVALID_ARGS;
288  return deactivateObject(impl->name());
289  }
290 
299  throw()
300  {
301  if (name == NULL) return INVALID_ARGS;
302 
303  Entry* entry = m_map.find(name);
304  if (entry == NULL) return NOT_FOUND;
305  if (::CORBA::is_nil(entry->objref_)) return NOT_FOUND;
306 
307  PortableServer::ObjectId_var oid;
308  oid = m_poa->reference_to_id(entry->objref_);
309  m_poa->deactivate_object(oid);
310 
311  ServantFactory* factory = m_factory.find(entry->servant_->id());
312  factory->destroy(entry->servant_);
313  m_map.unregisterObject(entry->impl_->name());
314  delete entry;
315  return OK;
316  }
317 
326  throw()
327  {
328  if (name == NULL) return NULL;
329 
330  Entry* entry = m_map.find(name);
331  if (entry == NULL) return NULL;
332  return entry->impl_;
333  }
334 
343  throw()
344  {
345  if (servant == NULL) return NULL;
346 
347  std::cout << "toImpl(Servant)" << std::endl;
348  CORBAServantBase* csvt = dynamic_cast<CORBAServantBase*>(servant);
349 
350  std::cout << "name: " << csvt->name() << std::endl;
351  return getImpl(csvt->name());
352  }
353 
362  throw()
363  {
364  if (name == NULL) return NULL;
365 
366  Entry* entry = m_map.find(name);
367  if (entry == NULL) return NULL;
368  return entry->servant_;
369 
370  }
371 
380  throw()
381  {
382  if (impl == NULL) return NULL;
383  return getServant(impl->name());
384  }
385 
386  //------------------------------------------------------------
387  // CORBAManager interfaces
388  //------------------------------------------------------------
396  doil::ImplBase* CORBAManager::toImpl(::CORBA::Object_ptr obj)
397  throw()
398  {
399  PortableServer::ServantBase* svt = m_poa->reference_to_servant(obj);
400  CORBAServantBase* csvt = dynamic_cast<CORBAServantBase*>(svt);
401  if (csvt == NULL) return NULL;
402  return toImpl(csvt);
403  }
404 
412  ::CORBA::Object_ptr CORBAManager::getReference(const char* name)
413  throw()
414  {
415  Entry* entry = m_map.find(name);
416  return entry->objref_;
417  ;
418  }
419 
427  ::CORBA::Object_ptr CORBAManager::toReference(doil::ImplBase* impl)
428  throw()
429  {
430  return getReference(impl->name());
431  }
432 
440  ::CORBA::Object_ptr CORBAManager::toReference(doil::ServantBase* servant)
441  throw()
442  {
443  return getReference(servant->name());
444  }
445 
455  doil::ServantBase* CORBAManager::toServant(::CORBA::Object_ptr obj)
456  throw()
457  {
458  PortableServer::ServantBase* svt = m_poa->reference_to_servant(obj);
459  if (svt == NULL) return NULL;
460  CORBAServantBase* csvt = dynamic_cast<CORBAServantBase*>(svt);
461  if (csvt == NULL) return NULL;
462  return csvt;
463  }
464 
465  //------------------------------------------------------------
466  // CORBA functions
467  //------------------------------------------------------------
475  ::CORBA::ORB_ptr CORBAManager::getORB()
476  throw()
477  {
478  return m_orb;
479  }
480 
488  ::PortableServer::POA_ptr CORBAManager::getPOA()
489  throw()
490  {
491  return m_poa;
492  }
493 
501  ::PortableServer::POAManager_ptr CORBAManager::getPOAManager()
502  throw()
503  {
504  return m_poaManager;
505  }
506 
507 
508  //------------------------------------------------------------
509  // protected:
510  //------------------------------------------------------------
512  {
513  m_config = prop;
514  std::vector<std::string> args(coil::split(createORBOptions(), " "));
515  // TAO's ORB_init needs argv[0] as command name.
516  args.insert(args.begin(), "manager");
517  char** argv = coil::toArgv(args);
518  int argc(args.size());
519 
520  try
521  {
522 
523  m_orb = ::CORBA::ORB_init(argc, argv);
524  assert(!::CORBA::is_nil(m_orb));
525 
526  ::CORBA::Object_var obj = m_orb->resolve_initial_references("RootPOA");
527  m_poa = PortableServer::POA::_narrow(obj);
528  assert(!::CORBA::is_nil(m_poa));
529  m_poaManager = m_poa->the_POAManager();
530  m_poaManager->activate();
531  }
532  catch (...)
533  {
534  return;
535  }
536  }
537 
538 
540  {
541  std::string opt(m_config["args"]);
542  std::string corba(m_config["id"]);
543  std::string endpoint(m_config["endpoint"]);
544 
545  if (!endpoint.empty())
546  {
547  if (!opt.empty()) opt += " ";
548  if (corba == "omniORB") opt = "-ORBendPoint giop:tcp:" + endpoint;
549  else if (corba == "TAO") opt = "-ORBEndPoint iiop://" + endpoint;
550  else if (corba == "MICO") opt = "-ORBIIOPAddr inet:" + endpoint;
551  }
552  return opt;
553  }
554 };
555 };
556 
557 
558 extern "C"
559 {
561  {
564  }
565 }
static CORBAManager * manager
Definition: CORBAManager.h:546
virtual const char * name()
Getting ORB&#39;s name.
virtual ServantBase * create()
::PortableServer::POA_ptr getPOA()
Getting default POA pointer.
virtual ServantBase * toServant(doil::ImplBase *lobj)
Getting servant object by impl object.
ServantBase *(* ServantNewFunc)()
virtual const char * name() const
void(* ProxyDeleteFunc)(ProxyBase *)
Definition: ProxyFactory.h:31
void DoilCORBAInit(coil::Properties &prop)
virtual ReturnCode_t registerFactory(const char *id, doil::ServantNewFunc new_func, doil::ServantDeleteFunc delete_func)
Register servant&#39;s factory.
Mutex class.
static coil::Mutex mutex
Definition: CORBAManager.h:547
coil::Properties m_config
Definition: CORBAManager.h:559
::CORBA::ORB_ptr getORB()
Getting ORB pointer.
ReturnCode_t
Definition: doil.h:53
ObjectManager< const char *, Entry, EntryPred > m_map
Definition: CORBAManager.h:599
Object * find(const Identifier &id) const
Find the object.
virtual ReturnCode_t registerProxyFactory(const char *id, doil::ProxyNewFunc new_func, doil::ProxyDeleteFunc delete_func)
virtual ImplBase * toImpl(doil::ServantBase *servant)
Getting impl object by servant.
static CORBAManager & instance()
getting instance
RTC::ReturnCode_t ret(RTC::Local::ReturnCode_t r)
vstring split(const std::string &input, const std::string &delimiter, bool ignore_empty)
Split string by delimiter.
Definition: stringutil.cpp:341
virtual ServantBase * getServant(const char *name)
Getting servant object by name.
ObjectManager< const char *, ProxyFactory, ProxyFactoryPred > m_factory_proxy
Definition: CORBAManager.h:688
static CORBAManager * init(coil::Properties prop)
initializer
void initOrb(coil::Properties prop)
CORBA ORB initialization.
::CORBA::Object_ptr toReference(doil::ImplBase *impl)
Converting Impl object to object reference.
::CORBA::Object_ptr getReference(const char *name)
Getting object reference from the given name.
static ORBManager & instance()
getting instance
Definition: ORBManager.cpp:61
#define UNUSED_ARG(a)
virtual ImplBase * getImpl(const char *name)
Getting object by name.
CORBA manager for doil.
bool registerObject(Object *obj)
Register the specified object.
ProxyBase *(* ProxyNewFunc)(::CORBA::Object_ptr obj)
Definition: ProxyFactory.h:30
void(* ServantDeleteFunc)(ServantBase *)
::PortableServer::POAManager_ptr getPOAManager()
Getting POAManager pointer.
Object * unregisterObject(const Identifier &id)
Unregister the specified object.
char ** toArgv(const vstring &args)
Convert the given string list into the argument list.
Definition: stringutil.cpp:568
prop
Organization::get_organization_property ();.
ReturnCode_t addORB(IORB *orb)
Register an ORB to the ORBManager.
Definition: ORBManager.cpp:96
virtual const char * name()=0
Class represents a set of properties.
Definition: Properties.h:101
::CORBA::ORB_ptr m_orb
Definition: CORBAManager.h:550
ObjectManager< const char *, ServantFactory, FactoryPred > m_factory
Definition: CORBAManager.h:666
virtual ReturnCode_t deactivateObject(doil::ImplBase *impl)
Deactivate object.
virtual void shutdown()
Shutdown ORB.
virtual const char * id() const
::PortableServer::POAManager_ptr m_poaManager
Definition: CORBAManager.h:556
std::string createORBOptions()
Create ORB command options.
Definition: doil.h:55
::PortableServer::POA_ptr m_poa
Definition: CORBAManager.h:553
virtual ReturnCode_t activateObject(doil::ImplBase *impl)
Activate object.
virtual void destroy(ServantBase *servant)


openrtm_aist
Author(s): Noriaki Ando
autogenerated on Mon Jun 10 2019 14:07:51