00001
00020 #ifndef RTC_CORBAPORT_H
00021 #define RTC_CORBAPORT_H
00022
00023 #include <rtm/PortBase.h>
00024 #include <rtm/CorbaConsumer.h>
00025 #include <rtm/NVUtil.h>
00026 #include <list>
00027
00042 namespace RTC
00043 {
00620 class CorbaPort
00621 : public PortBase
00622 {
00623 public:
00650 CorbaPort(const char* name);
00651
00663 virtual ~CorbaPort(void);
00664
00692 void init(coil::Properties& prop);
00693
00732 bool registerProvider(const char* instance_name, const char* type_name,
00733 PortableServer::RefCountServantBase& provider);
00734
00776 bool registerConsumer(const char* instance_name, const char* type_name,
00777 CorbaConsumerBase& consumer);
00778
00779 protected:
00861 virtual ReturnCode_t
00862 publishInterfaces(ConnectorProfile& connector_profile);
00863
00996 virtual ReturnCode_t
00997 subscribeInterfaces(const ConnectorProfile& connector_profile);
00998
01020 virtual void
01021 unsubscribeInterfaces(const ConnectorProfile& connector_profile);
01022
01023
01024
01025
01042 virtual void activateInterfaces();
01043
01060 virtual void deactivateInterfaces();
01061
01062 protected:
01070 coil::Properties m_properties;
01071
01072 private:
01073 class CorbaConsumerHolder;
01104 virtual bool findProvider(const NVList& nv, CorbaConsumerHolder& cons,
01105 std::string& iorstr);
01106
01141 virtual bool findProviderOld(const NVList&nv, CorbaConsumerHolder& cons,
01142 std::string& iorstr);
01143
01171 bool setObject(const std::string& ior, CorbaConsumerHolder& cons);
01172
01199 bool releaseObject(const std::string& ior, CorbaConsumerHolder& cons);
01200
01201 private:
01217 class CorbaProviderHolder
01218 {
01219 public:
01220 CorbaProviderHolder(const char* type_name,
01221 const char* instance_name,
01222 PortableServer::RefCountServantBase* servant)
01223 : m_typeName(type_name),
01224 m_instanceName(instance_name),
01225 m_servant(servant),
01226 m_ior()
01227 {
01228 #ifndef ORB_IS_RTORB
01229 m_oid = Manager::instance().getPOA()->servant_to_id(m_servant);
01230 try
01231 {
01232 Manager::instance().
01233 getPOA()->activate_object_with_id(m_oid, m_servant);
01234 }
01235 catch(...)
01236 {
01237 ;
01238 }
01239 CORBA::Object_var obj;
01240 obj = Manager::instance().getPOA()->id_to_reference(m_oid);
01241 CORBA::ORB_ptr orb = Manager::instance().getORB();
01242 CORBA::String_var ior_var = orb->object_to_string(obj);
01243 m_ior = ior_var;
01244 deactivate();
01245 #else // ORB_IS_RTORB
01246
01247
01248 m_oid = Manager::instance().getPOA()->servant_to_id(m_servant);
01249 CORBA::Object_var obj;
01250 obj = CORBA::Object_var(m_servant->__this());
01251 CORBA::ORB_ptr orb = Manager::instance().getORB();
01252 CORBA::String_var ior_var = orb->object_to_string(obj);
01253 m_ior = ior_var;
01254 #endif // ORB_IS_RTORB
01255 }
01256 virtual ~CorbaProviderHolder()
01257 {
01258 deactivate();
01259 }
01260 std::string instanceName() { return m_instanceName; }
01261 std::string typeName() { return m_typeName; }
01262 std::string ior() { return m_ior; }
01263 std::string descriptor() { return m_typeName + "." + m_instanceName; }
01264
01265 void activate()
01266 {
01267 try
01268 {
01269 Manager::instance().
01270 getPOA()->activate_object_with_id(m_oid, m_servant);
01271 }
01272 catch(const ::PortableServer::POA::ServantAlreadyActive &)
01273 {
01274 ;
01275 }
01276 catch(...)
01277 {
01278 ;
01279 }
01280 }
01281 void deactivate()
01282 {
01283 try
01284 {
01285 Manager::instance().getPOA()->deactivate_object(m_oid);
01286 }
01287 catch(...)
01288 {
01289 ;
01290 }
01291 }
01292 private:
01293 std::string m_typeName;
01294 std::string m_instanceName;
01295 PortableServer::RefCountServantBase* m_servant;
01296 PortableServer::ObjectId_var m_oid;
01297 std::string m_ior;
01298 };
01299
01307 typedef std::vector<CorbaProviderHolder> CorbaProviderList;
01308 CorbaProviderList m_providers;
01309
01317 class CorbaConsumerHolder
01318 {
01319 public:
01320 CorbaConsumerHolder(const char* type_name,
01321 const char* instance_name,
01322 CorbaConsumerBase* consumer)
01323 : m_typeName(type_name),
01324 m_instanceName(instance_name),
01325 m_consumer(consumer),
01326 m_ior("")
01327 {
01328 }
01329 std::string instanceName() { return m_instanceName; }
01330 std::string typeName() { return m_typeName; }
01331 std::string descriptor() { return m_typeName + "." + m_instanceName; }
01332
01333 bool setObject(const char* ior)
01334 {
01335 m_ior = ior;
01336 CORBA::ORB_ptr orb = ::RTC::Manager::instance().getORB();
01337 CORBA::Object_var obj = orb->string_to_object(ior);
01338 if (CORBA::is_nil(obj))
01339 {
01340 return false;
01341 }
01342
01343 return m_consumer->setObject(obj.in());
01344 }
01345 void releaseObject()
01346 {
01347 m_consumer->releaseObject();
01348 }
01349 const std::string& getIor()
01350 {
01351 return m_ior;
01352 }
01353 private:
01354 std::string m_typeName;
01355 std::string m_instanceName;
01356 CorbaConsumerBase* m_consumer;
01357 std::string m_ior;
01358 };
01359 typedef std::vector<CorbaConsumerHolder> CorbaConsumerList;
01360 CorbaConsumerList m_consumers;
01361
01362
01370 struct unsubscribe
01371 {
01372 unsubscribe(CorbaConsumerList& consumers)
01373 : m_consumers(consumers)
01374 {
01375 }
01376
01377 void operator()(const SDOPackage::NameValue& nv)
01378 {
01379 for (CorbaConsumerList::iterator it(m_consumers.begin());
01380 it != m_consumers.end(); ++it)
01381 {
01382 std::string name(nv.name);
01383 if (it->descriptor() == (const char*)nv.name)
01384 {
01385 it->releaseObject();
01386 }
01387 }
01388 }
01389 CorbaConsumerList& m_consumers;
01390 };
01391 };
01392 };
01393 #endif // RTC_CORBAPORT_H