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_IMPL_HPP_
7 #define VSOMEIP_V3_ENDPOINT_MANAGER_IMPL_HPP_
8 
9 #include "../include/endpoint_manager_base.hpp"
10 
11 namespace vsomeip_v3 {
12 
13 class routing_host;
14 
15 class endpoint_manager_impl : public endpoint_manager_base {
16 public:
17     endpoint_manager_impl(routing_manager_base* const _rm,
18                           boost::asio::io_service& _io,
19                           const std::shared_ptr<configuration>& _configuration);
20     ~endpoint_manager_impl() = default;
21 
22     std::shared_ptr<endpoint> find_or_create_remote_client(service_t _service,
23                                                            instance_t _instance,
24                                                            bool _reliable);
25 
26     void find_or_create_remote_client(service_t _service, instance_t _instance);
27     void is_remote_service_known(
28             service_t _service, instance_t _instance, major_version_t _major,
29             minor_version_t _minor,
30             const boost::asio::ip::address &_reliable_address,
31             uint16_t _reliable_port, bool* _reliable_known,
32             const boost::asio::ip::address &_unreliable_address,
33             uint16_t _unreliable_port, bool* _unreliable_known) const;
34     void add_remote_service_info(
35             service_t _service, instance_t _instance,
36             const std::shared_ptr<endpoint_definition>& _ep_definition);
37     void add_remote_service_info(
38             service_t _service, instance_t _instance,
39             const std::shared_ptr<endpoint_definition>& _ep_definition_reliable,
40             const std::shared_ptr<endpoint_definition>& _ep_definition_unreliable);
41     void clear_remote_service_info(service_t _service, instance_t _instance,
42                                    bool _reliable);
43 
44     std::shared_ptr<endpoint> create_server_endpoint(uint16_t _port,
45                                                      bool _reliable,
46                                                      bool _start);
47 
48     std::shared_ptr<endpoint> find_server_endpoint(uint16_t _port,
49                                                    bool _reliable) const;
50 
51     std::shared_ptr<endpoint> find_or_create_server_endpoint(
52             uint16_t _port, bool _reliable, bool _start, service_t _service,
53             instance_t _instance, bool &_is_found, bool _is_multicast = false);
54     bool remove_server_endpoint(uint16_t _port, bool _reliable);
55 
56 
57     void clear_client_endpoints(service_t _service, instance_t _instance,
58                                 bool _reliable);
59     void find_or_create_multicast_endpoint(
60             service_t _service, instance_t _instance,
61             const boost::asio::ip::address &_sender,
62             const boost::asio::ip::address &_address, uint16_t _port);
63     void clear_multicast_endpoints(service_t _service, instance_t _instance);
64 
65     bool supports_selective(service_t _service, instance_t _instance) const;
66 
67     void print_status() const;
68 
69     std::shared_ptr<local_server_endpoint_impl> create_local_server(
70             bool* _is_socket_activated,
71             const std::shared_ptr<routing_host>& _routing_host);
72 
73     instance_t find_instance(service_t _service,
74                              endpoint* const _endpoint) const;
75     instance_t find_instance_multicast(service_t _service,
76             const boost::asio::ip::address &_sender) const;
77 
78     bool remove_instance(service_t _service, endpoint* const _endpoint);
79     bool remove_instance_multicast(service_t _service, instance_t _instance);
80 
81 
82     // endpoint_host interface
83     void on_connect(std::shared_ptr<endpoint> _endpoint);
84     void on_disconnect(std::shared_ptr<endpoint> _endpoint);
85     bool on_bind_error(std::shared_ptr<endpoint> _endpoint,
86             std::uint16_t _remote_port);
87     void on_error(const byte_t *_data, length_t _length,
88                           endpoint* const _receiver,
89                           const boost::asio::ip::address &_remote_address,
90                           std::uint16_t _remote_port);
91     void release_port(uint16_t _port, bool _reliable);
92 
93     // Statistics
94     void log_client_states() const;
95     void log_server_states() const;
96 
97 private:
98     std::shared_ptr<endpoint> find_remote_client(service_t _service,
99                                                  instance_t _instance,
100                                                  bool _reliable);
101     std::shared_ptr<endpoint> create_remote_client(service_t _service,
102                                                    instance_t _instance,
103                                                    bool _reliable);
104     std::shared_ptr<endpoint> create_client_endpoint(
105             const boost::asio::ip::address &_address, uint16_t _local_port,
106             uint16_t _remote_port, bool _reliable);
107 
108 private:
109     mutable std::recursive_mutex endpoint_mutex_;
110     // Client endpoints for remote services
111     std::map<service_t, std::map<instance_t,
112             std::map<bool, std::shared_ptr<endpoint_definition>>>> remote_service_info_;
113 
114     typedef std::map<service_t, std::map<instance_t,
115                 std::map<bool, std::shared_ptr<endpoint>>>> remote_services_t;
116     remote_services_t remote_services_;
117 
118     typedef std::map<boost::asio::ip::address,
119         std::map<uint16_t,
120             std::map<bool,
121                   std::map<partition_id_t,
122                     std::shared_ptr<endpoint>
123                 >
124             >
125         >
126     > client_endpoints_by_ip_t;
127     client_endpoints_by_ip_t client_endpoints_by_ip_;
128 
129     std::map<service_t, std::map<endpoint *, instance_t> > service_instances_;
130     std::map<service_t, std::map<boost::asio::ip::address, instance_t> > service_instances_multicast_;
131 
132     std::map<bool, std::set<uint16_t>> used_client_ports_;
133     std::mutex used_client_ports_mutex_;
134 
135     // Server endpoints for local services
136     typedef std::map<uint16_t, std::map<bool, std::shared_ptr<endpoint>>> server_endpoints_t;
137     server_endpoints_t server_endpoints_;
138 
139     // Multicast endpoint info (notifications)
140     std::map<service_t, std::map<instance_t,
141                     std::shared_ptr<endpoint_definition>>> multicast_info;
142 };
143 
144 } // namespace vsomeip_v3
145 
146 #endif // VSOMEIP_V3_ENDPOINT_MANAGER_IMPL_HPP_
147