1 // Copyright (C) 2014-2017 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)
2 // This Source Code Form is subject to the terms of the Mozilla Public
3 // License, v. 2.0. If a copy of the MPL was not distributed with this
4 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 
6 #ifndef VSOMEIP_V3_ENDPOINT_MANAGER_BASE_HPP_
7 #define VSOMEIP_V3_ENDPOINT_MANAGER_BASE_HPP_
8 
9 #include <mutex>
10 #include <map>
11 #include <set>
12 #include <unordered_set>
13 #include <memory>
14 
15 #include <boost/asio/io_service.hpp>
16 
17 #include <vsomeip/primitive_types.hpp>
18 
19 #include "endpoint.hpp"
20 #include "endpoint_host.hpp"
21 
22 namespace vsomeip_v3 {
23 
24 class routing_manager_base;
25 class configuration;
26 class local_server_endpoint_impl;
27 class routing_host;
28 
29 class endpoint_manager_base
30         : public std::enable_shared_from_this<endpoint_manager_base>,
31           public endpoint_host {
32 public:
33     endpoint_manager_base(routing_manager_base* const _rm,
34             boost::asio::io_service& _io,
35             const std::shared_ptr<configuration>& _configuration);
36     virtual ~endpoint_manager_base() = default;
37 
38     std::shared_ptr<endpoint> create_local(client_t _client);
39     void remove_local(client_t _client);
40 
41     std::shared_ptr<endpoint> find_or_create_local(client_t _client);
42     std::shared_ptr<endpoint> find_local(client_t _client);
43     std::shared_ptr<endpoint> find_local(service_t _service, instance_t _instance);
44 
45     std::unordered_set<client_t> get_connected_clients() const;
46 
47     std::shared_ptr<local_server_endpoint_impl> create_local_server(
48             const std::shared_ptr<routing_host> &_routing_host);
49 
50     // endpoint_host interface
51     virtual void on_connect(std::shared_ptr<endpoint> _endpoint);
52     virtual void on_disconnect(std::shared_ptr<endpoint> _endpoint);
53     virtual bool on_bind_error(std::shared_ptr<endpoint> _endpoint, uint16_t _remote_port);
54     virtual void on_error(const byte_t *_data, length_t _length,
55                           endpoint* const _receiver,
56                           const boost::asio::ip::address &_remote_address,
57                           std::uint16_t _remote_port);
58     virtual void release_port(uint16_t _port, bool _reliable);
59     client_t get_client() const;
60 
61     // Statistics
62     void log_client_states() const;
63 
64 protected:
65     std::map<client_t, std::shared_ptr<endpoint>> get_local_endpoints() const;
66 
67 private:
68     std::shared_ptr<endpoint> create_local_unlocked(client_t _client);
69     std::shared_ptr<endpoint> find_local_unlocked(client_t _client);
70 
71 protected:
72     routing_manager_base* const rm_;
73     boost::asio::io_service& io_;
74     std::shared_ptr<configuration> configuration_;
75 
76 private:
77     mutable std::mutex local_endpoint_mutex_;
78     std::map<client_t, std::shared_ptr<endpoint> > local_endpoints_;
79 };
80 
81 } // namespace vsomeip_v3
82 
83 #endif // VSOMEIP_V3_ENDPOINT_MANAGER_BASE_HPP_
84