Gazebo Transport

API Reference

11.4.1
gz/transport/HandlerStorage.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_HANDLERSTORAGE_HH_
19 #define GZ_TRANSPORT_HANDLERSTORAGE_HH_
20 
21 #include <map>
22 #include <memory>
23 #include <string>
24 #include <utility>
25 
26 #include "gz/transport/config.hh"
28 
29 namespace ignition::transport
30 {
31  // Inline bracket to help doxygen filtering.
32  inline namespace IGNITION_TRANSPORT_VERSION_NAMESPACE {
33  //
37  template<typename T> class HandlerStorage
38  {
45 
47  using TopicServiceCalls_M =
49 
51  public: HandlerStorage() = default;
52 
54  public: virtual ~HandlerStorage() = default;
55 
64  public: bool Handlers(const std::string &_topic,
66  std::map<std::string, std::shared_ptr<T> >> &_handlers) const
67  {
68  if (this->data.find(_topic) == this->data.end())
69  return false;
70 
71  _handlers = this->data.at(_topic);
72  return true;
73  }
74 
82  public: bool FirstHandler(const std::string &_topic,
83  const std::string &_reqTypeName,
84  const std::string &_repTypeName,
85  std::shared_ptr<T> &_handler) const
86  {
87  if (this->data.find(_topic) == this->data.end())
88  return false;
89 
90  const auto &m = this->data.at(_topic);
91  for (const auto &node : m)
92  {
93  for (const auto &handler : node.second)
94  {
95  if (_reqTypeName == handler.second->ReqTypeName() &&
96  _repTypeName == handler.second->RepTypeName())
97  {
98  _handler = handler.second;
99  return true;
100  }
101  }
102  }
103  return false;
104  }
105 
112  public: bool FirstHandler(const std::string &_topic,
113  const std::string &_msgTypeName,
114  std::shared_ptr<T> &_handler) const
115  {
116  if (this->data.find(_topic) == this->data.end())
117  return false;
118 
119  const auto &m = this->data.at(_topic);
120  for (const auto &node : m)
121  {
122  for (const auto &handler : node.second)
123  {
124  if (_msgTypeName == handler.second->TypeName() ||
125  handler.second->TypeName() == kGenericMessageType)
126  {
127  _handler = handler.second;
128  return true;
129  }
130  }
131  }
132  return false;
133  }
134 
141  public: bool Handler(const std::string &_topic,
142  const std::string &_nUuid,
143  const std::string &_hUuid,
144  std::shared_ptr<T> &_handler) const
145  {
146  if (this->data.find(_topic) == this->data.end())
147  return false;
148 
149  auto const &m = this->data.at(_topic);
150  if (m.find(_nUuid) == m.end())
151  return false;
152 
153  if (m.at(_nUuid).find(_hUuid) == m.at(_nUuid).end())
154  return false;
155 
156  _handler = m.at(_nUuid).at(_hUuid);
157  return true;
158  }
159 
165  public: void AddHandler(const std::string &_topic,
166  const std::string &_nUuid,
167  const std::shared_ptr<T> &_handler)
168  {
169  // Create the topic entry.
170  if (this->data.find(_topic) == this->data.end())
171  this->data[_topic] = UUIDHandler_Collection_M();
172 
173  // Create the Node UUID entry.
174  if (this->data[_topic].find(_nUuid) == this->data[_topic].end())
175  this->data[_topic][_nUuid] = UUIDHandler_M();
176 
177  // Add/Replace the Req handler.
178  this->data[_topic][_nUuid].insert(
179  std::make_pair(_handler->HandlerUuid(), _handler));
180  }
181 
186  public: bool HasHandlersForTopic(const std::string &_topic) const
187  {
188  if (this->data.find(_topic) == this->data.end())
189  return false;
190 
191  return !this->data.at(_topic).empty();
192  }
193 
198  public: bool HasHandlersForNode(const std::string &_topic,
199  const std::string &_nUuid) const
200  {
201  if (this->data.find(_topic) == this->data.end())
202  return false;
203 
204  return this->data.at(_topic).find(_nUuid) !=
205  this->data.at(_topic).end();
206  }
207 
214  public: bool RemoveHandler(const std::string &_topic,
215  const std::string &_nUuid,
216  const std::string &_reqUuid)
217  {
218  size_t counter = 0;
219  if (this->data.find(_topic) != this->data.end())
220  {
221  if (this->data[_topic].find(_nUuid) != this->data[_topic].end())
222  {
223  counter = this->data[_topic][_nUuid].erase(_reqUuid);
224  if (this->data[_topic][_nUuid].empty())
225  this->data[_topic].erase(_nUuid);
226  if (this->data[_topic].empty())
227  this->data.erase(_topic);
228  }
229  }
230 
231  return counter > 0;
232  }
233 
238  public: bool RemoveHandlersForNode(const std::string &_topic,
239  const std::string &_nUuid)
240  {
241  size_t counter = 0;
242  if (this->data.find(_topic) != this->data.end())
243  {
244  counter = this->data[_topic].erase(_nUuid);
245  if (this->data[_topic].empty())
246  this->data.erase(_topic);
247  }
248 
249  return counter > 0;
250  }
251 
255  private: TopicServiceCalls_M data;
256  };
257  }
258 }
259 
260 #endif
bool Handler(const std::string &_topic, const std::string &_nUuid, const std::string &_hUuid, std::shared_ptr< T > &_handler) const
Get a specific handler.
Definition: gz/transport/HandlerStorage.hh:141
bool RemoveHandler(const std::string &_topic, const std::string &_nUuid, const std::string &_reqUuid)
Remove a request handler. The node's uuid is used as a key to remove the appropriate request handler.
Definition: gz/transport/HandlerStorage.hh:214
bool HasHandlersForNode(const std::string &_topic, const std::string &_nUuid) const
Check if a node has at least one handler.
Definition: gz/transport/HandlerStorage.hh:198
STL class.
STL class.
bool FirstHandler(const std::string &_topic, const std::string &_reqTypeName, const std::string &_repTypeName, std::shared_ptr< T > &_handler) const
Get the first handler for a topic that matches a specific pair of request/response types.
Definition: gz/transport/HandlerStorage.hh:82
T find(T... args)
bool Handlers(const std::string &_topic, std::map< std::string, std::map< std::string, std::shared_ptr< T > >> &_handlers) const
Get the data handlers for a topic. A request handler stores the callback and types associated to a se...
Definition: gz/transport/HandlerStorage.hh:64
HandlerStorage()=default
Constructor.
bool FirstHandler(const std::string &_topic, const std::string &_msgTypeName, std::shared_ptr< T > &_handler) const
Get the first handler for a topic that matches a specific message type.
Definition: gz/transport/HandlerStorage.hh:112
T at(T... args)
void AddHandler(const std::string &_topic, const std::string &_nUuid, const std::shared_ptr< T > &_handler)
Add a request handler to a topic. A request handler stores the callback and types associated to a ser...
Definition: gz/transport/HandlerStorage.hh:165
bool RemoveHandlersForNode(const std::string &_topic, const std::string &_nUuid)
Remove all the handlers from a given node.
Definition: gz/transport/HandlerStorage.hh:238
T erase(T... args)
virtual ~HandlerStorage()=default
Destructor.
STL class.
bool HasHandlersForTopic(const std::string &_topic) const
Return true if we have stored at least one request for the topic.
Definition: gz/transport/HandlerStorage.hh:186
Definition: gz/transport/AdvertiseOptions.hh:28
T insert(T... args)
const std::string kGenericMessageType
The string type used for generic messages.
Definition: gz/transport/TransportTypes.hh:172
T empty(T... args)
T make_pair(T... args)
T end(T... args)
Class to store and manage service call handlers.
Definition: gz/transport/HandlerStorage.hh:37