summaryrefslogtreecommitdiff
path: root/implementation/routing/include/routing_manager_impl.hpp
blob: 7b803b7b9c1faf2eab75bf43dbd1f61df86fe34f (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
// Copyright (C) 2014 BMW Group
// Author: Lutz Bichler (lutz.bichler@bmw.de)
// 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_ROUTING_MANAGER_IMPL_HPP
#define VSOMEIP_ROUTING_MANAGER_IMPL_HPP

#include <map>
#include <memory>
#include <mutex>
#include <vector>

#include <boost/asio/ip/address.hpp>
#include <boost/asio/io_service.hpp>

#include "routing_manager.hpp"
#include "../../endpoints/include/endpoint_host.hpp"
#include "../../service_discovery/include/service_discovery_host.hpp"

namespace vsomeip {

class client_endpoint;
class configuration;
class deserializer;
class eventgroupinfo;
class routing_manager_host;
class routing_manager_stub;
class servicegroup;
class serviceinfo;
class serializer;
class service_endpoint;

namespace sd {

class service_discovery;

} // namespace sd

class routing_manager_impl: public routing_manager,
		public endpoint_host,
		public sd::service_discovery_host,
		public std::enable_shared_from_this<routing_manager_impl> {
public:
	routing_manager_impl(routing_manager_host *_host);
	~routing_manager_impl();

	boost::asio::io_service & get_io();
	std::shared_ptr<configuration> get_configuration() const;

	void init();
	void start();
	void stop();

	void offer_service(client_t _client, service_t _service,
			instance_t _instance, major_version_t _major,
			minor_version_t _minor, ttl_t _ttl);

	void stop_offer_service(client_t _client, service_t _service,
			instance_t _instance);

	void publish_eventgroup(client_t _client, service_t _service,
			instance_t _instance, eventgroup_t _eventgroup,
			major_version_t _major, ttl_t _ttl);

	void stop_publish_eventgroup(client_t _client, service_t _service,
			instance_t _instance, eventgroup_t _eventgroup);

	std::shared_ptr<event> add_event(client_t _client, service_t _service,
			instance_t _instance, eventgroup_t _eventgroup, event_t _event);

	std::shared_ptr<event> add_field(client_t _client, service_t _service,
			instance_t _instance, eventgroup_t _eventgroup, event_t _event,
			std::shared_ptr<payload> _payload);

	void remove_event_or_field(std::shared_ptr<event> _event);

	void request_service(client_t _client, service_t _service,
			instance_t _instance, major_version_t _major,
			minor_version_t _minor, ttl_t _ttl);

	void release_service(client_t _client, service_t _service,
			instance_t _instance);

	void subscribe(client_t _client, service_t _service, instance_t _instance,
			eventgroup_t _eventgroup, major_version_t _major, ttl_t _ttl);

	void unsubscribe(client_t _client, service_t _service, instance_t _instance,
			eventgroup_t _eventgroup);

	bool send(client_t _client, std::shared_ptr<message> _message, bool _flush,
			bool _reliable);

	bool send(client_t _client, const byte_t *_data, uint32_t _size,
			instance_t _instance, bool _flush, bool _reliable);

	void set(client_t _client, service_t _service, instance_t _instance,
			event_t _event, const std::vector<byte_t> &_value);

	bool is_available(service_t _service, instance_t _instance) const;

	// interface to stub
	std::shared_ptr<endpoint> create_local(client_t _client);
	std::shared_ptr<endpoint> find_local(client_t _client);
	std::shared_ptr<endpoint> find_or_create_local(client_t _client);
	void remove_local(client_t _client);
	std::shared_ptr<endpoint> find_local(service_t _service, instance_t _instance);

	// interface "endpoint_host"
	void on_connect(std::shared_ptr<endpoint> _endpoint);
	void on_disconnect(std::shared_ptr<endpoint> _endpoint);
	void on_message(const byte_t *_data, length_t _length, endpoint *_receiver);

	// interface "service_discovery_host"
	typedef std::map<std::string,
					 std::shared_ptr<servicegroup> > servicegroups_t;
	const servicegroups_t  & get_servicegroups() const;
	service_map_t get_offered_services(const std::string &_name) const;
	void create_service_discovery_endpoint(const std::string &_address,
			uint16_t _port, const std::string &_protocol);

private:
	bool deliver_message(const byte_t *_data, length_t _length,
			instance_t _instance);

	client_t find_local_client(service_t _service, instance_t _instance);
	instance_t find_instance(service_t _service, endpoint *_endpoint);

	std::shared_ptr<serviceinfo> find_service(service_t _service,
			instance_t _instance);
	std::shared_ptr<serviceinfo> create_service(service_t _service,
			instance_t _instance, major_version_t _major,
			minor_version_t _minor, ttl_t _ttl);

	std::shared_ptr<endpoint> find_remote_client(service_t _service,
			instance_t _instance, bool _reliable);

	std::shared_ptr<endpoint> create_client_endpoint(
			const boost::asio::ip::address &_address, uint16_t _port,
			bool _reliable);
	std::shared_ptr<endpoint> find_client_endpoint(
			const boost::asio::ip::address &_address, uint16_t _port,
			bool _reliable);
	std::shared_ptr<endpoint> find_or_create_client_endpoint(
			const boost::asio::ip::address &_address, uint16_t _port,
			bool _reliable);

	std::shared_ptr<endpoint> create_server_endpoint(uint16_t _port,
			bool _reliable);
	std::shared_ptr<endpoint> find_server_endpoint(uint16_t _port,
			bool _reliable);
	std::shared_ptr<endpoint> find_or_create_server_endpoint(uint16_t _port,
			bool _reliable);

	void init_routing_info();
	void add_routing_info(service_t _service, instance_t _instance,
			major_version_t _major, minor_version_t _minor, ttl_t _ttl,
			const boost::asio::ip::address &_address, uint16_t _port,
			bool _reliable);
	void del_routing_info(service_t _service, instance_t _instance,
			bool _reliable);

private:
	boost::asio::io_service &io_;
	routing_manager_host *host_;

	std::shared_ptr<configuration> configuration_;

	std::shared_ptr<deserializer> deserializer_;
	std::shared_ptr<serializer> serializer_;

	std::shared_ptr<routing_manager_stub> stub_;
	std::shared_ptr<sd::service_discovery> discovery_;

	// Routing info

	// Local
	std::map<client_t, std::shared_ptr<endpoint> > local_clients_;
	std::map<service_t, std::map<instance_t, client_t> > local_services_;

	// Server endpoints for local services
	std::map<uint16_t, std::map<bool, std::shared_ptr<endpoint> > > server_endpoints_;
	std::map<service_t, std::map<endpoint *, instance_t> > service_instances_;

	// Client endpoints for remote services
	std::map<boost::asio::ip::address,
			std::map<uint16_t, std::map<bool, std::shared_ptr<endpoint> > > > client_endpoints_;
	std::map<service_t,
			std::map<instance_t, std::map<bool, std::shared_ptr<endpoint> > > > remote_services_;

	// Servicegroups
	std::map<std::string, std::shared_ptr<servicegroup> > servicegroups_;
	std::map<service_t, std::map<instance_t, std::shared_ptr<serviceinfo> > > services_;

	// Eventgroups
	std::map<service_t,
			std::map<instance_t,
					std::map<eventgroup_t, std::set<std::shared_ptr<endpoint> > > > > eventgroups_;
	std::map<service_t, std::map<instance_t, std::map<event_t, eventgroup_t> > > events_;
	std::map<eventgroup_t, std::set<client_t> > eventgroup_clients_;

	// Mutexes
	std::recursive_mutex endpoint_mutex_;
	std::mutex serialize_mutex_;
};

} // namespace vsomeip

#endif // VSOMEIP_ROUTING_MANAGER_IMPL_HPP