TopicStorage.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_TOPICSTORAGE_HH_INCLUDED__
19 #define __IGN_TRANSPORT_TOPICSTORAGE_HH_INCLUDED__
20 
21 #include <algorithm>
22 #include <map>
23 #include <string>
24 #include <vector>
25 
29 
30 namespace ignition
31 {
32  namespace transport
33  {
37  template<typename T> class IGNITION_VISIBLE TopicStorage
38  {
40  public: TopicStorage() = default;
41 
43  public: virtual ~TopicStorage() = default;
44 
49  public: bool AddPublisher(const T &_publisher)
50  {
51  // The topic does not exist.
52  if (this->data.find(_publisher.Topic()) == this->data.end())
53  {
54  // VS2013 is buggy with initializer list here {}
55  this->data[_publisher.Topic()] =
56  std::map<std::string, std::vector<T>>();
57  }
58 
59  // Check if the process uuid exists.
60  auto &m = this->data[_publisher.Topic()];
61  if (m.find(_publisher.PUuid()) != m.end())
62  {
63  // Check that the Publisher does not exist.
64  auto &v = m[_publisher.PUuid()];
65  auto found = std::find_if(v.begin(), v.end(),
66  [&](const T &_pub)
67  {
68  return _pub.Addr() == _publisher.Addr() &&
69  _pub.NUuid() == _publisher.NUuid();
70  });
71 
72  // The publisher was already existing, just exit.
73  if (found != v.end())
74  return false;
75  }
76 
77  // Add a new Publisher entry.
78  m[_publisher.PUuid()].push_back(T(_publisher));
79  return true;
80  }
81 
85  public: bool HasTopic(const std::string &_topic) const
86  {
87  return this->data.find(_topic) != this->data.end();
88  }
89 
96  public: bool HasAnyPublishers(const std::string &_topic,
97  const std::string &_pUuid) const
98  {
99  if (!this->HasTopic(_topic))
100  return false;
101 
102  return this->data.at(_topic).find(_pUuid) !=
103  this->data.at(_topic).end();
104  }
105 
109  public: bool HasPublisher(const std::string &_addr) const
110  {
111  for (auto &topic : this->data)
112  {
113  for (auto &proc : topic.second)
114  {
115  for (auto &pub : proc.second)
116  {
117  if (pub.Addr() == _addr)
118  return true;
119  }
120  }
121  }
122  return false;
123  }
124 
131  public: bool Publisher(const std::string &_topic,
132  const std::string &_pUuid,
133  const std::string &_nUuid,
134  T &_publisher) const
135  {
136  // Topic not found.
137  if (this->data.find(_topic) == this->data.end())
138  return false;
139 
140  // m is {pUUID=>Publisher}.
141  auto &m = this->data.at(_topic);
142 
143  // pUuid not found.
144  if (m.find(_pUuid) == m.end())
145  return false;
146 
147  // Vector of 0MQ known addresses for a given topic and pUuid.
148  auto &v = m.at(_pUuid);
149  auto found = std::find_if(v.begin(), v.end(),
150  [&](const T &_pub)
151  {
152  return _pub.NUuid() == _nUuid;
153  });
154  // Address found!
155  if (found != v.end())
156  {
157  _publisher = *found;
158  return true;
159  }
160 
161  // nUuid not found.
162  return false;
163  }
164 
169  public: bool Publishers(const std::string &_topic,
170  std::map<std::string, std::vector<T>> &_info) const
171  {
172  if (!this->HasTopic(_topic))
173  return false;
174 
175  _info = this->data.at(_topic);
176  return true;
177  }
178 
184  public: bool DelPublisherByNode(const std::string &_topic,
185  const std::string &_pUuid,
186  const std::string &_nUuid)
187  {
188  size_t counter = 0;
189 
190  // Iterate over all the topics.
191  if (this->data.find(_topic) != this->data.end())
192  {
193  // m is {pUUID=>Publisher}.
194  auto &m = this->data[_topic];
195 
196  // The pUuid exists.
197  if (m.find(_pUuid) != m.end())
198  {
199  // Vector of 0MQ known addresses for a given topic and pUuid.
200  auto &v = m[_pUuid];
201  auto priorSize = v.size();
202  v.erase(std::remove_if(v.begin(), v.end(),
203  [&](const T &_pub)
204  {
205  return _pub.NUuid() == _nUuid;
206  }),
207  v.end());
208  counter = priorSize - v.size();
209 
210  if (v.empty())
211  m.erase(_pUuid);
212 
213  if (m.empty())
214  this->data.erase(_topic);
215  }
216  }
217 
218  return counter > 0;
219  }
220 
224  public: bool DelPublishersByProc(const std::string &_pUuid)
225  {
226  size_t counter = 0;
227 
228  // Iterate over all the topics.
229  for (auto it = this->data.begin(); it != this->data.end();)
230  {
231  // m is {pUUID=>Publisher}.
232  auto &m = it->second;
233  counter += m.erase(_pUuid);
234  if (m.empty())
235  this->data.erase(it++);
236  else
237  ++it;
238  }
239 
240  return counter > 0;
241  }
242 
248  public: void PublishersByProc(const std::string &_pUuid,
249  std::map<std::string, std::vector<T>> &_pubs) const
250  {
251  _pubs.clear();
252 
253  // Iterate over all the topics.
254  for (auto &topic : this->data)
255  {
256  // m is {pUUID=>Publisher}.
257  auto &m = topic.second;
258  if (m.find(_pUuid) != m.end())
259  {
260  auto &v = m.at(_pUuid);
261  for (auto &pub : v)
262  {
263  _pubs[topic.first].push_back(T(pub));
264  }
265  }
266  }
267  }
268 
271  public: void TopicList(std::vector<std::string> &_topics) const
272  {
273  for (auto &topic : this->data)
274  _topics.push_back(topic.first);
275  }
276 
278  public: void Print() const
279  {
280  std::cout << "---" << std::endl;
281  for (auto &topic : this->data)
282  {
283  std::cout << "[" << topic.first << "]" << std::endl;
284  auto &m = topic.second;
285  for (auto &proc : m)
286  {
287  std::cout << "\tProc. UUID: " << proc.first << std::endl;
288  auto &v = proc.second;
289  for (auto &publisher : v)
290  {
291  std::cout << publisher;
292  }
293  }
294  }
295  }
296 
299  private: std::map<std::string,
300  std::map<std::string, std::vector<T>>> data;
301  };
302  }
303 }
304 
305 #endif
bool AddPublisher(const T &_publisher)
Add a new address associated to a given topic and node UUID.
Definition: TopicStorage.hh:49
bool HasTopic(const std::string &_topic) const
Return if there is any publisher stored for the given topic.
Definition: TopicStorage.hh:85
#define IGNITION_VISIBLE
Use to represent "symbol visible" if supported.
Definition: Helpers.hh:56
void Print() const
Print all the information for debugging purposes.
Definition: TopicStorage.hh:278
bool HasAnyPublishers(const std::string &_topic, const std::string &_pUuid) const
Return if there is any publisher stored for the given topic and process UUID.
Definition: TopicStorage.hh:96
bool Publisher(const std::string &_topic, const std::string &_pUuid, const std::string &_nUuid, T &_publisher) const
Get the address information for a given topic and node UUID.
Definition: TopicStorage.hh:131
bool DelPublishersByProc(const std::string &_pUuid)
Remove all the publishers associated to a given process.
Definition: TopicStorage.hh:224
void PublishersByProc(const std::string &_pUuid, std::map< std::string, std::vector< T >> &_pubs) const
Given a process UUID, the function returns the list of publishers contained in this process UUID with...
Definition: TopicStorage.hh:248
void TopicList(std::vector< std::string > &_topics) const
Get the list of topics currently stored.
Definition: TopicStorage.hh:271
bool HasPublisher(const std::string &_addr) const
Return if the requested publisher&#39;s address is stored.
Definition: TopicStorage.hh:109
bool DelPublisherByNode(const std::string &_topic, const std::string &_pUuid, const std::string &_nUuid)
Remove a publisher associated to a given topic and UUID pair.
Definition: TopicStorage.hh:184
Definition: AdvertiseOptions.hh:25
Store address information about topics and provide convenient methods for adding new topics...
Definition: TopicStorage.hh:37
bool Publishers(const std::string &_topic, std::map< std::string, std::vector< T >> &_info) const
Get the map of publishers stored for a given topic.
Definition: TopicStorage.hh:169