-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
executable file
·190 lines (158 loc) · 5.06 KB
/
main.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#include "ProcCommunicator.h"
#include "ServerProcCommunicator.h"
#include "ClientProcCommunicator.h"
#include <iostream>
#include <thread>
#include <chrono>
#include <mutex>
#ifndef _WIN32
#include <unistd.h>
#endif
#include <future>
#include <cassert>
#include <thread>
#include <chrono>
static const std::string shared_mem_name{"/shmsh1"};
static constexpr size_t MSG_COUNT = 10000;
void backgroundTask1()
{
std::cout << "Background Master_1 starting...\n";
ClientProcCommunicator master(shared_mem_name);
Message request{1, MessageType::HANDSHAKE};
Message *response{nullptr};
int counter = 0;
while (counter < MSG_COUNT)
{
if (!master.sendRequestGetResponse(&request, &response))
printf("failed to get response in time, skipping response.\n");
else
{
assert(response->id == 1);
assert(response->type == 1);
}
// else // if returned true, ptr must be valid
// std::cout << "m 1 =" << response->id << " " << response->type << std::endl;
counter++;
}
std::cout << "Background Master_1 completed.\n";
}
void backgroundTask2()
{
std::cout << "Background Master_2 starting...\n";
ClientProcCommunicator master(shared_mem_name);
Message request{2, MessageType::HANDSHAKE};
Message *response;
int counter = 0;
while (counter < MSG_COUNT)
{
master.sendRequestGetResponse(&request, &response);
// std::cout << "m 2 =" << response.id << std::endl;
counter++;
}
std::cout << "Background Master_2 completed.\n";
}
void backgroundTask3()
{
std::cout << "Background Master_3 starting...\n";
ClientProcCommunicator master(shared_mem_name);
Message request{3, MessageType::HANDSHAKE};
Message *response;
int counter = 0;
while (counter < MSG_COUNT)
{
master.sendRequestGetResponse(&request, &response);
counter++;
}
std::cout << "Background Master_3 completed.\n";
}
void backgroundTask4()
{
std::cout << "Background Master_4 starting...\n";
ClientProcCommunicator master(shared_mem_name);
Message request{4, MessageType::HANDSHAKE};
Message *response;
int counter = 0;
while (counter < MSG_COUNT)
{
master.sendRequestGetResponse(&request, &response);
//std::cout << "m 4 =" << response->id << std::endl;
counter++;
}
std::cout << "Background Master_4 completed.\n";
}
void backgroundTask5()
{
std::cout << "Background Master_5 starting...\n";
ClientProcCommunicator master(shared_mem_name);
Message request{5, MessageType::HANDSHAKE};
Message *response;
int counter = 0;
while (counter < MSG_COUNT)
{
master.sendRequestGetResponse(&request, &response);
// std::cout << "m 5 =" << response->id << std::endl;
counter++;
}
std::cout << "Background Master_5 completed.\n";
}
int main()
{
/*{
// test: one to one
std::cout << "Server starts...\n";
ServerProcCommunicator *slave = new ServerProcCommunicator(shared_mem_name);
Message *res = nullptr;
int counter = 0;
std::thread worker_master1(backgroundTask1);
while (counter < MSG_COUNT * 1)
{
Message msg{777, MessageType::HANDSHAKE_OK};
Message *res = slave->receive();
msg.id = res->id;
slave->send(&msg);
counter++;
}
std::cout << "Server syncing...\n";
if (worker_master1.joinable())
worker_master1.join();
delete slave;
std::cout << "Server has finished work...\n";
}*/
{
// test: one to many
std::cout << "Server starts...\n";
ServerProcCommunicator *slave = new ServerProcCommunicator(shared_mem_name);
Message *res = nullptr;
int counter = 0;
std::thread worker_master1(backgroundTask1);
std::thread worker_master2(backgroundTask2);
std::thread worker_master3(backgroundTask3);
std::thread worker_master4(backgroundTask4);
std::thread worker_master5(backgroundTask5);
while (counter < MSG_COUNT * 5)
{
// std::cout << "Server st1 "<<counter<<std::endl;
Message msg{777, MessageType::HANDSHAKE_OK};
Message *res = slave->receive();
// std::this_thread::sleep_for(std::chrono::milliseconds(7)); //slow response to simulate different calcualations
msg.id = res->id;
slave->send(&msg);
counter++;
// std::cout << "Server st1 "<<counter<<std::endl;
}
std::cout << "Server syncing...\n";
if (worker_master1.joinable())
worker_master1.join();
if (worker_master2.joinable())
worker_master2.join();
if (worker_master3.joinable())
worker_master3.join();
if (worker_master4.joinable())
worker_master4.join();
if (worker_master5.joinable())
worker_master5.join();
delete slave;
std::cout << "Server has finished work...\n";
}
return 0;
}