1 // Copyright (C) 2018 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_REMOTE_SUBSCRIPTION_HPP_
7 #define VSOMEIP_V3_REMOTE_SUBSCRIPTION_HPP_
8 
9 #include <atomic>
10 #include <map>
11 #include <mutex>
12 #include <set>
13 
14 #include <vsomeip/primitive_types.hpp>
15 
16 #include "../../endpoints/include/endpoint_definition.hpp"
17 #include "types.hpp"
18 #include <vsomeip/export.hpp>
19 
20 
21 namespace vsomeip_v3 {
22 
23 class eventgroupinfo;
24 
25 const remote_subscription_id_t PENDING_SUBSCRIPTION_ID(0);
26 
27 class remote_subscription {
28 public:
29     VSOMEIP_EXPORT remote_subscription();
30     VSOMEIP_EXPORT ~remote_subscription();
31 
32     bool operator==(const remote_subscription &_other) const;
33     bool equals(const std::shared_ptr<remote_subscription> &_other) const;
34     bool address_equals(const std::shared_ptr<remote_subscription> &_other) const;
35 
36     VSOMEIP_EXPORT void reset(const std::set<client_t> &_clients);
37 
38     VSOMEIP_EXPORT bool is_initial() const;
39     VSOMEIP_EXPORT void set_initial(const bool _is_initial);
40 
41     VSOMEIP_EXPORT bool force_initial_events() const;
42     VSOMEIP_EXPORT void set_force_initial_events(const bool _force_initial_events);
43 
44     remote_subscription_id_t get_id() const;
45     void set_id(const remote_subscription_id_t _id);
46 
47     VSOMEIP_EXPORT std::shared_ptr<remote_subscription> get_parent() const;
48     void set_parent(const std::shared_ptr<remote_subscription> &_parent);
49 
50     VSOMEIP_EXPORT std::shared_ptr<eventgroupinfo> get_eventgroupinfo() const;
51     VSOMEIP_EXPORT void set_eventgroupinfo(const std::shared_ptr<eventgroupinfo> &_info);
52 
53     VSOMEIP_EXPORT ttl_t get_ttl() const;
54     VSOMEIP_EXPORT void set_ttl(const ttl_t _ttl);
55 
56     uint16_t get_reserved() const;
57     void set_reserved(const uint16_t _reserved);
58 
59     uint8_t get_counter() const;
60     void set_counter(uint8_t _counter);
61 
62     VSOMEIP_EXPORT std::set<client_t> get_clients() const;
63     bool has_client() const;
64     bool has_client(const client_t _client) const;
65     void remove_client(const client_t _client);
66 
67     VSOMEIP_EXPORT remote_subscription_state_e get_client_state(const client_t _client) const;
68     void set_client_state(const client_t _client,
69             remote_subscription_state_e _state);
70     void set_all_client_states(remote_subscription_state_e _state);
71 
72     std::chrono::steady_clock::time_point get_expiration(const client_t _client) const;
73 
74     VSOMEIP_EXPORT std::shared_ptr<endpoint_definition> get_subscriber() const;
75     VSOMEIP_EXPORT void set_subscriber(const std::shared_ptr<endpoint_definition> &_subscriber);
76 
77     VSOMEIP_EXPORT std::shared_ptr<endpoint_definition> get_reliable() const;
78     VSOMEIP_EXPORT void set_reliable(const std::shared_ptr<endpoint_definition> &_reliable);
79 
80     VSOMEIP_EXPORT std::shared_ptr<endpoint_definition> get_unreliable() const;
81     VSOMEIP_EXPORT void set_unreliable(const std::shared_ptr<endpoint_definition> &_unreliable);
82 
83     VSOMEIP_EXPORT bool is_pending() const;
84     bool is_acknowledged() const;
85 
86     std::set<client_t> update(const std::set<client_t> &_clients,
87             const std::chrono::steady_clock::time_point &_timepoint,
88             const bool _is_subscribe);
89 
90     VSOMEIP_EXPORT std::uint32_t get_answers() const;
91     VSOMEIP_EXPORT void set_answers(const std::uint32_t _answers);
92 
93     VSOMEIP_EXPORT bool get_ip_address(boost::asio::ip::address &_address) const;
94 
95 private:
96     std::atomic<remote_subscription_id_t> id_;
97     std::atomic<bool> is_initial_;
98     std::atomic<bool> force_initial_events_;
99     std::weak_ptr<remote_subscription> parent_;
100 
101     std::weak_ptr<eventgroupinfo> eventgroupinfo_;
102 
103     major_version_t major_;
104     ttl_t ttl_;
105     std::uint16_t reserved_;
106     std::uint8_t counter_;
107 
108     std::map<client_t,
109         std::pair<remote_subscription_state_e,
110             std::chrono::steady_clock::time_point
111         >
112     > clients_;
113 
114     // The endpoint that sent(!) the subscription
115     std::shared_ptr<endpoint_definition> subscriber_;
116 
117     // The endpoints defined by the endpoint options
118     std::shared_ptr<endpoint_definition> reliable_;
119     std::shared_ptr<endpoint_definition> unreliable_;
120 
121     // Number of acknowledgements that must be sent
122     // for the subscriptions. This is usally 1, but
123     // may be larger if a matching subscription arrived
124     // before the subscription could be acknowledged
125     std::uint32_t answers_;
126 
127     mutable std::mutex mutex_;
128 };
129 
130 } // namespace vsomeip_v3
131 
132 #endif // VSOMEIP_V3_REMOTE_SUBSCRIPTION_HPP_
133