-
Notifications
You must be signed in to change notification settings - Fork 0
/
serverP.cpp
456 lines (408 loc) · 12.7 KB
/
serverP.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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
#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>
#include <fstream>
#include <cassert>
#include <cmath>
using namespace std;
#define ERROR -1
#define TCPA_PORT 25061
#define TCPB_PORT 26061
#define UDP_PORT 24061
#define P_PORT 23061
#define localhost "127.0.0.1"
#define MAXMESSAGE 1000 //max size
#define BACKLOG 10 //pending connections queue
#define inf 1e8
typedef pair<string, double> PSD;
typedef pair<double, string> PDS;
int sockfdUDP;
struct sockaddr_in UDPaddr, UDPclientC;
char recvP[MAXMESSAGE], sendP[1000000];
char recvC[1000000], sendC[MAXMESSAGE];
// unordered_map<string, int> name_to_num;
unordered_map<string, vector<PSD> > edges;
unordered_map<string, int> scores;
unordered_map<string, double> dist;
unordered_map<string, string> prevto;
unordered_map<string, bool> state;
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
memset(&UDPaddr, 0, sizeof UDPaddr);
UDPaddr.sin_family = AF_INET;
UDPaddr.sin_port = htons(P_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);
}
double calclength(string a, string b){
if((!scores.count(a)) || (!scores.count(b))) return 1e8;
int sa = scores[a], sb = scores[b];
return fabs(sa-sb) / (sa+sb);
}
void calcpath(string a, string b){
for(auto& p : scores){
auto& k = p.first;
auto& v = p.second;
dist[k] = inf;
state[k] = false;
}
dist[a] = 0;
prevto[a] = "end";
priority_queue<PDS, vector<PDS>, greater<PDS> > pq;
pq.push(PDS(0, a));
while(pq.size()){
auto t = pq.top();
pq.pop();
double dis = t.first;
string ver = t.second;
// if(ver == b) break;
if(!state[ver]){
state[ver] = true;
// cout << "ver = " << ver << endl;
for(auto p : edges[ver]){
string to = p.first;
double lg = p.second;
if(dist[to] > dist[ver] + lg){
dist[to] = dist[ver] + lg;
prevto[to] = ver;
pq.push(PDS(dist[to], to));
}
}
}
}
int k = 3;
if(dist[b] > inf / 2){
sendP[0] = '@';
for(int i = 0; i < a.size(); i++) sendP[k++] = a[i];
sendP[k++] = '+';
for(int i = 0; i < b.size(); i++) sendP[k++] = b[i];
sendP[k++] = '+';
}
else{
auto distance_o = dist[b];
char distance[100];
memset(distance, '\0', sizeof distance);
sprintf(distance, "%.2f", distance_o);
int i = 0;
while(i < strlen(distance)){
sendP[k++] = distance[i++];
}
sendP[k++] = '*';
string start = b;
while(start != "end"){
int j = 0;
while(j < start.size()) sendP[k++] = start[j++];
sendP[k++] = '+';
start = prevto[start];
}
}
sendP[k++] = '#';
// cout << "sendp = " << sendP << endl;
}
int main(){
createUDPsocket();
cout << "The ServerP is up and running using UDP on port 23061." << endl;
while(true){
UDPclientC.sin_family = AF_INET;
UDPclientC.sin_addr.s_addr = inet_addr(localhost);
UDPclientC.sin_port = htons(UDP_PORT);
socklen_t UDPclientC_len = sizeof UDPclientC;
//receive the number of datagrams from central: edges
memset(recvP,'\0',sizeof recvP);
if (recvfrom(sockfdUDP,recvP, sizeof recvP,0,
(struct sockaddr *) &UDPclientC, &UDPclientC_len) == -1) {
perror("serverP Receive Error");
exit(1);
}
string packs = "";
for(int i = 0; i < strlen(recvP); i++){
packs += recvP[i];
}
int packn = stoi(packs);
//receive graph from central
int pt = 0;
memset(recvC, '\0', sizeof recvC);
for(int i = 0; i < packn; i++){
memset(recvP,'\0',sizeof recvP);
if (recvfrom(sockfdUDP,recvP, sizeof recvP,0,
(struct sockaddr *) &UDPclientC, &UDPclientC_len) == -1) {
perror("serverP Receive Error");
exit(1);
}
//copy small datagrams into a complete datagram
for(int j = 0; j < strlen(recvP); j++) recvC[pt++] = recvP[j];
// cout << recvP << endl;
}
//build the graph
int i = 0;
string a,b;
bool second = false;
while(i < strlen(recvC)){
if(second){
if(recvC[i] == '*'){
edges[a].push_back(PSD(b, inf));
edges[b].push_back(PSD(a, inf));
second = false;
a = "";
b = "";
i++;
}
else{
b += recvC[i++];
}
}
else{
if(recvC[i] == '+'){
second = true;
i++;
}
else{
a += recvC[i++];
}
}
}
for(auto& p : edges){
auto& k = p.first;
auto& v = p.second;
sort(v.begin(), v.end());
v.erase(unique(v.begin(), v.end()), v.end());
}
//receive the number of datagrams from central: scores
memset(recvP,'\0',sizeof recvP);
if (recvfrom(sockfdUDP,recvP, sizeof recvP,0,
(struct sockaddr *) &UDPclientC, &UDPclientC_len) == -1) {
perror("serverP Receive Error");
exit(1);
}
packs = "";
for(int i = 0; i < strlen(recvP); i++){
packs += recvP[i];
}
packn = stoi(packs);
//receive scores from central
pt = 0;
memset(recvC, '\0', sizeof recvC);
for(int i = 0; i < packn; i++){
memset(recvP,'\0',sizeof recvP);
if (recvfrom(sockfdUDP,recvP, sizeof recvP,0,
(struct sockaddr *) &UDPclientC, &UDPclientC_len) == -1) {
perror("serverP Receive Error");
exit(1);
}
//copy small datagrams into a complete datagram
for(int j = 0; j < strlen(recvP); j++) recvC[pt++] = recvP[j];
// cout << recvP << endl;
}
//store all the scores
i = 0;
a = "";
b = "";
second = false;
while(i < strlen(recvC)){
if(second){
if(recvC[i] == '*'){
scores[a] = stoi(b);
i++;
second = false;
a = "";
b = "";
}
else{
b += recvC[i++];
}
}else{
if(recvC[i] == '+'){
second = true;
i ++;
}
else{
a += recvC[i++];
}
}
}
//calculate "length" of each edge
for(auto& p : edges){
auto& k = p.first;
auto& v = p.second;
a = k;
for(auto& p: v){
b = p.first;
p.second = calclength(a,b);
}
}
//receive names from central
memset(recvP,'\0',sizeof recvP);
if (recvfrom(sockfdUDP,recvP, sizeof recvP,0,
(struct sockaddr *) &UDPclientC, &UDPclientC_len) == -1) {
perror("serverP Receive Error");
exit(1);
}
// cout << recvP << endl;
cout << "The ServerP received the topology and score information." << endl;
i = 0;
a = "";
b = "";
string c = "";
second = false;
bool third = false;
while(i < strlen(recvP)){
if(third){
c += recvP[i++];
}
else if(second){
if(recvP[i] == '+'){
third = true;
i++;
}
else b += recvP[i++];
}
else{
if(recvP[i] == '+'){
second = true;
i++;
}
else a += recvP[i++];
}
}
dist[b] = inf;
state[b] = false;
if(c.size()){
dist[c] = inf;
state[c] = false;
}
//'$' == good, '@' == bad
//'Y' == two paths, 'N' = one paths;
memset(sendP,'\0',sizeof sendP);
bool two_path = c.size()? true : false;
bool A,B,C;
A = scores.count(a);
B = scores.count(b);
C = scores.count(c);
if(!A){
sendP[0] = '@';
sendP[1] = two_path? 'Y' : 'N';
sendP[2] = '@';
int k = 3;
for(int i = 0; i < a.size(); i++) sendP[k++] = a[i];
sendP[k++] = '+';
for(int i = 0; i < b.size(); i++) sendP[k++] = b[i];
sendP[k++] = '+';
sendP[k++] = '#';
if(two_path){
for(int i = 0; i < a.size(); i++) sendP[k++] = a[i];
sendP[k++] = '+';
for(int i = 0; i < c.size(); i++) sendP[k++] = c[i];
sendP[k++] = '+';
}
}
else{
sendP[0] = '$';
sendP[1] = two_path? 'Y' : 'N';
sendP[2] = '$';
calcpath(a,b);
if(two_path){
int k = 0;
while(sendP[k] != '#') k++;
k++;
sendP[1] = 'Y';
if(dist[c] > inf / 2){
sendP[2] = '@';
for(int i = 0; i < a.size(); i++) sendP[k++] = a[i];
sendP[k++] = '+';
for(int i = 0; i < c.size(); i++) sendP[k++] = c[i];
sendP[k++] = '+';
}
else{
auto distance_o = dist[c];
char distance[100];
memset(distance, '\0', sizeof distance);
sprintf(distance, "%.2f", distance_o);
int i = 0;
while(i < strlen(distance)){
sendP[k++] = distance[i++];
}
sendP[k++] = '*';
string start = c;
while(start != "end"){
int j = 0;
while(j < start.size()) sendP[k++] = start[j++];
sendP[k++] = '+';
start = prevto[start];
}
}
}
}
// cout << "a == " << a << " c == " << c << endl;
// cout << "dist[c] == " << dist[c] << endl;
//send result to central
// cout << sendP << endl;
int sendsz = strlen(sendP);
int sendnum = (sendsz + 899) / 900;
char length[100];
memset(length, '\0', sizeof length);
string size_P = to_string(sendnum);
strcpy(length, size_P.c_str());
//send the number of datagrams to central
if (sendto(sockfdUDP, length, sizeof length, 0,
(const struct sockaddr *) &UDPclientC, (socklen_t)sizeof UDPclientC) == -1) {
perror("ServerP Response Error");
exit(1);
}
//send each small datagram to central
pt = 0;
for(int i = 0; i < sendnum; i++){
//get each small datagram
memset(sendC, '\0', sizeof sendC);
for(int j = 0; j < 900; j++){
sendC[j] = sendP[pt++];
if(pt == sendsz) break;
}
//send datagram to cenral
if (sendto(sockfdUDP, sendC, sizeof sendC, 0,
(const struct sockaddr *) &UDPclientC, (socklen_t)sizeof UDPclientC) == -1) {
perror("ServerS Response Error");
exit(1);
}
}
cout << "The ServerP finished sending the results to the Central." << endl;
}
close(sockfdUDP);
return 0;
}