-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaws.cpp
More file actions
354 lines (321 loc) · 10.2 KB
/
Copy pathaws.cpp
File metadata and controls
354 lines (321 loc) · 10.2 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <fstream>
#include <iostream>
#include <string>
#include <arpa/inet.h>
#include <sys/wait.h>
#include <signal.h>
#include <sstream>
#include <vector>
using namespace std;
#define AWS_UDP_PORT 23472
#define AWS_TCP_PORT 24472
#define SERVERA_PORT 21472
#define SERVERB_PORT 22472
#define MAXBUFLEN 10000
#define BACKLOG 10
//cite from beej
int sock_tcp_fd, new_tcp_fd; // listen on sock_tcp_fd, new connection on new_tcp_fd
int sock_udp_fd; // listen on sock_udp_fd
// int sock_udp_toA_fd;
struct addrinfo hints_TCP, hints_UDP, *servinfo_TCP, *servinfo_UDP, *p;
struct sockaddr_storage their_addr; // connector's address information
struct addrinfo *UDP_TO_INFO; // UDP info for serverA and serverB
// socket need below parameters
socklen_t addr_len;
struct sigaction sa;
int yes = 1;
char s[INET6_ADDRSTRLEN];
int rv;
int numbytes;
char buf[MAXBUFLEN];
// format the message from serverA and serverB
vector<vector<string>> sendToABuf;
stringstream sendToA;
int initialTCPServer();
int initialUDPServer();
int connectWithServerA(string parameters);
int connectWithServerB(string size);
// For socket
void sigchld_handler(int s)
{
// waitpid() might overwrite errno, so we save and restore it: int saved_errno = errno;
int saved_errno = errno;
while (waitpid(-1, NULL, WNOHANG) > 0)
;
errno = saved_errno;
}
int main(void)
{
int status;
if ((status = initialTCPServer()) != 0)
{
return status;
}
printf("The AWS is up and running.\n");
// if ((status = initialUDPServer()) != 0)
// {
// printf("server: UDP start failed!\n");
// return status;
// }
while (true)
{
addr_len = sizeof their_addr;
new_tcp_fd = accept(sock_tcp_fd, (struct sockaddr *)&their_addr, &addr_len);
if (new_tcp_fd == -1)
{
perror("accept");
continue;
}
// printf("server: got connection from %s\n", s);
if (!fork())
{
close(sock_tcp_fd); // child doesn't need this
if ((status = initialUDPServer()) != 0)
{
printf("server: UDP start failed!\n");
return status;
}
// receive data from client
if ((numbytes = recv(new_tcp_fd, buf, MAXBUFLEN - 1, 0)) == -1)
{
perror("recv");
exit(1);
}
buf[numbytes] = '\0';
// parse the data
char mapID = 0;
int vertex = 0;
string size;
int index = 0;
string input(buf);
index = input.find(" ", 2);
mapID = input.at(0); // One char mapID
vertex = stoi(input.substr(2, index-2)); // Only one digit because vertex number is no more than 10
size = input.substr(index + 1);
printf("The AWS has received map ID %c, start vertex %d and file size %s from the client using TCP over port %d\n", mapID, vertex, size.c_str(), AWS_TCP_PORT);
connectWithServerA(input);
char fromServerB[MAXBUFLEN];
connectWithServerB(size);
sendToA.str(std::string()); // newTCP, newOutput!
// parse the buffer
for (auto it = sendToABuf.begin(); it != sendToABuf.end(); ++it)
{
for (auto it2 = it->begin(); it2 != it->end(); ++it2)
{
sendToA << *it2;
}
sendToA << "\n";
}
// send data to client
if ((send(new_tcp_fd, sendToA.str().c_str(), MAXBUFLEN, 0) == -1))
{
perror("recv");
exit(1);
}
printf("The AWS has sent calculated delay to client using TCP over port %d.\n", AWS_TCP_PORT);
// close socket
close(new_tcp_fd);
close(sock_udp_fd);
exit(0);
}
close(new_tcp_fd); // parent doesn't need this
}
}
// cite from beej
int initialTCPServer()
{
memset(&hints_TCP, 0, sizeof hints_TCP);
hints_TCP.ai_family = AF_UNSPEC;
hints_TCP.ai_socktype = SOCK_STREAM; // TCP
hints_TCP.ai_flags = AI_PASSIVE; // my ip
if ((rv = getaddrinfo("127.0.0.1", to_string(AWS_TCP_PORT).c_str(), &hints_TCP, &servinfo_TCP)) != 0)
{
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
return 1;
}
for (p = servinfo_TCP; p != NULL; p = p->ai_next)
{
if ((sock_tcp_fd = socket(p->ai_family, p->ai_socktype,
p->ai_protocol)) == -1)
{
perror("server: socket");
continue;
}
if (setsockopt(sock_tcp_fd, SOL_SOCKET, SO_REUSEADDR, &yes,
sizeof(int)) == -1)
{
perror("setsockopt");
exit(1);
}
if (::bind(sock_tcp_fd, p->ai_addr, p->ai_addrlen) == -1)
{
close(sock_tcp_fd);
perror("server: bind");
continue;
}
break;
}
if (p == NULL)
{
fprintf(stderr, "server: failed to bind\n");
return 2;
}
freeaddrinfo(servinfo_TCP);
if (listen(sock_tcp_fd, BACKLOG) == -1)
{
perror("listen");
exit(1);
}
sa.sa_handler = sigchld_handler;
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_RESTART;
if (sigaction(SIGCHLD, &sa, NULL) == -1)
{
perror("sigaction");
exit(1);
}
return 0;
}
int initialUDPServer()
{
memset(&hints_UDP, 0, sizeof hints_UDP);
hints_UDP.ai_family = AF_UNSPEC; // set to AF_INET to force IPv4
hints_UDP.ai_socktype = SOCK_DGRAM;
hints_UDP.ai_flags = AI_PASSIVE; // use my IP
if ((rv = getaddrinfo("127.0.0.1", to_string(AWS_UDP_PORT).c_str(), &hints_UDP, &servinfo_UDP)) != 0)
{
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
return 1;
}
// loop through all the results and bind to the first we can
for (p = servinfo_UDP; p != NULL; p = p->ai_next)
{
if ((sock_udp_fd = socket(p->ai_family, p->ai_socktype,
p->ai_protocol)) == -1)
{
perror("aws listener: socket");
continue;
}
// int enable = 1;
// if (setsockopt(sock_udp_fd, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(int)) < 0)
// perror("setsockopt(SO_REUSEADDR) failed");
if (::bind(sock_udp_fd, p->ai_addr, p->ai_addrlen) == -1)
{
close(sock_udp_fd);
perror("aws listener: bind");
continue;
}
break;
}
if (p == NULL)
{
fprintf(stderr, "aws listener: failed to bind socket\n");
return 2;
}
freeaddrinfo(servinfo_UDP);
return 0;
}
int connectWithServerA(string parameters)
{
sendToABuf.clear(); // new buf for sendToA
addr_len = sizeof their_addr;
// get serverA address
int status;
if ((status = getaddrinfo("127.0.0.1", to_string(SERVERA_PORT).c_str(), &hints_UDP, &UDP_TO_INFO)) != 0)
{
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(status));
return 2;
}
// send to serverA
if ((numbytes = sendto(sock_udp_fd, parameters.c_str(), MAXBUFLEN, 0,
UDP_TO_INFO->ai_addr, UDP_TO_INFO->ai_addrlen)) == -1)
{
perror("talker aws: sendto");
exit(1);
}
printf("The AWS has sent map ID and starting vertex to server A using UDP over port %d\n", AWS_UDP_PORT);
// get result from serverA
if ((numbytes = recvfrom(sock_udp_fd, buf, MAXBUFLEN - 1, 0,
(struct sockaddr *)&their_addr, &addr_len)) == -1)
{
perror("recvfrom");
exit(1);
}
// output the result
// I use D as a delimiter
buf[numbytes] = '\n';
printf("The AWS has received shortest path from server A:\n------------------------------------------\n");
printf("Destination\tMin Length\n");
printf("------------------------------------------\n");
string dist(buf);
dist = dist.substr(dist.find("D") + 1); // D is the delimiter
printf("%s", dist.c_str());
printf("------------------------------------------\n");
// For output
stringstream stream(dist);
string line;
while (getline(stream, line))
{
vector<string> temp;
temp.push_back(line + "\t");
sendToABuf.push_back(temp);
}
return 0;
}
int connectWithServerB(string size)
{
// send the distance from A and the file size to B
addr_len = sizeof their_addr;
string output;
output = string(size);
output += "P"; // P is the delimiter for the prop
output += string(buf);
// get serverB address
int status;
if ((status = getaddrinfo("127.0.0.1", to_string(SERVERB_PORT).c_str(), &hints_UDP, &UDP_TO_INFO)) != 0)
{
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(status));
return 2;
}
// send to serverB
if ((numbytes = sendto(sock_udp_fd, output.c_str(), MAXBUFLEN, 0,
UDP_TO_INFO->ai_addr, UDP_TO_INFO->ai_addrlen)) == -1)
{
perror("talker aws: sendto");
exit(1);
}
printf("The AWS has sent path length, propagation speed and transmission speed to server B using UDP over port %d.\n", AWS_UDP_PORT);
// receive data from serverB
if ((numbytes = recvfrom(sock_udp_fd, buf, MAXBUFLEN - 1, 0,
(struct sockaddr *)&their_addr, &addr_len)) == -1)
{
perror("recvfrom");
exit(1);
}
buf[numbytes] = '\0';
printf("The AWS has received delays from server B:\n------------------------------------------\n");
printf("Destination\tTt\tTp\tDelay\n");
printf("------------------------------------------\n");
printf("%s", buf);
printf("------------------------------------------\n");
// For output
// we only need the Tt Tp and Delay!
stringstream stream(buf);
string line;
int vertexIndex = 0;
while (getline(stream, line))
{
sendToABuf[vertexIndex].push_back(line.substr(line.find("\t\t") + 1));
vertexIndex++;
}
return 0;
}