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_EVENT_IMPL_HPP_
7 #define VSOMEIP_V3_EVENT_IMPL_HPP_
8 
9 #include <list>
10 #include <map>
11 #include <memory>
12 #include <mutex>
13 #include <set>
14 #include <atomic>
15 
16 #include <boost/asio/io_service.hpp>
17 #include <boost/asio/ip/address.hpp>
18 #include <boost/asio/steady_timer.hpp>
19 
20 #include <vsomeip/primitive_types.hpp>
21 #include <vsomeip/function_types.hpp>
22 #include <vsomeip/payload.hpp>
23 
24 #ifdef ANDROID
25 #include "../../configuration/include/internal_android.hpp"
26 #else
27 #include "../../configuration/include/internal.hpp"
28 #endif // ANDROID
29 
30 namespace vsomeip_v3 {
31 
32 class endpoint;
33 class endpoint_definition;
34 class message;
35 class payload;
36 class routing_manager;
37 
38 class event
39         : public std::enable_shared_from_this<event> {
40 public:
41     event(routing_manager *_routing, bool _is_shadow = false);
42 
43     service_t get_service() const;
44     void set_service(service_t _service);
45 
46     instance_t get_instance() const;
47     void set_instance(instance_t _instance);
48 
49     major_version_t get_version() const;
50     void set_version(major_version_t _major);
51 
52     event_t get_event() const;
53     void set_event(event_t _event);
54 
55     const std::shared_ptr<payload> get_payload() const;
56 
57     void set_payload(const std::shared_ptr<payload> &_payload,
58             const client_t _client, bool _force);
59 
60     void set_payload(const std::shared_ptr<payload> &_payload,
61             const client_t _client,
62             const std::shared_ptr<endpoint_definition>& _target, bool _force);
63 
64     bool set_payload_dont_notify(const std::shared_ptr<payload> &_payload);
65     bool set_payload_notify_pending(const std::shared_ptr<payload> &_payload);
66 
67     void set_payload(const std::shared_ptr<payload> &_payload, bool _force);
68     void unset_payload(bool _force = false);
69 
70     event_type_e get_type() const;
71     void set_type(const event_type_e _type);
72 
73     reliability_type_e get_reliability() const;
74     void set_reliability(const reliability_type_e _reliability);
75 
76     bool is_field() const;
77     bool is_provided() const;
78     void set_provided(bool _is_provided);
79 
80     bool is_set() const;
81 
82     // SIP_RPC_357
83     void set_update_cycle(std::chrono::milliseconds &_cycle);
84     void set_change_resets_cycle(bool _change_resets_cycle);
85 
86     // SIP_RPC_358
87     void set_update_on_change(bool _is_active);
88 
89     // SIP_RPC_359 (epsilon change)
90     void set_epsilon_change_function(
91             const epsilon_change_func_t &_epsilon_change_func);
92 
93     const std::set<eventgroup_t> get_eventgroups() const;
94     std::set<eventgroup_t> get_eventgroups(client_t _client) const;
95     void add_eventgroup(eventgroup_t _eventgroup);
96     void set_eventgroups(const std::set<eventgroup_t> &_eventgroups);
97 
98     void notify_one(client_t _client,
99             const std::shared_ptr<endpoint_definition> &_target);
100     void notify_one(client_t _client);
101 
102 
103     bool add_subscriber(eventgroup_t _eventgroup, client_t _client, bool _force);
104     void remove_subscriber(eventgroup_t _eventgroup, client_t _client);
105     bool has_subscriber(eventgroup_t _eventgroup, client_t _client);
106     std::set<client_t> get_subscribers();
107     VSOMEIP_EXPORT std::set<client_t> get_subscribers(eventgroup_t _eventgroup);
108     void clear_subscribers();
109 
110     void add_ref(client_t _client, bool _is_provided);
111     void remove_ref(client_t _client, bool _is_provided);
112     bool has_ref();
113     bool has_ref(client_t _client, bool _is_provided);
114 
115     bool is_shadow() const;
116     void set_shadow(bool _shadow);
117 
118     bool is_cache_placeholder() const;
119     void set_cache_placeholder(bool _is_cache_place_holder);
120 
121     bool is_subscribed(client_t _client);
122 
123     void remove_pending(const std::shared_ptr<endpoint_definition> &_target);
124 
125 private:
126     void update_cbk(boost::system::error_code const &_error);
127     void notify();
128     void notify(client_t _client,
129             const std::shared_ptr<endpoint_definition> &_target);
130 
131     void start_cycle();
132     void stop_cycle();
133 
134     bool compare(const std::shared_ptr<payload> &_lhs,
135             const std::shared_ptr<payload> &_rhs) const;
136 
137     bool set_payload_helper(const std::shared_ptr<payload> &_payload,
138             bool _force);
139     void reset_payload(const std::shared_ptr<payload> &_payload);
140 
141     void notify_one_unlocked(client_t _client);
142     void notify_one_unlocked(client_t _client,
143             const std::shared_ptr<endpoint_definition> &_target);
144 
145 private:
146     routing_manager *routing_;
147     mutable std::mutex mutex_;
148     std::shared_ptr<message> message_;
149 
150     std::atomic<event_type_e> type_;
151 
152     boost::asio::steady_timer cycle_timer_;
153     std::chrono::milliseconds cycle_;
154 
155     std::atomic<bool> change_resets_cycle_;
156     std::atomic<bool> is_updating_on_change_;
157 
158     mutable std::mutex eventgroups_mutex_;
159     std::map<eventgroup_t, std::set<client_t> > eventgroups_;
160 
161     std::atomic<bool> is_set_;
162     std::atomic<bool> is_provided_;
163 
164     std::mutex refs_mutex_;
165     std::map<client_t, std::map<bool, uint32_t>> refs_;
166 
167     std::atomic<bool> is_shadow_;
168     std::atomic<bool> is_cache_placeholder_;
169 
170     epsilon_change_func_t epsilon_change_func_;
171 
172     std::atomic<reliability_type_e> reliability_;
173 
174     std::set<std::shared_ptr<endpoint_definition> > pending_;
175 };
176 
177 }  // namespace vsomeip_v3
178 
179 #endif // VSOMEIP_V3_EVENT_IMPL_HPP_
180