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 #include "../include/subscription.hpp"
7 
8 #include <vsomeip/internal/logger.hpp>
9 
10 namespace vsomeip_v3 {
11 namespace sd {
12 
get_major() const13 major_version_t subscription::get_major() const {
14     return major_;
15 }
16 
set_major(major_version_t _major)17 void subscription::set_major(major_version_t _major) {
18     major_ = _major;
19 }
20 
get_ttl() const21 ttl_t subscription::get_ttl() const {
22     return ttl_;
23 }
24 
set_ttl(ttl_t _ttl)25 void subscription::set_ttl(ttl_t _ttl) {
26     ttl_ = _ttl;
27 }
28 
get_endpoint(bool _reliable) const29 std::shared_ptr<endpoint> subscription::get_endpoint(bool _reliable) const {
30     return (_reliable ? reliable_ : unreliable_);
31 }
32 
set_endpoint(const std::shared_ptr<endpoint> & _endpoint,bool _reliable)33 void subscription::set_endpoint(const std::shared_ptr<endpoint>& _endpoint,
34         bool _reliable) {
35     if (_reliable)
36         reliable_ = _endpoint;
37     else
38         unreliable_ = _endpoint;
39 }
40 
is_selective() const41 bool subscription::is_selective() const {
42     return is_selective_;
43 }
set_selective(const bool _is_selective)44 void subscription::set_selective(const bool _is_selective) {
45     is_selective_ = _is_selective;
46 }
47 
48 subscription_state_e
get_state(const client_t _client) const49 subscription::get_state(const client_t _client) const {
50     std::lock_guard<std::mutex> its_lock(clients_mutex_);
51     auto found_client = clients_.find(_client);
52     if (found_client != clients_.end())
53         return found_client->second;
54     return subscription_state_e::ST_UNKNOWN;
55 }
56 
57 void
set_state(const client_t _client,const subscription_state_e _state)58 subscription::set_state(
59         const client_t _client, const subscription_state_e _state) {
60     std::lock_guard<std::mutex> its_lock(clients_mutex_);
61     auto found_client = clients_.find(_client);
62     if (found_client != clients_.end())
63         found_client->second = _state;
64 }
65 
is_tcp_connection_established() const66 bool subscription::is_tcp_connection_established() const {
67     return tcp_connection_established_;
68 }
set_tcp_connection_established(bool _is_established)69 void subscription::set_tcp_connection_established(bool _is_established) {
70     tcp_connection_established_ = _is_established;
71 }
72 
is_udp_connection_established() const73 bool subscription::is_udp_connection_established() const {
74     return udp_connection_established_;
75 }
set_udp_connection_established(bool _is_established)76 void subscription::set_udp_connection_established(bool _is_established) {
77     udp_connection_established_ = _is_established;
78 }
79 
80 bool
add_client(const client_t _client)81 subscription::add_client(const client_t _client) {
82     std::lock_guard<std::mutex> its_lock(clients_mutex_);
83     auto find_client = clients_.find(_client);
84     if (find_client != clients_.end())
85         return false;
86 
87     clients_[_client] = subscription_state_e::ST_UNKNOWN;
88     return true;
89 }
90 
91 bool
remove_client(const client_t _client)92 subscription::remove_client(const client_t _client) {
93     std::lock_guard<std::mutex> its_lock(clients_mutex_);
94     auto its_size = clients_.size();
95     clients_.erase(_client);
96     return (its_size > clients_.size());
97 }
98 
get_clients() const99 std::set<client_t> subscription::get_clients() const {
100     std::set<client_t> its_clients;
101     {
102         std::lock_guard<std::mutex> its_lock(clients_mutex_);
103         for (const auto& its_item : clients_)
104             its_clients.insert(its_item.first);
105     }
106     return its_clients;
107 }
108 
has_client() const109 bool subscription::has_client() const {
110     std::lock_guard<std::mutex> its_lock(clients_mutex_);
111     return (clients_.size() > 0);
112 }
113 
has_client(const client_t _client) const114 bool subscription::has_client(const client_t _client) const {
115     std::lock_guard<std::mutex> its_lock(clients_mutex_);
116     return (clients_.find(_client) != clients_.end());
117 }
118 
set_eventgroupinfo(const std::shared_ptr<eventgroupinfo> _info)119 void subscription::set_eventgroupinfo(
120         const std::shared_ptr<eventgroupinfo> _info) {
121     eg_info_ = _info;
122 }
get_eventgroupinfo() const123 std::weak_ptr<eventgroupinfo> subscription::get_eventgroupinfo() const {
124     return eg_info_;
125 }
126 
127 } // namespace sd
128 } // namespace vsomeip_v3
129