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 {
00174 template <class DataType>
00175 class InPort
00176 : public InPortBase
00177 {
00178 public:
00179 DATAPORTSTATUS_ENUM
00231 InPort(const char* name, DataType& value,
00232 int bufsize=64,
00233 bool read_block = false, bool write_block = false,
00234 int read_timeout = 0, int write_timeout = 0)
00235 : InPortBase(name, toTypename<DataType>()),
00236 m_name(name), m_value(value),
00237 m_OnRead(NULL), m_OnReadConvert(NULL)
00238 {
00239 }
00240
00256 virtual ~InPort(void){};
00257
00277 virtual const char* name()
00278 {
00279 return m_name.c_str();
00280 }
00281
00282
00307 virtual bool isNew()
00308 {
00309 RTC_TRACE(("isNew()"));
00310 if (m_connectors.size() == 0)
00311 {
00312 RTC_DEBUG(("no connectors"));
00313 return false;
00314 }
00315 int r(m_connectors[0]->getBuffer()->readable());
00316 if (r > 0)
00317 {
00318 RTC_DEBUG(("isNew() = true, readable data: %d", r));
00319 return true;
00320 }
00321
00322 RTC_DEBUG(("isNew() = false, no readable data"));
00323 return false;
00324 }
00325
00349 virtual bool isEmpty()
00350 {
00351 RTC_TRACE(("isEmpty()"));
00352
00353 if (m_connectors.size() == 0)
00354 {
00355 RTC_DEBUG(("no connectors"));
00356 return true;
00357 }
00358 int r(m_connectors[0]->getBuffer()->readable());
00359 if (r == 0)
00360 {
00361 RTC_DEBUG(("isEmpty() = true, buffer is empty"));
00362 return true;
00363 }
00364
00365 RTC_DEBUG(("isEmpty() = false, data exists in the buffer"));
00366 return false;
00367 }
00368
00443 bool read()
00444 {
00445 RTC_TRACE(("DataType read()"));
00446
00447 if (m_OnRead != NULL)
00448 {
00449 (*m_OnRead)();
00450 RTC_TRACE(("OnRead called"));
00451 }
00452
00453 if (m_connectors.size() == 0)
00454 {
00455 RTC_DEBUG(("no connectors"));
00456 return false;
00457 }
00458
00459 cdrMemoryStream cdr;
00460
00461 ReturnCode ret(m_connectors[0]->read(cdr));
00462 if (ret == PORT_OK)
00463 {
00464 RTC_DEBUG(("data read succeeded"));
00465 m_value <<= cdr;
00466 if (m_OnReadConvert != 0)
00467 {
00468 m_value = (*m_OnReadConvert)(m_value);
00469 RTC_DEBUG(("OnReadConvert called"));
00470 return true;
00471 }
00472 return true;
00473 }
00474 else if (ret == BUFFER_EMPTY)
00475 {
00476 RTC_WARN(("buffer empty"));
00477 return false;
00478 }
00479 else if (ret == BUFFER_TIMEOUT)
00480 {
00481 RTC_WARN(("buffer read timeout"));
00482 return false;
00483 }
00484 RTC_ERROR(("unknown retern value from buffer.read()"));
00485 return false;
00486 }
00487
00488
00511 virtual void update()
00512 {
00513 this->read();
00514 };
00515
00536 void operator>>(DataType& rhs)
00537 {
00538 this->read();
00539 rhs = m_value;
00540 return;
00541 }
00542
00564 inline void setOnRead(OnRead<DataType>* on_read)
00565 {
00566 m_OnRead = on_read;
00567 }
00568
00592 inline void setOnReadConvert(OnReadConvert<DataType>* on_rconvert)
00593 {
00594 m_OnReadConvert = on_rconvert;
00595 }
00596
00597 private:
00598 std::string m_typename;
00606 std::string m_name;
00607
00615 DataType& m_value;
00616
00624 OnRead<DataType>* m_OnRead;
00625
00633 OnReadConvert<DataType>* m_OnReadConvert;
00634
00635 };
00636 };
00637
00638 #endif // RTC_INPORT_H