-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcentral.cpp
351 lines (306 loc) · 11.5 KB
/
central.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
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
#include <iostream>
#include <cstdio>
#include <stdlib.h>
#include <unistd.h>
#include <cstring>
#include <netinet/in.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/wait.h>
#include <vector>
#include <fstream>
#include <sstream>
#include <unordered_map>
#include <unordered_set>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <deque>
#include <algorithm>
#include <tuple>
#include <sys/types.h>
#include <netdb.h>
using namespace std;
#define ERROR -1
#define TCPA_PORT 25061
#define TCPB_PORT 26061
#define UDP_PORT 24061
#define T_PORT 21061
#define S_PORT 22061
#define P_PORT 23061
#define localhost "127.0.0.1"
#define MAXMESSAGE 2048 //max size
#define BACKLOG 10 //pending connections queue
int sockfdA, sockfdB, sockfdUDP;;
struct sockaddr_in TCPaddrA, TCPaddrB;
struct sockaddr_in UDPaddr, UDPclientT, UDPclientS, UDPclientP;
int new_fda, new_fdb;
struct sockaddr_in addrA,addrB; //children sockets
char bufA[MAXMESSAGE], bufB[MAXMESSAGE];
char sendtoT[MAXMESSAGE], recvC[MAXMESSAGE], recvT[MAXMESSAGE], recvS[MAXMESSAGE], recvP[MAXMESSAGE];
char result[20000];
void createTCPsocket(){
sockfdA = socket(AF_INET, SOCK_STREAM, 0);
//create error happens
if(sockfdA == ERROR){
perror("client A socket TCP error");
exit(1);
}
//bind: from Beej’s guide to network programming
memset(&TCPaddrA, 0, sizeof TCPaddrA);
TCPaddrA.sin_family = AF_INET;
TCPaddrA.sin_port = htons(25061);
TCPaddrA.sin_addr.s_addr = inet_addr(localhost);
if(::bind(sockfdA, (struct sockaddr*) &TCPaddrA, sizeof TCPaddrA ) == ERROR){
perror("Client A TCP bind");
exit(1);
}
sockfdB = socket(AF_INET, SOCK_STREAM, 0);
//create error happens
if(sockfdB == ERROR){
perror("client B socket TCP error");
exit(1);
}
//bind: from Beej’s guide to network programming
memset(&TCPaddrB, 0, sizeof TCPaddrB);
TCPaddrB.sin_family = AF_INET;
TCPaddrB.sin_port = htons(26061);
TCPaddrB.sin_addr.s_addr = inet_addr(localhost);
if(::bind(sockfdB, (struct sockaddr*) &TCPaddrB, sizeof TCPaddrB ) == ERROR){
perror("clinet B TCP bind");
exit(1);
}
}
void createUDPsocket(){
sockfdUDP = socket(AF_INET, SOCK_DGRAM, 0);
//create error happens
if(sockfdUDP == ERROR){
perror("Central socket UDP error");
exit(1);
}
//bind: from Beej’s guide to network programming
memset(&UDPaddr, 0, sizeof UDPaddr);
UDPaddr.sin_family = AF_INET;
UDPaddr.sin_port = htons(UDP_PORT);
UDPaddr.sin_addr.s_addr = inet_addr(localhost);
if(::bind(sockfdUDP, (struct sockaddr*) &UDPaddr, sizeof UDPaddr ) == ERROR){
perror("clinet B TCP bind");
exit(1);
}
}
//get_in_addr: from Beej’s guide to network programming
void *get_in_addr(struct sockaddr *sa)
{
if (sa->sa_family == AF_INET) {
return &(((struct sockaddr_in*)sa)->sin_addr);
}
return &(((struct sockaddr_in6*)sa)->sin6_addr);
}
int main(){
createTCPsocket();
createUDPsocket();
// printf("The Central server is up and running."\n);
cout << "The Central server is up and running." << endl;
cout << endl;
if (listen(sockfdA, BACKLOG) == -1) {
perror("listen");
}
if (listen(sockfdB, BACKLOG) == -1) {
perror("listen");
}
while(true){
socklen_t sin_sizeA = sizeof addrA;
socklen_t sin_sizeB = sizeof addrB;
new_fda = accept(sockfdA, (struct sockaddr *) &addrA, &sin_sizeA);
if (new_fda == -1) {
perror("accept A error");
exit(1);
}
memset(bufA,'\0',sizeof bufA);
if (recv(new_fda, bufA, sizeof bufA, 0) == -1) {
perror("client A receive error");
exit(1);
}
// printf("The Central server received input= \"%s\" from the client using TCP over port 25061.", bufA);
cout << "The Central server received input= \"" << bufA << "\" from the client using TCP over port 25061." << endl;
new_fdb = accept(sockfdB, (struct sockaddr *) &addrB, &sin_sizeB);
if (new_fdb == -1) {
perror("accept B error");
exit(1);
}
memset(bufB,'\0',sizeof bufB);
if (recv(new_fdb, bufB, sizeof bufB, 0) == -1) {
perror("client B receive error");
exit(1);
}
// printf("The Central server received input= \"%s\" from the client using TCP over port 25061.", bufB);
string recvfrB;
for(int i = 1; i < strlen(bufB); i++){
if(bufB[i] == '+') recvfrB += ' ';
else recvfrB += bufB[i];
}
cout << "The Central server received input= \"" << recvfrB << "\" from the client using TCP over port 26061." << endl;
// cout << bufB << endl;
//get the number of names from clientB
char name_num = bufB[0];
//send two clients' name to server T
memset(sendtoT,'\0',sizeof sendtoT);
int i = 0, k = 0;
while(i < strlen(bufA)) sendtoT[k++] = bufA[i++];
sendtoT[k++] = '+';
i = 1;
while(bufB[i] != '+') sendtoT[k++] = bufB[i++];
if(name_num == '2'){
sendtoT[k++] = '+';
i++;
while(i < strlen(bufB)) sendtoT[k++] = bufB[i++];
}
// cout << sendtoT << endl;
//"connect" with serverP: NOT a real connect
UDPclientP.sin_family = AF_INET;
UDPclientP.sin_addr.s_addr = inet_addr(localhost);
UDPclientP.sin_port = htons(P_PORT);
socklen_t UDPclientP_len = sizeof UDPclientP;
//"connect" with serverT: NOT a real connect
UDPclientT.sin_family = AF_INET;
UDPclientT.sin_addr.s_addr = inet_addr(localhost);
UDPclientT.sin_port = htons(T_PORT);
//"connect" with serverS: NOT a real connect
UDPclientS.sin_family = AF_INET;
UDPclientS.sin_addr.s_addr = inet_addr(localhost);
UDPclientS.sin_port = htons(S_PORT);
//send two clients name to serverT
if (sendto(sockfdUDP, sendtoT, sizeof sendtoT, 0,
(const struct sockaddr *) &UDPclientT, (socklen_t)sizeof UDPclientT) == -1) {
perror("central send serverT Error");
exit(1);
}
cout << "The Central server sent a request to Backend-Server T." << endl;
//receive the number of datagrams from serverT
socklen_t UDPclientT_len = sizeof UDPclientT;
memset(recvC, '\0', sizeof recvC);
if (recvfrom(sockfdUDP, recvC, sizeof recvC,0,
(struct sockaddr *) &UDPclientT, &UDPclientT_len) == -1) {
perror("Receive From ServerT Error");
exit(1);
}
//send the number of dataframs to serverP
if (sendto(sockfdUDP, recvC, sizeof recvC, 0,
(const struct sockaddr *) &UDPclientP, (socklen_t)sizeof UDPclientP) == -1) {
perror("central send serverP Error");
exit(1);
}
string packs = "";
for(int i = 0; i < strlen(recvC); i++){
packs += recvC[i];
}
int packn = stoi(packs);
//receive edgelist from serverT: small datagrams!
for(int i = 0; i < packn; i++){
memset(recvC,'\0',sizeof recvC);
if (recvfrom(sockfdUDP, recvC, sizeof recvC,0,
(struct sockaddr *) &UDPclientT, &UDPclientT_len) == -1) {
perror("Receive From ServerT Error");
exit(1);
}
// cout << recvC << endl;
//send graph to serverP
if (sendto(sockfdUDP, recvC, sizeof recvC, 0,
(const struct sockaddr *) &UDPclientP, (socklen_t)sizeof UDPclientP) == -1) {
perror("central send serverP Error");
exit(1);
}
}
cout << "The Central server received information from Backend-Server T using UDP over port 24061." << endl;
// cout << recvC << endl;
//send two clients' name to server S
if (sendto(sockfdUDP, sendtoT, sizeof sendtoT, 0,
(const struct sockaddr *) &UDPclientS, (socklen_t)sizeof UDPclientS) == -1) {
perror("central send serverS Error");
exit(1);
}
cout << "The Central server sent a request to Backend-Server S" << endl;
//receive the number of datagrams from serverS
socklen_t UDPclientS_len = sizeof UDPclientS;
memset(recvC, '\0', sizeof recvC);
if (recvfrom(sockfdUDP, recvC, sizeof recvC,0,
(struct sockaddr *) &UDPclientS, &UDPclientS_len) == -1) {
perror("Receive From ServerT Error");
exit(1);
}
//send the number of dataframs to serverP
if (sendto(sockfdUDP, recvC, sizeof recvC, 0,
(const struct sockaddr *) &UDPclientP, (socklen_t)sizeof UDPclientP) == -1) {
perror("central send serverP Error");
exit(1);
}
packs = "";
for(int i = 0; i < strlen(recvC); i++){
packs += recvC[i];
}
packn = stoi(packs);
//receive scores from serverS: small datagrams!
for(int i = 0; i < packn; i++){
memset(recvC,'\0',sizeof recvC);
if (recvfrom(sockfdUDP, recvC, sizeof recvC,0,
(struct sockaddr *) &UDPclientS, &UDPclientS_len) == -1) {
perror("Receive From ServerS Error");
exit(1);
}
// cout << recvC << endl;
//send scores to serverP
if (sendto(sockfdUDP, recvC, sizeof recvC, 0,
(const struct sockaddr *) &UDPclientP, (socklen_t)sizeof UDPclientP) == -1) {
perror("central send serverP Error");
exit(1);
}
}
cout << "The Central server received information from Backend-Server S using UDP over port 24061." << endl;
// cout << recvC << endl;
//send two clients name to serverP
if (sendto(sockfdUDP, sendtoT, sizeof sendtoT, 0,
(const struct sockaddr *) &UDPclientP, (socklen_t)sizeof UDPclientP) == -1) {
perror("central send serverS Error");
exit(1);
}
cout << "The Central server sent a processing request to Backend-Server P" << endl;
//receive the numer of result's datagram from serverP
memset(recvC,'\0',sizeof recvC);
if (recvfrom(sockfdUDP, recvC, sizeof recvC,0,
(struct sockaddr *) &UDPclientP, &UDPclientP_len) == -1) {
perror("Receive From ServerP Error");
exit(1);
}
packs = "";
for(int i = 0; i < strlen(recvC); i++){
packs += recvC[i];
}
packn = stoi(packs);
//receive edgelist from serverT: small datagrams!
int pt = 0;
memset(result, '\0', sizeof result);
for(int i = 0; i < packn; i++){
memset(recvC,'\0',sizeof recvC);
if (recvfrom(sockfdUDP, recvC, sizeof recvC,0,
(struct sockaddr *) &UDPclientP, &UDPclientP_len) == -1) {
perror("Receive From ServerP Error");
exit(1);
}
for(int i = 0; i < strlen(recvC); i++) result[pt++] = recvC[i];
}
cout << "The Central server received information from Backend-Server P using UDP over port 24061." << endl;
// cout << result << endl;
if (send(new_fda, result, sizeof result, 0) == -1) {
perror("send to client A error");
exit(1);
}
cout << "The Central server sent the results to client A." << endl;
if (send(new_fdb, result, sizeof result, 0) == -1) {
perror("send to client B error");
exit(1);
}
cout << "The Central server sent the results to client B." << endl;
}
return 0;
}