-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathros_bridge.h
More file actions
151 lines (116 loc) · 5.49 KB
/
ros_bridge.h
File metadata and controls
151 lines (116 loc) · 5.49 KB
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#pragma once
#include <iostream>
#include <string>
#include <thread>
#include <functional>
#include <unordered_map>
#include <list>
#include <queue>
#include <chrono>
#include <stdio.h>
#include "types.h"
#include "helper.h"
#include "spinlock.h"
#include "itransport_layer.h"
#include "rapidjson/document.h"
#include "rapidjson/writer.h"
#include "rapidjson/stringbuffer.h"
#include "messages/rosbridge_advertise_msg.h"
#include "messages/rosbridge_advertise_service_msg.h"
#include "messages/rosbridge_call_service_msg.h"
#include "messages/rosbridge_msg.h"
#include "messages/rosbridge_publish_msg.h"
#include "messages/rosbridge_service_response_msg.h"
#include "messages/rosbridge_subscribe_msg.h"
#include "messages/rosbridge_unadvertise_msg.h"
#include "messages/rosbridge_unadvertise_service_msg.h"
#include "messages/rosbridge_unsubscribe_msg.h"
using json = rapidjson::Document;
namespace rosbridge2cpp {
/**
* The main rosbridge2cpp class that connects to the rosbridge server.
* The library is inspired by [roslibjs](http://wiki.ros.org/roslibjs),
* which is a feature-rich client-side implementation of the rosbridge protocol in java script.
*/
class ROSBridge {
public:
ROSBridge(ITransportLayer &transport) : transport_layer_(transport) {}
ROSBridge(ITransportLayer &transport, bool bson_only_mode) : transport_layer_(transport), bson_only_mode_(bson_only_mode) {}
~ROSBridge();
// Init the underlying transport layer and everything thats required
// to initialized in this class.
bool Init(std::string ip_addr, int port);
bool IsHealthy() const;
// Send arbitrary string-data over the given TransportLayer
bool SendMessage(std::string data);
// Send json data over the transport layer,
// by serializing it and using
// ROSBridge::send_message(std::string data)
bool SendMessage(json &data);
bool SendMessage(ROSBridgeMsg &msg);
bool QueueMessage(const std::string& topic_name, int queue_size, ROSBridgePublishMsg& msg);
// Registration function for topic callbacks.
// This method should ONLY be called by ROSTopic instances.
// It will pass the received data to the registered std::function.
//
// Please note:
// _If you register more than one callback for the
// same topic, the old one get's overwritten_
void RegisterTopicCallback(std::string topic_name, ROSCallbackHandle<FunVrROSPublishMsg>& callback_handle);
// This method should ONLY be called by ROSTopic instances.
// If you call this on your own, the housekeeping in ROSTopic
// might fail which leads to missing unsubscribe messages etc.
//
// @return true, if the passed callback has been found and removed. false otherwise.
bool UnregisterTopicCallback(std::string topic_name, const ROSCallbackHandle<FunVrROSPublishMsg>& callback_handle);
// Register the callback for a service call.
// This callback will be executed when we receive the response for a particular Service Request
void RegisterServiceCallback(std::string service_call_id, FunVrROSServiceResponseMsg fun);
// Register the callback that shall be executed,
// whenever we receive a request for a service that
// this ROSBridge has advertised via a ROSService.
void RegisterServiceRequestCallback(std::string service_name, FunVrROSCallServiceMsgrROSServiceResponseMsgrAllocator fun);
void RegisterServiceRequestCallback(std::string service_name, FunVrROSCallServiceMsgrROSServiceResponseMsg fun);
// An ID Counter that will be used to generate increasing
// IDs for service/topic etc. messages
long id_counter = 0;
// Returns true if the bson only mode is activated
bool bson_only_mode() {
return bson_only_mode_;
}
// Enable the BSON only mode.
// All communications with the rosbridge server
// will be in BSON, instead of JSON
void enable_bson_mode() { bson_only_mode_ = true; }
private:
// Callback function for the used ITransportLayer.
// It receives the received json that was contained
// in the incoming ROSBridge packet
//
// @pre This method assumes a valid json variable
void IncomingMessageCallback(json &data);
void IncomingMessageCallback(bson_t &bson);
// Handler Method for reply packet
void HandleIncomingPublishMessage(ROSBridgePublishMsg &data);
// Handler Method for reply packet
void HandleIncomingServiceResponseMessage(ROSBridgeServiceResponseMsg &data);
// Handler Method for reply packet
void HandleIncomingServiceRequestMessage(ROSBridgeCallServiceMsg &data);
int RunPublisherQueueThread();
ITransportLayer &transport_layer_;
std::unordered_map<std::string, std::list<ROSCallbackHandle<FunVrROSPublishMsg>>> registered_topic_callbacks_;
std::unordered_map<std::string, FunVrROSServiceResponseMsg> registered_service_callbacks_;
std::unordered_map<std::string, FunVrROSCallServiceMsgrROSServiceResponseMsgrAllocator> registered_service_request_callbacks_;
std::unordered_map<std::string, FunVrROSCallServiceMsgrROSServiceResponseMsg> registered_service_request_callbacks_bson_;
bool bson_only_mode_ = false;
spinlock transport_layer_access_mutex_;
spinlock change_topics_mutex_;
std::thread publisher_queue_thread_;
spinlock change_publisher_queues_mutex_;
std::unordered_map<std::string, int> publisher_topics_; // points to index in publisher_queues_
std::vector<std::queue<bson_t*>> publisher_queues_; // data to publish on the queue thread
int current_publisher_queue_ = 0;
bool run_publisher_queue_thread_ = true;
std::chrono::system_clock::time_point LastDataSendTime; // watchdog for send thread. Socket sometimes blocks infinitely.
};
}