OutPortCorbaCdrProvider.cpp
Go to the documentation of this file.
00001 // -*- C++ -*-
00020 #include <rtm/OutPortCorbaCdrProvider.h>
00021 
00022 #ifdef WIN32
00023 #pragma warning( disable : 4290 )
00024 #endif
00025 
00026 namespace RTC
00027 {
00035   OutPortCorbaCdrProvider::OutPortCorbaCdrProvider(void)
00036    : m_buffer(0) 
00037   {
00038     // PortProfile setting
00039     setInterfaceType("corba_cdr");
00040     
00041     // ConnectorProfile setting
00042     m_objref = this->_this();
00043     
00044     // set outPort's reference
00045     CORBA::ORB_ptr orb = ::RTC::Manager::instance().getORB();
00046     CORBA::String_var ior = orb->object_to_string(m_objref.in());
00047 #ifndef ORB_IS_RTORB
00048     CORBA_SeqUtil::
00049       push_back(m_properties,
00050                 NVUtil::newNV("dataport.corba_cdr.outport_ior", ior));
00051 #else // ORB_IS_RTORB
00052     CORBA_SeqUtil::
00053       push_back(m_properties,
00054                 NVUtil::newNV("dataport.corba_cdr.outport_ior", ior.in()));
00055 #endif // ORB_IS_RTORB
00056     CORBA_SeqUtil::
00057       push_back(m_properties,
00058                 NVUtil::newNV("dataport.corba_cdr.outport_ref", m_objref));
00059   }
00060   
00068   OutPortCorbaCdrProvider::~OutPortCorbaCdrProvider(void)
00069   {
00070     try
00071       {
00072         PortableServer::ObjectId_var oid;
00073         oid = _default_POA()->servant_to_id(this);
00074         _default_POA()->deactivate_object(oid);
00075       }
00076     catch (PortableServer::POA::ServantNotActive &e)
00077       {
00078         RTC_ERROR(("%s", e._name()));
00079       }
00080     catch (PortableServer::POA::WrongPolicy &e)
00081       {
00082         RTC_ERROR(("%s", e._name()));
00083       }
00084     catch (...)
00085       {
00086         // never throws exception
00087         RTC_ERROR(("Unknown exception caught."));
00088       }
00089   }
00090   
00098   void OutPortCorbaCdrProvider::init(coil::Properties& prop)
00099   {
00100   }
00101 
00109   void OutPortCorbaCdrProvider::setBuffer(CdrBufferBase* buffer)
00110   {
00111     m_buffer = buffer;
00112   }
00113 
00121   void OutPortCorbaCdrProvider::setListener(ConnectorInfo& info,
00122                                             ConnectorListeners* listeners)
00123   {
00124     m_profile = info;
00125     m_listeners = listeners;
00126   }
00127 
00135   void OutPortCorbaCdrProvider::setConnector(OutPortConnector* connector)
00136   {
00137     m_connector = connector;
00138   }
00139 
00147   ::OpenRTM::PortStatus
00148   OutPortCorbaCdrProvider::get(::OpenRTM::CdrData_out data)
00149     throw (CORBA::SystemException)
00150   {
00151     RTC_PARANOID(("OutPortCorbaCdrProvider::get()"));
00152     // at least the output "data" area should be allocated
00153     data = new ::OpenRTM::CdrData();
00154 
00155     if (m_buffer == 0)
00156       {
00157         onSenderError();
00158         return ::OpenRTM::UNKNOWN_ERROR;
00159       }
00160 
00161     cdrMemoryStream cdr;
00162     CdrBufferBase::ReturnCode ret(m_buffer->read(cdr));
00163     
00164     if (ret == CdrBufferBase::BUFFER_OK)
00165       {
00166         CORBA::ULong len((CORBA::ULong)cdr.bufSize());
00167         RTC_PARANOID(("converted CDR data size: %d", len));
00168 
00169         if (len == (CORBA::ULong)0) {
00170           RTC_ERROR(("buffer is empty."));
00171           return ::OpenRTM::BUFFER_EMPTY;
00172         }
00173 #ifndef ORB_IS_RTORB
00174         data->length(len);
00175         cdr.get_octet_array(&((*data)[0]), len);
00176 #else
00177         data->length(len);
00178         cdr.get_octet_array((char *)&((*data)[0]), (int)len);
00179 #endif // ORB_IS_RTORB
00180       }
00181 
00182     return convertReturn(ret, cdr);
00183   }
00184 
00192   ::OpenRTM::PortStatus
00193   OutPortCorbaCdrProvider::convertReturn(BufferStatus::Enum status,
00194                                         const cdrMemoryStream& data)
00195   {
00196     switch(status)
00197       {
00198       case BufferStatus::BUFFER_OK:
00199         onBufferRead(data);
00200         onSend(data);
00201         return ::OpenRTM::PORT_OK;
00202         break;
00203 
00204       case BufferStatus::BUFFER_ERROR:
00205         onSenderError();
00206         return ::OpenRTM::PORT_ERROR;
00207         break;
00208 
00209       case BufferStatus::BUFFER_FULL:
00210         // never come here
00211         return ::OpenRTM::BUFFER_FULL;
00212         break;
00213 
00214       case BufferStatus::BUFFER_EMPTY:
00215         onBufferEmpty();
00216         onSenderEmpty();
00217         return ::OpenRTM::BUFFER_EMPTY;
00218         break;
00219 
00220       case BufferStatus::PRECONDITION_NOT_MET:
00221         onSenderError();
00222         return ::OpenRTM::PORT_ERROR;
00223         break;
00224 
00225       case BufferStatus::TIMEOUT:
00226         onBufferReadTimeout();
00227         onSenderTimeout();
00228         return ::OpenRTM::BUFFER_TIMEOUT;
00229         break;
00230 
00231       default:
00232         return ::OpenRTM::UNKNOWN_ERROR;
00233       }
00234 
00235     onSenderError();
00236     return ::OpenRTM::UNKNOWN_ERROR;
00237   }
00238 
00239 };     // namespace RTC
00240 
00241 extern "C"
00242 {
00250   void OutPortCorbaCdrProviderInit(void)
00251   {
00252     RTC::OutPortProviderFactory&
00253       factory(RTC::OutPortProviderFactory::instance());
00254     factory.addFactory("corba_cdr",
00255                        ::coil::Creator< ::RTC::OutPortProvider,
00256                                         ::RTC::OutPortCorbaCdrProvider>,
00257                        ::coil::Destructor< ::RTC::OutPortProvider,
00258                                            ::RTC::OutPortCorbaCdrProvider>);
00259   }
00260 };


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