summaryrefslogtreecommitdiff
path: root/test/network_tests/npdu_tests/npdu_test_client.hpp
blob: 980c16aa8b70405e5971ae5a320fdd04de6bf902 (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
// 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 PAYLOADTESTCLIENT_HPP_
#define NPDUTESTCLIENT_HPP_

#include <gtest/gtest.h>

#include <vsomeip/vsomeip.hpp>

#include <thread>
#include <mutex>
#include <condition_variable>
#include <functional>
#include <map>

#include "../npdu_tests/npdu_test_globals.hpp"
#include "../someip_test_globals.hpp"

class npdu_test_client
{
public:
    npdu_test_client(bool _use_tcp, bool _call_service_sync,
                           std::uint32_t _sliding_window_size,
                           bool _wait_for_replies,
                           std::array<std::array<std::chrono::milliseconds, 4>, 4> _applicative_debounce);
    ~npdu_test_client();
    void init();
    void start();
    void stop();
    void join_sender_thread();
    void on_state(vsomeip::state_type_e _state);
    template<int service_idx> void on_availability(vsomeip::service_t _service,
                                                   vsomeip::instance_t _instance,
                                                   bool _is_available);
    template<int service_idx, int method_idx> void on_message(
            const std::shared_ptr<vsomeip::message> &_response);
    template<int service_idx> void send();
    template<int service_idx> void run();

private:
    template<int service_idx> void send_messages_sync();
    template<int service_idx, int method_idx> std::thread start_send_thread_sync();
    template<int service_idx> void send_messages_async();
    template<int service_idx, int method_idx> std::thread start_send_thread_async();
    template<int service_idx> void send_messages_and_dont_wait_for_reply();
    std::uint32_t get_max_allowed_payload();
    template<int service_idx> void register_availability_handler();
    template<int service_idx> void register_message_handler_for_all_service_methods();
    template<int service_idx, int method_idx> void register_message_handler();
    template<int service_idx, int method_idx>
        std::thread start_send_thread();
    void wait_for_all_senders();

private:
    std::shared_ptr<vsomeip::application> app_;
    std::shared_ptr<vsomeip::message> request_;
    bool call_service_sync_;
    bool wait_for_replies_;
    std::uint32_t sliding_window_size_;

    std::array<std::mutex, npdu_test::service_ids.size()> mutexes_;
    std::array<std::condition_variable, npdu_test::service_ids.size()> conditions_;
    std::array<bool, npdu_test::service_ids.size()> blocked_;
    std::array<bool, npdu_test::service_ids.size()> is_available_;
    const std::uint32_t number_of_messages_to_send_;
    std::uint32_t number_of_sent_messages_[npdu_test::service_ids.size()];
    std::array<std::array<std::uint32_t, npdu_test::method_ids[0].size()>,
            npdu_test::service_ids.size()> number_of_acknowledged_messages_;
    std::array<std::array<std::mutex, npdu_test::method_ids[0].size()>,
            npdu_test::service_ids.size()> number_of_acknowledged_messages_mutexes_;

    std::array<std::uint32_t, npdu_test::service_ids.size()> current_payload_size_;

    std::array<std::array<bool, npdu_test::method_ids[0].size()>,
            npdu_test::service_ids.size()> all_msg_acknowledged_;
    std::array<std::array<std::mutex, npdu_test::method_ids[0].size()>,
            npdu_test::service_ids.size()> all_msg_acknowledged_mutexes_;
    std::array<std::array<std::unique_lock<std::mutex>, npdu_test::method_ids[0].size()>,
            npdu_test::service_ids.size()> all_msg_acknowledged_unique_locks_;
    std::array<
            std::array<std::condition_variable,
                    npdu_test::method_ids[0].size()>,
            npdu_test::service_ids.size()> all_msg_acknowledged_cvs_;
    std::array<std::uint32_t, 4> acknowledgements_;
    std::array<std::array<std::chrono::milliseconds, 4>, 4> applicative_debounce_;
    std::array<
            std::array<std::shared_ptr<vsomeip::payload>,
                    npdu_test::method_ids[0].size()>,
            npdu_test::service_ids.size()> payloads_;
    std::array<
            std::array<std::vector<vsomeip::byte_t>,
                    npdu_test::method_ids[0].size()>,
            npdu_test::service_ids.size()> payload_data_;
    std::array<std::thread, npdu_test::service_ids.size()> senders_;
    std::mutex finished_mutex_;
    std::array<bool, npdu_test::service_ids.size()> finished_;
    std::thread finished_waiter_;
};

#endif /* NPDUTESTCLIENT_HPP_ */