InPortCorbaCdrConsumer.cpp
Go to the documentation of this file.
00001 // -*- C++ -*-
00020 #include <rtm/NVUtil.h>
00021 #include <rtm/InPortCorbaCdrConsumer.h>
00022 
00023 namespace RTC
00024 {
00033   InPortCorbaCdrConsumer::InPortCorbaCdrConsumer(void)
00034     : rtclog("InPortCorbaCdrConsumer")
00035   {
00036   }
00037   
00045   InPortCorbaCdrConsumer::~InPortCorbaCdrConsumer(void)
00046   {
00047     RTC_PARANOID(("~InPortCorbaCdrConsumer()"));
00048   }
00049 
00057   void InPortCorbaCdrConsumer::init(coil::Properties& prop)
00058   {
00059     m_properties = prop;
00060   }
00061 
00069   InPortConsumer::ReturnCode InPortCorbaCdrConsumer::
00070   put(const cdrMemoryStream& data)
00071   {
00072     RTC_PARANOID(("put()"));
00073 
00074 #ifndef ORB_IS_RTORB
00075     ::OpenRTM::CdrData tmp(data.bufSize(), data.bufSize(),
00076                            static_cast<CORBA::Octet*>(data.bufPtr()), 0);
00077 #else // ORB_IS_RTORB
00078     OpenRTM_CdrData *cdrdata_tmp = new OpenRTM_CdrData();
00079     cdrdata_tmp->_buffer = 
00080       (CORBA_octet *)RtORB_alloc(data.bufSize(), "InPortCorbaCdrComsumer::put");
00081     memcpy(cdrdata_tmp->_buffer, data.bufPtr(), data.bufSize());
00082     cdrdata_tmp->_length = cdrdata_tmp->_maximum= data.bufSize();
00083     ::OpenRTM::CdrData tmp(cdrdata_tmp);
00084 #endif // ORB_IS_RTORB
00085     try
00086       {
00087         // return code conversion
00088         // (IDL)OpenRTM::DataPort::ReturnCode_t -> DataPortStatus
00089         return convertReturnCode(_ptr()->put(tmp));
00090       }
00091     catch (...)
00092       {
00093         return CONNECTION_LOST;
00094       }
00095     return UNKNOWN_ERROR;
00096   }
00097   
00105   void InPortCorbaCdrConsumer::
00106   publishInterfaceProfile(SDOPackage::NVList& properties)
00107   {
00108     return;
00109   }
00110 
00118   bool InPortCorbaCdrConsumer::
00119   subscribeInterface(const SDOPackage::NVList& properties)
00120   {
00121     RTC_TRACE(("subscribeInterface()"));
00122     RTC_DEBUG_STR((NVUtil::toString(properties)));
00123     
00124     // getting InPort's ref from IOR string
00125     if (subscribeFromIor(properties)) { return true; }
00126     
00127     // getting InPort's ref from Object reference
00128     if (subscribeFromRef(properties)) { return true; }
00129     
00130     return false;;
00131   }
00132   
00140   void InPortCorbaCdrConsumer::
00141   unsubscribeInterface(const SDOPackage::NVList& properties)
00142   {
00143     RTC_TRACE(("unsubscribeInterface()"));
00144     RTC_DEBUG_STR((NVUtil::toString(properties)));
00145     
00146     if (unsubscribeFromIor(properties)) { return; }
00147     unsubscribeFromRef(properties);
00148   }
00149   
00150   //----------------------------------------------------------------------
00151   // private functions
00152 
00160   bool InPortCorbaCdrConsumer::
00161   subscribeFromIor(const SDOPackage::NVList& properties)
00162   {
00163     RTC_TRACE(("subscribeFromIor()"));
00164     
00165     CORBA::Long index;
00166     index = NVUtil::find_index(properties,
00167                                "dataport.corba_cdr.inport_ior");
00168     if (index < 0)
00169       {
00170         RTC_ERROR(("inport_ior not found"));
00171         return false;
00172       }
00173     
00174     const char* ior(0);
00175     if (!(properties[index].value >>= ior))
00176       {
00177         RTC_ERROR(("inport_ior has no string"));
00178         return false;
00179       }
00180     
00181     CORBA::ORB_ptr orb = RTC::Manager::instance().getORB();
00182     CORBA::Object_var obj = orb->string_to_object(ior);
00183     
00184     if (CORBA::is_nil(obj))
00185       {
00186         RTC_ERROR(("invalid IOR string has been passed"));
00187         return false;
00188       }
00189     
00190     if (!setObject(obj.in()))
00191       {
00192         RTC_WARN(("Setting object to consumer failed."));
00193         return false;
00194       }
00195     return true;
00196   }
00197   
00205   bool InPortCorbaCdrConsumer::
00206   subscribeFromRef(const SDOPackage::NVList& properties)
00207   {
00208     RTC_TRACE(("subscribeFromRef()"));
00209     CORBA::Long index;
00210     index = NVUtil::find_index(properties,
00211                                "dataport.corba_cdr.inport_ref");
00212     if (index < 0)
00213       {
00214         RTC_ERROR(("inport_ref not found"));
00215         return false;
00216       }
00217     
00218     CORBA::Object_var obj;
00219     if (!(properties[index].value >>= CORBA::Any::to_object(obj.out())))
00220       {
00221         RTC_ERROR(("prop[inport_ref] is not objref"));
00222         return true;
00223       }
00224     
00225     if (CORBA::is_nil(obj))
00226       {
00227         RTC_ERROR(("prop[inport_ref] is not objref"));
00228         return false;
00229       }
00230     
00231     if (!setObject(obj.in()))
00232       {
00233         RTC_ERROR(("Setting object to consumer failed."));
00234         return false;
00235       }
00236     return true;
00237   }
00238   
00246   bool InPortCorbaCdrConsumer::
00247   unsubscribeFromIor(const SDOPackage::NVList& properties)
00248   {
00249     RTC_TRACE(("unsubscribeFromIor()"));
00250     CORBA::Long index;
00251     index = NVUtil::find_index(properties,
00252                                "dataport.corba_cdr.inport_ior");
00253     if (index < 0)
00254       {
00255         RTC_ERROR(("inport_ior not found"));
00256         return false;
00257       }
00258     
00259     const char* ior;
00260     if (!(properties[index].value >>= ior))
00261       {
00262         RTC_ERROR(("prop[inport_ior] is not string"));
00263         return false;
00264       }
00265     
00266     CORBA::ORB_ptr orb = RTC::Manager::instance().getORB();
00267     CORBA::Object_var var = orb->string_to_object(ior);
00268     if (!(_ptr()->_is_equivalent(var)))
00269       {
00270         RTC_ERROR(("connector property inconsistency"));
00271         return false;
00272       }
00273     
00274     releaseObject();
00275     return true;
00276   }
00277   
00285   bool InPortCorbaCdrConsumer::
00286   unsubscribeFromRef(const SDOPackage::NVList& properties)
00287   {
00288     RTC_TRACE(("unsubscribeFromRef()"));
00289     CORBA::Long index;
00290     index = NVUtil::find_index(properties,
00291                                "dataport.corba_cdr.inport_ref");
00292     if (index < 0) { return false; }
00293     
00294     CORBA::Object_var obj;
00295     if (!(properties[index].value >>= CORBA::Any::to_object(obj.out()))) 
00296       {
00297         return false;
00298       }
00299     
00300     if (!(_ptr()->_is_equivalent(obj.in()))) { return false; }
00301     
00302     releaseObject();
00303     return true;
00304   }
00305 
00313   InPortConsumer::ReturnCode
00314   InPortCorbaCdrConsumer::convertReturnCode(OpenRTM::PortStatus ret)
00315   {
00316     switch (ret)
00317       {
00318       case OpenRTM::PORT_OK:
00319         return InPortConsumer::PORT_OK;
00320         break;
00321       case OpenRTM::PORT_ERROR:
00322         return InPortConsumer::PORT_ERROR;
00323         break;
00324       case OpenRTM::BUFFER_FULL:
00325         return InPortConsumer::SEND_FULL;
00326         break;
00327       case OpenRTM::BUFFER_TIMEOUT:
00328         return InPortConsumer::SEND_TIMEOUT;
00329         break;
00330       case OpenRTM::UNKNOWN_ERROR:
00331         return InPortConsumer::UNKNOWN_ERROR;
00332         break;
00333       default:
00334         return InPortConsumer::UNKNOWN_ERROR;
00335         break;
00336       }
00337     return InPortConsumer::UNKNOWN_ERROR;
00338   }
00339   
00340 };     // namespace RTC
00341 
00342 extern "C"
00343 { 
00351   void InPortCorbaCdrConsumerInit(void)
00352   {
00353     RTC::InPortConsumerFactory& factory(RTC::InPortConsumerFactory::instance());
00354     factory.addFactory("corba_cdr",
00355                        ::coil::Creator< ::RTC::InPortConsumer,
00356                                         ::RTC::InPortCorbaCdrConsumer>,
00357                        ::coil::Destructor< ::RTC::InPortConsumer,
00358                                            ::RTC::InPortCorbaCdrConsumer>);
00359   }
00360 };


openrtm_aist
Author(s): Noriaki Ando
autogenerated on Sun Mar 26 2017 03:37:15