-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspeak.c
More file actions
647 lines (573 loc) · 17.2 KB
/
speak.c
File metadata and controls
647 lines (573 loc) · 17.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
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
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
/******************************************************************************
*
* File Name........: speak.c
*
* Description......:
* Create a process that talks to the listen.c program. After
* connecting, each line of input is sent to listen.
*
* This program takes two arguments. The first is the host name on which
* the listen process is running. (Note: listen must be started first.)
* The second is the port number on which listen is accepting connections.
*
* Revision History.:
*
* When Who What
* 09/02/96 vin Created
*
*****************************************************************************/
//search for 'keep this on'
/*........................ Include Files ....................................*/
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#define LEN 64
#define baseport 65420
#define tries 500
struct sockaddr_in sin, pin, nin;
struct my_data {
int pid;
int prev; //this is previous neighbours port to which i need to connect
struct sockaddr_in pin;
int next; //this is my port where i will listen for next neighbour
int master;
}my_data;
int p, nacc, n, s, fd_max;
int rc, len, port, mynum, hisnum, nummax;
int temp, temp_socket, counter, batata_size, hops, hops_left;
char host[LEN], str[LEN];
char potato[20], id[2], pid_temp;
//char *batata_vada;
int *batata_vada;
struct hostent *hp, *hpp, *hpn, *hptemp;
struct sockaddr_in incoming;
fd_set read_set;
int fd_list[3];
int yes = 1, rnum, rfd, exit_flag;
time_t t;
void get_init_data(int argc, char* argv0, char *argv1, char *argv2)
{
/* read host and port number from command line */
if ( argc != 3 )
{
fprintf(stderr, "Usage: %s <host-name> <port-number>\n", argv0);
exit(1);
}
/* fill in hostent struct */
hp = gethostbyname(argv1);
if ( hp == NULL )
{
fprintf(stderr, "%s: host not found (%s)\n", argv0, host);
exit(1);
}
port = atoi(argv2);
my_data.master = port;
}
int create_socket(void)
{
/* use address family INET and STREAMing sockets (TCP) */
//this code has been lifted from Beej's guide.
if ((temp_socket = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
perror("socket: ");
exit(1);
}
// // lose the pesky "address already in use" error message
// if (setsockopt(temp_socket, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1)
// {
//
// perror("setsockopt");
// exit(1);
// }
return (temp_socket);
}
int largest(int s1, int s2, int s3)
{
if (s1 > s2)
{
if (s3 > s1)
return (s3);
else return (s1);
}
else if (s3 > s2)
return (s3);
else return (s2);
}
//sending and receiving large chunks of data////////////////////////////////////
//This code has been referenced from Beej's Guide///////////////////////////////////
int sendall(int fd, void *buf, int len)
{
int total = 0; // how many bytes we've sent
int bytesleft = len; // how many we have left to send
int n;
while(total < len)
{
n = send(fd, (buf+total), bytesleft, 0);
//if (n == -1) { break; }
if (n < 0 || n == 0) { break; }
/* */
//n = n - (n % 4);
// if (n%4 != 0)
// {
// n = n - n%4 - 4000;
// }
/* */
total += n;
bytesleft = bytesleft - n;
}
len = total; // return number actually sent here
//return n==-1?-1:0; // return -1 on failure, 0 on success
return (n);
}
int receiveall(int fd, void *buf, int len)
{
int total = 0;
int bytesleft = len;
int n;
while (total < len)
{
n = recv(fd, (buf+total), bytesleft, 0); //receive accept/connect order
//if ( n < 0 ) {break;}
if (n < 0 || n == 0) { break; }
/* */
//n = n - (n % 4);
// if (n%4 != 0)
// {
// n = n - n%4 - 4000;
// }
/* */
total = total + n;
bytesleft = bytesleft - n;
}
len = total;
//return n == -1? -1:0;
return (n);
}
//////////////////////////////////////////////////////////////////////////////////
main (int argc, void *argv[])
{
int i;
get_init_data(argc, argv[0], argv[1], argv[2]);
s = create_socket();
/* set up the address and port */
sin.sin_family = AF_INET;
sin.sin_port = htons(port);
memcpy(&sin.sin_addr, hp->h_addr_list[0], hp->h_length);
/* connect to socket at above addr and port */
rc = connect(s, (struct sockaddr *)&sin, sizeof(sin));
if ( rc < 0 )
{
perror("connect :");
exit(rc);
}
len = recv(s, &mynum, sizeof(int), 0); //receive pid
if ( len < 0 )
{
perror("recv:");
exit(1);
}
my_data.pid = mynum;
printf("Connected as player %d\n", my_data.pid+1);
//opening my listening port for next neighbour: from here
// n = socket(AF_INET, SOCK_STREAM, 0);
// if ( n < 0 )
// {
// perror("socket:");
// exit(n);
// }
n = create_socket();
gethostname(host, sizeof host);
hpn = gethostbyname(host);
if ( hpn == NULL )
{
fprintf(stderr, "%s: host not found (%s)\n", argv[0], host);
exit(1);
}
//printf("here1: %d\n", my_data.pid);
//this part trying- start
temp = baseport;
rc = -1;
//counter = tries;
while ((rc < 0) && (temp < 65500))
{
nin.sin_family = AF_INET;
nin.sin_port = htons(temp); //my_data.next is my local port to which i will bind and listen for the next player
memcpy(&nin.sin_addr, hpn->h_addr_list[0], hpn->h_length);
/* bind socket s to address sin */
//printf("here2: %d\t port: %d\n", my_data.pid, temp);
rc = bind(n, (struct sockaddr *)&nin, sizeof(nin));
//printf ("rc = %d\n", rc);
if ( rc < 0 )
{
//perror("bind :");
temp++;
}
}
if (temp >= 65500)//rc < 0)
{
printf("Player #%d: Unable to bind- Exiting\n", my_data.pid);
exit(rc);
}
//this part trying- end
//printf("here3: %d\n", my_data.pid);
my_data.next = temp;
len = send(s, &temp, sizeof(int), 0); //send port where i will receive
if (len < 0)
{
perror("send :");
exit(1);
}
//printf ("Player %d waiting to receive neighbours port\n", my_data.pid);
len = recv(s, &temp, sizeof(int), 0); //receive port where i can send to neighbour
if ( len < 0 )
{
perror("recv :");
exit(1);
}
my_data.prev = temp;
len = recv(s, &pin, sizeof(struct sockaddr_in), 0); //receive address of previous neighbour
if ( len < 0 )
{
perror("recv :");
exit(1);
}
pin.sin_port = htons(my_data.prev);
memcpy(&(my_data.pin), &pin, sizeof(struct sockaddr_in));
//printf("\n Player #%d at your service\n\tDark Lord resides at %d\n\tI listen at %d\n\tI send from %d\n", my_data.pid, my_data.master, my_data.prev, my_data.next);
//printf("Player #%d awaiting lord\n", my_data.pid);
if (1)
{
len = send(s, &my_data.pid, sizeof(int), 0); //send notification to lord that process is ready/
if (len < 0)
{
perror("send :");
exit(1);
}
}
for (i = 0; i < 2; i++)
{
len = recv(s, &temp, sizeof(int), 0); //receive accept/connect order
if ( len < 0 )
{
perror("recv :");
exit(1);
}
if (temp == 0)
{
//the other guy will connect to me
//printf("Player %d: received accept signal from lord\n", my_data.pid);
rc = listen(n, 3);
if ( rc < 0 )
{
perror("listen :");
exit(rc);
}
// printf(">> I, Player %d await my next players orders...Connected to %s;\n", my_data.pid, hptemp->h_name);
nacc = accept(n, (struct sockaddr *)&incoming, &len);
if ( nacc < 0 )
{
perror("accept :");
exit(rc);
}
//printf("Player %d: accept activated\n", my_data.pid);
//printf("I, Player %d have received the word of from younger Player\n", my_data.pid);
// printf(">> I, Player %d have received the word of from Player %d...Connected to %s;\n", my_data.pid, nprev.pid, hptemp->h_name);
}
else if (temp == 1)
{
//if (my_data.pid == 0)
//printf("received connect signal- waiting for neighbour to connect\n");
//connecting to prev neighours port: from here
//will need to change 2 lines below- the hostname of the prev neighbour should be in hp
//printf("Player %d: received connect signal from lord\n", my_data.pid);
// gethostname(host, sizeof host);
// hpp = gethostbyname(host);
// if ( hpp == NULL )
// {
// fprintf(stderr, "%s: host not found (%s)\n", argv[0], host);
// exit(1);
// }
p = create_socket();
/* set up the address and port */
// pin.sin_family = AF_INET;
// pin.sin_port = htons(my_data.prev);
// memcpy(&pin.sin_addr, my_data.hpp->h_addr_list[0], my_data.hpp->h_length);
/* connect to socket at above addr and port */
counter = tries;
rc = -1;
while ((rc < 0) && (counter > 0))
{
//printf("\tPlayer %d trying to connect\n", my_data.pid);
rc = connect(p, (struct sockaddr *)&pin, sizeof(pin));
if ( rc < 0 )
{
// printf("Player #%d:\n", my_data.pid);
// perror("connect :");
//exit(rc);
}
counter--;
}
if ((rc < 0) && (counter == 0))
{
printf("Player %d: I have failed to connect after trying repeatedly: exiting\n", my_data.pid);
exit(rc);
}
len = send(s, &rc, sizeof(int), 0); //send notification to lord that process is connected and ready to accept
if (len < 0)
{
perror("send :");
exit(1);
}
}
}
len = recv(s, &nummax, sizeof(int), 0); //receive total number of players
if ( len < 0 )
{
perror("recv :");
exit(1);
}
len = recv(s, &hops, sizeof(int), 0); //receive final go signal with total number of hops
if ( len < 0 )
{
perror("recv :");
exit(1);
}
//printf("Player %d: total hops = %d\n", my_data.pid, hops);
hops_left = hops-1;
//////Preparing bata vada///////////////////////////////////////
//batata_vada = (char*) malloc (hops + 1);
batata_vada = 0;
batata_vada = (int*) malloc ((hops) * sizeof(int));
//printf ("batata_vada size = %d\n", (hops+1) * sizeof(int));
if (!batata_vada)
{
printf("Couldn't allocate memory for potato: Exiting\n");
exit (1);
}
// for (counter = 0; counter < batata_size; counter++)
// batata_vada[counter] = 'o';
// batata_vada[batata_size]='\0';
/////////////////////////////////////////////////
//Sending confirmation that tiffin box is ready/////////////////////////////////////
counter = tries;
rc = -1;
while ((rc < 0) && (counter > 0))
{
rc = send(s, &hops, sizeof(int), 0); //send notification to lord that the tiffin box is ready
if (rc < 0)
{
perror("send :");
exit(1);
}
if ( rc < 0 )
{
//printf("Player #%d:\n", my_data.pid);
perror("Send: Error sending confirmation :");
//exit(rc);
}
counter--;
}
if ((rc < 0) && (counter == 0))
{
printf("Player %d: Master doesn't know I have succeeded in allocating memory: Exiting\n", my_data.pid);
exit(rc);
}
///////////////////////////////////////////////////////////////////////////////////////
//Pass around the potato/////////////////////////////////////////////////////////////////
while (1)
{
fd_list[0] = s;
fd_list[1] = p;
fd_list[2] = nacc;
FD_ZERO(&read_set); // clear the master and temp sets
fd_max = 0;
exit_flag = 0;
// hops_left = -69999; //do not clear this guy
// for (i = 0; i < num; i++)
// {
// if (fd_max < players[i].fd)
// fd_max = players[i].fd;
// FD_SET(players[i].fd, &read_set);
// }
fd_max = largest(s, p, nacc);
FD_SET(s, &read_set);
FD_SET(p, &read_set);
FD_SET(nacc, &read_set);
while (1)
{
temp = (select(fd_max+1, &read_set, NULL, NULL, NULL));
if (temp == -1)
{
perror("select: error: Players exiting at select: Ring broken");
exit(1);
}
if (temp > 0)
break;
}
for (i = 0; i < 3; i++)
{
if (FD_ISSET(fd_list[i], &read_set)) //this one has received
{
//printf("Player %d: Batata is mine. Received from source %d\t", my_data.pid, i);
if (i != 0)
{
len = recv(fd_list[i], &hops_left, sizeof(int), 0);
if ( len < 0 )
{
perror("recv : error while receiving hops left: ");
exit(1);
}
if (len == 0)
{
exit_flag = 1;
//printf("Player #%d: Bretheren %d has fallen into the darkness\n", my_data.pid, i);
break;
}
//printf("Player %d: %d hops left\n", my_data.pid, hops_left); //keep this on
fflush(stdout);
temp = 69;
len = send(fd_list[i], &temp, sizeof(int), 0); //send confirmation that hops_left is received
if (len < 0)
{
perror("send :");
exit(1);
}
}
//len = recv(fd_list[i], batata_vada, (hops + 1) * (sizeof(char)), 0);
memset(batata_vada, -1, (hops) * (sizeof(char)));
//len = recv(fd_list[i], batata_vada, (hops+1) * (sizeof(int)), 0);
len = receiveall(fd_list[i], batata_vada, (hops) * (sizeof(int)));
if (len <= 0)
{
// got error or connection closed by client
if (len == 0) // connection closed
{
exit_flag = 1;
//printf("Player #%d: Bretheren %d has been messily devoured\n", my_data.pid, i);
}
else
{
perror("recv");
}
close(fd_list[i]); // bye!
// FD_CLR(fd_list[i], &master); // remove from master set
}
else //len is greater than 0 and hence this guy has successfully received
{
break; //break from for loop
}
}
}//end of for loop
if (exit_flag == 1) //received notification that another process was messily devoured- I don't do anything but wait for my own signal of doom
{
//printf("Player %d: Greived I am at the demise of my bretheren %d\n", my_data.pid, i);
continue;
}
if (batata_vada[0] == -69)
{
//printf("Player #%d: Master needs me no more: exiting\n", my_data.pid);
close(p);
//len = -69;
rc = send(s, &i, sizeof(int), 0); //sending confirmation to Dark Lord that previous connection is closed and I am shutting down
if (rc < 0)
{
perror("send : error while sending exit confirmation:");
exit(1);
}
close(s);
exit(1);
}
if (hops_left < 0)
{
if (hops_left < -1) printf("Player %d: SOMETHING IS WRONG- negative number of hops: exiting\n", my_data.pid);
exit (0);
}
if (hops_left == 0)
{
//sprintf(&pid_temp, "%d", my_data.pid);
//batata_vada[hops - hops_left] = pid_temp;
mynum = my_data.pid + 1;
batata_vada[hops - hops_left - 1] = mynum;
hops_left--;
//printf ("Player %d: last hop. Batata vada- %s\n", my_data.pid, batata_vada);
//printf ("Player %d: last hop. Batata vada is: ", my_data.pid);//instead of this really nice line, dr.freeh wants:
printf("I'm it\n");
// for (counter = 0; counter < (hops+1); counter++)
// {
// printf("%d ", batata_vada[counter]);
// }
// printf("\n");
//len = send(s, batata_vada, (hops + 1) * (sizeof(char)), 0); //send batata vada to master
//len = send(s, batata_vada, (hops+1) * (sizeof(int)), 0); //send batata vada to master
len = sendall(s, batata_vada, (hops) * (sizeof(int)));
if (len < 0)
{
perror("send : error sending potato to master: ");
exit(1);
}
//exit(1);
}
else
{
// sprintf(&pid_temp, "%d", my_data.pid);
// printf("Player %d: char conversion: %c\n", my_data.pid, pid_temp);
// batata_vada[hops - hops_left] = pid_temp;
// if (hops_left < 1000)
// printf("Player #%d: dumping my data into batata_vada[%d]\n", my_data.pid, hops-hops_left);
mynum = my_data.pid + 1;
batata_vada[hops - hops_left - 1] = mynum;
srand((unsigned int)hops_left * ((int)time(&t)));
rnum = (rand()%2);
hops_left--;
//len = send(nacc, &hops_left, sizeof(int), 0); //send hops left to next player
mynum = my_data.pid + 1;
if (rnum == 0)
{
if (mynum == 1) hisnum = nummax;
else hisnum = mynum - 1;
printf("Sending potato to %d\n", hisnum);//keep this on
rfd = p;
}
else
{
if (mynum == nummax) hisnum = 1;
else hisnum = mynum + 1;
printf("Sending potato to %d\n", hisnum); //keep this on
rfd = nacc;
}
len = send(rfd, &hops_left, sizeof(int), 0); //send hops left to next/previous player
if (len < 0)
{
perror("send :");
exit(1);
}
len = recv(rfd, &temp, sizeof(int), 0);
if ( len < 0 || temp != 69)
{
perror("recv : error while receiving confirmation after hops left: ");
exit(1);
}
//fflush(stdout);
//if (hops_left < 1000) printf("Player #%d: batata_vada[%d] = %d\n", my_data.pid, hops-hops_left-1, batata_vada[hops-hops_left-1]);
//len = send(rfd, batata_vada, (hops + 1) * (sizeof(char)), 0); //send batata vada to next/previous Player
//len = send(rfd, batata_vada, (hops+1) * (sizeof(int)), 0); //send batata vada to next/previous Player
len = sendall(rfd, batata_vada, (hops) * (sizeof(int)));
if (len < 0)
{
perror("send : error sending batata_vada to next Player: ");
exit(1);
}
}
}//end of while (1) loop
/////////////////////////////////////////////////////////////////////////////////////////
// close(s);
// close(p);
// close(n);
exit(0);
}
/*........................ end of speak.c ...................................*/