summaryrefslogtreecommitdiff
path: root/implementation/endpoints/include/endpoint_manager_base.hpp
blob: aa21269d1c9bb458bb666886fc3f8aa2e7cd88a1 (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
// Copyright (C) 2014-2017 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/.

#ifndef VSOMEIP_V3_ENDPOINT_MANAGER_BASE_HPP_
#define VSOMEIP_V3_ENDPOINT_MANAGER_BASE_HPP_

#include <mutex>
#include <map>
#include <set>
#include <unordered_set>
#include <memory>

#include <boost/asio/io_service.hpp>

#include <vsomeip/primitive_types.hpp>

#include "endpoint.hpp"
#include "endpoint_host.hpp"

namespace vsomeip_v3 {

class routing_manager_base;
class configuration;
class local_server_endpoint_impl;
class routing_host;

class endpoint_manager_base
        : public std::enable_shared_from_this<endpoint_manager_base>,
          public endpoint_host {
public:
    endpoint_manager_base(routing_manager_base* const _rm,
            boost::asio::io_service& _io,
            const std::shared_ptr<configuration>& _configuration);
    virtual ~endpoint_manager_base() = default;

    std::shared_ptr<endpoint> create_local(client_t _client);
    void remove_local(client_t _client);

    std::shared_ptr<endpoint> find_or_create_local(client_t _client);
    std::shared_ptr<endpoint> find_local(client_t _client);
    std::shared_ptr<endpoint> find_local(service_t _service, instance_t _instance);

    std::unordered_set<client_t> get_connected_clients() const;

    std::shared_ptr<local_server_endpoint_impl> create_local_server(
            const std::shared_ptr<routing_host> &_routing_host);

    // endpoint_host interface
    virtual void on_connect(std::shared_ptr<endpoint> _endpoint);
    virtual void on_disconnect(std::shared_ptr<endpoint> _endpoint);
    virtual bool on_bind_error(std::shared_ptr<endpoint> _endpoint, uint16_t _remote_port);
    virtual void on_error(const byte_t *_data, length_t _length,
                          endpoint* const _receiver,
                          const boost::asio::ip::address &_remote_address,
                          std::uint16_t _remote_port);
    virtual void release_port(uint16_t _port, bool _reliable);
    client_t get_client() const;

    // Statistics
    void log_client_states() const;

protected:
    std::map<client_t, std::shared_ptr<endpoint>> get_local_endpoints() const;

private:
    std::shared_ptr<endpoint> create_local_unlocked(client_t _client);
    std::shared_ptr<endpoint> find_local_unlocked(client_t _client);

protected:
    routing_manager_base* const rm_;
    boost::asio::io_service& io_;
    std::shared_ptr<configuration> configuration_;

private:
    mutable std::mutex local_endpoint_mutex_;
    std::map<client_t, std::shared_ptr<endpoint> > local_endpoints_;
};

} // namespace vsomeip_v3

#endif // VSOMEIP_V3_ENDPOINT_MANAGER_BASE_HPP_