summaryrefslogtreecommitdiff
path: root/test/network_tests/npdu_tests/npdu_test_service.hpp
blob: bef06806e314ea2bf4db70c0a56dc992bf8baaf2 (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
// Copyright (C) 2015-2019 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 NPDUTESTSERVICE_HPP_
#define NPDUTESTSERVICE_HPP_
#include <gtest/gtest.h>

#include <vsomeip/vsomeip.hpp>

#include <thread>
#include <mutex>
#include <condition_variable>
#include <functional>
#include <chrono>
#include <deque>

class npdu_test_service
{
public:
    npdu_test_service(vsomeip::service_t _service_id,
                            vsomeip::instance_t _instance_id,
                            std::array<vsomeip::method_t, 4> _method_ids,
                            std::array<std::chrono::nanoseconds, 4> _debounce_times,
                            std::array<std::chrono::nanoseconds, 4> _max_retention_times);
    void init();
    void start();
    void stop();
    void offer();
    void stop_offer();
    void join_shutdown_thread();
    void on_state(vsomeip::state_type_e _state);
    template<int method_idx> void on_message(const std::shared_ptr<vsomeip::message> &_request);
    void on_message_shutdown(const std::shared_ptr<vsomeip::message> &_request);
    void run();

private:
    template<int method_idx> void check_times();
    template <int method_idx> void register_message_handler();

private:
    std::shared_ptr<vsomeip::application> app_;
    bool is_registered_;
    std::array<vsomeip::method_t, 4> method_ids_;
    std::array<std::chrono::nanoseconds, 4> debounce_times_;
    std::array<std::chrono::nanoseconds, 4> max_retention_times_;
    std::array<std::chrono::steady_clock::time_point, 4> timepoint_last_received_message_;
    std::array<std::mutex, 4> timepoint_mutexes_;
    std::deque<std::chrono::microseconds> undershot_debounce_times_;
    vsomeip::service_t service_id_;
    vsomeip::instance_t instance_id_;
    std::mutex mutex_;
    std::condition_variable condition_;
    bool blocked_;
    std::mutex shutdown_mutex_;
    std::condition_variable shutdown_condition_;
    bool allowed_to_shutdown_;
    std::uint32_t number_of_received_messages_;
    std::thread offer_thread_;
    std::thread shutdown_thread_;
};

#endif /* NPDUTESTSERVICE_HPP_ */