summaryrefslogtreecommitdiff
path: root/test/subscribe_notify_tests/subscribe_notify_test_one_event_two_eventgroups_service.cpp
blob: 671089227f1634c7e94ea2f373e4ff823270dad4 (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
// Copyright (C) 2014-2017 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/.
#ifndef VSOMEIP_ENABLE_SIGNAL_HANDLING
#include <csignal>
#endif
#include <chrono>
#include <condition_variable>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <thread>
#include <atomic>

#include <gtest/gtest.h>
#include <vsomeip/internal/logger.hpp>

#include <vsomeip/vsomeip.hpp>

#include "subscribe_notify_test_globals.hpp"

class subscribe_notify_test_one_event_two_eventgroups_service {
public:
    subscribe_notify_test_one_event_two_eventgroups_service(subscribe_notify_test::service_info _info) :
            app_(vsomeip::runtime::get()->create_application()),
            wait_for_shutdown_(true),
            info_(_info),
            notify_thread_(std::bind(&subscribe_notify_test_one_event_two_eventgroups_service::wait_for_shutdown, this)) {
    }

    ~subscribe_notify_test_one_event_two_eventgroups_service() {
        notify_thread_.join();
    }

    bool init() {
        if (!app_->init()) {
            ADD_FAILURE() << "Couldn't initialize application";
            return false;
        }
        app_->register_state_handler(
                std::bind(&subscribe_notify_test_one_event_two_eventgroups_service::on_state, this,
                        std::placeholders::_1));

        app_->register_message_handler(
                info_.service_id,
                info_.instance_id,
                subscribe_notify_test::set_method_id,
                std::bind(&subscribe_notify_test_one_event_two_eventgroups_service::on_set, this,
                          std::placeholders::_1));

        app_->register_message_handler(
                info_.service_id,
                info_.instance_id,
                info_.method_id,
                std::bind(&subscribe_notify_test_one_event_two_eventgroups_service::on_message, this,
                          std::placeholders::_1));

        app_->register_message_handler(
                info_.service_id,
                info_.instance_id,
                subscribe_notify_test::shutdown_method_id,
                std::bind(&subscribe_notify_test_one_event_two_eventgroups_service::on_shutdown, this,
                          std::placeholders::_1));

        std::set<vsomeip::eventgroup_t> its_groups;
        // the service offers three events in two eventgroups
        // one of the events is in both eventgroups
        its_groups.insert(info_.eventgroup_id);
        app_->offer_event(info_.service_id, info_.instance_id,
                info_.event_id, its_groups, vsomeip::event_type_e::ET_FIELD,
                std::chrono::milliseconds::zero(),
                false, true, nullptr, vsomeip::reliability_type_e::RT_UNKNOWN);
        app_->offer_event(info_.service_id, info_.instance_id,
                 static_cast<vsomeip::event_t>(info_.event_id + 2),
                 its_groups, vsomeip::event_type_e::ET_FIELD,
                 std::chrono::milliseconds::zero(),
                 false, true, nullptr, vsomeip::reliability_type_e::RT_UNKNOWN);
        its_groups.erase(info_.eventgroup_id);
        its_groups.insert(static_cast<vsomeip::eventgroup_t>(info_.eventgroup_id + 1));
        app_->offer_event(info_.service_id, info_.instance_id,
                static_cast<vsomeip::event_t>(info_.event_id + 1),
                its_groups, vsomeip::event_type_e::ET_FIELD,
                std::chrono::milliseconds::zero(),
                false, true, nullptr, vsomeip::reliability_type_e::RT_UNKNOWN);
        app_->offer_event(info_.service_id, info_.instance_id,
                static_cast<vsomeip::event_t>(info_.event_id + 2),
                its_groups, vsomeip::event_type_e::ET_FIELD,
                std::chrono::milliseconds::zero(),
                false, true, nullptr, vsomeip::reliability_type_e::RT_UNKNOWN);
        payload_ = vsomeip::runtime::get()->create_payload();

        return true;
    }

    void start() {
        app_->start();
    }

#ifndef VSOMEIP_ENABLE_SIGNAL_HANDLING
    /*
     * Handle signal to shutdown
     */
    void stop() {
        {
            std::lock_guard<std::mutex> its_lock(shutdown_mutex_);
            wait_for_shutdown_ = false;
            shutdown_condition_.notify_one();
        }
        app_->clear_all_handler();
        stop_offer();
        notify_thread_.join();
        app_->stop();
    }
#endif

    void offer() {
        app_->offer_service(info_.service_id, info_.instance_id);
    }

    void stop_offer() {
        app_->stop_offer_service(info_.service_id, info_.instance_id);
    }

    void on_state(vsomeip::state_type_e _state) {
        std::cout << "Application " << app_->get_name() << " is "
        << (_state == vsomeip::state_type_e::ST_REGISTERED ?
                "registered." : "deregistered.") << std::endl;

        if (_state == vsomeip::state_type_e::ST_REGISTERED) {
            offer();
        }
    }

    void on_shutdown(const std::shared_ptr<vsomeip::message> &_message) {
        std::shared_ptr<vsomeip::message> its_response
            = vsomeip::runtime::get()->create_response(_message);
        its_response->set_payload(payload_);
        app_->send(its_response);
        {
            std::lock_guard<std::mutex> its_lock(shutdown_mutex_);
            wait_for_shutdown_ = false;
            shutdown_condition_.notify_one();
        }
    }

    void on_set(const std::shared_ptr<vsomeip::message> &_message) {
        std::shared_ptr<vsomeip::message> its_response
            = vsomeip::runtime::get()->create_response(_message);
        payload_ = _message->get_payload();
        its_response->set_payload(payload_);
        app_->send(its_response);
        app_->notify(info_.service_id, info_.instance_id, info_.event_id, payload_);
        app_->notify(info_.service_id, info_.instance_id, static_cast<vsomeip::event_t>(info_.event_id + 1), payload_);
        app_->notify(info_.service_id, info_.instance_id, static_cast<vsomeip::event_t>(info_.event_id + 2), payload_);
    }

    void on_message(const std::shared_ptr<vsomeip::message> &_message) {
        app_->send(vsomeip::runtime::get()->create_response(_message));
    }

    void wait_for_shutdown() {
        {
            std::unique_lock<std::mutex> its_lock(shutdown_mutex_);
            while (wait_for_shutdown_) {
                shutdown_condition_.wait(its_lock);
            }
            wait_for_shutdown_= true;
        }

        app_->clear_all_handler();
        std::this_thread::sleep_for(std::chrono::milliseconds(100));
        stop_offer();
        app_->stop();
    }

private:
    std::shared_ptr<vsomeip::application> app_;

    std::mutex shutdown_mutex_;
    bool wait_for_shutdown_;
    std::condition_variable shutdown_condition_;

    std::shared_ptr<vsomeip::payload> payload_;

    subscribe_notify_test::service_info info_;

    std::thread notify_thread_;
};

#ifndef VSOMEIP_ENABLE_SIGNAL_HANDLING
    subscribe_notify_test_one_event_two_eventgroups_service *its_service_ptr(nullptr);
    void handle_signal(int _signal) {
        if (its_service_ptr != nullptr &&
                (_signal == SIGINT || _signal == SIGTERM))
            its_service_ptr->stop();
    }
#endif


TEST(someip_subscribe_notify_test_one_event_two_eventgroups, wait_for_attribute_set)
{
    subscribe_notify_test_one_event_two_eventgroups_service its_service(
            subscribe_notify_test::service_info_subscriber_based_notification);
#ifndef VSOMEIP_ENABLE_SIGNAL_HANDLING
    its_service_ptr = &its_service;
    signal(SIGINT, handle_signal);
    signal(SIGTERM, handle_signal);
#endif
    if (its_service.init()) {
        its_service.start();
    }
}

#ifndef _WIN32
int main(int argc, char** argv)
{
    ::testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}
#endif