forked from BehaviorTree/BehaviorTree.CPP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgroot2_publisher.cpp
More file actions
729 lines (622 loc) · 19.7 KB
/
groot2_publisher.cpp
File metadata and controls
729 lines (622 loc) · 19.7 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
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
#include "behaviortree_cpp/loggers/groot2_publisher.h"
#include "zmq_addon.hpp"
#include "behaviortree_cpp/loggers/groot2_protocol.h"
#include "behaviortree_cpp/xml_parsing.h"
#include <tuple>
namespace BT
{
//------------------------------------------------------
std::mutex Groot2Publisher::used_ports_mutex;
std::set<unsigned> Groot2Publisher::used_ports;
enum
{
IDLE_FROM_SUCCESS = 10 + static_cast<int>(NodeStatus::SUCCESS),
IDLE_FROM_FAILURE = 10 + static_cast<int>(NodeStatus::FAILURE),
IDLE_FROM_RUNNING = 10 + static_cast<int>(NodeStatus::RUNNING)
};
struct Transition
{
// when serializing, we will remove the initial time and serialize only
// 6 bytes, instead of 8
uint64_t timestamp_usec;
// if you have more than 64.000 nodes, you are doing something wrong :)
uint16_t node_uid;
// enough bits to contain NodeStatus
uint8_t status;
uint8_t padding[5];
};
namespace
{
std::array<char, 16> CreateRandomUUID()
{
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<uint32_t> dist;
std::array<char, 16> out{};
char* bytes = out.data();
for(int i = 0; i < 16; i += 4)
{
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
*reinterpret_cast<uint32_t*>(bytes + i) = dist(gen);
}
// variant must be 10xxxxxx
bytes[8] &= static_cast<char>(0xBF);
bytes[8] |= static_cast<char>(0x80);
// version must be 0100xxxx
bytes[6] &= 0x4F;
bytes[6] |= 0x40;
return out;
}
} // namespace
struct Groot2Publisher::PImpl
{
PImpl() : context(), server(context, ZMQ_REP), publisher(context, ZMQ_PUB)
{
server.set(zmq::sockopt::linger, 0);
publisher.set(zmq::sockopt::linger, 0);
const int timeout_rcv = 100;
server.set(zmq::sockopt::rcvtimeo, timeout_rcv);
publisher.set(zmq::sockopt::rcvtimeo, timeout_rcv);
const int timeout_ms = 1000;
server.set(zmq::sockopt::sndtimeo, timeout_ms);
publisher.set(zmq::sockopt::sndtimeo, timeout_ms);
}
unsigned server_port = 0;
std::string server_address;
std::string publisher_address;
std::string tree_xml;
std::atomic_bool active_server = true;
std::thread server_thread;
std::mutex status_mutex;
std::string status_buffer;
// each element of this map points to a character in _p->status_buffer
std::unordered_map<uint16_t, char*> status_buffermap;
// weak reference to the tree.
std::unordered_map<std::string, std::weak_ptr<BT::Tree::Subtree>> subtrees;
std::unordered_map<uint16_t, std::weak_ptr<BT::TreeNode>> nodes_by_uid;
std::mutex hooks_map_mutex;
std::unordered_map<uint16_t, Monitor::Hook::Ptr> pre_hooks;
std::unordered_map<uint16_t, Monitor::Hook::Ptr> post_hooks;
std::mutex last_heartbeat_mutex;
std::chrono::steady_clock::time_point last_heartbeat;
std::chrono::milliseconds max_heartbeat_delay = std::chrono::milliseconds(5000);
std::atomic_bool recording = false;
std::deque<Transition> transitions_buffer;
std::chrono::microseconds recording_fist_time{};
std::thread heartbeat_thread;
zmq::context_t context;
zmq::socket_t server;
zmq::socket_t publisher;
};
Groot2Publisher::Groot2Publisher(const BT::Tree& tree, unsigned server_port)
: StatusChangeLogger(tree.rootNode()), _p(new PImpl())
{
_p->server_port = server_port;
{
const std::unique_lock<std::mutex> lk(Groot2Publisher::used_ports_mutex);
if(Groot2Publisher::used_ports.count(server_port) != 0 ||
Groot2Publisher::used_ports.count(server_port + 1) != 0)
{
auto msg = StrCat("Another instance of Groot2Publisher is using port ",
std::to_string(server_port));
throw LogicError(msg);
}
Groot2Publisher::used_ports.insert(server_port);
Groot2Publisher::used_ports.insert(server_port + 1);
}
_p->tree_xml = WriteTreeToXML(tree, true, true);
//-------------------------------
// Prepare the status buffer
size_t node_count = 0;
for(const auto& subtree : tree.subtrees)
{
node_count += subtree->nodes.size();
}
_p->status_buffer.resize(3 * node_count);
unsigned ptr_offset = 0;
char* buffer_ptr = _p->status_buffer.data();
for(const auto& subtree : tree.subtrees)
{
auto name =
subtree->instance_name.empty() ? subtree->tree_ID : subtree->instance_name;
_p->subtrees.insert({ name, subtree });
for(const auto& node : subtree->nodes)
{
_p->nodes_by_uid.insert({ node->UID(), node });
ptr_offset += Monitor::Serialize(buffer_ptr, ptr_offset, node->UID());
_p->status_buffermap.insert({ node->UID(), buffer_ptr + ptr_offset });
ptr_offset += Monitor::Serialize(buffer_ptr, ptr_offset, uint8_t(NodeStatus::IDLE));
}
}
//-------------------------------
_p->server_address = StrCat("tcp://*:", std::to_string(server_port));
_p->publisher_address = StrCat("tcp://*:", std::to_string(server_port + 1));
_p->server.bind(_p->server_address.c_str());
_p->publisher.bind(_p->publisher_address.c_str());
_p->server_thread = std::thread(&Groot2Publisher::serverLoop, this);
_p->heartbeat_thread = std::thread(&Groot2Publisher::heartbeatLoop, this);
}
void Groot2Publisher::setMaxHeartbeatDelay(std::chrono::milliseconds delay)
{
_p->max_heartbeat_delay = delay;
}
std::chrono::milliseconds Groot2Publisher::maxHeartbeatDelay() const
{
return _p->max_heartbeat_delay;
}
Groot2Publisher::~Groot2Publisher()
{
removeAllHooks();
_p->active_server = false;
if(_p->server_thread.joinable())
{
_p->server_thread.join();
}
if(_p->heartbeat_thread.joinable())
{
_p->heartbeat_thread.join();
}
flush();
{
const std::unique_lock<std::mutex> lk(Groot2Publisher::used_ports_mutex);
Groot2Publisher::used_ports.erase(_p->server_port);
Groot2Publisher::used_ports.erase(_p->server_port + 1);
}
}
void Groot2Publisher::callback(Duration ts, const TreeNode& node, NodeStatus prev_status,
NodeStatus new_status)
{
const std::unique_lock<std::mutex> lk(_p->status_mutex);
auto status = static_cast<char>(new_status);
if(new_status == NodeStatus::IDLE)
{
status = static_cast<char>(10 + static_cast<int>(prev_status));
}
*(_p->status_buffermap.at(node.UID())) = status;
if(_p->recording)
{
Transition trans{};
trans.node_uid = node.UID();
trans.status = static_cast<uint8_t>(new_status);
auto timestamp = ts - _p->recording_fist_time;
trans.timestamp_usec =
std::chrono::duration_cast<std::chrono::microseconds>(timestamp).count();
_p->transitions_buffer.push_back(trans);
while(_p->transitions_buffer.size() > 1000)
{
_p->transitions_buffer.pop_front();
}
}
}
void Groot2Publisher::flush()
{
// nothing to do here...
}
void Groot2Publisher::serverLoop()
{
auto const serialized_uuid = CreateRandomUUID();
auto& socket = _p->server;
auto sendErrorReply = [&socket](const std::string& msg) {
zmq::multipart_t error_msg;
error_msg.addstr("error");
error_msg.addstr(msg);
error_msg.send(socket);
};
// initialize _p->last_heartbeat
{
const std::unique_lock lk(_p->last_heartbeat_mutex);
_p->last_heartbeat = std::chrono::steady_clock::now();
}
while(_p->active_server)
{
zmq::multipart_t requestMsg;
if(!requestMsg.recv(socket) || requestMsg.size() == 0)
{
continue;
}
// this heartbeat will help establishing if Groot is connected or not
{
const std::unique_lock lk(_p->last_heartbeat_mutex);
_p->last_heartbeat = std::chrono::steady_clock::now();
}
std::string const request_str = requestMsg[0].to_string();
if(request_str.size() != Monitor::RequestHeader::size())
{
sendErrorReply("wrong request header");
continue;
}
auto request_header = Monitor::DeserializeRequestHeader(request_str);
Monitor::ReplyHeader reply_header;
reply_header.request = request_header;
reply_header.request.protocol = Monitor::kProtocolID;
reply_header.tree_id = serialized_uuid;
zmq::multipart_t reply_msg;
reply_msg.addstr(Monitor::SerializeHeader(reply_header));
switch(request_header.type)
{
case Monitor::RequestType::FULLTREE: {
reply_msg.addstr(_p->tree_xml);
}
break;
case Monitor::RequestType::STATUS: {
const std::unique_lock<std::mutex> lk(_p->status_mutex);
reply_msg.addstr(_p->status_buffer);
}
break;
case Monitor::RequestType::BLACKBOARD: {
if(requestMsg.size() != 2)
{
sendErrorReply("must be 2 parts message");
continue;
}
std::string const bb_names_str = requestMsg[1].to_string();
auto msg = generateBlackboardsDump(bb_names_str);
reply_msg.addmem(msg.data(), msg.size());
}
break;
case Monitor::RequestType::HOOK_INSERT: {
if(requestMsg.size() != 2)
{
sendErrorReply("must be 2 parts message");
continue;
}
auto InsertHook = [this](nlohmann::json const& json) {
uint16_t const node_uid = json["uid"].get<uint16_t>();
Position const pos = static_cast<Position>(json["position"].get<int>());
if(auto hook = getHook(pos, node_uid))
{
std::unique_lock<std::mutex> lk(hook->mutex);
const bool was_interactive = (hook->mode == Monitor::Hook::Mode::BREAKPOINT);
BT::Monitor::from_json(json, *hook);
// if it WAS interactive and it is not anymore, unlock it
if(was_interactive && (hook->mode == Monitor::Hook::Mode::REPLACE))
{
hook->ready = true;
lk.unlock();
hook->wakeup.notify_all();
}
}
else // if not found, create a new one
{
auto new_hook = std::make_shared<Monitor::Hook>();
BT::Monitor::from_json(json, *new_hook);
insertHook(new_hook);
}
};
auto const received_json = nlohmann::json::parse(requestMsg[1].to_string());
// the json may contain a Hook or an array of Hooks
if(received_json.is_array())
{
for(auto const& json : received_json)
{
InsertHook(json);
}
}
else
{
InsertHook(received_json);
}
}
break;
case Monitor::RequestType::BREAKPOINT_UNLOCK: {
if(requestMsg.size() != 2)
{
sendErrorReply("must be 2 parts message");
continue;
}
auto json = nlohmann::json::parse(requestMsg[1].to_string());
const uint16_t node_uid = json.at("uid").get<uint16_t>();
const std::string status_str = json.at("desired_status").get<std::string>();
auto position = static_cast<Position>(json.at("position").get<int>());
const bool remove = json.at("remove_when_done").get<bool>();
NodeStatus desired_status = NodeStatus::SKIPPED;
if(status_str == "SUCCESS")
{
desired_status = NodeStatus::SUCCESS;
}
else if(status_str == "FAILURE")
{
desired_status = NodeStatus::FAILURE;
}
if(!unlockBreakpoint(position, node_uid, desired_status, remove))
{
sendErrorReply("Node ID not found");
continue;
}
}
break;
case Monitor::RequestType::REMOVE_ALL_HOOKS: {
removeAllHooks();
}
break;
case Monitor::RequestType::DISABLE_ALL_HOOKS: {
enableAllHooks(false);
}
break;
case Monitor::RequestType::HOOK_REMOVE: {
if(requestMsg.size() != 2)
{
sendErrorReply("must be 2 parts message");
continue;
}
auto json = nlohmann::json::parse(requestMsg[1].to_string());
const uint16_t node_uid = json.at("uid").get<uint16_t>();
auto position = static_cast<Position>(json.at("position").get<int>());
if(!removeHook(position, node_uid))
{
sendErrorReply("Node ID not found");
continue;
}
}
break;
case Monitor::RequestType::HOOKS_DUMP: {
const std::unique_lock<std::mutex> lk(_p->hooks_map_mutex);
auto json_out = nlohmann::json::array();
for(const auto& [node_uid, breakpoint] : _p->pre_hooks)
{
std::ignore = node_uid; // unused in this loop
json_out.push_back(*breakpoint);
}
reply_msg.addstr(json_out.dump());
}
break;
case Monitor::RequestType::TOGGLE_RECORDING: {
if(requestMsg.size() != 2)
{
sendErrorReply("must be 2 parts message");
continue;
}
auto const cmd = (requestMsg[1].to_string());
if(cmd == "start")
{
_p->recording = true;
// to keep the first time for callback
_p->recording_fist_time = std::chrono::duration_cast<std::chrono::microseconds>(
std::chrono::high_resolution_clock::now().time_since_epoch());
// to send consistent time for client
auto now = std::chrono::duration_cast<std::chrono::microseconds>(
std::chrono::system_clock::now().time_since_epoch());
reply_msg.addstr(std::to_string(now.count()));
const std::unique_lock<std::mutex> lk(_p->status_mutex);
_p->transitions_buffer.clear();
}
else if(cmd == "stop")
{
_p->recording = false;
}
}
break;
case Monitor::RequestType::GET_TRANSITIONS: {
thread_local std::string trans_buffer;
trans_buffer.resize(9 * _p->transitions_buffer.size());
const std::unique_lock<std::mutex> lk(_p->status_mutex);
size_t offset = 0;
for(const auto& trans : _p->transitions_buffer)
{
std::memcpy(&trans_buffer[offset], &trans.timestamp_usec, 6);
offset += 6;
std::memcpy(&trans_buffer[offset], &trans.node_uid, 2);
offset += 2;
std::memcpy(&trans_buffer[offset], &trans.status, 1);
offset += 1;
}
_p->transitions_buffer.clear();
trans_buffer.resize(offset);
reply_msg.addstr(trans_buffer);
}
break;
default: {
sendErrorReply("Request not recognized");
continue;
}
}
// send the reply
reply_msg.send(socket);
}
}
void BT::Groot2Publisher::enableAllHooks(bool enable)
{
const std::unique_lock<std::mutex> lk(_p->hooks_map_mutex);
for(const auto& [node_uid, hook] : _p->pre_hooks)
{
std::ignore = node_uid; // unused in this loop
std::unique_lock<std::mutex> lk(hook->mutex);
hook->enabled = enable;
// when disabling, remember to wake up blocked ones
if(!hook->enabled && hook->mode == Monitor::Hook::Mode::BREAKPOINT)
{
lk.unlock();
hook->wakeup.notify_all();
}
}
}
void Groot2Publisher::heartbeatLoop()
{
bool has_heartbeat = true;
while(_p->active_server)
{
std::this_thread::sleep_for(std::chrono::milliseconds(10));
auto now = std::chrono::steady_clock::now();
const bool prev_heartbeat = has_heartbeat;
{
const std::unique_lock lk(_p->last_heartbeat_mutex);
has_heartbeat = (now - _p->last_heartbeat < _p->max_heartbeat_delay);
}
// if we loose or gain heartbeat, disable/enable all breakpoints
if(has_heartbeat != prev_heartbeat)
{
enableAllHooks(has_heartbeat);
}
}
}
std::vector<uint8_t> Groot2Publisher::generateBlackboardsDump(const std::string& bb_list)
{
auto json = nlohmann::json();
auto const bb_names = BT::splitString(bb_list, ';');
for(auto name : bb_names)
{
std::string const bb_name(name);
auto it = _p->subtrees.find(bb_name);
if(it != _p->subtrees.end())
{
// lock the weak pointer
if(auto subtree = it->second.lock())
{
json[bb_name] = ExportBlackboardToJSON(*subtree->blackboard);
}
}
}
return nlohmann::json::to_msgpack(json);
}
bool Groot2Publisher::insertHook(std::shared_ptr<Monitor::Hook> hook)
{
auto const node_uid = hook->node_uid;
auto it = _p->nodes_by_uid.find(node_uid);
if(it == _p->nodes_by_uid.end())
{
return false;
}
const TreeNode::Ptr node = it->second.lock();
if(!node)
{
return false;
}
auto injectedCallback = [hook, this](TreeNode& node) -> NodeStatus {
std::unique_lock<std::mutex> lk(hook->mutex);
if(!hook->enabled)
{
return NodeStatus::SKIPPED;
}
// Notify that a breakpoint was reached, using the _p->publisher
const Monitor::RequestHeader breakpoint_request(Monitor::BREAKPOINT_REACHED);
zmq::multipart_t request_msg;
request_msg.addstr(Monitor::SerializeHeader(breakpoint_request));
request_msg.addstr(std::to_string(hook->node_uid));
request_msg.send(_p->publisher);
// wait until someone wake us up
if(hook->mode == Monitor::Hook::Mode::BREAKPOINT)
{
hook->wakeup.wait(lk, [hook]() { return hook->ready || !hook->enabled; });
hook->ready = false;
// wait was unblocked but it could be the breakpoint becoming disabled.
// in this case, just skip
if(!hook->enabled)
{
return NodeStatus::SKIPPED;
}
}
if(hook->remove_when_done)
{
// self-destruction at the end of this lambda function
const std::unique_lock<std::mutex> lk(_p->hooks_map_mutex);
_p->pre_hooks.erase(hook->node_uid);
node.setPreTickFunction({});
}
return hook->desired_status;
};
const std::unique_lock<std::mutex> lk(_p->hooks_map_mutex);
_p->pre_hooks[node_uid] = hook;
node->setPreTickFunction(injectedCallback);
return true;
}
bool Groot2Publisher::unlockBreakpoint(Position pos, uint16_t node_uid, NodeStatus result,
bool remove)
{
auto it = _p->nodes_by_uid.find(node_uid);
if(it == _p->nodes_by_uid.end())
{
return false;
}
const TreeNode::Ptr node = it->second.lock();
if(!node)
{
return false;
}
auto hook = getHook(pos, node_uid);
if(!hook)
{
return false;
}
{
std::unique_lock<std::mutex> lk(hook->mutex);
hook->desired_status = result;
hook->remove_when_done |= remove;
if(hook->mode == Monitor::Hook::Mode::BREAKPOINT)
{
hook->ready = true;
lk.unlock();
hook->wakeup.notify_all();
}
}
return true;
}
bool Groot2Publisher::removeHook(Position pos, uint16_t node_uid)
{
auto it = _p->nodes_by_uid.find(node_uid);
if(it == _p->nodes_by_uid.end())
{
return false;
}
const TreeNode::Ptr node = it->second.lock();
if(!node)
{
return false;
}
auto hook = getHook(pos, node_uid);
if(!hook)
{
return false;
}
{
const std::unique_lock<std::mutex> lk(_p->hooks_map_mutex);
_p->pre_hooks.erase(node_uid);
}
node->setPreTickFunction({});
// Disable breakpoint, if it was interactive and blocked
{
std::unique_lock<std::mutex> lk(hook->mutex);
if(hook->mode == Monitor::Hook::Mode::BREAKPOINT)
{
hook->enabled = false;
lk.unlock();
hook->wakeup.notify_all();
}
}
return true;
}
void Groot2Publisher::removeAllHooks()
{
std::vector<uint16_t> uids;
for(auto pos : { Position::PRE, Position::POST })
{
uids.clear();
auto* hooks = pos == Position::PRE ? &_p->pre_hooks : &_p->post_hooks;
std::unique_lock<std::mutex> lk(_p->hooks_map_mutex);
if(!hooks->empty())
{
uids.reserve(hooks->size());
for(const auto& [node_uid, hook_ptr] : *hooks)
{
std::ignore = hook_ptr; // unused in this loop
uids.push_back(node_uid);
}
lk.unlock();
for(auto node_uid : uids)
{
removeHook(pos, node_uid);
}
}
}
}
Monitor::Hook::Ptr Groot2Publisher::getHook(Position pos, uint16_t node_uid)
{
auto* hooks = pos == Position::PRE ? &_p->pre_hooks : &_p->post_hooks;
const std::unique_lock<std::mutex> lk(_p->hooks_map_mutex);
auto bk_it = hooks->find(node_uid);
if(bk_it == hooks->end())
{
return {};
}
return bk_it->second;
}
} // namespace BT