forked from RobotLocomotion/drake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
drake_mock_lcm.cc
56 lines (47 loc) · 1.72 KB
/
drake_mock_lcm.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
#include "drake/lcm/drake_mock_lcm.h"
#include <cstring>
#include <utility>
#include "drake/common/drake_throw.h"
namespace drake {
namespace lcm {
DrakeMockLcm::DrakeMockLcm() {}
void DrakeMockLcm::Publish(const std::string& channel, const void* data,
int data_size, optional<double>) {
DRAKE_THROW_UNLESS(!channel.empty());
LastPublishedMessage* per_channel_data{};
auto iter = last_published_messages_.find(channel);
if (iter != last_published_messages_.end()) {
per_channel_data = &iter->second;
} else {
per_channel_data = &last_published_messages_[channel];
}
const uint8_t* bytes = static_cast<const uint8_t*>(data);
per_channel_data->data = std::vector<uint8_t>(&bytes[0], &bytes[data_size]);
per_channel_data->handled = false;
}
std::shared_ptr<DrakeSubscriptionInterface> DrakeMockLcm::Subscribe(
const std::string& channel, HandlerFunction handler) {
DRAKE_THROW_UNLESS(!channel.empty());
subscriptions_.emplace(channel, std::move(handler));
// TODO(jwnimmer-tri) Handle unsubscribe.
return nullptr;
}
int DrakeMockLcm::HandleSubscriptions(int) {
int result = 0;
for (auto& pub_iter : last_published_messages_) {
const std::string& channel = pub_iter.first;
LastPublishedMessage& per_channel_data = pub_iter.second;
if (!per_channel_data.handled) {
const auto& sub_range = subscriptions_.equal_range(channel);
for (auto iter = sub_range.first; iter != sub_range.second; ++iter) {
const HandlerFunction& handler = iter->second;
handler(per_channel_data.data.data(), per_channel_data.data.size());
}
++result;
per_channel_data.handled = true;
}
}
return result;
}
} // namespace lcm
} // namespace drake