00001
00020 #ifndef RTC_PUBLISHERPERIODIC_H
00021 #define RTC_PUBLISHERPERIODIC_H
00022
00023 #include <coil/Task.h>
00024 #include <coil/Mutex.h>
00025 #include <coil/Condition.h>
00026 #include <coil/PeriodicTask.h>
00027
00028 #include <rtm/RTC.h>
00029 #include <rtm/PublisherBase.h>
00030 #include <rtm/CdrBufferBase.h>
00031 #include <rtm/SystemLogger.h>
00032 #include <rtm/ConnectorBase.h>
00033 #include <rtm/ConnectorListener.h>
00034
00035 namespace coil
00036 {
00037 class Properties;
00038 };
00039
00040 namespace RTC
00041 {
00042 class InPortConsumer;
00060 class PublisherPeriodic
00061 : public PublisherBase
00062 {
00063 public:
00064 typedef coil::Mutex Mutex;
00065 typedef coil::Condition<Mutex> Condition;
00066 typedef coil::Guard<coil::Mutex> Guard;
00067 DATAPORTSTATUS_ENUM
00068
00097 PublisherPeriodic();
00098
00112 virtual ~PublisherPeriodic(void);
00113
00121 virtual ReturnCode init(coil::Properties& prop);
00130 virtual ReturnCode setConsumer(InPortConsumer* consumer);
00144 virtual ReturnCode setBuffer(CdrBufferBase* buffer);
00160 virtual ReturnCode setListener(ConnectorInfo& info,
00161 ConnectorListeners* listeners);
00177 virtual ReturnCode write(const cdrMemoryStream& data,
00178 unsigned long sec,
00179 unsigned long usec);
00200 virtual bool isActive();
00213 virtual ReturnCode activate();
00226 virtual ReturnCode deactivate();
00227
00241 virtual int svc(void);
00242
00243 protected:
00244 enum Policy
00245 {
00246 ALL,
00247 FIFO,
00248 SKIP,
00249 NEW
00250 };
00251
00259 void setPushPolicy(const coil::Properties& prop);
00260
00268 bool createTask(const coil::Properties& prop);
00269
00273 ReturnCode pushAll();
00274
00278 ReturnCode pushFifo();
00279
00283 ReturnCode pushSkip();
00284
00288 ReturnCode pushNew();
00289
00293 ReturnCode convertReturn(BufferStatus::Enum status,
00294 const cdrMemoryStream& data);
00295
00296
00313 ReturnCode invokeListener(DataPortStatus::Enum status,
00314 const cdrMemoryStream& data);
00315
00328 inline void onBufferWrite(const cdrMemoryStream& data)
00329 {
00330 m_listeners->
00331 connectorData_[ON_BUFFER_WRITE].notify(m_profile, data);
00332 }
00333
00343 inline void onBufferFull(const cdrMemoryStream& data)
00344 {
00345 m_listeners->
00346 connectorData_[ON_BUFFER_FULL].notify(m_profile, data);
00347 }
00348
00358 inline void onBufferWriteTimeout(const cdrMemoryStream& data)
00359 {
00360 m_listeners->
00361 connectorData_[ON_BUFFER_WRITE_TIMEOUT].notify(m_profile, data);
00362 }
00363
00364
00365
00366
00367
00368
00369
00379 inline void onBufferRead(const cdrMemoryStream& data)
00380 {
00381 m_listeners->
00382 connectorData_[ON_BUFFER_READ].notify(m_profile, data);
00383 }
00384
00394 inline void onSend(const cdrMemoryStream& data)
00395 {
00396 m_listeners->
00397 connectorData_[ON_SEND].notify(m_profile, data);
00398 }
00399
00409 inline void onReceived(const cdrMemoryStream& data)
00410 {
00411 m_listeners->
00412 connectorData_[ON_RECEIVED].notify(m_profile, data);
00413 }
00414
00424 inline void onReceiverFull(const cdrMemoryStream& data)
00425 {
00426 m_listeners->
00427 connectorData_[ON_RECEIVER_FULL].notify(m_profile, data);
00428 }
00429
00439 inline void onReceiverTimeout(const cdrMemoryStream& data)
00440 {
00441 m_listeners->
00442 connectorData_[ON_RECEIVER_TIMEOUT].notify(m_profile, data);
00443 }
00444
00454 inline void onReceiverError(const cdrMemoryStream& data)
00455 {
00456 m_listeners->
00457 connectorData_[ON_RECEIVER_ERROR].notify(m_profile, data);
00458 }
00459
00470 inline void onBufferEmpty()
00471 {
00472 m_listeners->
00473 connector_[ON_BUFFER_EMPTY].notify(m_profile);
00474 }
00475
00476
00477
00478
00479
00480
00481
00489 inline void onSenderEmpty()
00490 {
00491 m_listeners->
00492 connector_[ON_SENDER_EMPTY].notify(m_profile);
00493 }
00494
00495
00496
00497
00498
00499
00500
00508 inline void onSenderError()
00509 {
00510 m_listeners->
00511 connector_[ON_SENDER_ERROR].notify(m_profile);
00512 }
00513
00514
00515 private:
00516 bool bufferIsEmpty()
00517 {
00518 if (m_buffer->empty() && !m_readback)
00519 {
00520 RTC_DEBUG(("buffer empty"));
00521 onBufferEmpty();
00522 onSenderEmpty();
00523 return true;
00524 }
00525 return false;
00526 }
00527
00528 Logger rtclog;
00529 InPortConsumer* m_consumer;
00530 CdrBufferBase* m_buffer;
00531 ConnectorInfo m_profile;
00532 coil::PeriodicTaskBase* m_task;
00533 ConnectorListeners* m_listeners;
00534 ReturnCode m_retcode;
00535 Mutex m_retmutex;
00536 Policy m_pushPolicy;
00537 int m_skipn;
00538 bool m_active;
00539 bool m_readback;
00540 int m_leftskip;
00541 };
00542 };
00543
00544 extern "C"
00545 {
00546 void DLL_EXPORT PublisherPeriodicInit();
00547 };
00548
00549 #endif // RTC_PUBLISHERPERIODIC_H
00550