/src/Fast-DDS/src/cpp/rtps/attributes/ServerAttributes.hpp
Line | Count | Source |
1 | | // Copyright 2024 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 | | |
15 | | /** |
16 | | * @file ServerAttributes.hpp |
17 | | * |
18 | | */ |
19 | | |
20 | | #ifndef FASTDDS_RTPS_ATTRIBUTES__SERVERATTRIBUTES_HPP |
21 | | #define FASTDDS_RTPS_ATTRIBUTES__SERVERATTRIBUTES_HPP |
22 | | #ifndef DOXYGEN_SHOULD_SKIP_THIS_PUBLIC |
23 | | |
24 | | #include <fastdds/rtps/common/Guid.hpp> |
25 | | #include <fastdds/rtps/common/LocatorList.hpp> |
26 | | |
27 | | #include <algorithm> |
28 | | #include <iterator> |
29 | | #include <list> |
30 | | |
31 | | namespace eprosima { |
32 | | namespace fastdds { |
33 | | namespace rtps { |
34 | | |
35 | | class ParticipantProxyData; |
36 | | |
37 | | /** |
38 | | * Class RemoteServerAttributes, to define the attributes of the Discovery Server Protocol. |
39 | | * @ingroup RTPS_ATTRIBUTES_MODULE |
40 | | */ |
41 | | |
42 | | class RemoteServerAttributes |
43 | | { |
44 | | public: |
45 | | |
46 | | inline bool operator ==( |
47 | | const RemoteServerAttributes& r) const |
48 | 0 | { |
49 | 0 | return guidPrefix == r.guidPrefix |
50 | 0 | && metatrafficUnicastLocatorList == r.metatrafficUnicastLocatorList |
51 | 0 | && metatrafficMulticastLocatorList == r.metatrafficMulticastLocatorList; |
52 | 0 | } |
53 | | |
54 | | void clear() |
55 | 0 | { |
56 | 0 | guidPrefix = fastdds::rtps::GuidPrefix_t::unknown(); |
57 | 0 | metatrafficUnicastLocatorList.clear(); |
58 | 0 | metatrafficMulticastLocatorList.clear(); |
59 | 0 | } |
60 | | |
61 | | fastdds::rtps::GUID_t GetParticipant() const; |
62 | | |
63 | | fastdds::rtps::GUID_t GetPDPReader() const; |
64 | | fastdds::rtps::GUID_t GetPDPWriter() const; |
65 | | |
66 | | inline bool ReadguidPrefix( |
67 | | const char* pfx) |
68 | 0 | { |
69 | 0 | return bool(std::istringstream(pfx) >> guidPrefix); |
70 | 0 | } |
71 | | |
72 | | //!Metatraffic Unicast Locator List |
73 | | LocatorList metatrafficUnicastLocatorList; |
74 | | //!Metatraffic Multicast Locator List. |
75 | | LocatorList metatrafficMulticastLocatorList; |
76 | | |
77 | | //!Guid prefix |
78 | | fastdds::rtps::GuidPrefix_t guidPrefix; |
79 | | |
80 | | // Check if there are specific transport locators associated |
81 | | // the template parameter is the locator kind (e.g. LOCATOR_KIND_UDPv4) |
82 | | template<int kind> bool requires_transport() const |
83 | | { |
84 | | return metatrafficUnicastLocatorList.has_kind<kind>() || |
85 | | metatrafficMulticastLocatorList.has_kind<kind>(); |
86 | | } |
87 | | |
88 | | }; |
89 | | |
90 | | typedef std::list<RemoteServerAttributes> RemoteServerList_t; |
91 | | |
92 | | template<class charT> |
93 | | struct server_ostream_separators |
94 | | { |
95 | | static const charT* list_separator; |
96 | | static const charT* locator_separator; |
97 | | }; |
98 | | |
99 | | #ifndef _MSC_VER |
100 | | template<> const char* server_ostream_separators<char>::list_separator; |
101 | | template<> const wchar_t* server_ostream_separators<wchar_t>::list_separator; |
102 | | |
103 | | template<> const char* server_ostream_separators<char>::locator_separator; |
104 | | template<> const wchar_t* server_ostream_separators<wchar_t>::locator_separator; |
105 | | #endif // _MSC_VER |
106 | | |
107 | | template<class charT> |
108 | | std::basic_ostream<charT>& operator <<( |
109 | | std::basic_ostream<charT>& output, |
110 | | const RemoteServerAttributes& sa) |
111 | | { |
112 | | typename std::basic_ostream<charT>::sentry s(output); |
113 | | output << sa.guidPrefix; |
114 | | if (!sa.metatrafficUnicastLocatorList.empty()) |
115 | | { |
116 | | output << server_ostream_separators<charT>::locator_separator << sa.metatrafficUnicastLocatorList; |
117 | | } |
118 | | if (!sa.metatrafficMulticastLocatorList.empty()) |
119 | | { |
120 | | output << server_ostream_separators<charT>::locator_separator << sa.metatrafficMulticastLocatorList; |
121 | | } |
122 | | return output; |
123 | | } |
124 | | |
125 | | template<class charT> |
126 | | std::basic_ostream<charT>& operator <<( |
127 | | std::basic_ostream<charT>& output, |
128 | | const RemoteServerList_t& list) |
129 | | { |
130 | | typename std::basic_ostream<charT>::sentry s(output); |
131 | | std::ostream_iterator<RemoteServerAttributes> os_iterator(output, server_ostream_separators<charT>::list_separator); |
132 | | std::copy(list.begin(), list.end(), os_iterator); |
133 | | return output; |
134 | | } |
135 | | |
136 | | // Default server base guidPrefix |
137 | | const char* const DEFAULT_ROS2_SERVER_GUIDPREFIX = "44.53.00.5f.45.50.52.4f.53.49.4d.41"; |
138 | | |
139 | | /* Environment variable that can either serve to: |
140 | | * - Specify the Discovery Server auto mode by setting its value to AUTO. |
141 | | * - Specify a semicolon-separated list of locators ([transport]ip:port) that define remote server |
142 | | * locators. The [transport] specification is optional. The default transport is UDPv4. |
143 | | * For the variable to take any effect, the following pre-condition must be met: |
144 | | * - The discovery protocol must be either SIMPLE or SERVER. |
145 | | * a. In the case of SIMPLE, the participant is created as a CLIENT instead. |
146 | | * b. In the case of SERVER, the participant is created as a SERVER, using the DEFAULT_ROS2_MASTER_URI list to |
147 | | * expand the list of remote servers. |
148 | | */ |
149 | | const char* const DEFAULT_ROS2_MASTER_URI = "ROS_DISCOVERY_SERVER"; |
150 | | |
151 | | /* Environment variable that: |
152 | | * - Will spawn a background Discovery Server in the current domain (if there were not). |
153 | | * - Specify an external ip address to connect the background Discovery Server (the port is deduced from the domain). |
154 | | * - Set the transports to TCP and SHM. |
155 | | * - Make the participant a SUPER_CLIENT. |
156 | | */ |
157 | | const char* const ROS2_EASY_MODE_URI = "ROS2_EASY_MODE"; |
158 | | |
159 | | /* Environment variable to transform a SIMPLE participant in a SUPER CLIENT. |
160 | | * If the participant is not SIMPLE, the variable doesn't have any effects. |
161 | | * The variable can assume the following values: |
162 | | * - FALSE, false, False, 0 |
163 | | * - TRUE, true, True, 1 |
164 | | * If the variable is not set, the program will behave like the variable is set to false. |
165 | | */ |
166 | | const char* const ROS_SUPER_CLIENT = "ROS_SUPER_CLIENT"; |
167 | | |
168 | | /** |
169 | | * Retrieves a semicolon-separated list of locators from a string, and |
170 | | * populates a LocatorList_t. |
171 | | * @param [in] list servers listening locator list. |
172 | | * @param [out] servers_list reference to a LocatorList_t to populate. |
173 | | * @return true if parsing succeeds, false otherwise (or if the list is empty) |
174 | | */ |
175 | | bool load_environment_server_info( |
176 | | const std::string& list, |
177 | | LocatorList& servers_list); |
178 | | |
179 | | /** |
180 | | * Retrieves a semicolon-separated list of locators from DEFAULT_ROS2_MASTER_URI environment variable, and |
181 | | * populates a LocatorList_t. |
182 | | * |
183 | | * The environment variable can be read from an environment file (which allows runtime modification of the remote |
184 | | * servers list) or directly from the environment. |
185 | | * The value contained in the file takes precedence over the environment value (if both are set). |
186 | | * |
187 | | * @param [out] servers_list reference to a LocatorList_t to populate. |
188 | | * @return true if parsing succeeds, false otherwise |
189 | | */ |
190 | | bool load_environment_server_info( |
191 | | LocatorList& servers_list); |
192 | | |
193 | | /** |
194 | | * Get the value of environment variable DEFAULT_ROS2_MASTER_URI |
195 | | * @return The value of environment variable DEFAULT_ROS2_MASTER_URI. Empty string if the variable is not defined. |
196 | | */ |
197 | | const std::string& ros_discovery_server_env(); |
198 | | |
199 | | /** |
200 | | * Get the value of environment variable ROS2_EASY_MODE_URI |
201 | | * @return The value of environment variable ROS2_EASY_MODE_URI. |
202 | | * Empty string if the variable is not defined or does not have a valid IPv4 format. |
203 | | */ |
204 | | const std::string& ros_easy_mode_env(); |
205 | | |
206 | | /** |
207 | | * Get the value of environment variable ROS_SUPER_CLIENT |
208 | | * @return The value of environment variable ROS_SUPER_CLIENT. False if the variable is not defined. |
209 | | */ |
210 | | bool ros_super_client_env(); |
211 | | |
212 | | } // rtps |
213 | | } // fastdds |
214 | | } // namespace eprosima |
215 | | |
216 | | #endif // DOXYGEN_SHOULD_SKIP_THIS_PUBLIC |
217 | | #endif // FASTDDS_RTPS_ATTRIBUTES__SERVERATTRIBUTES_HPP |