summaryrefslogtreecommitdiff
path: root/implementation/routing/include/routing_manager_proxy.hpp
blob: c73fd3d2ef64c28860533177e4666dab8d98952a (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
// 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_PROXY_HPP
#define VSOMEIP_ROUTING_MANAGER_PROXY_HPP

#include <map>
#include <mutex>

#include <boost/asio/io_service.hpp>

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

namespace vsomeip {

class configuration;
class event;
class routing_manager_host;

// TODO: encapsulate common parts of classes "routing_manager_impl"
// and "routing_manager_proxy" into a base class.

class routing_manager_proxy: public routing_manager,
		public endpoint_host,
		public std::enable_shared_from_this<routing_manager_proxy> {
public:
	routing_manager_proxy(routing_manager_host *_host);
	virtual ~routing_manager_proxy();

	boost::asio::io_service & get_io();
	client_t get_client() 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 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 = true, bool _reliable = false);

	bool send_to(const std::shared_ptr<endpoint_definition> &_target,
			std::shared_ptr<message> _message);

	bool send_to(const std::shared_ptr<endpoint_definition> &_target,
			const byte_t *_data, uint32_t _size);

	void notify(service_t _service, instance_t _instance, event_t _event,
				std::shared_ptr<payload> _payload) const;

	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);

	void on_routing_info(const byte_t *_data, uint32_t _size);

	bool is_available(service_t _service, instance_t _instance) const;

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

private:
	void register_application();
	void deregister_application();

	std::shared_ptr<endpoint> create_local(client_t _client);
	std::shared_ptr<event> find_event(service_t _service, instance_t _instance,
			event_t _event) const;

	void send_pong() const;

private:
	boost::asio::io_service &io_;
	bool is_connected_;
	bool is_started_;
	event_type_e state_;
	routing_manager_host *host_;
	client_t client_; // store locally as it is needed in each message

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

	std::shared_ptr<endpoint> sender_;	// --> stub
	std::shared_ptr<endpoint> receiver_;  // --> from everybody

	std::map<client_t, std::shared_ptr<endpoint> > local_endpoints_;
	std::map<service_t, std::map<instance_t, client_t> > local_services_;

	std::map<service_t,
			std::map<instance_t, std::map<event_t, std::shared_ptr<event> > > > events_;

	std::mutex send_mutex_;
	std::mutex serialize_mutex_;
	std::mutex deserialize_mutex_;
};

} // namespace vsomeip

#endif // VSOMEIP_ROUTING_MANAGER_PROXY_HPP