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

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

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

#include "config.hpp"

server::server() : applet{"server"}, counter_event_sent{}, counter_method_request{}, counter_method_response{}
{
    this->application->register_message_handler(
        config::SERVICE_ID,
        config::INSTANCE_ID,
        config::METHOD_ID,
        [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_REQUEST:
                std::cout << "GOT REQUEST\n";
                this->counter_method_request++;
                {
                    std::shared_ptr response = runtime->create_response(message);
                    response->set_payload(payload);

                    this->application->send(response);
                    this->counter_method_response++;

                    this->application->notify(
                        config::SERVICE_ID,
                        config::INSTANCE_ID,
                        config::EVENT_ID,
                        payload,
                        true
                    );
                    this->counter_event_sent++;
                }
                break;

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

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

    std::thread{
        [this]{
            using namespace std::chrono_literals;
            std::this_thread::sleep_for(1s);

            std::shared_ptr runtime = vsomeip_v3::runtime::get();
            std::shared_ptr payload = runtime->create_payload();
            for(int i = 0; i < 10; i++)
            {
                int j = i | 0x30;
                payload->set_data(reinterpret_cast<vsomeip_v3::byte_t*>(&j), sizeof(j));
                this->application->notify(
                    config::SERVICE_ID,
                    config::INSTANCE_ID,
                    config::EVENT_ID,
                    payload,
                    true
                );
                this->counter_event_sent++;

                std::this_thread::sleep_for(1s);
            }
        }
    }.detach();
}

server::~server()
{
    this->application->stop_offer_event(
        config::SERVICE_ID,
        config::INSTANCE_ID,
        config::EVENT_ID
    );

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

std::size_t server::get_event_count() noexcept
{
    return this->counter_event_sent;
}

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

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

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

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