forked from cfanatic/vsomeip-fuzzing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequest.cpp
151 lines (134 loc) · 4.86 KB
/
request.cpp
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
#include <csignal>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <condition_variable>
#include <thread>
#include <vsomeip/vsomeip.hpp>
#include <vsomeip/internal/logger.hpp>
#define SERVICE_ID 0x1234
#define SERVICE_METHOD_ID 0x0421
#define SERVICE_INSTANCE_ID 0x5678
class Request
{
public:
Request() : app_(vsomeip::runtime::get()->create_application("!!CLIENT!!"))
{
}
~Request()
{
if (message_loop_.joinable() == true)
{
message_loop_.join();
VSOMEIP_INFO << "--REQUEST-- Message loop joined";
}
}
void init()
{
app_->init();
app_->register_availability_handler(Request::service_id__,
Request::service_instance_id__,
std::bind(&Request::on_availability_cbk,
this,
std::placeholders::_1,
std::placeholders::_2,
std::placeholders::_3));
app_->register_state_handler(std::bind(&Request::on_state_cbk,
this,
std::placeholders::_1));
app_->register_message_handler(Request::service_id__,
Request::service_instance_id__,
Request::service_method_id__,
std::bind(&Request::on_message_cbk,
this,
std::placeholders::_1));
}
void start()
{
app_->start();
}
void stop()
{
stop_ = true;
app_->stop();
}
void on_state_cbk(vsomeip::state_type_e _state)
{
if (_state == vsomeip::state_type_e::ST_REGISTERED)
{
app_->request_service(Request::service_id__, Request::service_instance_id__);
}
}
void on_availability_cbk(vsomeip::service_t _service, vsomeip::instance_t _instance, bool _is_available)
{
if (_service == Request::service_id__ && _instance == Request::service_instance_id__)
{
VSOMEIP_INFO << "--REQUEST-- Service ["
<< std::setw(4) << std::setfill('0') << std::hex << _service << "." << _instance
<< "] is "
<< (_is_available ? "available." : "NOT available.");
if (_is_available == true)
{
message_loop_ = std::thread(&Request::message_loop, this);
}
}
}
void on_message_cbk(const std::shared_ptr<vsomeip::message> &_response)
{
std::string str_payload;
str_payload.append(reinterpret_cast<const char *>(_response->get_payload()->get_data()), 0, _response->get_payload()->get_length());
VSOMEIP_INFO << "--REQUEST-- Received response with Service/Session ["
<< std::setw(4) << std::setfill('0') << std::hex << _response->get_service() << "/"
<< std::setw(4) << std::setfill('0') << std::hex << _response->get_session() << "] "
<< str_payload;
}
void send_message()
{
std::shared_ptr<vsomeip::message> request = vsomeip::runtime::get()->create_request();
request->set_service(Request::service_id__);
request->set_instance(Request::service_instance_id__);
request->set_method(Request::service_method_id__);
std::shared_ptr<vsomeip::payload> its_payload = vsomeip::runtime::get()->create_payload();
std::string str("Hello Service!");
std::vector<vsomeip::byte_t> its_payload_data(std::begin(str), std::end(str));
its_payload->set_data(its_payload_data);
request->set_payload(its_payload);
app_->send(request);
}
void message_loop()
{
char chr;
while (stop_ == false && std::cin.get(chr))
{
if (chr == 's')
{
std::thread sender(&Request::send_message, this);
sender.detach();
}
}
}
private:
std::shared_ptr<vsomeip::application> app_;
std::thread message_loop_;
bool stop_ = false;
static const vsomeip::service_t service_id__ = SERVICE_ID;
static const vsomeip::method_t service_method_id__ = SERVICE_METHOD_ID;
static const vsomeip::instance_t service_instance_id__ = SERVICE_INSTANCE_ID;
};
Request *req_ptr(nullptr);
void terminate(int _signal)
{
if (req_ptr != nullptr && (_signal == SIGINT || _signal == SIGTERM))
{
req_ptr->stop();
}
}
int main()
{
Request req;
req_ptr = &req;
signal(SIGINT, terminate);
signal(SIGTERM, terminate);
req.init();
req.start();
}