1 // Copyright (C) 2015-2019 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 #ifndef PAYLOADTESTCLIENT_HPP_
7 #define NPDUTESTCLIENT_HPP_
8 
9 #include <gtest/gtest.h>
10 
11 #include <vsomeip/vsomeip.hpp>
12 
13 #include <thread>
14 #include <mutex>
15 #include <condition_variable>
16 #include <functional>
17 #include <map>
18 
19 #include "../npdu_tests/npdu_test_globals.hpp"
20 #include "../someip_test_globals.hpp"
21 
22 class npdu_test_client
23 {
24 public:
25     npdu_test_client(bool _use_tcp, bool _call_service_sync,
26                            std::uint32_t _sliding_window_size,
27                            bool _wait_for_replies,
28                            std::array<std::array<std::chrono::milliseconds, 4>, 4> _applicative_debounce);
29     ~npdu_test_client();
30     void init();
31     void start();
32     void stop();
33     void join_sender_thread();
34     void on_state(vsomeip::state_type_e _state);
35     template<int service_idx> void on_availability(vsomeip::service_t _service,
36                                                    vsomeip::instance_t _instance,
37                                                    bool _is_available);
38     template<int service_idx, int method_idx> void on_message(
39             const std::shared_ptr<vsomeip::message> &_response);
40     template<int service_idx> void send();
41     template<int service_idx> void run();
42 
43 private:
44     template<int service_idx> void send_messages_sync();
45     template<int service_idx, int method_idx> std::thread start_send_thread_sync();
46     template<int service_idx> void send_messages_async();
47     template<int service_idx, int method_idx> std::thread start_send_thread_async();
48     template<int service_idx> void send_messages_and_dont_wait_for_reply();
49     std::uint32_t get_max_allowed_payload();
50     template<int service_idx> void register_availability_handler();
51     template<int service_idx> void register_message_handler_for_all_service_methods();
52     template<int service_idx, int method_idx> void register_message_handler();
53     template<int service_idx, int method_idx>
54         std::thread start_send_thread();
55     void wait_for_all_senders();
56 
57 private:
58     std::shared_ptr<vsomeip::application> app_;
59     std::shared_ptr<vsomeip::message> request_;
60     bool call_service_sync_;
61     bool wait_for_replies_;
62     std::uint32_t sliding_window_size_;
63 
64     std::array<std::mutex, npdu_test::service_ids.size()> mutexes_;
65     std::array<std::condition_variable, npdu_test::service_ids.size()> conditions_;
66     std::array<bool, npdu_test::service_ids.size()> blocked_;
67     std::array<bool, npdu_test::service_ids.size()> is_available_;
68     const std::uint32_t number_of_messages_to_send_;
69     std::uint32_t number_of_sent_messages_[npdu_test::service_ids.size()];
70     std::array<std::array<std::uint32_t, npdu_test::method_ids[0].size()>,
71             npdu_test::service_ids.size()> number_of_acknowledged_messages_;
72     std::array<std::array<std::mutex, npdu_test::method_ids[0].size()>,
73             npdu_test::service_ids.size()> number_of_acknowledged_messages_mutexes_;
74 
75     std::array<std::uint32_t, npdu_test::service_ids.size()> current_payload_size_;
76 
77     std::array<std::array<bool, npdu_test::method_ids[0].size()>,
78             npdu_test::service_ids.size()> all_msg_acknowledged_;
79     std::array<std::array<std::mutex, npdu_test::method_ids[0].size()>,
80             npdu_test::service_ids.size()> all_msg_acknowledged_mutexes_;
81     std::array<std::array<std::unique_lock<std::mutex>, npdu_test::method_ids[0].size()>,
82             npdu_test::service_ids.size()> all_msg_acknowledged_unique_locks_;
83     std::array<
84             std::array<std::condition_variable,
85                     npdu_test::method_ids[0].size()>,
86             npdu_test::service_ids.size()> all_msg_acknowledged_cvs_;
87     std::array<std::uint32_t, 4> acknowledgements_;
88     std::array<std::array<std::chrono::milliseconds, 4>, 4> applicative_debounce_;
89     std::array<
90             std::array<std::shared_ptr<vsomeip::payload>,
91                     npdu_test::method_ids[0].size()>,
92             npdu_test::service_ids.size()> payloads_;
93     std::array<
94             std::array<std::vector<vsomeip::byte_t>,
95                     npdu_test::method_ids[0].size()>,
96             npdu_test::service_ids.size()> payload_data_;
97     std::array<std::thread, npdu_test::service_ids.size()> senders_;
98     std::mutex finished_mutex_;
99     std::array<bool, npdu_test::service_ids.size()> finished_;
100     std::thread finished_waiter_;
101 };
102 
103 #endif /* NPDUTESTCLIENT_HPP_ */
104