-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaws.c
More file actions
322 lines (268 loc) · 9.09 KB
/
aws.c
File metadata and controls
322 lines (268 loc) · 9.09 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
// Copyright©TonyOuyang 10/2018
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/wait.h>
#define BACKLOG 10
//My USC ID is xxxx765584
#define SERVAWS_PORT 25584
#define UDP_RECEIVE_PORT 24584
#define SERVER_A 21584
#define SERVER_B 22584
#define SERVER_C 23584
#define Data_BUF_SIZE 10
#define RESULT_BUF_SIZE 10
#define TRUE 1
int TCPSocketInit(int sk);
void UDPSocketInit(void);
void DivideString(void);
void Calculation(void);
void DIV_Calculation(void);
void LOG_Calculation(void);
int AWS_Socket, Client_Socket, UDP_Socket, SERV_A_Socket;
struct sockaddr_in ServerAWS_addr, Client_addr, UDPA_addr, UDPB_addr, UDPC_addr, UDPReceive_addr;
socklen_t clin_len,udp_len;
char Data_BUF[Data_BUF_SIZE], Result_BUF[RESULT_BUF_SIZE];
char FunctionAWS[3],ValueString[Data_BUF_SIZE];
int UDPReceive_Flag,UDPSend_Flag;
double Receive_Num;
double Value,X,X1,X2,X3,X4,X5,X6;
int main(int argc, char const *argv[])
{
while(TRUE)
{
/******************************
Address Initiation
Initiation of Socket_addr address set up. This codes is from Beej's tutorial guide.
******************************/
memset(&ServerAWS_addr,0,sizeof(ServerAWS_addr));
memset(&Client_addr,0,sizeof(Client_addr));
memset(&UDPA_addr,0,sizeof(UDPA_addr));
memset(&UDPB_addr,0,sizeof(UDPB_addr));
memset(&UDPC_addr,0,sizeof(UDPC_addr));
memset(&UDPReceive_addr,0,sizeof(UDPA_addr));
printf("The AWS is up and running\n");
AWS_Socket = TCPSocketInit(AWS_Socket);
UDPSocketInit();
udp_len = sizeof(struct sockaddr_in);
// Reslove port reuse, I got this idea from stackoverflow
if (setsockopt(AWS_Socket, SOL_SOCKET, SO_REUSEADDR, &(int){ 1 }, sizeof(int)) < 0)
{
printf("setsockopt(SO_REUSEADDR) failed");
}
/******************************
Bind TCP socket and Receive Data
******************************/
bind(AWS_Socket, (struct sockaddr *)&ServerAWS_addr, sizeof(ServerAWS_addr));
listen(AWS_Socket,BACKLOG);
clin_len = sizeof(Client_addr);
Client_Socket = accept(AWS_Socket, (struct sockaddr *)&Client_addr,&clin_len);
recv(Client_Socket, Data_BUF, sizeof(Data_BUF),0);
/******************************
Find Function and value
******************************/
DivideString();
printf("The AWS received <%g> and function=<%s> from the client using TCP over port <%d>\n",atof(ValueString),FunctionAWS,ntohs(ServerAWS_addr.sin_port));
X = atof(ValueString); //Save the value of X after divided for further calculation.
sprintf(ValueString,"%g",X);
/******************************
Send Value to ServerA to get x2
******************************/
UDPSend_Flag = sendto(UDP_Socket, ValueString, sizeof(ValueString), 0, (struct sockaddr *)&UDPA_addr, sizeof(UDPA_addr));
if(UDPSend_Flag < 0)
{
perror("sendto");
}
printf("The AWS sent <%g> to Backend-Server <A>\n",atof(ValueString));
UDPReceive_Flag = recvfrom(UDP_Socket, ValueString,sizeof(ValueString),0,(struct sockaddr *)&UDPA_addr,&udp_len);
if (UDPReceive_Flag < 0)
{
perror("recvfrom");
}
X2 = atof(ValueString);
printf("The AWS received <%g> Backend-Server <A> using UDP over port <%d>\n",X2,ntohs(UDPReceive_addr.sin_port));
/******************************
Send Value to ServerB to get x3
******************************/
sprintf(ValueString,"%g",X);
UDPSend_Flag = sendto(UDP_Socket, ValueString, sizeof(ValueString), 0, (struct sockaddr *)&UDPB_addr, sizeof(UDPB_addr));
if(UDPSend_Flag < 0)
{
perror("sendto");
}
printf("The AWS sent <%g> to Backend-Server <B>\n",atof(ValueString));
UDPReceive_Flag = recvfrom(UDP_Socket, ValueString,sizeof(ValueString),0,(struct sockaddr *)&UDPB_addr,&udp_len);
if (UDPReceive_Flag < 0)
{
perror("recvfrom");
}
X3 = atof(ValueString);
printf("The AWS received <%g> Backend-Server <B> using UDP over port <%d>\n",X3,ntohs(UDPReceive_addr.sin_port));
/******************************
Send Value to ServerA to get x4
******************************/
sprintf(ValueString,"%g",X2);
UDPSend_Flag = sendto(UDP_Socket, ValueString, sizeof(ValueString), 0, (struct sockaddr *)&UDPA_addr, sizeof(UDPA_addr));
if(UDPSend_Flag < 0)
{
perror("sendto");
}
printf("The AWS sent <%g> to Backend-Server <A>\n",atof(ValueString));
UDPReceive_Flag = recvfrom(UDP_Socket, ValueString,sizeof(ValueString),0,(struct sockaddr *)&UDPA_addr,&udp_len);
if (UDPReceive_Flag < 0)
{
perror("recvfrom");
}
X4 = atof(ValueString);
printf("The AWS received <%g> Backend-Server <A> using UDP over port <%d>\n",X4,ntohs(UDPReceive_addr.sin_port));
/******************************
Send Value to ServerC to get x5
******************************/
sprintf(ValueString,"%g",X);
UDPSend_Flag = sendto(UDP_Socket, ValueString, sizeof(ValueString), 0, (struct sockaddr *)&UDPC_addr, sizeof(UDPC_addr));
if(UDPSend_Flag < 0)
{
perror("sendto");
}
printf("The AWS sent <%g> to Backend-Server <C>\n",atof(ValueString));
UDPReceive_Flag = recvfrom(UDP_Socket, ValueString,sizeof(ValueString),0,(struct sockaddr *)&UDPC_addr,&udp_len);
if (UDPReceive_Flag < 0)
{
perror("recvfrom");
}
X5 = atof(ValueString);
printf("The AWS received <%g> Backend-Server <C> using UDP over port <%d>\n",X5,ntohs(UDPReceive_addr.sin_port));
/******************************
Send Value to ServerB to get x6
******************************/
sprintf(ValueString,"%g",X2);
UDPSend_Flag = sendto(UDP_Socket, ValueString, sizeof(ValueString), 0, (struct sockaddr *)&UDPB_addr, sizeof(UDPB_addr));
if(UDPSend_Flag < 0)
{
perror("sendto");
}
printf("The AWS sent <%g> to Backend-Server <B>\n",atof(ValueString));
UDPReceive_Flag = recvfrom(UDP_Socket, ValueString,sizeof(ValueString),0,(struct sockaddr *)&UDPB_addr,&udp_len);
if (UDPReceive_Flag < 0)
{
perror("recvfrom");
}
X6 = atof(ValueString);
printf("The AWS received <%g> Backend-Server <B> using UDP over port <%d>\n",X6,ntohs(UDPReceive_addr.sin_port));
printf("Values of powers received by AWS:<%g,%g,%g,%g,%g,%g>\n",X,X2,X3,X4,X5,X6);
Calculation();
//sprintf(Result_BUF,"%g",Receive_Num);
//printf("Result BUF:%s\n",Result_BUF);
printf("AWS calculate %s on <%g>: <%s>\n", FunctionAWS,X,Result_BUF);
printf("The AWS sent <%s> to client\n", Result_BUF);
if (send(Client_Socket,Result_BUF,sizeof(Result_BUF),0) < 0)
{
perror("send");
}
close(AWS_Socket);
close(Client_Socket);
close(UDP_Socket);
}
return 0;
}
/******************************
TCP Server Initiation
******************************/
int TCPSocketInit(int sk)
{
sk = socket(AF_INET, SOCK_STREAM, 0);
if (-1 == sk)
{
perror("sk");
}
ServerAWS_addr.sin_family = AF_INET;
ServerAWS_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
ServerAWS_addr.sin_port = htons(SERVAWS_PORT);
return sk;
}
/******************************
UDP Socket Initiation
******************************/
void UDPSocketInit(void)
{
UDP_Socket = socket(AF_INET, SOCK_DGRAM, 0);
if (-1 == UDP_Socket)
{
perror("udpsk");
}
UDPA_addr.sin_family = AF_INET;
UDPA_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
UDPA_addr.sin_port = htons(SERVER_A);
UDPB_addr.sin_family = AF_INET;
UDPB_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
UDPB_addr.sin_port = htons(SERVER_B);
UDPC_addr.sin_family = AF_INET;
UDPC_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
UDPC_addr.sin_port = htons(SERVER_C);
UDPReceive_addr.sin_family = AF_INET;
UDPReceive_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
UDPReceive_addr.sin_port = htons(UDP_RECEIVE_PORT);
if (setsockopt(UDP_Socket, SOL_SOCKET, SO_REUSEADDR, &(int){ 1 }, sizeof(int)) < 0)
{
printf("setsockopt(SO_REUSEADDR) failed");
}
if (bind(UDP_Socket,(struct sockaddr *)&UDPReceive_addr, sizeof(UDPReceive_addr)) < 0)
{
perror("bind");
}
//printf("Bind UDP receive success on port:%d\n", ntohs(UDPReceive_addr.sin_port));
}
/******************************
Divide into FunctionAWS and value
******************************/
void DivideString(void)
{
//printf("DataBUF is: %s ,Divide the string\n",Data_BUF);
strncpy(FunctionAWS,Data_BUF,sizeof(FunctionAWS));
FunctionAWS[3] = '\0';
strncpy(ValueString,Data_BUF + 3,sizeof(ValueString));
ValueString[Data_BUF_SIZE] = '\0';
//printf("FunctionAWS is :%s\n",FunctionAWS);
//printf("ValueString is :%s\n",ValueString);
}
void Calculation(void)
{
if (0 == strcmp("DIV",FunctionAWS))
{
//printf("You are so DIV\n");
DIV_Calculation();
}
else if (0 == strcmp("LOG",FunctionAWS))
{
//printf("You are so LOG\n");
LOG_Calculation();
}
else
{
printf("Function Error\n");
}
}
void DIV_Calculation(void)
{
double DIV_Result;
DIV_Result = 1 + X + X2 + X3 + X4 + X5 + X6;
//printf("DIV calculate:%g\n", DIV_Result);
sprintf(Result_BUF,"%g",DIV_Result);
//printf("Result BUFFER: %s\n", Result_BUF);
}
void LOG_Calculation(void)
{
double LOG_Result;
LOG_Result = - X - (X2/2) - (X3/3) - (X4/4) - (X5/5) - (X6/6);
//printf("LOG calculate:%g\n", LOG_Result);
sprintf(Result_BUF,"%g",LOG_Result);
//printf("Result BUFFER: %s\n", Result_BUF);
}