Fast DDS  Version 3.6.1.0
Fast DDS
RemoteLocators.hpp
1 // Copyright 2019 Proyectos y Sistemas de Mantenimiento SL (eProsima).
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
19 #ifndef FASTDDS_RTPS_COMMON__REMOTELOCATORS_HPP
20 #define FASTDDS_RTPS_COMMON__REMOTELOCATORS_HPP
21 
22 #include <fastdds/rtps/common/Locator.hpp>
23 #include <fastdds/utils/collections/ResourceLimitedVector.hpp>
24 #include <fastdds/dds/log/Log.hpp>
25 
26 namespace eprosima {
27 namespace fastdds {
28 namespace rtps {
29 
34 {
39  {
40  }
41 
49  size_t max_unicast_locators,
50  size_t max_multicast_locators)
51  : unicast(ResourceLimitedContainerConfig::fixed_size_configuration(max_unicast_locators))
52  , multicast(ResourceLimitedContainerConfig::fixed_size_configuration(max_multicast_locators))
53  {
54  }
55 
62  const RemoteLocatorList& other)
63  : unicast(other.unicast)
64  , multicast(other.multicast)
65  {
66  }
67 
78  const RemoteLocatorList& other)
79  {
80  unicast = other.unicast;
81  multicast = other.multicast;
82  return *this;
83  }
84 
94  const Locator_t& locator)
95  {
96  for (const Locator_t& loc : unicast)
97  {
98  if (loc == locator)
99  {
100  return;
101  }
102  }
103 
104  unicast.push_back(locator);
105  }
106 
116  const Locator_t& locator)
117  {
118  for (const Locator_t& loc : multicast)
119  {
120  if (loc == locator)
121  {
122  return;
123  }
124  }
125 
126  multicast.push_back(locator);
127  }
128 
133 };
134 
135 /*
136  * multicast max_size , multicast size , unicast max_size , unicast size ( locator[0] , locator[1] , ... )
137  */
138 inline std::ostream& operator <<(
139  std::ostream& output,
140  const RemoteLocatorList& remote_locators)
141 {
142  // Stored multicast locators
143  output << "{";
144  if (!remote_locators.multicast.empty())
145  {
146  output << "MULTICAST:[";
147  output << remote_locators.multicast[0];
148  for (auto it = remote_locators.multicast.begin() + 1; it != remote_locators.multicast.end(); ++it)
149  {
150  output << "," << *it;
151  }
152  output << "]";
153  }
154 
155  // Stored unicast locators
156  if (!remote_locators.unicast.empty())
157  {
158  output << "UNICAST:[";
159  output << remote_locators.unicast[0];
160  for (auto it = remote_locators.unicast.begin() + 1; it != remote_locators.unicast.end(); ++it)
161  {
162  output << "," << *it;
163  }
164  output << "]";
165  }
166  output << "}";
167  return output;
168 }
169 
170 inline std::istream& operator >>(
171  std::istream& input,
172  RemoteLocatorList& locList)
173 {
174  std::istream::sentry s(input);
175  locList = RemoteLocatorList();
176 
177  if (s)
178  {
179  char punct;
180  char letter;
181  Locator_t l;
182  std::ios_base::iostate excp_mask = input.exceptions();
183 
184  try
185  {
186  input.exceptions(excp_mask | std::ios_base::failbit | std::ios_base::badbit);
187  std::stringbuf sb_aux;
188  Locator_t locator;
189 
190  // Read {_
191  input >> punct >> letter;
192 
193  if (letter == 'M')
194  {
195  input.get(sb_aux, '[');
196  input >> punct;
197 
198  // Read every locator
199  while (punct != ']')
200  {
201  input >> locator;
202  locList.add_multicast_locator(locator);
203  input >> punct;
204  }
205 
206  input >> letter;
207  }
208 
209  if (letter == 'U')
210  {
211  input.get(sb_aux, '[');
212  input >> punct;
213 
214  // Read every locator
215  while (punct != ']')
216  {
217  input >> locator;
218  locList.add_unicast_locator(locator);
219  input >> punct;
220  }
221 
222  input >> letter;
223  }
224  }
225  catch (std::ios_base::failure& )
226  {
227  locList.unicast.clear();
228  locList.multicast.clear();
229  EPROSIMA_LOG_WARNING(REMOTE_LOCATOR_LIST, "Error deserializing RemoteLocatorList");
230  }
231 
232  input.exceptions(excp_mask);
233  }
234 
235  return input;
236 }
237 
238 } // namespace rtps
239 } // namespace fastdds
240 } // namespace eprosima
241 
242 #endif // FASTDDS_RTPS_COMMON__REMOTELOCATORS_HPP
Resource limited wrapper of std::vector.
Definition: ResourceLimitedVector.hpp:59
Class Locator_t, uniquely identifies a communication channel for a particular transport.
Definition: Locator.hpp:74
std::istream & operator>>(std::istream &input, EntityId_t &enP)
Definition: EntityId_t.hpp:289
std::ostream & operator<<(std::ostream &output, BuiltinTransports transports)
Definition: BuiltinTransports.hpp:118
Specifies the configuration of a resource limited collection.
Definition: ResourceLimitedContainerConfig.hpp:36
Holds information about the locators of a remote entity.
Definition: RemoteLocators.hpp:34
RemoteLocatorList()
Default constructor of RemoteLocatorList for deserialize.
Definition: RemoteLocators.hpp:38
RemoteLocatorList & operator=(const RemoteLocatorList &other)
Assign locator values from other RemoteLocatorList.
Definition: RemoteLocators.hpp:77
ResourceLimitedVector< Locator_t > unicast
List of unicast locators.
Definition: RemoteLocators.hpp:130
RemoteLocatorList(size_t max_unicast_locators, size_t max_multicast_locators)
Construct a RemoteLocatorList.
Definition: RemoteLocators.hpp:48
void add_multicast_locator(const Locator_t &locator)
Adds a locator to the multicast list.
Definition: RemoteLocators.hpp:115
RemoteLocatorList(const RemoteLocatorList &other)
Copy-construct a RemoteLocatorList.
Definition: RemoteLocators.hpp:61
ResourceLimitedVector< Locator_t > multicast
List of multicast locators.
Definition: RemoteLocators.hpp:132
void add_unicast_locator(const Locator_t &locator)
Adds a locator to the unicast list.
Definition: RemoteLocators.hpp:93