Coverage Report

Created: 2025-06-13 06:46

/src/Fast-DDS/include/fastdds/rtps/common/RemoteLocators.hpp
Line
Count
Source (jump to first uncovered line)
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
15
/**
16
 * @file RemoteLocators.hpp
17
 */
18
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
30
/**
31
 * Holds information about the locators of a remote entity.
32
 */
33
struct RemoteLocatorList
34
{
35
    /**
36
     * Default constructor of RemoteLocatorList for deserialize.
37
     */
38
    RemoteLocatorList()
39
0
    {
40
0
    }
41
42
    /**
43
     * Construct a RemoteLocatorList.
44
     *
45
     * @param max_unicast_locators Maximum number of unicast locators to hold.
46
     * @param max_multicast_locators Maximum number of multicast locators to hold.
47
     */
48
    RemoteLocatorList(
49
            size_t max_unicast_locators,
50
            size_t max_multicast_locators)
51
0
        : unicast(ResourceLimitedContainerConfig::fixed_size_configuration(max_unicast_locators))
52
0
        , multicast(ResourceLimitedContainerConfig::fixed_size_configuration(max_multicast_locators))
53
0
    {
54
0
    }
55
56
    /**
57
     * Copy-construct a RemoteLocatorList.
58
     *
59
     * @param other RemoteLocatorList to copy data from.
60
     */
61
    RemoteLocatorList(
62
            const RemoteLocatorList& other)
63
0
        : unicast(other.unicast)
64
0
        , multicast(other.multicast)
65
0
    {
66
0
    }
67
68
    /**
69
     * Assign locator values from other RemoteLocatorList.
70
     *
71
     * @param other RemoteLocatorList to copy data from.
72
     *
73
     * @remarks Using the assignment operator is different from copy-constructing as in the first case the
74
     * configuration with the maximum number of locators is not copied. This means that, for two lists with
75
     * different maximum number of locators, the expression `(a = b) == b` may not be true.
76
     */
77
    RemoteLocatorList& operator = (
78
            const RemoteLocatorList& other)
79
0
    {
80
0
        unicast = other.unicast;
81
0
        multicast = other.multicast;
82
0
        return *this;
83
0
    }
84
85
    /**
86
     * Adds a locator to the unicast list.
87
     *
88
     * If the locator already exists in the unicast list, or the maximum number of unicast locators has been reached,
89
     * the new locator is silently discarded.
90
     *
91
     * @param locator Unicast locator to be added.
92
     */
93
    void add_unicast_locator(
94
            const Locator_t& locator)
95
0
    {
96
0
        for (const Locator_t& loc : unicast)
97
0
        {
98
0
            if (loc == locator)
99
0
            {
100
0
                return;
101
0
            }
102
0
        }
103
104
0
        unicast.push_back(locator);
105
0
    }
106
107
    /**
108
     * Adds a locator to the multicast list.
109
     *
110
     * If the locator already exists in the multicast list, or the maximum number of multicast locators has been reached,
111
     * the new locator is silently discarded.
112
     *
113
     * @param locator Multicast locator to be added.
114
     */
115
    void add_multicast_locator(
116
            const Locator_t& locator)
117
0
    {
118
0
        for (const Locator_t& loc : multicast)
119
0
        {
120
0
            if (loc == locator)
121
0
            {
122
0
                return;
123
0
            }
124
0
        }
125
126
0
        multicast.push_back(locator);
127
0
    }
128
129
    //! List of unicast locators
130
    ResourceLimitedVector<Locator_t> unicast;
131
    //! List of multicast locators
132
    ResourceLimitedVector<Locator_t> multicast;
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
0
{
142
    // Stored multicast locators
143
0
    output << "{";
144
0
    if (!remote_locators.multicast.empty())
145
0
    {
146
0
        output << "MULTICAST:[";
147
0
        output << remote_locators.multicast[0];
148
0
        for (auto it = remote_locators.multicast.begin() + 1; it != remote_locators.multicast.end(); ++it)
149
0
        {
150
0
            output << "," << *it;
151
0
        }
152
0
        output << "]";
153
0
    }
154
155
    // Stored unicast locators
156
0
    if (!remote_locators.unicast.empty())
157
0
    {
158
0
        output << "UNICAST:[";
159
0
        output << remote_locators.unicast[0];
160
0
        for (auto it = remote_locators.unicast.begin() + 1; it != remote_locators.unicast.end(); ++it)
161
0
        {
162
0
            output << "," << *it;
163
0
        }
164
0
        output << "]";
165
0
    }
166
0
    output << "}";
167
0
    return output;
168
0
}
169
170
inline std::istream& operator >>(
171
        std::istream& input,
172
        RemoteLocatorList& locList)
173
0
{
174
0
    std::istream::sentry s(input);
175
0
    locList = RemoteLocatorList();
176
177
0
    if (s)
178
0
    {
179
0
        char punct;
180
0
        char letter;
181
0
        Locator_t l;
182
0
        std::ios_base::iostate excp_mask = input.exceptions();
183
184
0
        try
185
0
        {
186
0
            input.exceptions(excp_mask | std::ios_base::failbit | std::ios_base::badbit);
187
0
            std::stringbuf sb_aux;
188
0
            Locator_t locator;
189
190
            // Read {_
191
0
            input >> punct >> letter;
192
193
0
            if (letter == 'M')
194
0
            {
195
0
                input.get(sb_aux, '[');
196
0
                input >> punct;
197
198
                // Read every locator
199
0
                while (punct != ']')
200
0
                {
201
0
                    input >> locator;
202
0
                    locList.add_multicast_locator(locator);
203
0
                    input >> punct;
204
0
                }
205
206
0
                input >> letter;
207
0
            }
208
209
0
            if (letter == 'U')
210
0
            {
211
0
                input.get(sb_aux, '[');
212
0
                input >> punct;
213
214
                // Read every locator
215
0
                while (punct != ']')
216
0
                {
217
0
                    input >> locator;
218
0
                    locList.add_unicast_locator(locator);
219
0
                    input >> punct;
220
0
                }
221
222
0
                input >> letter;
223
0
            }
224
0
        }
225
0
        catch (std::ios_base::failure& )
226
0
        {
227
0
            locList.unicast.clear();
228
0
            locList.multicast.clear();
229
0
            EPROSIMA_LOG_WARNING(REMOTE_LOCATOR_LIST, "Error deserializing RemoteLocatorList");
230
0
        }
231
232
0
        input.exceptions(excp_mask);
233
0
    }
234
235
0
    return input;
236
0
}
237
238
} // namespace rtps
239
} // namespace fastdds
240
} // namespace eprosima
241
242
#endif // FASTDDS_RTPS_COMMON__REMOTELOCATORS_HPP