summaryrefslogtreecommitdiff
path: root/implementation/routing/src/remote_subscription.cpp
blob: daec6f991a3e28415270789394bbb9efcf38e597 (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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
// Copyright (C) 2018 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)
// 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/.

#include "../include/remote_subscription.hpp"

#include <vsomeip/internal/logger.hpp>

namespace vsomeip_v3 {

remote_subscription::remote_subscription()
    : id_(PENDING_SUBSCRIPTION_ID),
      is_initial_(true),
      force_initial_events_(false),
      major_(DEFAULT_MAJOR),
      ttl_(DEFAULT_TTL),
      reserved_(0),
      counter_(0),
      answers_(1) {
}

remote_subscription::~remote_subscription() {
}

bool
remote_subscription::operator==(
        const remote_subscription &_other) const {
    auto own_egi = eventgroupinfo_.lock();
    auto other_egi = _other.eventgroupinfo_.lock();
    bool reliable_equal(true);
    if (reliable_ && _other.reliable_) {
        reliable_equal = (reliable_ == _other.reliable_);
    }
    bool unreliable_equal(true);
    if (unreliable_ && _other.unreliable_) {
        unreliable_equal = (unreliable_ == _other.unreliable_);
    }
    return (own_egi && other_egi && own_egi == other_egi && unreliable_equal
            && reliable_equal);
}

bool
remote_subscription::equals(
        const std::shared_ptr<remote_subscription> &_other) const {
    return operator ==(*_other);
}

void
remote_subscription::reset(const std::set<client_t> &_clients) {
    auto its_client_state = std::make_pair(
            remote_subscription_state_e::SUBSCRIPTION_PENDING,
            std::chrono::steady_clock::time_point());
    if (_clients.empty()) {
        clients_[0] = its_client_state;
    } else {
        for (const auto &its_client : _clients)
            clients_[its_client] = its_client_state;
    }
}

bool
remote_subscription::is_initial() const {
    return is_initial_;
}

void
remote_subscription::set_initial(const bool _is_initial) {
    is_initial_ = _is_initial;
}

bool
remote_subscription::force_initial_events() const {
    return force_initial_events_;
}

void
remote_subscription::set_force_initial_events(
        const bool _force_initial_events) {
    force_initial_events_ = _force_initial_events;
}

remote_subscription_id_t
remote_subscription::get_id() const {
    return id_;
}

void
remote_subscription::set_id(const remote_subscription_id_t _id) {
    id_ = _id;
}

std::shared_ptr<eventgroupinfo>
remote_subscription::get_eventgroupinfo() const {
    return eventgroupinfo_.lock();
}

void
remote_subscription::set_eventgroupinfo(
        const std::shared_ptr<eventgroupinfo> &_info) {
    eventgroupinfo_ = _info;
}

ttl_t
remote_subscription::get_ttl() const {
    return ttl_;
}

void
remote_subscription::set_ttl(const ttl_t _ttl) {
    ttl_ = _ttl;
}

uint16_t
remote_subscription::get_reserved() const {
    return reserved_;
}

void
remote_subscription::set_reserved(const uint16_t _reserved) {
    reserved_ = _reserved;
}

uint8_t
remote_subscription::get_counter() const {
    return counter_;
}

void
remote_subscription::set_counter(uint8_t _counter) {
    counter_ = _counter;
}

std::set<client_t>
remote_subscription::get_clients() const {
    std::lock_guard<std::mutex> its_lock(mutex_);
    std::set<client_t> its_clients;
    for (const auto& its_item : clients_)
        its_clients.insert(its_item.first);
    return its_clients;
}

bool
remote_subscription::has_client() const {
    std::lock_guard<std::mutex> its_lock(mutex_);
    return (clients_.size() > 0);
}

bool
remote_subscription::has_client(const client_t _client) const {
    std::lock_guard<std::mutex> its_lock(mutex_);
    return (clients_.find(_client) != clients_.end());
}

void
remote_subscription::remove_client(const client_t _client) {
    std::lock_guard<std::mutex> its_lock(mutex_);
    clients_.erase(_client);
}

remote_subscription_state_e
remote_subscription::get_client_state(const client_t _client) const {
    std::lock_guard<std::mutex> its_lock(mutex_);
    auto found_client = clients_.find(_client);
    if (found_client != clients_.end()) {
        return found_client->second.first;
    }
    return remote_subscription_state_e::SUBSCRIPTION_UNKNOWN;
}

void
remote_subscription::set_client_state(const client_t _client,
        remote_subscription_state_e _state) {
    std::lock_guard<std::mutex> its_lock(mutex_);
    auto found_item = clients_.find(_client);
    if (found_item != clients_.end()) {
        found_item->second.first = _state;
        if (found_item->second.second == std::chrono::steady_clock::time_point()
            && (_state == remote_subscription_state_e::SUBSCRIPTION_ACKED
                || _state == remote_subscription_state_e::SUBSCRIPTION_NACKED)) {
            found_item->second.second = std::chrono::steady_clock::now()
                + std::chrono::seconds(ttl_);
        }
    }
}

void
remote_subscription::set_all_client_states(remote_subscription_state_e _state) {
    std::lock_guard<std::mutex> its_lock(mutex_);
    for (auto &its_item : clients_)
        its_item.second.first = _state;
}

std::shared_ptr<endpoint_definition>
remote_subscription::get_subscriber() const {
    return subscriber_;
}

void
remote_subscription::set_subscriber(
        const std::shared_ptr<endpoint_definition> &_subscriber) {
    subscriber_ = _subscriber;
}

std::shared_ptr<endpoint_definition>
remote_subscription::get_reliable() const {
    return reliable_;
}

void
remote_subscription::set_reliable(
        const std::shared_ptr<endpoint_definition> &_reliable) {
    reliable_ = _reliable;
}

std::shared_ptr<endpoint_definition>
remote_subscription::get_unreliable() const {
    return unreliable_;
}

void
remote_subscription::set_unreliable(
        const std::shared_ptr<endpoint_definition> &_unreliable) {
    unreliable_ = _unreliable;
}

bool
remote_subscription::is_pending() const {
    std::lock_guard<std::mutex> its_lock(mutex_);
    for (auto its_client : clients_) {
        if (its_client.second.first
                == remote_subscription_state_e::SUBSCRIPTION_PENDING) {
            return true;
        }
    }
    return false;
}

bool
remote_subscription::is_acknowledged() const {
    std::lock_guard<std::mutex> its_lock(mutex_);
    for (auto its_client : clients_) {
        if (its_client.second.first
                != remote_subscription_state_e::SUBSCRIPTION_ACKED) {
            return false;
        }
    }
    return true;
}

std::chrono::steady_clock::time_point
remote_subscription::get_expiration(const client_t _client) const {
    std::lock_guard<std::mutex> its_lock(mutex_);
    auto found_client = clients_.find(_client);
    if (found_client != clients_.end()) {
        return found_client->second.second;
    }
    return std::chrono::steady_clock::now();
}

std::set<client_t>
remote_subscription::update(const std::set<client_t> &_clients,
        const std::chrono::steady_clock::time_point &_timepoint,
        const bool _is_subscribe) {
    std::set<client_t> its_changed;

    std::lock_guard<std::mutex> its_lock(mutex_);
    for (const auto &its_client : _clients) {
        auto found_client = clients_.find(its_client);
        if (_is_subscribe) {
            if (found_client != clients_.end()) {
                found_client->second.second = _timepoint;
            } else {
                its_changed.insert(its_client);
            }
        } else {
            if (found_client != clients_.end()) {
                its_changed.insert(its_client);
            }
        }
    }

    for (const auto &its_client : its_changed) {
        if (_is_subscribe) {
            clients_[its_client] = std::make_pair(
                    remote_subscription_state_e::SUBSCRIPTION_PENDING, _timepoint);
        } else {
            clients_.erase(its_client);
        }
    }

    return its_changed;
}

std::shared_ptr<remote_subscription>
remote_subscription::get_parent() const {
    return parent_.lock();
}

void
remote_subscription::set_parent(
        const std::shared_ptr<remote_subscription> &_parent) {
    parent_ = _parent;
}

std::uint32_t
remote_subscription::get_answers() const {
    return answers_;
}

void
remote_subscription::set_answers(const std::uint32_t _answers) {
    answers_ = _answers;
}

} // namespace vsomeip_v3