summaryrefslogtreecommitdiff
path: root/implementation/endpoints/src/endpoint_manager_base.cpp
blob: b8e18af901105f7453431a19379464faa12c9527 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
// Copyright (C) 2014-2021 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

#include "../include/endpoint_manager_base.hpp"

#include <vsomeip/internal/logger.hpp>
#include "../../utility/include/utility.hpp"
#include "../../routing/include/routing_manager_base.hpp"
#include "../../configuration/include/configuration.hpp"
#include "../include/local_tcp_client_endpoint_impl.hpp"
#include "../include/local_tcp_server_endpoint_impl.hpp"

#if defined(__linux__) || defined(ANDROID)
#include "../include/local_uds_client_endpoint_impl.hpp"
#include "../include/local_uds_server_endpoint_impl.hpp"
#endif

#include <iomanip>

namespace vsomeip_v3 {

endpoint_manager_base::endpoint_manager_base(
        routing_manager_base* const _rm,
        boost::asio::io_context &_io,
        const std::shared_ptr<configuration>& _configuration)
    : rm_(_rm),
      io_(_io),
      configuration_(_configuration),
      local_port_(ILLEGAL_PORT) {

    is_local_routing_ = configuration_->is_local_routing();
}

std::shared_ptr<endpoint> endpoint_manager_base::create_local(client_t _client) {
    std::lock_guard<std::mutex> its_lock(local_endpoint_mutex_);
    return create_local_unlocked(_client);
}

void endpoint_manager_base::remove_local(client_t _client) {
    std::shared_ptr<endpoint> its_endpoint(find_local(_client));
    if (its_endpoint) {
        its_endpoint->register_error_handler(nullptr);
        its_endpoint->stop();
        VSOMEIP_INFO << "Client [" << std::hex << rm_->get_client() << "] is closing connection to ["
                      << std::hex << _client << "]";
        std::lock_guard<std::mutex> its_lock(local_endpoint_mutex_);
        local_endpoints_.erase(_client);
    }
}

std::shared_ptr<endpoint> endpoint_manager_base::find_or_create_local(client_t _client) {
    std::lock_guard<std::mutex> its_lock(local_endpoint_mutex_);
    std::shared_ptr<endpoint> its_endpoint(find_local_unlocked(_client));
    if (!its_endpoint) {
        its_endpoint = create_local_unlocked(_client);
        its_endpoint->start();
    }
    return (its_endpoint);
}

std::shared_ptr<endpoint> endpoint_manager_base::find_local(client_t _client) {
    std::lock_guard<std::mutex> its_lock(local_endpoint_mutex_);
    return find_local_unlocked(_client);
}

std::shared_ptr<endpoint> endpoint_manager_base::find_local(service_t _service,
        instance_t _instance) {
    return find_local(rm_->find_local_client(_service, _instance));
}


std::unordered_set<client_t> endpoint_manager_base::get_connected_clients() const {
    std::lock_guard<std::mutex> its_lock(local_endpoint_mutex_);
    std::unordered_set<client_t> clients;
    for (const auto& its_client : local_endpoints_) {
        clients.insert(its_client.first);
    }
    return clients;
}

std::shared_ptr<endpoint> endpoint_manager_base::create_local_server(
        const std::shared_ptr<routing_host> &_routing_host) {
    std::shared_ptr<endpoint> its_server_endpoint;
    std::stringstream its_path;
    its_path << utility::get_base_path(configuration_->get_network())
             << std::hex << rm_->get_client();
    const client_t its_client = rm_->get_client();

#if defined(__linux__) || defined(ANDROID)
    if (is_local_routing_) {
        if (-1 == ::unlink(its_path.str().c_str()) && errno != ENOENT) {
            VSOMEIP_ERROR << "endpoint_manager_base::init_receiver unlink failed ("
                    << its_path.str() << "): "<< std::strerror(errno);
        }
        try {
            its_server_endpoint = std::make_shared<local_uds_server_endpoint_impl>(
                    shared_from_this(), _routing_host,
#    if VSOMEIP_BOOST_VERSION < 106600
                    boost::asio::local::stream_protocol_ext::endpoint(its_path.str()),
#    else
                    boost::asio::local::stream_protocol::endpoint(its_path.str()),
#    endif
                    io_,
                    configuration_, false);

            VSOMEIP_INFO << __func__ << ": Listening @ " << its_path.str();

        } catch (const std::exception &e) {
            VSOMEIP_ERROR << "Local UDS server endpoint creation failed. Client "
                    << std::hex << std::setw(4) << std::setfill('0') << its_client
                    << " Path: " << its_path.str()
                    << " Reason: " << e.what();
        }
    } else {
#else
    {
#endif
        std::lock_guard<std::mutex> its_lock(create_local_server_endpoint_mutex_);
        ::unlink(its_path.str().c_str());
        port_t its_port;
        std::set<port_t> its_used_ports;
        auto its_address = configuration_->get_routing_guest_address();
        while (get_local_server_port(its_port, its_used_ports) && !its_server_endpoint) {
            try {
                its_server_endpoint = std::make_shared<local_tcp_server_endpoint_impl>(
                        shared_from_this(), _routing_host,
                        boost::asio::ip::tcp::endpoint(its_address, its_port),
                        io_,
                        configuration_, false);

                VSOMEIP_INFO << __func__ << ": Listening @ "
                        << its_address.to_string() << ":" << std::dec << its_port;

                if (rm_->is_routing_manager())
                    local_port_ = port_t(configuration_->get_routing_host_port() + 1);
                else
                    local_port_ = port_t(its_port + 1);
                VSOMEIP_INFO << __func__ << ": Connecting to other clients from "
                        << its_address.to_string() << ":" << std::dec << local_port_;

            } catch (const std::exception&) {
                its_used_ports.insert(its_port);
            }
        }

        if (!its_server_endpoint) {
            VSOMEIP_ERROR << "Local TCP server endpoint creation failed. Client "
                    << std::hex << std::setw(4) << std::setfill('0') << its_client
                    << " Reason: No local port available!";
        } else {
            rm_->add_guest(its_client, its_address, its_port);
        }
    }

    return (its_server_endpoint);
}

void endpoint_manager_base::on_connect(std::shared_ptr<endpoint> _endpoint) {
    rm_->on_connect(_endpoint);
}

void endpoint_manager_base::on_disconnect(std::shared_ptr<endpoint> _endpoint) {
    rm_->on_disconnect(_endpoint);
}

bool endpoint_manager_base::on_bind_error(std::shared_ptr<endpoint> _endpoint,
        const boost::asio::ip::address &_remote_address,
        uint16_t _remote_port) {

    (void)_endpoint;
    (void)_remote_address;
    (void)_remote_port;

    return true;
}

void endpoint_manager_base::on_error(
        const byte_t *_data, length_t _length, endpoint* const _receiver,
        const boost::asio::ip::address &_remote_address,
        std::uint16_t _remote_port) {

    (void)_data;
    (void)_length;
    (void)_receiver;
    (void)_remote_address;
    (void)_remote_port;
}

void endpoint_manager_base::release_port(uint16_t _port, bool _reliable) {
    (void)_port;
    (void)_reliable;
    // intentionally left blank
}

client_t endpoint_manager_base::get_client() const {
    return rm_->get_client();
}

std::string endpoint_manager_base::get_client_host() const {
    return rm_->get_client_host();
}

std::map<client_t, std::shared_ptr<endpoint>>
endpoint_manager_base::get_local_endpoints() const {
    std::lock_guard<std::mutex> its_lock(local_endpoint_mutex_);
    return local_endpoints_;
}

void
endpoint_manager_base::log_client_states() const {
    std::vector<std::pair<client_t, size_t> > its_client_queue_sizes;
    std::stringstream its_log;

    {
        std::lock_guard<std::mutex> its_lock(local_endpoint_mutex_);
        for (const auto &e : local_endpoints_) {
            size_t its_queue_size = e.second->get_queue_size();
            if (its_queue_size > VSOMEIP_DEFAULT_QUEUE_WARN_SIZE) {
                its_client_queue_sizes.push_back(
                        std::make_pair(e.first, its_queue_size));
            }
        }
    }

    std::sort(its_client_queue_sizes.begin(), its_client_queue_sizes.end(),
            [](const std::pair<client_t, size_t> &_a,
               const std::pair<client_t, size_t> &_b) {
        return (_a.second > _b.second);
    });

    size_t its_max(std::min(size_t(10), its_client_queue_sizes.size()));
    for (size_t i = 0; i < its_max; i++) {
        its_log << std::hex << std::setw(4) << std::setfill('0')
                << its_client_queue_sizes[i].first << ":"
                << std::dec << its_client_queue_sizes[i].second;
        if (i < its_max-1)
            its_log << ", ";
    }

    if (its_log.str().length() > 0)
        VSOMEIP_WARNING << "ICQ: [" << its_log.str() << "]";
}

std::shared_ptr<endpoint>
endpoint_manager_base::create_local_unlocked(client_t _client) {

    std::stringstream its_path;
    its_path << utility::get_base_path(configuration_->get_network())
             << std::hex << _client;
    std::shared_ptr<endpoint> its_endpoint;

#if defined(__linux__) || defined(ANDROID)
    if (is_local_routing_) {
        VSOMEIP_INFO << "Client [" << std::hex << rm_->get_client() << "] is connecting to ["
            << std::hex << _client << "] at " << its_path.str();
        its_endpoint = std::make_shared<local_uds_client_endpoint_impl>(
            shared_from_this(), rm_->shared_from_this(),
            boost::asio::local::stream_protocol::endpoint(its_path.str()),
            io_, configuration_);
    } else {
#else
    {
#endif
        boost::asio::ip::address its_local_address, its_remote_address;
        port_t its_remote_port;

        bool is_guest = rm_->get_guest(_client, its_remote_address, its_remote_port);
        if (is_guest) {
            try {
                its_local_address = configuration_->get_routing_guest_address();
                its_endpoint = std::make_shared<local_tcp_client_endpoint_impl>(
                        shared_from_this(), rm_->shared_from_this(),
                        boost::asio::ip::tcp::endpoint(its_local_address, local_port_),
                        boost::asio::ip::tcp::endpoint(its_remote_address, its_remote_port),
                        io_, configuration_);

                VSOMEIP_INFO << "Client ["
                        << std::hex << std::setw(4) << std::setfill('0') << rm_->get_client()
                        << "] @ "
                        << its_local_address.to_string() << ":" << std::dec << local_port_
                        << " is connecting to ["
                        << std::hex << std::setw(4) << std::setfill('0') << _client << "] @ "
                        << its_remote_address.to_string() << ":" << std::dec << its_remote_port;

            } catch (...) {
            }
        } else {
            VSOMEIP_ERROR << __func__
                    << ": Cannot get guest address of client ["
                    << std::hex << std::setw(4) << std::setfill('0')
                    << _client << "]";
        }
    }

    if (its_endpoint) {
        // Messages sent to the VSOMEIP_ROUTING_CLIENT are meant to be routed to
        // external devices. Therefore, its local endpoint must not be found by
        // a call to find_local. Thus it must not be inserted to the list of local
        // clients.
        if (_client != VSOMEIP_ROUTING_CLIENT) {
            local_endpoints_[_client] = its_endpoint;
        }
        rm_->register_client_error_handler(_client, its_endpoint);
    }

    return (its_endpoint);
}

std::shared_ptr<endpoint> endpoint_manager_base::find_local_unlocked(client_t _client) {
    std::shared_ptr<endpoint> its_endpoint;
    auto found_endpoint = local_endpoints_.find(_client);
    if (found_endpoint != local_endpoints_.end()) {
        its_endpoint = found_endpoint->second;
    }
    return (its_endpoint);
}

instance_t endpoint_manager_base::find_instance(
        service_t _service, endpoint* const _endpoint) const {

    (void)_service;
    (void)_endpoint;

    return (0xFFFF);
}

bool
endpoint_manager_base::get_local_server_port(port_t &_port,
        const std::set<port_t> &_used_ports) const {

#define SERVER_PORT_OFFSET 2

    auto its_port_ranges = configuration_->get_routing_guest_ports();

    for (const auto &its_range : its_port_ranges) {
        for (int r = its_range.first; r < its_range.second;
                r += SERVER_PORT_OFFSET) {

            if (_used_ports.find(port_t(r)) == _used_ports.end()
                    && r != configuration_->get_routing_host_port()) {

                _port = port_t(r);
                return (true);
            }
        }
    }

    return (false);
}

void
endpoint_manager_base::add_multicast_option(const multicast_option_t &_option) {

    (void)_option;
}

} // namespace vsomeip_v3