All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 IGN_TRANSPORT_HANDLERSTORAGE_HH_
19 #define IGN_TRANSPORT_HANDLERSTORAGE_HH_
20 
21 #include <map>
22 #include <string>
23 
25 
26 namespace ignition
27 {
28  namespace transport
29  {
33  template<typename T> class HandlerStorage
34  {
39  using UUIDHandler_M = std::map<std::string, std::shared_ptr<T>>;
40  using UUIDHandler_Collection_M = std::map<std::string, UUIDHandler_M>;
41 
43  using TopicServiceCalls_M =
44  std::map<std::string, UUIDHandler_Collection_M>;
45 
47  public: HandlerStorage() = default;
48 
50  public: virtual ~HandlerStorage() = default;
51 
60  public: bool Handlers(const std::string &_topic,
61  std::map<std::string,
62  std::map<std::string, std::shared_ptr<T> >> &_handlers) const
63  {
64  if (this->data.find(_topic) == this->data.end())
65  return false;
66 
67  _handlers = this->data.at(_topic);
68  return true;
69  }
70 
78  public: bool FirstHandler(const std::string &_topic,
79  const std::string &_reqTypeName,
80  const std::string &_repTypeName,
81  std::shared_ptr<T> &_handler) const
82  {
83  if (this->data.find(_topic) == this->data.end())
84  return false;
85 
86  const auto &m = this->data.at(_topic);
87  for (const auto &node : m)
88  {
89  for (const auto &handler : node.second)
90  {
91  if (_reqTypeName == handler.second->ReqTypeName() &&
92  _repTypeName == handler.second->RepTypeName())
93  {
94  _handler = handler.second;
95  return true;
96  }
97  }
98  }
99  return false;
100  }
101 
108  public: bool FirstHandler(const std::string &_topic,
109  const std::string &_msgTypeName,
110  std::shared_ptr<T> &_handler) const
111  {
112  if (this->data.find(_topic) == this->data.end())
113  return false;
114 
115  const auto &m = this->data.at(_topic);
116  for (const auto &node : m)
117  {
118  for (const auto &handler : node.second)
119  {
120  if (_msgTypeName == handler.second->TypeName() ||
121  handler.second->TypeName() == kGenericMessageType)
122  {
123  _handler = handler.second;
124  return true;
125  }
126  }
127  }
128  return false;
129  }
130 
137  public: bool Handler(const std::string &_topic,
138  const std::string &_nUuid,
139  const std::string &_hUuid,
140  std::shared_ptr<T> &_handler) const
141  {
142  if (this->data.find(_topic) == this->data.end())
143  return false;
144 
145  auto const &m = this->data.at(_topic);
146  if (m.find(_nUuid) == m.end())
147  return false;
148 
149  if (m.at(_nUuid).find(_hUuid) == m.at(_nUuid).end())
150  return false;
151 
152  _handler = m.at(_nUuid).at(_hUuid);
153  return true;
154  }
155 
161  public: void AddHandler(const std::string &_topic,
162  const std::string &_nUuid,
163  const std::shared_ptr<T> &_handler)
164  {
165  // Create the topic entry.
166  if (this->data.find(_topic) == this->data.end())
167  this->data[_topic] = UUIDHandler_Collection_M();
168 
169  // Create the Node UUID entry.
170  if (this->data[_topic].find(_nUuid) == this->data[_topic].end())
171  this->data[_topic][_nUuid] = UUIDHandler_M();
172 
173  // Add/Replace the Req handler.
174  this->data[_topic][_nUuid].insert(
175  std::make_pair(_handler->HandlerUuid(), _handler));
176  }
177 
182  public: bool HasHandlersForTopic(const std::string &_topic) const
183  {
184  if (this->data.find(_topic) == this->data.end())
185  return false;
186 
187  return !this->data.at(_topic).empty();
188  }
189 
194  public: bool HasHandlersForNode(const std::string &_topic,
195  const std::string &_nUuid) const
196  {
197  if (this->data.find(_topic) == this->data.end())
198  return false;
199 
200  return this->data.at(_topic).find(_nUuid) !=
201  this->data.at(_topic).end();
202  }
203 
210  public: bool RemoveHandler(const std::string &_topic,
211  const std::string &_nUuid,
212  const std::string &_reqUuid)
213  {
214  size_t counter = 0;
215  if (this->data.find(_topic) != this->data.end())
216  {
217  if (this->data[_topic].find(_nUuid) != this->data[_topic].end())
218  {
219  counter = this->data[_topic][_nUuid].erase(_reqUuid);
220  if (this->data[_topic][_nUuid].empty())
221  this->data[_topic].erase(_nUuid);
222  if (this->data[_topic].empty())
223  this->data.erase(_topic);
224  }
225  }
226 
227  return counter > 0;
228  }
229 
234  public: bool RemoveHandlersForNode(const std::string &_topic,
235  const std::string &_nUuid)
236  {
237  size_t counter = 0;
238  if (this->data.find(_topic) != this->data.end())
239  {
240  counter = this->data[_topic].erase(_nUuid);
241  if (this->data[_topic].empty())
242  this->data.erase(_topic);
243  }
244 
245  return counter > 0;
246  }
247 
251  private: TopicServiceCalls_M data;
252  };
253  }
254 }
255 
256 #endif
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: HandlerStorage.hh:108
bool RemoveHandlersForNode(const std::string &_topic, const std::string &_nUuid)
Remove all the handlers from a given node.
Definition: HandlerStorage.hh:234
ignition/transport/HandlerStorage.hh
Definition: HandlerStorage.hh:33
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: HandlerStorage.hh:78
bool HasHandlersForTopic(const std::string &_topic) const
Return true if we have stored at least one request for the topic.
Definition: HandlerStorage.hh:182
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.
Definition: HandlerStorage.hh:60
virtual ~HandlerStorage()=default
Destructor.
bool HasHandlersForNode(const std::string &_topic, const std::string &_nUuid) const
Check if a node has at least one handler.
Definition: HandlerStorage.hh:194
HandlerStorage()=default
Constructor.
bool RemoveHandler(const std::string &_topic, const std::string &_nUuid, const std::string &_reqUuid)
Remove a request handler.
Definition: HandlerStorage.hh:210
const std::string kGenericMessageType
The string type used for generic messages.
Definition: TransportTypes.hh:132
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: HandlerStorage.hh:137
void AddHandler(const std::string &_topic, const std::string &_nUuid, const std::shared_ptr< T > &_handler)
Add a request handler to a topic.
Definition: HandlerStorage.hh:161