OutPortCorbaCdrConsumer.cpp
Go to the documentation of this file.
00001 // -*- C++ -*-
00020 #include <rtm/Manager.h>
00021 #include <rtm/OutPortCorbaCdrConsumer.h>
00022 #include <rtm/NVUtil.h>
00023 
00024 namespace RTC
00025 {
00033   OutPortCorbaCdrConsumer::OutPortCorbaCdrConsumer()
00034   {
00035     rtclog.setName("OutPortCorbaCdrConsumer");
00036   }
00037     
00045   OutPortCorbaCdrConsumer::~OutPortCorbaCdrConsumer(void)
00046   {
00047   } 
00048 
00056   void OutPortCorbaCdrConsumer::init(coil::Properties& prop)
00057   {
00058     RTC_TRACE(("OutPortCorbaCdrConsumer::init()"));
00059   }
00060 
00068   void OutPortCorbaCdrConsumer::setBuffer(CdrBufferBase* buffer)
00069   {
00070     RTC_TRACE(("OutPortCorbaCdrConsumer::setBuffer()"));
00071     m_buffer = buffer;
00072   }
00073 
00081   void OutPortCorbaCdrConsumer::setListener(ConnectorInfo& info,
00082                                             ConnectorListeners* listeners)
00083   {
00084     RTC_TRACE(("OutPortCorbaCdrConsumer::setListener()"));
00085     m_listeners = listeners;
00086     m_profile = info;
00087   }
00088 
00096   OutPortConsumer::ReturnCode
00097   OutPortCorbaCdrConsumer::get(cdrMemoryStream& data)
00098   {
00099     RTC_TRACE(("OutPortCorbaCdrConsumer::get()"));
00100     ::OpenRTM::CdrData_var cdr_data;
00101 
00102     try
00103       {
00104         ::OpenRTM::PortStatus ret(_ptr()->get(cdr_data.out()));
00105 
00106         if (ret == ::OpenRTM::PORT_OK)
00107           {
00108             RTC_DEBUG(("get() successful"));
00109             data.put_octet_array(&(cdr_data[0]), (int)cdr_data->length());
00110             RTC_PARANOID(("CDR data length: %d", cdr_data->length()));
00111 
00112             onReceived(data);
00113             onBufferWrite(data);
00114 
00115             if (m_buffer->full())
00116               {
00117                 RTC_INFO(("InPort buffer is full."));
00118                 onBufferFull(data);
00119                 onReceiverFull(data);
00120               }
00121             m_buffer->put(data);
00122             m_buffer->advanceWptr();
00123             m_buffer->advanceRptr();
00124 
00125             return PORT_OK;
00126           }
00127         return convertReturn(ret, data);
00128       }
00129     catch (...)
00130       {
00131         RTC_WARN(("Exception caought from OutPort::get()."));
00132         return CONNECTION_LOST;
00133       }
00134     RTC_ERROR(("OutPortCorbaCdrConsumer::get(): Never comes here."));
00135     return UNKNOWN_ERROR;
00136   }
00137     
00145   bool OutPortCorbaCdrConsumer::
00146   subscribeInterface(const SDOPackage::NVList& properties)
00147   {
00148     RTC_TRACE(("OutPortCorbaCdrConsumer::subscribeInterface()"));
00149     CORBA::Long index;
00150     index = NVUtil::find_index(properties,
00151                                "dataport.corba_cdr.outport_ior");
00152     if (index < 0)
00153       {
00154         RTC_DEBUG(("dataport.corba_cdr.outport_ior not found."));
00155         return false;
00156       }
00157     
00158     if (NVUtil::isString(properties,
00159                          "dataport.corba_cdr.outport_ior"))
00160       {
00161         RTC_DEBUG(("dataport.corba_cdr.outport_ior found."));
00162         const char* ior;
00163         properties[index].value >>= ior;
00164 
00165         CORBA::ORB_ptr orb = ::RTC::Manager::instance().getORB();
00166         CORBA::Object_var var = orb->string_to_object(ior);
00167         bool ret(setObject(var.in()));
00168         if (ret)
00169           {
00170             RTC_DEBUG(("CorbaConsumer was set successfully."));
00171           }
00172         else
00173           {
00174             RTC_ERROR(("Invalid object reference."))
00175           }
00176         return ret;
00177       }
00178     
00179     return false;
00180   }
00181   
00189   void OutPortCorbaCdrConsumer::
00190   unsubscribeInterface(const SDOPackage::NVList& properties)
00191   {
00192     RTC_TRACE(("OutPortCorbaCdrConsumer::unsubscribeInterface()"));
00193     CORBA::Long index;
00194     index = NVUtil::find_index(properties,
00195                                "dataport.corba_cdr.outport_ior");
00196     if (index < 0)
00197       {
00198         RTC_DEBUG(("dataport.corba_cdr.outport_ior not found."));
00199         return;
00200       }
00201     
00202     const char* ior;
00203     if (properties[index].value >>= ior)
00204       {
00205         RTC_DEBUG(("dataport.corba_cdr.outport_ior found."));
00206         CORBA::ORB_ptr orb = RTC::Manager::instance().getORB();
00207         CORBA::Object_var var = orb->string_to_object(ior);
00208         if (_ptr()->_is_equivalent(var))
00209           {
00210             releaseObject();
00211             RTC_DEBUG(("CorbaConsumer's reference was released."));
00212             return;
00213           }
00214         RTC_ERROR(("hmm. Inconsistent object reference."));
00215       }
00216   }
00217 
00225   OutPortConsumer::ReturnCode
00226   OutPortCorbaCdrConsumer::convertReturn(::OpenRTM::PortStatus status,
00227                                          const cdrMemoryStream& data)
00228   {
00229     switch(status)
00230       {
00231       case ::OpenRTM::PORT_OK:
00232         // never comes here
00233         return PORT_OK;
00234         break;
00235         
00236       case ::OpenRTM::PORT_ERROR:
00237         onSenderError();
00238         return PORT_ERROR;
00239         break;
00240 
00241       case ::OpenRTM::BUFFER_FULL:
00242         // never comes here
00243         return BUFFER_FULL;
00244         break;
00245 
00246       case ::OpenRTM::BUFFER_EMPTY:
00247         onSenderEmpty();
00248         return BUFFER_EMPTY;
00249         break;
00250 
00251       case ::OpenRTM::BUFFER_TIMEOUT:
00252         onSenderTimeout();
00253         return BUFFER_TIMEOUT;
00254         break;
00255 
00256       case ::OpenRTM::UNKNOWN_ERROR:
00257         onSenderError();
00258         return UNKNOWN_ERROR;
00259         break;
00260 
00261       default:
00262         onSenderError();
00263         return UNKNOWN_ERROR;
00264       }
00265 
00266     onSenderError();
00267     return UNKNOWN_ERROR;
00268   }
00269 
00270 
00271 };     // namespace RTC
00272 
00273 extern "C"
00274 {
00282   void OutPortCorbaCdrConsumerInit(void)
00283   {
00284     RTC::OutPortConsumerFactory&
00285       factory(RTC::OutPortConsumerFactory::instance());
00286     factory.addFactory("corba_cdr",
00287                        ::coil::Creator< ::RTC::OutPortConsumer,
00288                                         ::RTC::OutPortCorbaCdrConsumer>,
00289                        ::coil::Destructor< ::RTC::OutPortConsumer,
00290                                            ::RTC::OutPortCorbaCdrConsumer>);
00291   }
00292 };


openrtm_aist
Author(s): Noriaki Ando
autogenerated on Thu Aug 27 2015 14:16:38