-
Notifications
You must be signed in to change notification settings - Fork 486
/
Transport.cc
354 lines (283 loc) · 9 KB
/
Transport.cc
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/*
* Copyright 2012 Open Source Robotics Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#include <list>
#include <boost/algorithm/string.hpp>
#include <boost/lexical_cast.hpp>
#include <string>
#include "gazebo/transport/Node.hh"
#include "gazebo/transport/Publisher.hh"
#include "gazebo/transport/Subscriber.hh"
#include "gazebo/transport/ConnectionManager.hh"
#include "Transport.hh"
using namespace gazebo;
boost::thread *g_runThread = NULL;
boost::condition_variable g_responseCondition;
boost::mutex requestMutex;
bool g_stopped = true;
bool g_minimalComms = false;
std::list<msgs::Request *> g_requests;
std::list<boost::shared_ptr<msgs::Response> > g_responses;
#define DEFAULT_MASTER_PORT 11345
/////////////////////////////////////////////////
void dummy_callback_fn(uint32_t)
{
}
/////////////////////////////////////////////////
bool transport::get_master_uri(std::string &_masterHost,
unsigned int &_masterPort)
{
char *charURI = getenv("GAZEBO_MASTER_URI");
// Set to default host and port
if (!charURI || strlen(charURI) == 0)
{
_masterHost = "localhost";
_masterPort = DEFAULT_MASTER_PORT;
return false;
}
std::string masterURI = charURI;
boost::replace_first(masterURI, "http://", "");
size_t lastColon = masterURI.find_last_of(":");
_masterHost = masterURI.substr(0, lastColon);
if (lastColon == std::string::npos)
{
gzerr << "Port missing in master URI[" << masterURI
<< "]. Using default value of " << DEFAULT_MASTER_PORT << ".\n";
_masterPort = DEFAULT_MASTER_PORT;
}
else
{
_masterPort = boost::lexical_cast<unsigned int>(
masterURI.substr(lastColon + 1, masterURI.size() - (lastColon + 1)));
}
return true;
}
/////////////////////////////////////////////////
bool transport::init(const std::string &_masterHost, unsigned int _masterPort)
{
std::string host = _masterHost;
unsigned int port = _masterPort;
if (host.empty())
get_master_uri(host, port);
transport::TopicManager::Instance()->Init();
if (!transport::ConnectionManager::Instance()->Init(host, port))
return false;
return true;
}
/////////////////////////////////////////////////
void transport::run()
{
g_stopped = false;
g_runThread = new boost::thread(&transport::ConnectionManager::Run,
transport::ConnectionManager::Instance());
std::list<std::string> namespaces;
// This chunk of code just waits until we get a list of topic namespaces.
unsigned int trys = 0;
unsigned int limit = 50;
while (namespaces.empty() && trys < limit)
{
TopicManager::Instance()->GetTopicNamespaces(namespaces);
if (namespaces.empty())
{
// 25 seconds max wait time
common::Time::MSleep(500);
}
trys++;
}
if (trys >= limit)
gzerr << "Unable to get topic namespaces in [" << trys << "] tries.\n";
}
/////////////////////////////////////////////////
bool transport::is_stopped()
{
return g_stopped;
}
/////////////////////////////////////////////////
void transport::stop()
{
g_stopped = true;
g_responseCondition.notify_all();
transport::ConnectionManager::Instance()->Stop();
}
/////////////////////////////////////////////////
void transport::fini()
{
transport::stop();
transport::TopicManager::Instance()->Fini();
if (g_runThread)
{
g_runThread->join();
delete g_runThread;
g_runThread = NULL;
}
transport::ConnectionManager::Instance()->Fini();
}
/////////////////////////////////////////////////
void transport::clear_buffers()
{
transport::TopicManager::Instance()->ClearBuffers();
}
/////////////////////////////////////////////////
void transport::pause_incoming(bool _pause)
{
transport::TopicManager::Instance()->PauseIncoming(_pause);
}
/////////////////////////////////////////////////
void on_response(ConstResponsePtr &_msg)
{
if (g_requests.empty())
return;
std::list<msgs::Request *>::iterator iter;
for (iter = g_requests.begin(); iter != g_requests.end(); ++iter)
{
if (_msg->id() == (*iter)->id())
break;
}
// Stop if the response is not for any of the request messages.
if (iter == g_requests.end())
return;
boost::shared_ptr<msgs::Response> response(new msgs::Response);
response->CopyFrom(*_msg);
g_responses.push_back(response);
g_responseCondition.notify_all();
}
/////////////////////////////////////////////////
void transport::get_topic_namespaces(std::list<std::string> &_namespaces)
{
TopicManager::Instance()->GetTopicNamespaces(_namespaces);
}
/////////////////////////////////////////////////
boost::shared_ptr<msgs::Response> transport::request(
const std::string &_worldName, const std::string &_request,
const std::string &_data)
{
boost::mutex::scoped_lock lock(requestMutex);
msgs::Request *request = msgs::CreateRequest(_request, _data);
g_requests.push_back(request);
NodePtr node = NodePtr(new Node());
node->Init(_worldName);
PublisherPtr requestPub = node->Advertise<msgs::Request>("~/request");
requestPub->WaitForConnection();
SubscriberPtr responseSub = node->Subscribe("~/response", &on_response);
requestPub->Publish(*request, true);
boost::shared_ptr<msgs::Response> response;
std::list<boost::shared_ptr<msgs::Response> >::iterator iter;
bool valid = false;
while (!valid)
{
// Wait for a response
g_responseCondition.wait(lock);
for (iter = g_responses.begin(); iter != g_responses.end(); ++iter)
{
if ((*iter)->id() == request->id())
{
response = *iter;
g_responses.erase(iter);
valid = true;
break;
}
}
}
requestPub.reset();
responseSub.reset();
node.reset();
delete request;
return response;
}
/////////////////////////////////////////////////
void transport::requestNoReply(const std::string &_worldName,
const std::string &_request,
const std::string &_data)
{
// Create a node for communication.
NodePtr node = NodePtr(new Node());
// Initialize the node, use the world name for the topic namespace.
node->Init(_worldName);
// Process the request.
requestNoReply(node, _request, _data);
// Cleanup the node.
node.reset();
}
/////////////////////////////////////////////////
void transport::requestNoReply(NodePtr _node, const std::string &_request,
const std::string &_data)
{
// Create a publisher on the request topic.
PublisherPtr requestPub = _node->Advertise<msgs::Request>("~/request");
// Create a new request message
msgs::Request *request = msgs::CreateRequest(_request, _data);
// Publish the request message
requestPub->Publish(*request);
// Cleanup the request
delete request;
// Clean up the publisher.
requestPub.reset();
}
/////////////////////////////////////////////////
std::map<std::string, std::list<std::string> > transport::getAdvertisedTopics()
{
std::map<std::string, std::list<std::string> > result;
std::list<msgs::Publish> publishers;
ConnectionManager::Instance()->GetAllPublishers(publishers);
for (std::list<msgs::Publish>::iterator iter = publishers.begin();
iter != publishers.end(); ++iter)
{
result[(*iter).msg_type()].push_back((*iter).topic());
}
return result;
}
/////////////////////////////////////////////////
std::list<std::string> transport::getAdvertisedTopics(
const std::string &_msgType)
{
std::list<std::string> result;
std::list<msgs::Publish> publishers;
ConnectionManager::Instance()->GetAllPublishers(publishers);
for (std::list<msgs::Publish>::iterator iter = publishers.begin();
iter != publishers.end(); ++iter)
{
if (std::find(result.begin(), result.end(), (*iter).topic()) !=
result.end())
continue;
if (_msgType.empty() || _msgType == (*iter).msg_type())
result.push_back((*iter).topic());
}
return result;
}
/////////////////////////////////////////////////
std::string transport::getTopicMsgType(const std::string &_topicName)
{
std::string result;
std::list<msgs::Publish> publishers;
ConnectionManager::Instance()->GetAllPublishers(publishers);
for (std::list<msgs::Publish>::iterator iter = publishers.begin();
iter != publishers.end() && result.empty(); ++iter)
{
if (_topicName == (*iter).topic())
result = (*iter).msg_type();
}
return result;
}
/////////////////////////////////////////////////
void transport::setMinimalComms(bool _enabled)
{
g_minimalComms = _enabled;
}
/////////////////////////////////////////////////
bool transport::getMinimalComms()
{
return g_minimalComms;
}