-
Notifications
You must be signed in to change notification settings - Fork 35
/
gz.cc
320 lines (278 loc) · 7.35 KB
/
gz.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
/*
* Copyright (C) 2014 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 <chrono>
#include <functional>
#include <iostream>
#include <string>
#include <vector>
#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable: 4251)
#endif
#include <google/protobuf/util/json_util.h>
#ifdef _MSC_VER
#pragma warning(pop)
#endif
#include <gz/msgs/Factory.hh>
#include "gz.hh"
#include "gz/transport/config.hh"
#include "gz/transport/Helpers.hh"
#include "gz/transport/Node.hh"
using namespace gz;
using namespace transport;
//////////////////////////////////////////////////
extern "C" void cmdTopicList()
{
Node node;
std::vector<std::string> topics;
node.TopicList(topics);
for (auto const &topic : topics)
std::cout << topic << std::endl;
}
//////////////////////////////////////////////////
extern "C" void cmdTopicInfo(const char *_topic)
{
if (!_topic || std::string(_topic).empty())
{
std::cerr << "Invalid topic. Topic must not be empty.\n";
return;
}
Node node;
// Get the publishers on the requested topic
std::vector<MessagePublisher> publishers;
node.TopicInfo(_topic, publishers);
if (!publishers.empty())
{
std::cout << "Publishers [Address, Message Type]:\n";
/// List the publishers
for (std::vector<MessagePublisher>::iterator iter = publishers.begin();
iter != publishers.end(); ++iter)
{
std::cout << " " << (*iter).Addr() << ", "
<< (*iter).MsgTypeName() << std::endl;
}
}
else
{
std::cout << "No publishers on topic [" << _topic << "]\n";
}
// TODO(anyone): Add subscribers lists
}
//////////////////////////////////////////////////
extern "C" void cmdServiceList()
{
Node node;
std::vector<std::string> services;
node.ServiceList(services);
for (auto const &service : services)
std::cout << service << std::endl;
}
//////////////////////////////////////////////////
extern "C" void cmdServiceInfo(const char *_service)
{
if (!_service || std::string(_service).empty())
{
std::cerr << "Invalid service. Service must not be empty.\n";
return;
}
Node node;
// Get the publishers on the requested topic
std::vector<ServicePublisher> publishers;
node.ServiceInfo(_service, publishers);
if (!publishers.empty())
{
std::cout << "Service providers [Address, Request Message Type, "
<< "Response Message Type]:\n";
/// List the publishers
for (std::vector<ServicePublisher>::iterator iter = publishers.begin();
iter != publishers.end(); ++iter)
{
std::cout << " " << (*iter).Addr() << ", "
<< (*iter).ReqTypeName() << ", " << (*iter).RepTypeName()
<< std::endl;
}
}
else
{
std::cout << "No service providers on service [" << _service << "]\n";
}
}
//////////////////////////////////////////////////
extern "C" void cmdTopicPub(const char *_topic,
const char *_msgType, const char *_msgData)
{
if (!_topic)
{
std::cerr << "Topic name is null\n";
return;
}
if (!_msgType)
{
std::cerr << "Message type is null\n";
return;
}
if (!_msgData)
{
std::cerr << "Message data is null\n";
return;
}
// Create the message, and populate the field with _msgData
auto msg = msgs::Factory::New(_msgType, _msgData);
if (msg)
{
// Create the node and advertise the topic
Node node;
auto pub = node.Advertise(_topic, msg->GetTypeName());
// Publish the message
if (pub)
{
// \todo(anyone) Change this sleep to a WaitForSubscribers() call.
// See issue #47.
std::this_thread::sleep_for(std::chrono::milliseconds(800));
pub.Publish(*msg);
}
else
{
std::cerr << "Unable to publish on topic[" << _topic << "] "
<< "with message type[" << _msgType << "].\n";
}
}
else
{
std::cerr << "Unable to create message of type[" << _msgType << "] "
<< "with data[" << _msgData << "].\n";
}
}
//////////////////////////////////////////////////
extern "C" void cmdServiceReq(const char *_service,
const char *_reqType, const char *_repType, const int _timeout,
const char *_reqData)
{
if (!_service)
{
std::cerr << "Service name is null\n";
return;
}
if (!_reqType)
{
std::cerr << "Request type is null\n";
return;
}
if (!_repType)
{
std::cerr << "Response type is null\n";
return;
}
if (!_reqData)
{
std::cerr << "Request data is null\n";
return;
}
// Create the request, and populate the field with _reqData
auto req = msgs::Factory::New(_reqType, _reqData);
if (!req)
{
std::cerr << "Unable to create request of type[" << _reqType << "] "
<< "with data[" << _reqData << "].\n";
return;
}
// Create the response.
auto rep = msgs::Factory::New(_repType);
if (!rep)
{
std::cerr << "Unable to create response of type[" << _repType << "].\n";
return;
}
// Create the node.
Node node;
bool result;
// Request the service.
bool executed = node.Request(_service, *req, _timeout, *rep, result);
if (executed)
{
if (result)
std::cout << rep->DebugString() << std::endl;
else
std::cout << "Service call failed" << std::endl;
}
else
std::cerr << "Service call timed out" << std::endl;
}
//////////////////////////////////////////////////
extern "C" void cmdTopicEcho(const char *_topic,
const double _duration, int _count, MsgOutputFormat _outputFormat)
{
if (!_topic || std::string(_topic).empty())
{
std::cerr << "Invalid topic. Topic must not be empty.\n";
return;
}
std::mutex mutex;
std::condition_variable condition;
int count = 0;
std::function<void(const ProtoMsg&)> cb = [&](const ProtoMsg &_msg)
{
std::lock_guard<std::mutex> lock(mutex);
switch (_outputFormat)
{
case MsgOutputFormat::kDefault:
case MsgOutputFormat::kDebugString:
std::cout << _msg.DebugString() << std::endl;
break;
case MsgOutputFormat::kJSON:
{
std::string jsonStr;
google::protobuf::util::MessageToJsonString(_msg, &jsonStr);
std::cout << jsonStr << std::endl;
}
break;
default:
std::cerr << "Invalid output format selected.\n";
return;
}
++count;
condition.notify_one();
};
Node node;
if (!node.Subscribe(_topic, cb))
return;
if (_duration >= 0)
{
std::this_thread::sleep_for(std::chrono::milliseconds(
static_cast<int64_t>(_duration * 1000)));
return;
}
// Wait forever if _count <= 0. Otherwise wait for a specific number of
// messages.
if (_count <= 0)
{
waitForShutdown();
}
else
{
while (count < _count)
{
std::unique_lock<std::mutex> lock(mutex);
condition.wait(lock, [&]{return count >= _count;});
}
}
}
//////////////////////////////////////////////////
extern "C" const char *gzVersion()
{
return GZ_TRANSPORT_VERSION_FULL;
}