00001
00020 #ifndef RTC_INPORT_H
00021 #define RTC_INPORT_H
00022
00023 #include <string>
00024 #include <vector>
00025 #include <iostream>
00026
00027 #include <coil/TimeValue.h>
00028 #include <coil/Time.h>
00029 #include <coil/OS.h>
00030
00031 #include <rtm/RTC.h>
00032 #include <rtm/Typename.h>
00033 #include <rtm/InPortBase.h>
00034 #include <rtm/CdrBufferBase.h>
00035 #include <rtm/PortCallback.h>
00036 #include <rtm/InPortConnector.h>
00037
00038 namespace RTC
00039 {
00088 template <class DataType>
00089 class InPort
00090 : public InPortBase
00091 {
00092 public:
00093 DATAPORTSTATUS_ENUM
00145 InPort(const char* name, DataType& value,
00146 int bufsize=64,
00147 bool read_block = false, bool write_block = false,
00148 int read_timeout = 0, int write_timeout = 0)
00149 #if defined(__GNUC__) && (__GNUC__ <= 3 && __GNUC_MINOR__ <= 3)
00150 : InPortBase(name, ::CORBA_Util::toRepositoryIdOfStruct<DataType>()),
00151 #else
00152 : InPortBase(name, ::CORBA_Util::toRepositoryId<DataType>()),
00153 #endif
00154 m_name(name), m_value(value),
00155 m_OnRead(NULL), m_OnReadConvert(NULL),
00156 m_status(1)
00157 {
00158 }
00159
00175 virtual ~InPort(void){};
00176
00196 virtual const char* name()
00197 {
00198 return m_name.c_str();
00199 }
00200
00201
00226 virtual bool isNew()
00227 {
00228 RTC_TRACE(("isNew()"));
00229
00230
00231
00232
00233 int r(0);
00234 {
00235 Guard guard(m_connectorsMutex);
00236 if (m_connectors.size() == 0)
00237 {
00238 RTC_DEBUG(("no connectors"));
00239 return false;
00240 }
00241 r = m_connectors[0]->getBuffer()->readable();
00242 }
00243
00244 if (r > 0)
00245 {
00246 RTC_DEBUG(("isNew() = true, readable data: %d", r));
00247 return true;
00248 }
00249
00250 RTC_DEBUG(("isNew() = false, no readable data"));
00251 return false;
00252 }
00253
00277 virtual bool isEmpty()
00278 {
00279 RTC_TRACE(("isEmpty()"));
00280 int r(0);
00281
00282 {
00283 Guard guard(m_connectorsMutex);
00284 if (m_connectors.size() == 0)
00285 {
00286 RTC_DEBUG(("no connectors"));
00287 return true;
00288 }
00289
00290
00291
00292 r = m_connectors[0]->getBuffer()->readable();
00293 }
00294
00295 if (r == 0)
00296 {
00297 RTC_DEBUG(("isEmpty() = true, buffer is empty"));
00298 return true;
00299 }
00300
00301 RTC_DEBUG(("isEmpty() = false, data exists in the buffer"));
00302 return false;
00303 }
00304
00379 bool read()
00380 {
00381 RTC_TRACE(("DataType read()"));
00382
00383 if (m_OnRead != NULL)
00384 {
00385 (*m_OnRead)();
00386 RTC_TRACE(("OnRead called"));
00387 }
00388
00389 cdrMemoryStream cdr;
00390 ReturnCode ret;
00391 {
00392 Guard guard(m_connectorsMutex);
00393 if (m_connectors.size() == 0)
00394 {
00395 RTC_DEBUG(("no connectors"));
00396 return false;
00397 }
00398
00399
00400
00401
00402 ret = m_connectors[0]->read(cdr);
00403 m_status[0] = ret;
00404 }
00405 if (ret == PORT_OK)
00406 {
00407 RTC_DEBUG(("data read succeeded"));
00408 m_value <<= cdr;
00409 if (m_OnReadConvert != 0)
00410 {
00411 m_value = (*m_OnReadConvert)(m_value);
00412 RTC_DEBUG(("OnReadConvert called"));
00413 return true;
00414 }
00415 return true;
00416 }
00417 else if (ret == BUFFER_EMPTY)
00418 {
00419 RTC_WARN(("buffer empty"));
00420 return false;
00421 }
00422 else if (ret == BUFFER_TIMEOUT)
00423 {
00424 RTC_WARN(("buffer read timeout"));
00425 return false;
00426 }
00427 RTC_ERROR(("unknown retern value from buffer.read()"));
00428 return false;
00429 }
00430
00431
00454 virtual void update()
00455 {
00456 this->read();
00457 };
00458
00479 void operator>>(DataType& rhs)
00480 {
00481 this->read();
00482 rhs = m_value;
00483 return;
00484 }
00485
00518 DataPortStatus::Enum getStatus(int index)
00519 {
00520 return m_status[0];
00521 }
00552 DataPortStatusList getStatusList()
00553 {
00554 return m_status;
00555 }
00556
00578 inline void setOnRead(OnRead<DataType>* on_read)
00579 {
00580 m_OnRead = on_read;
00581 }
00582
00606 inline void setOnReadConvert(OnReadConvert<DataType>* on_rconvert)
00607 {
00608 m_OnReadConvert = on_rconvert;
00609 }
00610
00611 private:
00612 std::string m_typename;
00620 std::string m_name;
00621
00629 DataType& m_value;
00630
00638 OnRead<DataType>* m_OnRead;
00639
00647 OnReadConvert<DataType>* m_OnReadConvert;
00648
00649 DataPortStatusList m_status;
00650 };
00651 };
00652
00653 #endif // RTC_INPORT_H