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_EVENTGROUPINFO_HPP_
7 #define VSOMEIP_V3_EVENTGROUPINFO_HPP_
8 
9 #include <atomic>
10 #include <chrono>
11 #include <list>
12 #include <memory>
13 #include <mutex>
14 #include <set>
15 #include <vector>
16 
17 #include <boost/asio/ip/address.hpp>
18 
19 #include <vsomeip/export.hpp>
20 #include <vsomeip/primitive_types.hpp>
21 
22 #include "remote_subscription.hpp"
23 #include "types.hpp"
24 
25 namespace vsomeip_v3 {
26 
27 class endpoint_definition;
28 class event;
29 
30 class eventgroupinfo {
31 public:
32     struct subscription_t {
33         std::shared_ptr<remote_subscription> subscription_;
34         std::chrono::steady_clock::time_point expiration_;
35 
operator ==vsomeip_v3::eventgroupinfo::subscription_t36         bool operator==(const subscription_t &_other) const {
37             return (subscription_ == _other.subscription_);
38         }
39     };
40 
41     VSOMEIP_EXPORT eventgroupinfo();
42     VSOMEIP_EXPORT eventgroupinfo(
43             const service_t _service, const service_t _instance,
44             const eventgroup_t _eventgroup, const major_version_t _major,
45             const ttl_t _ttl, const uint8_t _max_remote_subscribers);
46     VSOMEIP_EXPORT ~eventgroupinfo();
47 
48     VSOMEIP_EXPORT service_t get_service() const;
49     VSOMEIP_EXPORT void set_service(const service_t _service);
50 
51     VSOMEIP_EXPORT instance_t get_instance() const;
52     VSOMEIP_EXPORT void set_instance(const instance_t _instance);
53 
54     VSOMEIP_EXPORT eventgroup_t get_eventgroup() const;
55     VSOMEIP_EXPORT void set_eventgroup(const eventgroup_t _eventgroup);
56 
57     VSOMEIP_EXPORT major_version_t get_major() const;
58     VSOMEIP_EXPORT void set_major(const major_version_t _major);
59 
60     VSOMEIP_EXPORT ttl_t get_ttl() const;
61     VSOMEIP_EXPORT void set_ttl(const ttl_t _ttl);
62 
63     VSOMEIP_EXPORT bool is_multicast() const;
64     VSOMEIP_EXPORT bool get_multicast(boost::asio::ip::address &_address,
65             uint16_t &_port) const;
66     VSOMEIP_EXPORT void set_multicast(const boost::asio::ip::address &_address,
67             uint16_t _port);
68     VSOMEIP_EXPORT bool is_sending_multicast() const;
69 
70     VSOMEIP_EXPORT const std::set<std::shared_ptr<event> > get_events() const;
71     VSOMEIP_EXPORT void add_event(const std::shared_ptr<event>& _event);
72     VSOMEIP_EXPORT void remove_event(const std::shared_ptr<event>& _event);
73     VSOMEIP_EXPORT reliability_type_e get_reliability() const;
74     VSOMEIP_EXPORT void set_reliability(reliability_type_e _reliability);
75     VSOMEIP_EXPORT bool is_reliability_auto_mode() const;
76 
77     VSOMEIP_EXPORT std::set<std::shared_ptr<remote_subscription>>
78             get_remote_subscriptions() const;
79 
80     std::shared_ptr<remote_subscription> get_remote_subscription(
81             const remote_subscription_id_t _id);
82 
83     bool update_remote_subscription(
84             const std::shared_ptr<remote_subscription> &_subscription,
85             const std::chrono::steady_clock::time_point &_expiration,
86             std::set<client_t> &_changed, remote_subscription_id_t &_id,
87             const bool _is_subscribe);
88 
89     bool is_remote_subscription_limit_reached(
90             const std::shared_ptr<remote_subscription> &_subscription);
91 
92     remote_subscription_id_t add_remote_subscription(
93             const std::shared_ptr<remote_subscription> &_subscription);
94 
95     VSOMEIP_EXPORT void remove_remote_subscription(
96             const remote_subscription_id_t _id);
97 
98     void clear_remote_subscriptions();
99 
100     VSOMEIP_EXPORT std::set<std::shared_ptr<endpoint_definition> >
101     get_unicast_targets() const;
102     VSOMEIP_EXPORT std::set<std::shared_ptr<endpoint_definition> >
103     get_multicast_targets() const;
104 
105     VSOMEIP_EXPORT uint8_t get_threshold() const;
106     VSOMEIP_EXPORT void set_threshold(uint8_t _threshold);
107 
108     VSOMEIP_EXPORT bool is_selective() const;
109 
110     VSOMEIP_EXPORT void send_initial_events(
111             const std::shared_ptr<endpoint_definition> &_reliable,
112             const std::shared_ptr<endpoint_definition> &_unreliable) const;
113 
114     VSOMEIP_EXPORT uint8_t get_max_remote_subscribers() const;
115     VSOMEIP_EXPORT void set_max_remote_subscribers(uint8_t _max_remote_subscribers);
116 
117 private:
118     void update_id();
119     uint32_t get_unreliable_target_count() const;
120 
121     std::atomic<service_t> service_;
122     std::atomic<instance_t> instance_;
123     std::atomic<eventgroup_t> eventgroup_;
124     std::atomic<major_version_t> major_;
125     std::atomic<ttl_t> ttl_;
126 
127     mutable std::mutex address_mutex_;
128     boost::asio::ip::address address_;
129     uint16_t port_;
130 
131     mutable std::mutex events_mutex_;
132     std::set<std::shared_ptr<event> > events_;
133 
134     std::atomic<uint8_t> threshold_;
135 
136     mutable std::mutex subscriptions_mutex_;
137     std::map<remote_subscription_id_t,
138         std::shared_ptr<remote_subscription>
139     > subscriptions_;
140     remote_subscription_id_t id_;
141     std::map<boost::asio::ip::address, uint8_t> remote_subscribers_count_;
142 
143     std::atomic<reliability_type_e> reliability_;
144     std::atomic<bool> reliability_auto_mode_;
145 
146     uint8_t max_remote_subscribers_;
147 };
148 
149 } // namespace vsomeip_v3
150 
151 #endif // VSOMEIP_V3_EVENTGROUPINFO_HPP_
152