Drake
drake_mock_lcm.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <map>
4 #include <memory>
5 #include <string>
6 #include <vector>
7 
12 
13 namespace drake {
14 namespace lcm {
15 
16 /**
17  * A *mock* LCM instance. This does not actually publish or subscribe to LCM
18  * messages. It contains additional methods for accessing the most recent
19  * message that was "published," and faking a callback.
20  */
22  public:
24 
25  /**
26  * A constructor that creates a DrakeMockLcm with loopback disabled, i.e., a
27  * call to Publish() will not result in subscriber callback functions being
28  * executed. To enable loopback behavior, call EnableLoopBack().
29  */
30  DrakeMockLcm();
31 
32  /**
33  * Enables loopback behavior. With loopback enabled, a call to Publish() will
34  * result in subscriber callback functions being called. Without loopback
35  * enabled, the only way to induce a call to a subscriber's callback function
36  * is through InduceSubscriberCallback().
37  */
38  void EnableLoopBack() { enable_loop_back_ = true; }
39 
40  void Publish(const std::string&, const void*, int, optional<double>) override;
41 
42  /**
43  * Obtains the most recently "published" message on a particular channel.
44  * This method automatically decodes the message into an LCM message whose
45  * type is specified by the template type. Throws a std::runtime_error
46  * exception if no LCM message was published on the provided channel or if the
47  * message failed to be decoded by the provided LCM message type.
48  *
49  * @tparam T The LCM message type.
50  *
51  * @param[in] channel the LCM channel for which the last published message is
52  * returned.
53  *
54  * @return the decoded most recently transmitted LCM message on the provided
55  * channel.
56  */
57  template<typename T>
58  T DecodeLastPublishedMessageAs(const std::string& channel) const {
59  const std::vector<uint8_t>& message_bytes =
61 
62  T transmitted_message{};
63  const int num_bytes = transmitted_message.decode(message_bytes.data(), 0,
64  message_bytes.size());
65 
66  if (num_bytes != static_cast<int>(message_bytes.size())) {
67  throw std::runtime_error("DrakeMockLcm::DecodeLastPublishedMessageAs(): "
68  "ERROR: Failed to decode the LCM message. Number of bytes decoded: " +
69  std::to_string(num_bytes) + ". Number of message bytes that should "
70  "have been decoded: " + std::to_string(message_bytes.size()) + ".");
71  }
72 
73  return transmitted_message;
74  }
75 
76  /**
77  * Obtains the most recently "published" message on a particular channel. A
78  * std::runtime_error will be thrown if no message was published on the
79  * provide channel.
80  *
81  * @param[in] channel The channel on which the LCM message was published.
82  *
83  * @return A reference to a vector containing the serialized bytes of the
84  * LCM message that was previously published on channel @p channel.
85  *
86  * @pre A message was previously published on channel @p channel.
87  */
88  const std::vector<uint8_t>& get_last_published_message(
89  const std::string& channel) const;
90 
91  /**
92  * Returns the time of the most recent publication on a particular channel.
93  * Returns nullopt iff a message has never been published on this channel or
94  * the most recent Publish call had no time_sec.
95  */
96  optional<double> get_last_publication_time(const std::string& channel) const;
97 
98  void Subscribe(const std::string&, HandlerFunction) override;
99 
100 #pragma GCC diagnostic push
101 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
102  void Subscribe(const std::string&,
103  DrakeLcmMessageHandlerInterface*) override;
104 #pragma GCC diagnostic pop // pop -Wdeprecated-declarations
105 
106  /**
107  * Fakes a callback. The callback is executed by the same thread as the one
108  * calling this method.
109  *
110  * @param[in] channel The channel on which to publish the message.
111  *
112  * @param[in] data A buffer containing the serialized bytes of the message to
113  * publish.
114  *
115  * @param[in] data_size The length of @data in bytes.
116  */
117  void InduceSubscriberCallback(const std::string& channel, const void* data,
118  int data_size);
119 
120  private:
121  bool enable_loop_back_{false};
122 
123  struct LastPublishedMessage {
124  std::vector<uint8_t> data{};
125  optional<double> time_sec{};
126  };
127 
128  std::map<std::string, LastPublishedMessage> last_published_messages_;
129 
130  // Maps the channel name to the subscriber.
131  std::multimap<std::string, HandlerFunction> subscriptions_;
132 };
133 
134 } // namespace lcm
135 } // namespace drake
void EnableLoopBack()
Enables loopback behavior.
Definition: drake_mock_lcm.h:38
void Subscribe(const std::string &, HandlerFunction) override
Subscribes to an LCM channel without automatic message decoding.
Definition: drake_mock_lcm.cc:62
std::function< void(const void *, int)> HandlerFunction
A callback used by DrakeLcmInterface::Subscribe(), with arguments:
Definition: drake_lcm_interface.h:33
void InduceSubscriberCallback(const std::string &channel, const void *data, int data_size)
Fakes a callback.
Definition: drake_mock_lcm.cc:78
void Publish(const std::string &, const void *, int, optional< double >) override
Publishes an LCM message on channel channel.
Definition: drake_mock_lcm.cc:16
Definition: automotive_demo.cc:90
optional< double > get_last_publication_time(const std::string &channel) const
Returns the time of the most recent publication on a particular channel.
Definition: drake_mock_lcm.cc:53
std::string to_string(const drake::geometry::Identifier< Tag > &id)
Enables use of identifiers with to_string.
Definition: identifier.h:211
stx::optional< T > optional
Definition: drake_optional.h:22
A pure virtual interface that enables LCM to be mocked.
Definition: drake_lcm_interface.h:22
Provides drake::optional as an alias for the appropriate implementation of std::optional or std::expe...
const std::vector< uint8_t > & get_last_published_message(const std::string &channel) const
Obtains the most recently "published" message on a particular channel.
Definition: drake_mock_lcm.cc:37
A mock LCM instance.
Definition: drake_mock_lcm.h:21
DrakeMockLcm()
A constructor that creates a DrakeMockLcm with loopback disabled, i.e., a call to Publish() will not ...
Definition: drake_mock_lcm.cc:13
T DecodeLastPublishedMessageAs(const std::string &channel) const
Obtains the most recently "published" message on a particular channel.
Definition: drake_mock_lcm.h:58
#define DRAKE_NO_COPY_NO_MOVE_NO_ASSIGN(Classname)
DRAKE_NO_COPY_NO_MOVE_NO_ASSIGN deletes the special member functions for copy-construction, copy-assignment, move-construction, and move-assignment.
Definition: drake_copyable.h:33
Provides careful macros to selectively enable or disable the special member functions for copy-constr...
int data
Definition: value_test.cc:20