Gazebo Transport
API Reference
15.0.2
insert_drive_file
Tutorials
library_books
Classes
toc
Namespaces
insert_drive_file
Files
launch
Gazebo Website
Index
List
Hierarchy
Members: All
Members: Functions
Members: Variables
Members: Typedefs
Members: Enumerations
Members: Enumerator
List
Members
Functions
Typedefs
Variables
Enumerations
Enumerator
src
gz-transport
include
gz
transport
ReqHandler.hh
Go to the documentation of this file.
1
/*
2
* Copyright (C) 2014 Open Source Robotics Foundation
3
*
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
7
*
8
* http://www.apache.org/licenses/LICENSE-2.0
9
*
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
15
*
16
*/
17
18
#ifndef GZ_TRANSPORT_REQHANDLER_HH_
19
#define GZ_TRANSPORT_REQHANDLER_HH_
20
21
#ifdef _MSC_VER
22
#pragma warning(push)
23
#pragma warning(disable: 4251)
24
#endif
25
#include <google/protobuf/message.h>
26
#ifdef _MSC_VER
27
#pragma warning(pop)
28
#endif
29
30
#include <condition_variable>
31
#include <functional>
32
#include <memory>
33
#include <string>
34
#include <utility>
35
36
#include "gz/transport/config.hh"
37
#include "gz/transport/Export.hh"
38
#include "
gz/transport/TransportTypes.hh
"
39
#include "
gz/transport/Uuid.hh
"
40
41
namespace
zenoh
42
{
43
// Forward declaration.
44
class
Session;
45
}
46
47
namespace
gz::transport
48
{
49
// Inline bracket to help doxygen filtering.
50
inline
namespace
GZ_TRANSPORT_VERSION_NAMESPACE {
51
//
53
class
IReqHandlerPrivate;
54
57
class
GZ_TRANSPORT_VISIBLE
IReqHandler
58
{
61
public
:
explicit
IReqHandler
(
const
std::string
&_nUuid);
62
64
public
:
virtual
~IReqHandler
();
65
72
public
:
virtual
void
NotifyResult
(
const
std::string
&_rep,
73
const
bool
_result) = 0;
74
78
public
:
virtual
bool
Serialize
(
std::string
&_buffer)
const
= 0;
79
82
public
:
virtual
std::string
ReqTypeName
()
const
= 0;
83
86
public
:
virtual
std::string
RepTypeName
()
const
= 0;
87
90
public
:
std::string
HandlerUuid
()
const
;
91
94
public
:
std::string
NodeUuid
()
const
;
95
98
public
:
std::string
Response
()
const
99
{
100
return
this->rep;
101
}
102
105
public
:
bool
Result
()
const
106
{
107
return
this->result;
108
}
109
112
public
:
bool
Requested
()
const
;
113
116
public
:
void
Requested
(
const
bool
_value);
117
125
public
:
template
<
typename
Lock>
bool
WaitUntil
(Lock &_lock,
126
const
unsigned
int
_timeout)
127
{
128
auto
now =
std::chrono::steady_clock::now
();
129
return
this->condition.wait_until(_lock,
130
now +
std::chrono::milliseconds
(_timeout),
131
[
this
]
132
{
133
return
this->repAvailable;
134
});
135
}
136
137
#ifdef HAVE_ZENOH
141
public
:
void
CreateZenohGet(
std::shared_ptr<zenoh::Session>
_session,
142
const
std::string
&_service);
143
#endif
144
145
#ifdef _WIN32
146
// Disable warning C4251 which is triggered by
147
// std::*
148
#pragma warning(push)
149
#pragma warning(disable: 4251)
150
#endif
152
protected
:
std::unique_ptr<IReqHandlerPrivate>
dataPtr
;
153
156
protected
:
std::condition_variable_any
condition
;
157
159
protected
:
std::string
rep
;
160
#ifdef _WIN32
161
#pragma warning(pop)
162
#endif
163
165
protected
:
bool
result
;
166
170
public
:
bool
repAvailable
;
171
};
172
178
template
<
typename
Req,
typename
Rep>
class
ReqHandler
179
:
public
IReqHandler
180
{
181
// Documentation inherited.
182
public
:
explicit
ReqHandler
(
const
std::string
&_nUuid)
183
:
IReqHandler
(_nUuid)
184
{
185
}
186
190
public
:
std::shared_ptr<Rep>
CreateMsg
(
const
std::string
&_data)
const
191
{
192
// Instantiate a specific protobuf message
193
auto
msgPtr = std::make_shared<Rep>();
194
195
// Create the message using some serialized data
196
if
(!msgPtr->ParseFromString(_data))
197
{
198
std::cerr
<<
"ReqHandler::CreateMsg() error: ParseFromString failed"
199
<<
std::endl
;
200
}
201
202
return
msgPtr;
203
}
204
210
public
:
void
SetCallback
(
const
std::function
<
void
(
211
const
Rep &_rep,
const
bool
_result)> &_cb)
212
{
213
this->cb = _cb;
214
}
215
219
public
:
void
SetMessage
(
const
Req *_reqMsg)
220
{
221
if
(!_reqMsg)
222
{
223
std::cerr
<<
"ReqHandler::SetMessage() _reqMsg is null"
<<
std::endl
;
224
return
;
225
}
226
227
this->reqMsg.CopyFrom(*_reqMsg);
228
}
229
235
public
:
void
SetResponse
(
const
Rep *_repMsg)
236
{
237
(void)_repMsg;
238
}
239
240
// Documentation inherited
241
public
:
bool
Serialize
(
std::string
&_buffer)
const
242
{
243
if
(!this->reqMsg.SerializeToString(&_buffer))
244
{
245
std::cerr
<<
"ReqHandler::Serialize(): Error serializing the request"
246
<<
std::endl
;
247
return
false
;
248
}
249
250
return
true
;
251
}
252
253
// Documentation inherited.
254
public
:
void
NotifyResult
(
const
std::string
&_rep,
const
bool
_result)
255
{
256
// Execute the callback (if existing).
257
if
(this->cb)
258
{
259
// Instantiate the specific protobuf message associated to this topic.
260
auto
msg = this->CreateMsg(_rep);
261
262
this->cb(*msg, _result);
263
}
264
else
265
{
266
this->rep = _rep;
267
this->result = _result;
268
}
269
270
this->repAvailable =
true
;
271
this->condition.notify_one();
272
}
273
274
// Documentation inherited.
275
public
:
virtual
std::string
ReqTypeName
()
const
276
{
277
return
std::string
(Req().GetTypeName());
278
}
279
280
// Documentation inherited.
281
public
:
virtual
std::string
RepTypeName
()
const
282
{
283
return
std::string
(Rep().GetTypeName());
284
}
285
287
private
: Req reqMsg;
288
294
private
:
std::function
<void(
const
Rep &_rep,
const
bool
_result)> cb;
295
};
296
300
template
<>
class
ReqHandler
<google::protobuf::Message,
301
google::protobuf::Message>
302
:
public
IReqHandler
303
{
304
// Documentation inherited.
305
public
:
explicit
ReqHandler
(
const
std::string
&_nUuid)
306
:
IReqHandler
(_nUuid)
307
{
308
}
309
313
public
:
void
SetMessage
(
const
google::protobuf::Message *_reqMsg)
314
{
315
if
(!_reqMsg)
316
{
317
std::cerr
<<
"ReqHandler::SetMessage() _reqMsg is null"
<<
std::endl
;
318
return
;
319
}
320
321
this->reqMsg.reset(_reqMsg->New());
322
this->reqMsg->CopyFrom(*_reqMsg);
323
}
324
329
public
:
void
SetResponse
(
const
google::protobuf::Message *_repMsg)
330
{
331
if
(!_repMsg)
332
{
333
std::cerr
<<
"ReqHandler::SetResponse() _repMsg is null"
<<
std::endl
;
334
return
;
335
}
336
337
this->repMsg.reset(_repMsg->New());
338
this->repMsg->CopyFrom(*_repMsg);
339
}
340
341
// Documentation inherited
342
public
:
bool
Serialize
(
std::string
&_buffer)
const
343
{
344
if
(!this->reqMsg)
345
{
346
std::cerr
<<
"ReqHandler::Serialize() reqMsg is null"
<<
std::endl
;
347
return
false
;
348
}
349
350
if
(!this->reqMsg->SerializeToString(&_buffer))
351
{
352
std::cerr
<<
"ReqHandler::Serialize(): Error serializing the request"
353
<<
std::endl
;
354
return
false
;
355
}
356
357
return
true
;
358
}
359
360
// Documentation inherited.
361
public
:
void
NotifyResult
(
const
std::string
&_rep,
const
bool
_result)
362
{
363
this->rep = _rep;
364
this->result = _result;
365
366
this->repAvailable =
true
;
367
this->condition.notify_one();
368
}
369
370
// Documentation inherited.
371
public
:
virtual
std::string
ReqTypeName
()
const
372
{
373
if
(this->reqMsg)
374
return
std::string
(this->reqMsg->GetTypeName());
375
else
376
{
377
std::cerr
<<
"ReqHandler::ReqTypeName() Warning: Using ReqTypeName() "
378
<<
"without type information"
<<
std::endl
;
379
return
""
;
380
}
381
}
382
384
public
:
virtual
std::string
RepTypeName
()
const
385
{
386
if
(this->repMsg)
387
return
std::string
(this->repMsg->GetTypeName());
388
else
389
{
390
std::cerr
<<
"ReqHandler::RepTypeName() Warning: Using RepTypeName() "
391
<<
"without type information"
<<
std::endl
;
392
return
""
;
393
}
394
}
395
397
private
:
std::unique_ptr<google::protobuf::Message>
reqMsg;
398
400
private
:
std::unique_ptr<google::protobuf::Message>
repMsg;
401
};
402
}
403
}
404
405
#endif