Go to the documentation of this file.00001 #include <ros/ros.h>
00002 #include <ros/package.h>
00003 #include <std_msgs/String.h>
00004 #include <topic_tools/shape_shifter.h>
00005
00006 #ifndef SUBSCRIBER_H
00007 #define SUBSCRIBER_H
00008
00009 #include "rosR.h"
00010
00011 struct IStreamR : public StreamR
00012 {
00013 IStreamR(uint8_t* data, uint32_t count) : StreamR(data, count)
00014 {}
00015
00016 template<typename T>
00017 inline void next(T& t) {
00018 ros::serialization::deserialize(*this, t);
00019 }
00020
00021 template<typename T>
00022 inline IStreamR& operator>>(T& t) {
00023 ros::serialization::deserialize(*this, t);
00024 return *this;
00025 }
00026 };
00027
00028 class SubscribeR {
00029 public:
00030 SubscribeR(NodeR* handle, const char* cTopic, const char* cMsgType, const char* cMsgDefinition, const char* cMsgMD5){
00031 m_bNewMessage = false;
00032 m_bAnyMessage = false;
00033 m_rosSubscriber = (handle->getHandle())->subscribe(cTopic, 1, &SubscribeR::callback, this);
00034
00035 m_strMsgType = cMsgType;
00036 m_strMsgDefinition = cMsgDefinition;
00037 m_strMsgMD5 = cMsgMD5;
00038
00039 m_iBufferSize = 10000000;
00040
00041 m_ui8Buffer = new boost::shared_array<uint8_t> (new uint8_t[m_iBufferSize]);
00042 m_isStream = new IStreamR(m_ui8Buffer->get(), m_iBufferSize);
00043 };
00044 ~SubscribeR() {
00045 delete m_isStream;
00046 delete m_ui8Buffer;
00047 };
00048
00049 void callback(const ros::MessageEvent<topic_tools::ShapeShifter const>& event) {
00050 m_bNewMessage = true;
00051 m_bAnyMessage = true;
00052
00053 m_Event = event;
00054
00055
00056 if(m_strMsgType.size() == 0){
00057 m_strMsgType = m_Event.getMessage()->getDataType();
00058 m_strMsgMD5 = m_Event.getMessage()->getMD5Sum();
00059 m_strMsgDefinition = m_Event.getMessage()->getMessageDefinition();
00060 }
00061 };
00062
00063 bool hasAnyMessage() { return m_bAnyMessage; };
00064 bool hasNewMessage() { return m_bNewMessage; };
00065
00066 const char* getPublisher() {
00067 return m_Event.getPublisherName().c_str(); };
00068 const char* getMessageType() {
00069 return m_strMsgType.c_str(); };
00070 const char* getMessageDefinition() {
00071 return m_strMsgDefinition.c_str(); };
00072 const char* getMessageMD5() {
00073 return m_strMsgMD5.c_str(); };
00074
00075 IStreamR * getMessageStream(){
00076 m_bNewMessage = false;
00077
00078 if(m_isStream->getLength() != m_iBufferSize){
00079 m_isStream->advance(-(m_iBufferSize-m_isStream->getLength()));
00080 }
00081
00082 m_Event.getMessage()->write(*m_isStream);
00083 m_isStream->advance(-m_Event.getMessage()->size());
00084
00085 return m_isStream;
00086 };
00087
00088 private:
00089 bool m_bNewMessage;
00090 bool m_bAnyMessage;
00091
00092 ros::Subscriber m_rosSubscriber;
00093 std::string m_strMsgType;
00094 std::string m_strMsgDefinition;
00095 std::string m_strMsgMD5;
00096
00097 ros::MessageEvent<topic_tools::ShapeShifter const> m_Event;
00098
00099 boost::shared_array<uint8_t> *m_ui8Buffer;
00100 int m_iBufferSize;
00101 IStreamR *m_isStream;
00102 };
00103
00104 SubscribeR* rrosSubscriber( NodeR* handle,
00105 const char* topic,
00106 const char* type,
00107 const char* msg_def,
00108 const char* msg_md5);
00109
00110 bool rrosSubscriberHasNewMsg(SubscribeR* subscriber);
00111 bool rrosSubscriberHasAnyMsg(SubscribeR* subscriber);
00112
00113 IStreamR* rrosSubscriberGetMessageStream(SubscribeR* subscriber);
00114
00115 const char* rrosSubscriberGetPublisher(SubscribeR* subscriber);
00116 const char* rrosSubscriberGetMessageType(SubscribeR* subscriber);
00117 const char* rrosSubscriberGetMessageMD5(SubscribeR* subscriber);
00118 const char* rrosSubscriberGetMessageDefinition(SubscribeR* subscriber);
00119
00120 bool rros_stream_read_bool (IStreamR *s);
00121 signed char rros_stream_read_int8 (IStreamR *s);
00122 unsigned char rros_stream_read_uint8 (IStreamR *s);
00123 signed short rros_stream_read_int16 (IStreamR *s);
00124 unsigned short rros_stream_read_uint16 (IStreamR *s);
00125 signed int rros_stream_read_int32 (IStreamR *s);
00126 unsigned int rros_stream_read_uint32 (IStreamR *s);
00127 signed long rros_stream_read_int64 (IStreamR *s);
00128 unsigned long rros_stream_read_uint64 (IStreamR *s);
00129 float rros_stream_read_float32 (IStreamR *s);
00130 double rros_stream_read_float64 (IStreamR *s);
00131 char* rros_stream_read_string (IStreamR *s);
00132
00133 std::vector<signed char>* rros_stream_read_int8_array (IStreamR *s, unsigned int size=0);
00134 std::vector<unsigned char>* rros_stream_read_uint8_array (IStreamR *s, unsigned int size=0);
00135 std::vector<signed short>* rros_stream_read_int16_array (IStreamR *s, unsigned int size=0);
00136 std::vector<unsigned short>* rros_stream_read_uint16_array (IStreamR *s, unsigned int size=0);
00137 std::vector<signed int>* rros_stream_read_int32_array (IStreamR *s, unsigned int size=0);
00138 std::vector<unsigned int>* rros_stream_read_uint32_array (IStreamR *s, unsigned int size=0);
00139 std::vector<signed long>* rros_stream_read_int64_array (IStreamR *s, unsigned int size=0);
00140 std::vector<unsigned long>* rros_stream_read_uint64_array (IStreamR *s, unsigned int size=0);
00141 std::vector<float>* rros_stream_read_float32_array (IStreamR *s, unsigned int size=0);
00142 std::vector<double>* rros_stream_read_float64_array (IStreamR *s, unsigned int size=0);
00143 std::vector<std::string>* rros_stream_read_string_array (IStreamR *s, unsigned int size=0);
00144
00145 #endif