summaryrefslogtreecommitdiff
path: root/test/internal_routing_disabled_acceptance_test/client.cpp
blob: ff7be781c0deccf2ac389f9e4c47a05e228f879e (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
#include "client.hpp"

#include <chrono>
#include <iostream>
#include <thread>

#include <vsomeip/constants.hpp>
#include <vsomeip/enumeration_types.hpp>
#include <vsomeip/message.hpp>
#include <vsomeip/payload.hpp>
#include <vsomeip/primitive_types.hpp>
#include <vsomeip/runtime.hpp>

#include "config.hpp"

client::client() : applet{"client"}, counter_event_received{}, counter_method_request{}, counter_method_response{}
{
    this->application->register_message_handler(
        config::SERVICE_ID,
        config::INSTANCE_ID,
        vsomeip_v3::ANY_METHOD,
        [this](const std::shared_ptr<vsomeip_v3::message>& message){
            std::shared_ptr runtime = vsomeip_v3::runtime::get();
            std::shared_ptr payload = message->get_payload();

            switch(message->get_message_type())
            {
            case vsomeip_v3::message_type_e::MT_RESPONSE:
                std::cout
                    << "received:\n"
                    << "\tservice:  " << std::hex << message->get_service() << '\n'
                    << "\tinstance: " << std::hex << message->get_instance() << '\n'
                    << "\tmethod:   " << std::hex << message->get_method() << '\n'
                    << "\tpayload:  " << payload->get_data() << '\n';
                this->counter_method_response++;
                break;

            case vsomeip_v3::message_type_e::MT_NOTIFICATION:
                std::cout << "GOT NOTIFICATION\n";
                this->counter_event_received++;
                [[fallthrough]];

            default:
                std::cout << "unhandled message type: " << unsigned(message->get_message_type()) << '\n';
            }
        }
    );

    this->application->register_availability_handler(
        config::SERVICE_ID,
        config::INSTANCE_ID,
        [this](vsomeip_v3::service_t service, vsomeip_v3::instance_t instance, bool available){
            std::cout
                << __func__ << '('
                << std::hex << service << ", "
                << std::hex << instance << ", "
                << std::boolalpha << available << ")\n";

            if(service != config::SERVICE_ID)
                return;
            if(instance != config::INSTANCE_ID)
                return;
            if(!available)
                return;

            std::shared_ptr runtime = vsomeip_v3::runtime::get();

            std::shared_ptr payload = runtime->create_payload();
            constexpr vsomeip_v3::byte_t str[]{"hello world"};
            payload->set_data(str, sizeof(str));

            std::shared_ptr request = runtime->create_request();
            request->set_service(config::SERVICE_ID);
            request->set_instance(config::INSTANCE_ID);
            request->set_method(config::METHOD_ID);
            request->set_payload(payload);

            for(int i = 0; i < 10; i++)
            {
                std::cout << "sending: " << str << '\n';
                this->application->send(request);
                this->counter_method_request++;

                using namespace std::chrono_literals;
                std::this_thread::sleep_for(1s);
            }
        }
    );

    this->application->request_event(
        config::SERVICE_ID,
        config::INSTANCE_ID,
        config::EVENT_ID,
        {config::EVENTGROUP_ID},
        vsomeip_v3::event_type_e::ET_FIELD,
        vsomeip_v3::reliability_type_e::RT_UNRELIABLE
    );

    this->application->subscribe(
        config::SERVICE_ID,
        config::INSTANCE_ID,
        config::EVENTGROUP_ID
    );
}

client::~client()
{
    this->application->unsubscribe(
        config::SERVICE_ID,
        config::INSTANCE_ID,
        config::EVENTGROUP_ID
    );

    this->application->release_event(
        config::SERVICE_ID,
        config::INSTANCE_ID,
        config::EVENT_ID
    );

    this->application->release_service(
        config::SERVICE_ID,
        config::INSTANCE_ID
    );
}

std::size_t client::get_event_count() noexcept
{
    return this->counter_event_received;
}

std::size_t client::get_method_request_count() noexcept
{
    return this->counter_method_request;
}

std::size_t client::get_method_response_count() noexcept
{
    return this->counter_method_response;
}

void client::on_state_registered()
{
    this->application->request_service(
        config::SERVICE_ID,
        config::INSTANCE_ID
    );
}

void client::on_state_deregistered()
{
    std::cout << "Client is deregistered!!! Probably could not be registered!!!\n";
}