-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServers.h
261 lines (224 loc) · 6.58 KB
/
Servers.h
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
#ifndef NEWPROJ_SERVERS_H
#define NEWPROJ_SERVERS_H
#include "ClientHandler.h"
#include <thread>
#include <netinet/in.h>
#include <vector>
#include <strings.h>
#include <unistd.h>
#include <iostream>
#include <cstring>
#include <queue>
#include <list>
#include "Server.h"
#include <string.h>
#include <stdio.h>
using std::string;
using std::cout;
using std::endl;
namespace server_side {
template<class Problem, class Solution>
class Server {
protected:
int socketId;
bool shouldStop;
public:
Server() {}
virtual void start(int port, ClientHandler<Problem, Solution> *clientHandler) = 0;
virtual void stop() = 0;
};
}
using namespace server_side;
using std::cout;
using std::endl;
using std::thread;
using std::queue;
using std::list;
using std::string;
bool serialStop;
bool parallelStop;
template<class P, class S>
void serial(ClientHandler<P, S> *clientHandler, int new_sock, bool *isTimeOut) {
cout << "start" << endl;
string nextBuffer, connectedBuffer;
char buffer[4096];
*isTimeOut = false;
try {
while (!serialStop) {
bzero(buffer, 256);
read(new_sock, buffer, 255);
connectedBuffer += buffer;
string strBuff(connectedBuffer);
if (strBuff.find("end") != string::npos) {
string answer;
clientHandler->handleClient(connectedBuffer, answer);
const char *cstr = answer.c_str();
write(new_sock, cstr, answer.size());
break;
}
}
} catch (...) {
}
close(new_sock);
cout << "stop" << endl;
}
template<class P, class S>
void parallel(ClientHandler<P, S> *clientHandler, int new_sock, bool *isRun) {
cout << "start" << endl;
string nextBuffer, connectedBuffer;
char buffer[4096];
string strBuff;
*isRun = true;
try {
while (!parallelStop) {
bzero(buffer, 256);
read(new_sock, buffer, 255);
connectedBuffer += buffer;
string strBuff(connectedBuffer);
if (strBuff.find("end") != string::npos) {
string answer;
clientHandler->handleClient(connectedBuffer, answer);
const char *cstr = answer.c_str();
write(new_sock, cstr, answer.size());
break;
}
}
} catch (...) {
cout << "catch" << endl;
}
close(new_sock);
*isRun = false;
cout << "stop" << endl;
}
template<class P, class S>
void openSerial(ClientHandler<P, S> *clientHandler, int port) {
int s = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in serv;
serv.sin_addr.s_addr = INADDR_ANY;
serv.sin_port = htons(port);
serv.sin_family = AF_INET;
bool *isTimeOut = new bool;
*isTimeOut = false;
timeval timeout;
timeout.tv_sec = 1;
timeout.tv_usec = 0;
bind(s, (sockaddr *) &serv, sizeof(serv));
setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, (char *) &timeout, sizeof(timeout));
while (!serialStop) {
try {
int new_sock;
listen(s, 20000);
struct sockaddr_in client;
socklen_t clilen = sizeof(client);
new_sock = accept(s, (struct sockaddr *) &client, &clilen);
if (*isTimeOut) {
if (new_sock < 0) {
if (errno == EWOULDBLOCK) {
cout << "timeout of Serial!" << endl;
break;
} else {
perror("other error");
break;
}
}
}
if (new_sock >= 0) {
thread *t = new thread(serial, clientHandler, new_sock, isTimeOut);
t->join();
delete t;
*isTimeOut = true;
}
} catch (...) {
cout << "connection with client stopped" << endl;
}
}
delete isTimeOut;
}
template<class P, class S>
void openParallel(ClientHandler<P, S> *clientHandler, int port) {
int s = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in serv;
serv.sin_addr.s_addr = INADDR_ANY;
serv.sin_port = htons(port);
serv.sin_family = AF_INET;
queue<thread *> threads;
list<bool *> isRunning;
bool isFirst = true;
bind(s, (sockaddr *) &serv, sizeof(serv));
listen(s, 20000);
struct sockaddr_in client;
socklen_t clilen = sizeof(client);
timeval timeout;
timeout.tv_sec = 1;
timeout.tv_usec = 0;
while (!parallelStop) {
try {
int new_sock;
setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, (char *) &timeout, sizeof(timeout));
new_sock = accept(s, (struct sockaddr *) &client, &clilen);
bool isTimeOut = !isFirst;
for (auto &r : isRunning) {
if (*r) {
isTimeOut = false;
break;
}
}
if (isTimeOut) {
if (new_sock < 0) {
if (errno == EWOULDBLOCK) {
cout << "paralel timeout!" << endl;
break;
} else {
perror("other error");
break;
}
}
}
if (new_sock >= 0) {
bool *isRun = new bool;
isRunning.push_back(isRun);
threads.push(new thread(parallel<P, S>, clientHandler, new_sock, isRun));
isFirst = false;
}
} catch (...) {
cout << "connection with client stopped" << endl;
}
}
while (!threads.empty()) {
threads.front()->join();
delete threads.front();
threads.pop();
}
for (auto &i : isRunning) {
delete i;
}
}
template<class P, class S>
class MySerialServer : public Server<P, S> {
public:
MySerialServer() = default;
void stop() {
serialStop = true;
}
void start(int port, ClientHandler<P, S> *clientHandler) {
serialStop = false;
thread *t = new thread(openSerial, clientHandler, port);
t->join();
delete t;
}
};
template<class P, class S>
class MyParallelServer : public server_side::Server<P, S> {
public:
MyParallelServer() = default;
void stop() {
// parallelStop = true;
}
void start(int port, ClientHandler<P, S> *clientHandler) {
parallelStop = false;
thread *t = new thread(openParallel<P, S>, clientHandler, port);
t->join();
delete t;
}
};
#endif //NEWPROJ_SERVERS_H