-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsubscriber.hpp
executable file
·83 lines (67 loc) · 1.81 KB
/
subscriber.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
* subscriber.hpp
*
* Created on: May 16, 2018
* Author: Kei
*/
#ifndef ROS2_SUBSCRIBER_HPP_
#define ROS2_SUBSCRIBER_HPP_
#include <stdio.h>
#include "xrcedds/xrcedds.hpp"
#include "node_handle.hpp"
#include "topic.hpp"
namespace ros2
{
class Node;
template <typename MsgT>
class Subscriber:public SubscriberHandle
{
public:
Subscriber(xrcedds::Subscriber_t* subscriber, const char* name, CallbackFunc callback, void* callback_arg)
: SubscriberHandle()
{
name_ = name;
subscriber_ = subscriber;
this->callback = callback;
this->callback_arg = callback_arg;
this->recreate();
}
void subscribe(void)
{
if(is_registered_ == false)
{
return;
}
xrcedds::readData(&data_reader_);
request_id_ = data_reader_.request_id;
}
void runCallback(void* serialized_msg)
{
if(this->callback != nullptr)
{
topic_.deserialize((ucdrBuffer*)serialized_msg, &topic_);
this->callback(&topic_, this->callback_arg);
}
}
void recreate()
{
#ifdef UXR_CREATE_ENTITIES_USING_XML
char reader_name[64];
sprintf(reader_name, "%s/%s", getPrefixString(TOPICS_SUBSCRIBE), name_);
is_registered_ = xrcedds::createDataReader(subscriber_, &data_reader_, reader_name, topic_.type_);
#else
is_registered_ = xrcedds::createDataReader(subscriber_, &data_reader_, (char*)name_, topic_.type_);
#endif
this->reader_id_ = data_reader_.id;
};
void destroy(void)
{
xrcedds::deleteEntity(&data_reader_);
}
private:
MsgT topic_;
xrcedds::Subscriber_t* subscriber_;
xrcedds::DataReader_t data_reader_;
};
} // namespace ros2
#endif /* ROS2_SUBSCRIBER_HPP_ */