InPortPushConnector.cpp
Go to the documentation of this file.
00001 // -*- C++ -*-
00020 #include <rtm/InPortPushConnector.h>
00021 #include <rtm/InPortProvider.h>
00022 #include <rtm/ConnectorListener.h>
00023 
00024 namespace RTC
00025 {
00033   InPortPushConnector::InPortPushConnector(ConnectorInfo info, 
00034                                            InPortProvider* provider,
00035                                            ConnectorListeners& listeners,
00036                                            CdrBufferBase* buffer)
00037     : InPortConnector(info, buffer),
00038       m_provider(provider), 
00039       m_listeners(listeners), 
00040       m_deleteBuffer(buffer == 0 ? true : false)
00041   {
00042     // publisher/buffer creation. This may throw std::bad_alloc;
00043     if (m_buffer == 0)
00044       {
00045         m_buffer = createBuffer(info);
00046       }
00047     if (m_buffer == 0 || m_provider==0) throw std::bad_alloc();
00048 
00049     m_buffer->init(info.properties.getNode("buffer"));
00050     m_provider->init(info.properties);
00051     m_provider->setBuffer(m_buffer);
00052     m_provider->setListener(info, &m_listeners);
00053 
00054     onConnect();
00055   }
00056 
00064   InPortPushConnector::~InPortPushConnector()
00065   {
00066     onDisconnect();
00067     disconnect();
00068   }
00069 
00077   ConnectorBase::ReturnCode
00078   InPortPushConnector::read(cdrMemoryStream& data)
00079   {
00080     RTC_TRACE(("read()"));
00081     /*
00082      * buffer returns
00083      *   BUFFER_OK
00084      *   BUFFER_EMPTY
00085      *   TIMEOUT
00086      *   PRECONDITION_NOT_MET
00087      */
00088     if (m_buffer == 0)
00089       {
00090         return PRECONDITION_NOT_MET;
00091       }
00092     BufferStatus::Enum ret = m_buffer->read(data);
00093     switch (ret)
00094       {
00095       case BufferStatus::BUFFER_OK:
00096         return PORT_OK;
00097         break;
00098       case BufferStatus::BUFFER_EMPTY:
00099         return BUFFER_EMPTY;
00100         break;
00101       case BufferStatus::TIMEOUT:
00102         return BUFFER_TIMEOUT;
00103         break;
00104       case BufferStatus::PRECONDITION_NOT_MET:
00105         return PRECONDITION_NOT_MET;
00106         break;
00107       default:
00108         return PORT_ERROR;
00109       }
00110   }
00111 
00119   ConnectorBase::ReturnCode InPortPushConnector::disconnect()
00120   {
00121     RTC_TRACE(("disconnect()"));
00122     // delete provider
00123     if (m_provider != 0)
00124       {
00125         InPortProviderFactory& cfactory(InPortProviderFactory::instance());
00126         cfactory.deleteObject(m_provider);
00127       }
00128     m_provider = 0;
00129 
00130     // delete buffer
00131     if (m_buffer != 0 && m_deleteBuffer == true)
00132       {
00133         CdrBufferFactory& bfactory(CdrBufferFactory::instance());
00134         bfactory.deleteObject(m_buffer);
00135       }
00136     m_buffer = 0;
00137 
00138     return PORT_OK;
00139   }
00140   
00148   CdrBufferBase* InPortPushConnector::createBuffer(ConnectorInfo& info)
00149   {
00150     std::string buf_type;
00151     buf_type = info.properties.getProperty("buffer_type",
00152                                               "ring_buffer");
00153     return CdrBufferFactory::instance().createObject(buf_type);
00154   }
00155 
00163   void InPortPushConnector::onConnect()
00164   {
00165     m_listeners.connector_[ON_CONNECT].notify(m_profile);
00166   }
00167 
00175   void InPortPushConnector::onDisconnect()
00176   {
00177     m_listeners.connector_[ON_DISCONNECT].notify(m_profile);
00178   }
00179 
00180 };
00181 


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