IceManager.cpp
Go to the documentation of this file.
1 // -*- C++ -*-
18 #include <assert.h>
19 #include <iostream>
20 #include <coil/stringutil.h>
21 #include <doil/ice/IceManager.h>
22 #include <doil/ORBManager.h>
23 
24 #define UNUSED_ARG(a) do {} while (&a == 0)
25 
26 namespace doil
27 {
28 namespace Ice
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 IceManager();
51  manager->initIce(prop);
52  }
53  }
54  return manager;
55  };
56 
65  throw()
66  {
68  return *IceManager::init(prop);
69  }
70 
78  const char* IceManager::name()
79  throw()
80  {
81  return "ice";
82  }
84  throw()
85  {
86  // Do nothing
87  return;
88  }
89 
90 
92  throw()
93  {
94  if (m_ice)
95  {
96  try
97  {
98  m_ice->destroy();
99  }
100  catch (const ::Ice::Exception& e)
101  {
102  std::cerr << e << std::endl;
103  }
104  }
105  }
106 
107 
116  doil::ServantNewFunc new_func,
117  doil::ServantDeleteFunc delete_func)
118  throw()
119  {
120  if (id == NULL) return INVALID_ARGS;
121  if (new_func == NULL) return INVALID_ARGS;
122  if (delete_func == NULL) return INVALID_ARGS;
123 
124  bool ret;
125  ServantFactory* factory = new ServantFactory(id, new_func, delete_func);
126  ret = m_factory.registerObject(factory);
127  if (ret) return OK;
128  else return ALREADY_EXISTS;
129  }
130 
139  throw()
140  {
141  if (impl == NULL) return INVALID_ARGS;
142 
143  const char* id = impl->id();
144  std::cout << "IceManager::activateObject: id " << id << std::endl;
145  std::cout << "IceManager::activateObject: name " << impl->name() << std::endl;
146  ServantFactory* factory = m_factory.find(id);
147 
148  // Factory NOT_FOUND
149  if (factory == NULL) return NOT_FOUND;
150 
151  try
152  {
153  // INVALID_ARGS
154  doil::ServantBase* svt = factory->create(impl);
155  IceServantBase* csvt = dynamic_cast<IceServantBase*>(svt);
156  if (csvt == NULL)
157  {
158  std::cout << "dynamic_cast<IceServantBase*> failed" << std::endl;
159  delete svt;
160  return INVALID_ARGS;
161  }
162  return activateObject(impl, csvt);
163  }
164  catch (std::bad_alloc& e)
165  {
166  UNUSED_ARG(e);
167  return INVALID_ARGS;
168  }
169  catch (...)
170  {
171  return UNKNOWN;
172  }
173  return UNKNOWN;
174  }
175 
184  doil::ServantBase* servant)
185  throw()
186  {
187  if (impl == NULL) return INVALID_ARGS;
188  if (servant == NULL) return INVALID_ARGS;
189 
190  // check ID
191  if (strcmp(impl->id(), servant->id()) != 0)
192  return INVALID_ARGS;
193 
195  svt = dynamic_cast<doil::Ice::IceServantBase*>(servant);
196 
197  if (svt == NULL) return INVALID_ARGS;
198 
199  // activate Ice object
200  ::Ice::ObjectPrx obj;
201  obj = m_adapter->add(svt,
202  m_ice->stringToIdentity(impl->name()));
203 
204  Entry* entry = new Entry(impl, svt, obj);
205  if (m_map.registerObject(entry)) return OK;
206  return UNKNOWN;
207  }
208 
217  throw()
218  {
219  if (impl == NULL) return INVALID_ARGS;
220  return deactivateObject(impl->name());
221  }
222 
231  throw()
232  {
233  if (name == NULL) return INVALID_ARGS;
234 
235  Entry* entry = m_map.find(name);
236  if (entry == NULL) return NOT_FOUND;
237  // ### objref should be checked if nil reference
238  // if (::Ice::is_nil(entry->objref_)) return NOT_FOUND;
239 
240  ::Ice::ObjectPtr obj;
241  obj = m_adapter->remove(m_ice->stringToIdentity(name));
242  // ### obj should be deleted.
243 
244  ServantFactory* factory = m_factory.find(entry->servant_->id());
245  factory->destroy(entry->servant_);
246  m_map.unregisterObject(entry->impl_->name());
247  delete entry;
248  return OK;
249  }
250 
259  throw()
260  {
261  if (name == NULL) return NULL;
262 
263  Entry* entry = m_map.find(name);
264  if (entry == NULL) return NULL;
265  return entry->impl_;
266  }
267 
276  throw()
277  {
278  if (servant == NULL) return NULL;
279 
280  std::cout << "toImpl(Servant)" << std::endl;
281  IceServantBase* csvt = dynamic_cast<IceServantBase*>(servant);
282 
283  std::cout << "name: " << csvt->name() << std::endl;
284  return getImpl(csvt->name());
285  }
286 
295  throw()
296  {
297  if (name == NULL) return NULL;
298 
299  Entry* entry = m_map.find(name);
300  if (entry == NULL) return NULL;
301  return entry->servant_;
302 
303  }
304 
313  throw()
314  {
315  if (impl == NULL) return NULL;
316  return getServant(impl->name());
317  }
318 
319  //------------------------------------------------------------
320  // IceManager interfaces
321  //------------------------------------------------------------
329  doil::ImplBase* IceManager::toImpl(::Ice::ObjectPrx obj)
330  throw()
331  {
332  find_by_obj p(obj);
333  p = m_map.for_each(p);
334  if (p.result_ == NULL) return NULL;
335  return p.result_->impl_;
336  }
337 
345  ::Ice::ObjectPrx IceManager::getReference(const char* name)
346  throw()
347  {
348  Entry* entry = m_map.find(name);
349  return entry->objref_;
350  ;
351  }
352 
360  ::Ice::ObjectPrx IceManager::toReference(doil::ImplBase* impl)
361  throw()
362  {
363  return getReference(impl->name());
364  }
365 
373  ::Ice::ObjectPrx IceManager::toReference(doil::ServantBase* servant)
374  throw()
375  {
376  return getReference(servant->name());
377  }
378 
389  throw()
390  {
391  return toServant(toImpl(obj));
392  }
393 
394  //------------------------------------------------------------
395  // Ice functions
396  //------------------------------------------------------------
404  ::Ice::CommunicatorPtr IceManager::getORB()
405  throw()
406  {
407  return m_ice;
408  }
409 
417  ::Ice::ObjectAdapterPtr IceManager::getAdapter()
418  throw()
419  {
420  return m_adapter;
421  }
422 
423 
424  //------------------------------------------------------------
425  // protected:
426  //------------------------------------------------------------
428  {
429  m_config = prop;
430  std::vector<std::string> args(coil::split(createIceOptions(), " "));
431  // TAO's ORB_init needs argv[0] as command name.
432  args.insert(args.begin(), "manager");
433  char** argv = coil::toArgv(args);
434  int argc(args.size());
435 
436  try
437  {
438  m_ice = ::Ice::initialize(argc, argv);
439  // assert(!::Ice::is_nil(m_orb));
440  m_adapter = m_ice->createObjectAdapterWithEndpoints(
441  "OpenRTM",
442  "default -p 10000");
443  m_adapter->activate();
444  }
445  catch (...)
446  {
447  return;
448  }
449  }
450 
451 
453  {
454  std::string opt(m_config["args"]);
455  return opt;
456  }
457 };
458 };
459 
460 extern "C"
461 {
463  {
465  mgr.addORB((doil::Ice::IceManager::init(prop)));
466  }
467 }
::Ice::CommunicatorPtr m_ice
Definition: IceManager.h:519
Ice manager for doil.
virtual ImplBase * toImpl(doil::ServantBase *servant)
Getting impl object by servant.
Definition: IceManager.cpp:275
static coil::Mutex mutex
Definition: IceManager.h:516
virtual ServantBase * create()
ServantBase *(* ServantNewFunc)()
::Ice::ObjectAdapterPtr m_adapter
Definition: IceManager.h:522
Mutex class.
ReturnCode_t
Definition: doil.h:53
Object * find(const Identifier &id) const
Find the object.
virtual ImplBase * getImpl(const char *name)
Getting object by name.
Definition: IceManager.cpp:258
RTC::ReturnCode_t ret(RTC::Local::ReturnCode_t r)
IceServantBase * servant_
Definition: IceManager.h:541
vstring split(const std::string &input, const std::string &delimiter, bool ignore_empty)
Split string by delimiter.
Definition: stringutil.cpp:341
virtual void run()
Definition: IceManager.cpp:83
static ORBManager & instance()
getting instance
Definition: ORBManager.cpp:61
ObjectManager< const char *, ServantFactory, FactoryPred > m_factory
Definition: IceManager.h:582
virtual ReturnCode_t registerFactory(const char *id, doil::ServantNewFunc new_func, doil::ServantDeleteFunc delete_func)
Register servant&#39;s factory.
Definition: IceManager.cpp:115
bool registerObject(Object *obj)
Register the specified object.
#define UNUSED_ARG(a)
Definition: IceManager.cpp:24
void(* ServantDeleteFunc)(ServantBase *)
Object * unregisterObject(const Identifier &id)
Unregister the specified object.
::Ice::ObjectPrx toReference(doil::ImplBase *impl)
Converting Impl object to object reference.
Definition: IceManager.cpp:360
char ** toArgv(const vstring &args)
Convert the given string list into the argument list.
Definition: stringutil.cpp:568
virtual const char * name() const
::Ice::CommunicatorPtr getORB()
Getting ORB pointer.
Definition: IceManager.cpp:404
virtual ReturnCode_t deactivateObject(doil::ImplBase *impl)
Deactivate object.
Definition: IceManager.cpp:216
virtual ServantBase * getServant(const char *name)
Getting servant object by name.
Definition: IceManager.cpp:294
Pred for_each(Pred p)
Functor for searching object.
prop
Organization::get_organization_property ();.
virtual void shutdown()
Shutdown ORB.
Definition: IceManager.cpp:91
ReturnCode_t addORB(IORB *orb)
Register an ORB to the ORBManager.
Definition: ORBManager.cpp:96
::Ice::ObjectPrx objref_
Definition: IceManager.h:542
virtual const char * name()=0
Class represents a set of properties.
Definition: Properties.h:101
::Ice::ObjectAdapterPtr getAdapter()
Getting default POA pointer.
Definition: IceManager.cpp:417
virtual ServantBase * toServant(doil::ImplBase *lobj)
Getting servant object by impl object.
Definition: IceManager.cpp:312
static IceManager * init(coil::Properties prop)
initializer
Definition: IceManager.cpp:42
::Ice::ObjectPrx getReference(const char *name)
Getting object reference from the given name.
Definition: IceManager.cpp:345
static IceManager * manager
Definition: IceManager.h:515
void initIce(coil::Properties prop)
Ice ORB initialization.
Definition: IceManager.cpp:427
void DoilIceInit(coil::Properties &prop)
Definition: IceManager.cpp:462
virtual const char * name()
Getting ORB&#39;s name.
Definition: IceManager.cpp:78
virtual const char * id() const
std::string createIceOptions()
Create ORB command options.
Definition: IceManager.cpp:452
Definition: doil.h:55
static IceManager & instance()
getting instance
Definition: IceManager.cpp:64
virtual void destroy(ServantBase *servant)
coil::Properties m_config
Definition: IceManager.h:525
virtual ReturnCode_t activateObject(doil::ImplBase *impl)
Activate object.
Definition: IceManager.cpp:138


openrtm_aist
Author(s): Noriaki Ando
autogenerated on Thu Jun 6 2019 19:25:58