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_INCLUDED__
19 #define __IGN_TRANSPORT_HANDLERSTORAGE_HH_INCLUDED__
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  {
122  _handler = handler.second;
123  return true;
124  }
125  }
126  }
127  return false;
128  }
129 
136  public: bool Handler(const std::string &_topic,
137  const std::string &_nUuid,
138  const std::string &_hUuid,
139  std::shared_ptr<T> &_handler) const
140  {
141  if (this->data.find(_topic) == this->data.end())
142  return false;
143 
144  auto const &m = this->data.at(_topic);
145  if (m.find(_nUuid) == m.end())
146  return false;
147 
148  if (m.at(_nUuid).find(_hUuid) == m.at(_nUuid).end())
149  return false;
150 
151  _handler = m.at(_nUuid).at(_hUuid);
152  return true;
153  }
154 
160  public: void AddHandler(const std::string &_topic,
161  const std::string &_nUuid,
162  const std::shared_ptr<T> &_handler)
163  {
164  // Create the topic entry.
165  if (this->data.find(_topic) == this->data.end())
166  this->data[_topic] = UUIDHandler_Collection_M();
167 
168  // Create the Node UUID entry.
169  if (this->data[_topic].find(_nUuid) == this->data[_topic].end())
170  this->data[_topic][_nUuid] = UUIDHandler_M();
171 
172  // Add/Replace the Req handler.
173  this->data[_topic][_nUuid].insert(
174  std::make_pair(_handler->HandlerUuid(), _handler));
175  }
176 
181  public: bool HasHandlersForTopic(const std::string &_topic) const
182  {
183  if (this->data.find(_topic) == this->data.end())
184  return false;
185 
186  return !this->data.at(_topic).empty();
187  }
188 
193  public: bool HasHandlersForNode(const std::string &_topic,
194  const std::string &_nUuid) const
195  {
196  if (this->data.find(_topic) == this->data.end())
197  return false;
198 
199  return this->data.at(_topic).find(_nUuid) !=
200  this->data.at(_topic).end();
201  }
202 
209  public: bool RemoveHandler(const std::string &_topic,
210  const std::string &_nUuid,
211  const std::string &_reqUuid)
212  {
213  size_t counter = 0;
214  if (this->data.find(_topic) != this->data.end())
215  {
216  if (this->data[_topic].find(_nUuid) != this->data[_topic].end())
217  {
218  counter = this->data[_topic][_nUuid].erase(_reqUuid);
219  if (this->data[_topic][_nUuid].empty())
220  this->data[_topic].erase(_nUuid);
221  if (this->data[_topic].empty())
222  this->data.erase(_topic);
223  }
224  }
225 
226  return counter > 0;
227  }
228 
233  public: bool RemoveHandlersForNode(const std::string &_topic,
234  const std::string &_nUuid)
235  {
236  size_t counter = 0;
237  if (this->data.find(_topic) != this->data.end())
238  {
239  counter = this->data[_topic].erase(_nUuid);
240  if (this->data[_topic].empty())
241  this->data.erase(_topic);
242  }
243 
244  return counter > 0;
245  }
246 
250  private: TopicServiceCalls_M data;
251  };
252  }
253 }
254 
255 #endif
bool RemoveHandlersForNode(const std::string &_topic, const std::string &_nUuid)
Remove all the handlers from a given node.
Definition: HandlerStorage.hh:233
bool HasHandlersForTopic(const std::string &_topic) const
Return true if we have stored at least one request for the topic.
Definition: HandlerStorage.hh:181
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
ignition/transport/HandlerStorage.hh
Definition: HandlerStorage.hh:33
bool HasHandlersForNode(const std::string &_topic, const std::string &_nUuid) const
Check if a node has at least one handler.
Definition: HandlerStorage.hh:193
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
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
virtual ~HandlerStorage()=default
Destructor.
HandlerStorage()=default
Constructor.
bool RemoveHandler(const std::string &_topic, const std::string &_nUuid, const std::string &_reqUuid)
Remove a request handler.
Definition: HandlerStorage.hh:209
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:136
Definition: AdvertiseOptions.hh:25
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:160