Go to the documentation of this file.00001
00018 #include <assert.h>
00019 #include <iostream>
00020 #include <coil/stringutil.h>
00021 #include <doil/ice/IceManager.h>
00022 #include <doil/ORBManager.h>
00023
00024 #define UNUSED_ARG(a) do {} while (&a == 0)
00025
00026 namespace doil
00027 {
00028 namespace Ice
00029 {
00030
00031 IceManager* IceManager::manager = NULL;
00032 coil::Mutex IceManager::mutex;
00033
00034
00042 IceManager* IceManager::init(coil::Properties prop)
00043 throw()
00044 {
00045 if (!manager)
00046 {
00047 coil::Guard<coil::Mutex> guard(mutex);
00048 if (!manager)
00049 {
00050 manager = new IceManager();
00051 manager->initIce(prop);
00052 }
00053 }
00054 return manager;
00055 };
00056
00064 IceManager& IceManager::instance()
00065 throw()
00066 {
00067 coil::Properties prop;
00068 return *IceManager::init(prop);
00069 }
00070
00078 const char* IceManager::name()
00079 throw()
00080 {
00081 return "ice";
00082 }
00083 void IceManager::run()
00084 throw()
00085 {
00086
00087 return;
00088 }
00089
00090
00091 void IceManager::shutdown()
00092 throw()
00093 {
00094 if (m_ice)
00095 {
00096 try
00097 {
00098 m_ice->destroy();
00099 }
00100 catch (const ::Ice::Exception& e)
00101 {
00102 std::cerr << e << std::endl;
00103 }
00104 }
00105 }
00106
00107
00115 ReturnCode_t IceManager::registerFactory(const char* id,
00116 doil::ServantNewFunc new_func,
00117 doil::ServantDeleteFunc delete_func)
00118 throw()
00119 {
00120 if (id == NULL) return INVALID_ARGS;
00121 if (new_func == NULL) return INVALID_ARGS;
00122 if (delete_func == NULL) return INVALID_ARGS;
00123
00124 bool ret;
00125 ServantFactory* factory = new ServantFactory(id, new_func, delete_func);
00126 ret = m_factory.registerObject(factory);
00127 if (ret) return OK;
00128 else return ALREADY_EXISTS;
00129 }
00130
00138 ReturnCode_t IceManager::activateObject(doil::ImplBase* impl)
00139 throw()
00140 {
00141 if (impl == NULL) return INVALID_ARGS;
00142
00143 const char* id = impl->id();
00144 std::cout << "IceManager::activateObject: id " << id << std::endl;
00145 std::cout << "IceManager::activateObject: name " << impl->name() << std::endl;
00146 ServantFactory* factory = m_factory.find(id);
00147
00148
00149 if (factory == NULL) return NOT_FOUND;
00150
00151 try
00152 {
00153
00154 doil::ServantBase* svt = factory->create(impl);
00155 IceServantBase* csvt = dynamic_cast<IceServantBase*>(svt);
00156 if (csvt == NULL)
00157 {
00158 std::cout << "dynamic_cast<IceServantBase*> failed" << std::endl;
00159 delete svt;
00160 return INVALID_ARGS;
00161 }
00162 return activateObject(impl, csvt);
00163 }
00164 catch (std::bad_alloc& e)
00165 {
00166 UNUSED_ARG(e);
00167 return INVALID_ARGS;
00168 }
00169 catch (...)
00170 {
00171 return UNKNOWN;
00172 }
00173 return UNKNOWN;
00174 }
00175
00183 ReturnCode_t IceManager::activateObject(doil::ImplBase* impl,
00184 doil::ServantBase* servant)
00185 throw()
00186 {
00187 if (impl == NULL) return INVALID_ARGS;
00188 if (servant == NULL) return INVALID_ARGS;
00189
00190
00191 if (strcmp(impl->id(), servant->id()) != 0)
00192 return INVALID_ARGS;
00193
00194 doil::Ice::IceServantBase* svt;
00195 svt = dynamic_cast<doil::Ice::IceServantBase*>(servant);
00196
00197 if (svt == NULL) return INVALID_ARGS;
00198
00199
00200 ::Ice::ObjectPrx obj;
00201 obj = m_adapter->add(svt,
00202 m_ice->stringToIdentity(impl->name()));
00203
00204 Entry* entry = new Entry(impl, svt, obj);
00205 if (m_map.registerObject(entry)) return OK;
00206 return UNKNOWN;
00207 }
00208
00216 ReturnCode_t IceManager::deactivateObject(doil::ImplBase* impl)
00217 throw()
00218 {
00219 if (impl == NULL) return INVALID_ARGS;
00220 return deactivateObject(impl->name());
00221 }
00222
00230 ReturnCode_t IceManager::deactivateObject(const char* name)
00231 throw()
00232 {
00233 if (name == NULL) return INVALID_ARGS;
00234
00235 Entry* entry = m_map.find(name);
00236 if (entry == NULL) return NOT_FOUND;
00237
00238
00239
00240 ::Ice::ObjectPtr obj;
00241 obj = m_adapter->remove(m_ice->stringToIdentity(name));
00242
00243
00244 ServantFactory* factory = m_factory.find(entry->servant_->id());
00245 factory->destroy(entry->servant_);
00246 m_map.unregisterObject(entry->impl_->name());
00247 delete entry;
00248 return OK;
00249 }
00250
00258 doil::ImplBase* IceManager::getImpl(const char* name)
00259 throw()
00260 {
00261 if (name == NULL) return NULL;
00262
00263 Entry* entry = m_map.find(name);
00264 if (entry == NULL) return NULL;
00265 return entry->impl_;
00266 }
00267
00275 doil::ImplBase* IceManager::toImpl(doil::ServantBase* servant)
00276 throw()
00277 {
00278 if (servant == NULL) return NULL;
00279
00280 std::cout << "toImpl(Servant)" << std::endl;
00281 IceServantBase* csvt = dynamic_cast<IceServantBase*>(servant);
00282
00283 std::cout << "name: " << csvt->name() << std::endl;
00284 return getImpl(csvt->name());
00285 }
00286
00294 doil::ServantBase* IceManager::getServant(const char* name)
00295 throw()
00296 {
00297 if (name == NULL) return NULL;
00298
00299 Entry* entry = m_map.find(name);
00300 if (entry == NULL) return NULL;
00301 return entry->servant_;
00302
00303 }
00304
00312 doil::ServantBase* IceManager::toServant(doil::ImplBase* impl)
00313 throw()
00314 {
00315 if (impl == NULL) return NULL;
00316 return getServant(impl->name());
00317 }
00318
00319
00320
00321
00329 doil::ImplBase* IceManager::toImpl(::Ice::ObjectPrx obj)
00330 throw()
00331 {
00332 find_by_obj p(obj);
00333 p = m_map.for_each(p);
00334 if (p.result_ == NULL) return NULL;
00335 return p.result_->impl_;
00336 }
00337
00345 ::Ice::ObjectPrx IceManager::getReference(const char* name)
00346 throw()
00347 {
00348 Entry* entry = m_map.find(name);
00349 return entry->objref_;
00350 ;
00351 }
00352
00360 ::Ice::ObjectPrx IceManager::toReference(doil::ImplBase* impl)
00361 throw()
00362 {
00363 return getReference(impl->name());
00364 }
00365
00373 ::Ice::ObjectPrx IceManager::toReference(doil::ServantBase* servant)
00374 throw()
00375 {
00376 return getReference(servant->name());
00377 }
00378
00388 doil::ServantBase* IceManager::toServant(::Ice::ObjectPrx obj)
00389 throw()
00390 {
00391 return toServant(toImpl(obj));
00392 }
00393
00394
00395
00396
00404 ::Ice::CommunicatorPtr IceManager::getORB()
00405 throw()
00406 {
00407 return m_ice;
00408 }
00409
00417 ::Ice::ObjectAdapterPtr IceManager::getAdapter()
00418 throw()
00419 {
00420 return m_adapter;
00421 }
00422
00423
00424
00425
00426
00427 void IceManager::initIce(coil::Properties prop)
00428 {
00429 m_config = prop;
00430 std::vector<std::string> args(coil::split(createIceOptions(), " "));
00431
00432 args.insert(args.begin(), "manager");
00433 char** argv = coil::toArgv(args);
00434 int argc(args.size());
00435
00436 try
00437 {
00438 m_ice = ::Ice::initialize(argc, argv);
00439
00440 m_adapter = m_ice->createObjectAdapterWithEndpoints(
00441 "OpenRTM",
00442 "default -p 10000");
00443 m_adapter->activate();
00444 }
00445 catch (...)
00446 {
00447 return;
00448 }
00449 }
00450
00451
00452 std::string IceManager::createIceOptions()
00453 {
00454 std::string opt(m_config["args"]);
00455 return opt;
00456 }
00457 };
00458 };
00459
00460 extern "C"
00461 {
00462 void DoilIceInit(coil::Properties& prop)
00463 {
00464 doil::ORBManager& mgr(doil::ORBManager::instance());
00465 mgr.addORB((doil::Ice::IceManager::init(prop)));
00466 }
00467 }