-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaws.cpp
392 lines (333 loc) · 8.55 KB
/
aws.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
/*Title: Server-Client*/
/*Author: Sandeep Ravi*/
/*Date: 3-Oct-2016*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>
#include <strings.h>
#include <ctype.h>
#include <sys/wait.h>
//#define MY_DEBUG
#define MAX_RECBUF_SIZE_BYTES 4400
#define MAX_RECBUF_ELEMENTS 1100
#define AWS_UDP_PORT 24674
#define AWS_TCP_PORT 25674
#define SUM 0
#define MIN 1
#define MAX 2
#define SOS 3
struct sockaddr_in aws_server,client,aws_client,BackEndServer;
unsigned int CliAddrlen;
int tcp_sock,udp_sock,cli;
long out_arr[4];
char genStr[3][200];
int SetupTCP();
int SetupUDP();
void Send2BackEnd(int servNo,int* val,int bytes,char* op);
char* strupr(char* p);
void MakeBufferAndSend(int* buf,int);
long processData(long vals[], char*proc);
int main()
{
int sent,i,recLen,totalNums;
char c;
signed long val;
int sp,ep;
int recBuf[MAX_RECBUF_ELEMENTS];
// char sam[100]="9288\r\n";
// sscanf(sam,"%d",&val);
memset(recBuf,0,sizeof(recBuf));
CliAddrlen = sizeof(struct sockaddr_in);
tcp_sock = SetupTCP();
udp_sock = SetupUDP();
printf("AWS Server up and running...\n");
while(1)
{
#ifdef MY_DEBUG
printf("Waiting for Client....\n");
#endif
if((cli = accept(tcp_sock,(struct sockaddr*)&client,&CliAddrlen))==-1)
{
perror("Accept: ");
}
printf("\nConnected to %s on port %d\n",inet_ntoa(client.sin_addr),ntohs(client.sin_port));
recLen=1;
while(recLen)
{
#ifdef MY_DEBUG
printf("LOG: while(recLen)\n");
#endif
recLen=recv(cli,recBuf,MAX_RECBUF_SIZE_BYTES,0);
if(recLen>0)
{
totalNums = recLen/sizeof(int); //this includes the operator
printf("The AWS has received %d numbers from the client using TCP over port %d.\n",totalNums-1,AWS_TCP_PORT);
MakeBufferAndSend(recBuf,totalNums);
#ifdef MY_DEBUG
for(i=0;i<totalNums;i++)
printf("%d-> %d\n",i,recBuf[i]);
#endif
}
}
#ifdef MY_DEBUG
printf("Client Disconnected\n");
#endif
}
c = getchar();
return 0;
}
void MakeBufferAndSend(int* buf,int len)
{
char op[4];
int smallBuf[334],partition,i,sizeBytes;
sscanf((char*)buf,"%s",op);
#ifdef MY_DEBUG
printf("Extracted op: %s\n",op);
printf("Extracted op(hex): %04x\n",op);
#endif
op[3]='\0';
memset(smallBuf,0,sizeof(smallBuf));
memcpy(smallBuf,op,4);
strupr(op);
partition = (len-1)/3; // -1 to remove op at ths top // elements in one partition
sizeBytes = (partition+1)*sizeof(int); //we cannot divide len by 3 as it is a even no.
memset(out_arr,0,sizeof(out_arr));
memset(genStr,0,sizeof(genStr));
out_arr[2]='$'; // special character to know if I got output from Server C
for(i=0;i<3;i++)
{
#ifdef MY_DEBUG
printf("\nAt MakeBufferAndSend i:%d op:%s\n",i,op);
#endif
memcpy(&smallBuf[1],(&buf[1])+(i*partition),partition*sizeof(int));
Send2BackEnd(i,smallBuf,sizeBytes,op);
#ifdef MY_DEBUG
printf("After MakeBufferAndSend i:%d op:%s\n",i,op);
#endif
memset(&smallBuf[1],0,(partition*sizeof(int)));
#ifdef MY_DEBUG
printf("After memset(&smallBuf[1],0,sizeBytes = %d; i:%d op:%s\n\n",sizeBytes,i,op);
#endif
}
}
void Send2BackEnd(int servNo,int* val,int bytes,char* op)
{
struct sockaddr_in from;
struct hostent *hostDetails;
int addrStructSize,noBytesSent,noBytesRecv,BytesSent,error,ret;
long out;
int length;
int RecvBuffer_BackEndServ=0;
char c,serverName;
socklen_t len1 = sizeof(int);
length = sizeof(struct sockaddr_in);
addrStructSize = sizeof(struct sockaddr_in);
#ifdef MY_DEBUG
printf("Entered Send2BackEnd with value of op being %s\n",op);
#endif
if((hostDetails = gethostbyname("localhost"))==0) // this would fill the structure in hostDetails
perror("Unknown Host\n");
BackEndServer.sin_family=AF_INET;
bcopy((char*)hostDetails->h_addr,(char*)&BackEndServer.sin_addr,hostDetails->h_length); //the socket needs IP addr
switch(servNo)
{
case 0:
serverName = 'A';
BackEndServer.sin_port=htons(21674);
break;
case 1:
serverName = 'B';
BackEndServer.sin_port=htons(22674);
break;
case 2:
serverName = 'C';
BackEndServer.sin_port=htons(23674);
break;
default:
break;
}
noBytesSent=sendto(udp_sock,val,bytes,0,(struct sockaddr*)&BackEndServer,addrStructSize);
if(noBytesSent < 0)
perror("ending failed\n");
else
{
printf("The AWS has sent %d numbers to Backend-Server %c.\n",(noBytesSent-1)/4,serverName);
}
noBytesRecv=recvfrom(udp_sock,&RecvBuffer_BackEndServ,sizeof(long),0,(sockaddr*)&from,(socklen_t*)&length);
if(noBytesRecv < 0)
perror("recv Error..\n");
else
{
#ifdef MY_DEBUG
printf("%d -> val of op: %s and addr: %d\n",servNo,op,&op);
#endif
sprintf(&genStr[servNo][0],
"The AWS received reduction result of %s from Backend-Server %c using UDP over port %d and it is %d",
op,serverName,ntohs(from.sin_port),(long)RecvBuffer_BackEndServ);
#ifdef MY_DEBUG
printf("After sprintf %d -> val of op: %s and addr: %d\n",servNo,op,&op);
#endif
out_arr[servNo]=RecvBuffer_BackEndServ;
#ifdef MY_DEBUG
printf("out_arr: %d %d %d\n",out_arr[0],out_arr[1],out_arr[2]);
#endif
if(out_arr[2]!='$')
{
#ifdef MY_DEBUG
printf("No $\n");
#endif
printf("%s\n",genStr[0]);
printf("%s\n",genStr[1]);
printf("%s\n",genStr[2]);
// then process the outputs
out=processData(out_arr,op);
//sprintf(genstr,"%d\0",&out);
#ifdef MY_DEBUG
printf("Output to Client: %d\n",out);
#endif
printf("The AWS has successfully finished the reduction %s: %d\n",op,out);
#ifdef MY_DEBUG
printf("Now trying to send to to Client\n");
printf("tcp_sock: %d long size: %d\n", tcp_sock,sizeof(long));
#endif
BytesSent = send(cli,&out,sizeof(long),0);//,(struct sockaddr*)&client,(socklen_t)CliAddrlen);
#ifdef MY_DEBUG
printf("BytesSent: %d\n",BytesSent);
#endif
if(BytesSent>0)
{
printf("The AWS has successfully finished sending the reduction value to client.\n");
}
else
{
printf("Sent %d bytes\n",BytesSent);
perror("AWS->Client:");
}
}
}
}
long processData(long vals[], char*proc)
{
int ope=9,i,size=3;
long output=0;
#ifdef MY_DEBUG
printf("Entered processData\n");
// printf("proc Data 1st element: %s\n",*proc);
#endif
if(!strcmp((char*)proc,"SUM") || !strcmp((char*)proc,"sum"))
{
ope = SUM;
}
if(!strcmp((char*)proc,"MIN") || !strcmp((char*)proc,"min"))
{
ope = MIN;
}
if(!strcmp((char*)proc,"MAX") || !strcmp((char*)proc,"max"))
{
ope = MAX;
}
if(!strcmp((char*)proc,"SOS") || !strcmp((char*)proc,"sos"))
{
ope = SOS;
}
#ifdef MY_DEBUG
printf("op: %d\n", ope);
#endif
switch(ope)
{
case SUM:
for(i=0;i<size;i++)
output += *(vals+i);
break;
case MIN:
output = *(vals);
for(i=0;i<size;i++)
if(output>*(vals+i))
output = *(vals+i);
break;
case MAX:
output = *(vals);
for(i=0;i<size;i++)
if(output<*(vals+i))
output = *(vals+i);
break;
case SOS:
for(i=0;i<size;i++)
output += *(vals+i); // for SOS we need to just add it
break;
default:
printf("Not valid operator\n");
break;
}
return output;
}
int SetupUDP()
{
int udp_sock=0,len=0,yes=1;
if((udp_sock = socket(AF_INET, SOCK_DGRAM, 0))== -1)
{
perror("UDP-socket: ");
exit(-1);
}
aws_client.sin_family = AF_INET;
aws_client.sin_port = htons(AWS_UDP_PORT);
aws_client.sin_addr.s_addr = INADDR_ANY;
bzero(&aws_client.sin_zero,8);
len = sizeof(struct sockaddr_in);
setsockopt(udp_sock, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int));
if((bind(udp_sock,(struct sockaddr*)&aws_client,len))<0)
{
perror("UDP-bind: ");
}
#ifdef MY_DEBUG
printf("LOG: SetupUDP()\n");
#endif
return udp_sock;
}
int SetupTCP()
{
int sock,len=0,yes=1;
if((sock = socket(AF_INET, SOCK_STREAM, 0))== -1)
{
perror("socket: ");
}
#ifdef MY_DEBUG
printf("Setup TCP sock: %d\n",sock);
#endif
aws_server.sin_family = AF_INET;
aws_server.sin_port = htons(25674);
aws_server.sin_addr.s_addr = INADDR_ANY;
bzero(&aws_server.sin_zero,8);
len = sizeof( struct sockaddr_in);
setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int));
if((bind(sock,(struct sockaddr*)&aws_server,len)))
{
perror("bind: ");
}
if((listen(sock,5) == -1))
{
perror("listen ");
}
#ifdef MY_DEBUG
printf("LOG: SetupTCP()\n");
#endif
return sock;
}
char* strupr(char* p)
{
unsigned char* q;
q = (unsigned char*) p;
while(*q)
{
*q = toupper(*q);
q++;
}
return p;
}