-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassignment3.c
857 lines (690 loc) · 25.6 KB
/
assignment3.c
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
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdio.h>
#include <signal.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <sys/select.h>
#include <time.h>
#include <stdbool.h>
#include "arpnet.h"
// initialize global variables
bool turnLeader = false;
node_id idNode = 0;
int state = 0;
char* ipAddr = "40.68.246.18";
int fdSocket = 0;
FILE *fLog; // Log file
int voteCount = 0;
int lenghtTable = 0;
struct timeval timeout;
// Declare functions
void handshake();
void vote();
void turn(message_t *);
void handshake()
{
// Define the message variable
handshake_t hshmsg;
int firstNode = hsh_imfirst(ipAddr);
// Control if we are the first of the address table
if (firstNode == 1)
{
printf("I'm the first node");
fflush(stdout);
// Initialize the message
hsh_init(&hshmsg);
// Control if our version of the libray is the correct one
if (hsh_check_availability(idNode, &hshmsg) == 1)
{
// Define variables
node_id nextNodeId;
// Find the next available node and send a message to it
for(int j = 0; j < lenghtTable; j++) {
// Obtain the id of the next node
node_id nextNodeId = iptab_get_next(idNode + j);
// Get ip address of the next node
char* ipNextNode = iptab_getaddr(nextNodeId);
// Connect to the next node
int fdSocketNextNode = net_client_connection_timeout(ipNextNode, &timeout);
// Control if the node is available
if(fdSocketNextNode > 0) {
printf("Node with id %i is available \n", nextNodeId);
fflush(stdout);
// Send the message to the next node
if (write(fdSocketNextNode, &hshmsg, sizeof(handshake_t)) <= 0)
{
perror("Error sending message to the next node");
fprintf(fLog, "Error sending message to the next node \n");
fflush(fLog);
exit(-1);
}
// Close the socket
close(fdSocketNextNode);
printf("Message sent \n");
fflush(stdout);
// Exit from the for loop
break;
}
printf("Node with id %i is not available \n", nextNodeId);
fflush(stdout);
}
// Wait the message of the last node
int fdSocketClient = net_accept_client(fdSocket, NULL);
// Read the message from the client
if (read(fdSocketClient, &hshmsg, sizeof(handshake_t)) <= 0)
{
perror("Error reading message form last node");
fprintf(fLog, "Error reading message form last node \n");
fflush(fLog);
exit(-1);
}
// Close the last node socket
close(fdSocketClient);
printf("Message received \n");
fflush(stdout);
// Update address table
hsh_update_iptab(&hshmsg);
// Obtain the id of the next node
nextNodeId = iptab_get_next(idNode);
// Get ip address of the next node
char* ipNextNode = iptab_getaddr(nextNodeId);
// Connect to the next node
int fdSocketNextNode = net_client_connection(ipNextNode);
// Send the message to the next node
if (write(fdSocketNextNode, &hshmsg, sizeof(handshake_t)) <= 0)
{
perror("Error sending message to the next node");
fprintf(fLog, "Error sending message to the next node \n");
fflush(fLog);
exit(-1);
}
// Close the socket
close(fdSocketNextNode);
printf("Message sent \n");
fflush(stdout);
// Wait the message of the last node
fdSocketClient = net_accept_client(fdSocket, NULL);
// Read the message from the client
if (read(fdSocketClient, &hshmsg, sizeof(handshake_t)) <= 0)
{
perror("Error reading message form last node");
fprintf(fLog, "Error reading message form last node \n");
fflush(fLog);
exit(-1);
}
// Close the last node socket
close(fdSocketClient);
printf("Message received \n");
fflush(stdout);
}
else
{
printf("The version of the arpnet library is not updated!");
fflush(stdout);
fprintf(fLog, "The version of the arpnet library is not updated! \n");
fflush(fLog);
fprintf(fLog, "Process exiting... \n");
fflush(fLog);
exit(-1);
}
}
// Our node has not id zero in the address table
else
{
// Initialize the message
hsh_init(&hshmsg);
// Define node_id variable
node_id nextNodeId;
// Wait the message of the previous
int fdSocketClient = net_accept_client(fdSocket, NULL);
// Read the message from the client
if (read(fdSocketClient, &hshmsg, sizeof(handshake_t)) <= 0)
{
perror("Error reading message form last node");
fprintf(fLog, "Error reading message form last node \n");
fflush(fLog);
exit(-1);
}
// Close the previous node socket
close(fdSocketClient);
printf("Message received \n");
fflush(stdout);
// Control if our version of the libray is the correct one
int correctVersion = hsh_check_availability(idNode, &hshmsg);
printf("Check availability done! \n");
fflush(stdout);
// Find the next available node and send a message to it
for(int j = 0; j < lenghtTable - idNode+1; j++) {
// Obtain the id of the next node
node_id nextNodeId = iptab_get_next(idNode + j);
// Get ip address of the next node
char* ipNextNode = iptab_getaddr(nextNodeId);
// Connect to the next node
int fdSocketNextNode = net_client_connection_timeout(ipNextNode, &timeout);
// Control if the node is available
if(fdSocketNextNode > 0) {
printf("Node with id %i is available \n", nextNodeId);
fflush(stdout);
// Send the message to the next node
if (write(fdSocketNextNode, &hshmsg, sizeof(handshake_t)) <= 0)
{
perror("Error sending message to the next node");
fprintf(fLog, "Error sending message to the next node \n");
fflush(fLog);
exit(-1);
}
// Close the socket
close(fdSocketNextNode);
printf("Message sent \n");
fflush(stdout);
break;
}
printf("Node with id %i is not available \n", nextNodeId);
fflush(stdout);
}
if (correctVersion == 0)
{
printf("The version of the arpnet library is not updated!");
fflush(stdout);
fprintf(fLog, "The version of the arpnet library is not updated! \n");
fflush(fLog);
fprintf(fLog, "Process exiting... \n");
fflush(fLog);
exit(-1);
}
// Wait the message of the last node
fdSocketClient = net_accept_client(fdSocket, NULL);
// Read the message from the client
if (read(fdSocketClient, &hshmsg, sizeof(handshake_t)) <= 0)
{
perror("Error reading message form last node");
fprintf(fLog, "Error reading message form last node \n");
fflush(fLog);
exit(-1);
}
// Close the last node socket
close(fdSocketClient);
printf("Message received \n");
fflush(stdout);
// Update address table
hsh_update_iptab(&hshmsg);
// Obtain the id of the next node
nextNodeId = iptab_get_next(idNode);
// Get ip address of the next node
char* ipNextNode = iptab_getaddr(nextNodeId);
// Connect to the next node
int fdSocketNextNode = net_client_connection(ipNextNode);
// Send the message to the next node
if (write(fdSocketNextNode, &hshmsg, sizeof(handshake_t)) <= 0)
{
perror("Error sending message to the next node");
fprintf(fLog, "Error sending message to the next node \n");
fflush(fLog);
exit(-1);
}
// Close the socket
close(fdSocketNextNode);
printf("Message sent \n");
fflush(stdout);
}
// Change state
state = 1;
}
void vote()
{
// Obtain the id of our node
//idNode = iptab_get_ID_of(ipAddr);
int firstNode = hsh_imfirst(ipAddr);
// Control if is the fisrt iteration of vote state and control
// if we are the first of the address table
if ((voteCount == 0 && firstNode == 1) || turnLeader == true)
{
// Define the message variable
votation_t votemsg;
// Initialize the message
vote_init(&votemsg);
// Do the votation
vote_do_votation(&votemsg);
// Obtain the id of the next node
node_id nextNodeId = iptab_get_next(idNode);
// Get ip address of the next node
char* ipNextNode = iptab_getaddr(nextNodeId);
// Connect to the next node
int fdSocketNextNode = net_client_connection(ipNextNode);
// Send the message to the next node
if (write(fdSocketNextNode, &votemsg, sizeof(votation_t)) <= 0)
{
perror("Error sending message to the next node");
fprintf(fLog, "Error sending message to the next node \n");
fflush(fLog);
exit(-1);
}
// Close the socket
close(fdSocketNextNode);
printf("Message sent \n");
fflush(stdout);
// Wait the message of the last node
int fdSocketClient = net_accept_client(fdSocket, NULL);
// Read the message from the client
if (read(fdSocketClient, &votemsg, sizeof(votation_t)) <= 0)
{
perror("Error reading message form last node");
fprintf(fLog, "Error reading message form last node \n");
fflush(fLog);
exit(-1);
}
// Close the last node socket
close(fdSocketClient);
printf("Message received \n");
fflush(stdout);
//
node_id idWinner = vote_getWinner(&votemsg);
// Control if our node is the winner
if (idWinner == idNode)
{
turnLeader = true;
}
else
{
// Define the message variable
message_t winmsg;
// Initialize the message
msg_init(&winmsg);
// Set the id of the turn leader in the message
msg_set_ids(&winmsg, idNode, idWinner);
// Get ip address of the next node
char* ipWinnerNode = iptab_getaddr(idWinner);
// Connect to the next node
int fdSocketWinnerNode = net_client_connection(ipWinnerNode);
// Send the message to the next node
if (write(fdSocketWinnerNode, &winmsg, sizeof(message_t)) <= 0)
{
perror("Error sending message to the winner node");
fprintf(fLog, "Error sending message to the winner node \n");
fflush(fLog);
exit(-1);
}
// Close the winner node socket
close(fdSocketWinnerNode);
printf("Message sent \n");
fflush(stdout);
}
// Change state
state = 2;
}
// Our node is not the first node of the table
else
{
// Define the message variable
votation_t votemsg;
// Initialize the message
vote_init(&votemsg);
// Wait the message of the previous node
int fdSocketClient = net_accept_client(fdSocket, NULL);
// Read the message from the client
if (read(fdSocketClient, &votemsg, sizeof(votation_t)) <= 0)
{
perror("Error reading message form last node");
fprintf(fLog, "Error reading message form last node \n");
fflush(fLog);
exit(-1);
}
// Close the previous node socket
close(fdSocketClient);
printf("Message received \n");
fflush(stdout);
// Do the votation
vote_do_votation(&votemsg);
// Obtain the id of the next node
node_id nextNodeId = iptab_get_next(idNode);
// Get ip address of the next node
char* ipNextNode = iptab_getaddr(nextNodeId);
// Connect to the next node
int fdSocketNextNode = net_client_connection(ipNextNode);
// Send the message to the next node
if (write(fdSocketNextNode, &votemsg, sizeof(votation_t)) <= 0)
{
perror("Error sending message to the next node");
fprintf(fLog, "Error sending message to the next node \n");
fflush(fLog);
exit(-1);
}
// Close the socket
close(fdSocketNextNode);
printf("Message sent \n");
fflush(stdout);
// Close the the communication with the next node
close(fdSocketClient);
// Define the message variable
message_t msg;
// Initialize the message
msg_init(&msg);
// Wait the message of the previous node
fdSocketClient = net_accept_client(fdSocket, NULL);
// Read the message from the client
if (read(fdSocketClient, &msg, sizeof(message_t)) <= 0)
{
perror("Error reading message form last node");
fprintf(fLog, "Error reading message form last node \n");
fflush(fLog);
exit(-1);
}
// Close the the communication with the next node
close(fdSocketClient);
printf("Message received \n");
fflush(stdout);
// Control if we are the turn leader
if (msg.turnLeader == idNode)
{
turnLeader = true;
state = 2;
}
// Our node is not the turn leader
else
{
// Call the turn function
printf("Turn \n");
fflush(stdout);
turn(&msg);
}
}
// Increment the counter
voteCount++;
}
void turn(message_t *msg)
{
// Find the number of available nodes
int availableNodes = iptab_len_av();
// Initilize average variables
struct timeval totalTimes[availableNodes];
struct timeval flyTimes[availableNodes];
// Obtain the id of our node
//idNode = iptab_get_ID_of(ipAddr);
// Start the loop
for (int count = 0; count < 10; count++)
{
// Define the message variable
message_t message;
// Initialize the message
msg_init(&message);
// Control if we are the turn leader
if (turnLeader == true)
{
if(count == 0) {
printf("I'm turn leader \n");
fflush(stdout);
}
// Define variables
struct timeval firstSendTime;
struct timeval lastRecTime;
struct timeval totalSendTime;
struct timeval totalRecTime;
// Set the id of the turn leader in the message
msg_set_ids(&message, idNode, idNode);
// Start loop for set v totalSendTimeisited all the unavailable nodes
for (int i = 0; i < lenghtTable; i++)
{
node_id tempNode = i;
// Control if the i node of the table is available
if (iptab_is_available(tempNode) == 0)
{
// Set visited the node
msg_mark(&message, tempNode);
}
}
// Set our node as visited
msg_mark(&message, idNode);
// Select randomly the next node
int idNextNode = msg_rand(&message);
// Insert the sending time in the message
msg_set_sent(&message);
// Save the first sending time
firstSendTime = message.sent;
// Get ip address of the next node
char* ipNextNode = iptab_getaddr(idNextNode);
// Connect to the next node
int fdSocketNextNode = net_client_connection(ipNextNode);
// Send the message to the next node
if (write(fdSocketNextNode, &message, sizeof(message_t)) <= 0)
{
perror("Error sending message to the next node");
fprintf(fLog, "Error sending message to the next node \n");
fflush(fLog);
exit(-1);
}
// Close the the communication with the next node
close(fdSocketNextNode);
printf("Message sent \n");
fflush(stdout);
for (int var = 0; var < availableNodes - 1; var++)
{
// Wait the message from a node
int fdSocketClient = net_accept_client(fdSocket, NULL);
// Read the message from the client
if (read(fdSocketClient, &message, sizeof(message_t)) <= 0)
{
perror("Error reading message from node");
fprintf(fLog, "Error reading message from node \n");
fflush(fLog);
exit(-1);
}
// Close the node socket
close(fdSocketClient);
printf("Message received \n");
fflush(stdout);
// Calculate the partial total send time and total recvd time
timeradd(&totalSendTime, &message.sent, &totalSendTime);
timeradd(&totalRecTime, &message.recvd, &totalRecTime);
// If this is the last iteration compute total time and fly time
if (var == availableNodes - 2)
{
lastRecTime = message.recvd;
timersub(&firstSendTime, &message.recvd, &totalTimes[count]);
timersub(&totalRecTime, &totalSendTime, &flyTimes[count]);
}
}
// If this is the last iteration compute
if (count == 9)
{
struct timeval averageTotTime;
struct timeval averageFlyTime;
// Define the message variable
stat_t stmsg;
// Initialize the message
stat_message_init(&stmsg);
for (int var = 0; var < 10; var++)
{
timeradd(&averageTotTime, &totalTimes[var], &averageTotTime);
timeradd(&averageFlyTime, &flyTimes[var], &averageFlyTime);
}
// Compute total bandwith
float totBand = (float)((sizeof(message_t) * availableNodes) / (averageTotTime.tv_sec + (averageTotTime.tv_usec * 1000)));
float flyBand = (float)((sizeof(message_t) * availableNodes) / (averageFlyTime.tv_sec + (averageFlyTime.tv_usec * 1000)));
// Store results in the meessage
stat_message_set_totBitrate(&stmsg, totBand);
stat_message_set_flyBitrate(&stmsg, flyBand);
// Obtain the ip of the Professor server
char* ipProf = stat_get_RURZ_addr();
// Connect to the Professor server
int fdSocketProf = net_client_connection(ipProf);
// Send the message to the server
if (write(fdSocketProf, &stmsg, sizeof(stat_t)) <= 0)
{
perror("Error sending message to the next node");
fprintf(fLog, "Error sending message to the next node \n");
fflush(fLog);
exit(-1);
}
// Close the the communication with server
close(fdSocketProf);
printf("Message sent \n");
}
}
// our node is not the turn leader
else
{
if(count == 0) {
printf("I'm not the turn leader \n");
fflush(stdout);
}
// Initialize variable for last node
bool lastNode = false;
// Our node has not already received the message
if (msg == NULL)
{
// Wait the message from a node
int fdSocketClient = net_accept_client(fdSocket, NULL);
// Read the message from the client
if (read(fdSocketClient, &message, sizeof(message_t)) <= 0)
{
perror("Error reading message from previous node");
fprintf(fLog, "Error reading message from previous node \n");
fflush(fLog);
exit(-1);
}
// Close the socket node
close(fdSocketClient);
printf("Message received \n");
fflush(stdout);
}
// Our node has already received the message
else
{
message = *msg;
msg = NULL;
}
// Reset the receiving time
msg_set_recv(&message);
// Mark our node as visited
msg_mark(&message, idNode);
// Initilize the id of next node
node_id idNextNode;
// Control if all nodes are visited
if (msg_all_visited(&message))
{
// Select the turn leader as next node
idNextNode = message.turnLeader;
lastNode = true;
}
else
{
// Select randomly the next node
idNextNode = msg_rand(&message);
}
// Set our id in the message
msg_set_ids(&message, idNode, message.turnLeader);
// Set sending time n the message
msg_set_sent(&message);
// Get ip address of the next node
char* ipNextNode = iptab_getaddr(idNextNode);
// Connect to the next node
int fdSocketNextNode = net_client_connection(ipNextNode);
// Send the message to the next node
if (write(fdSocketNextNode, &message, sizeof(message_t)) <= 0)
{
perror("Error sending message to the next node");
fprintf(fLog, "Error sending message to the next node \n");
fflush(fLog);
exit(-1);
}
// Close the the communication with the next node
close(fdSocketNextNode);
printf("Message sent \n");
fflush(stdout);
if (lastNode == true)
{
// Get ip address of the turn leader
char* ipTurnLeader = iptab_getaddr(message.turnLeader);
// Connect to the turn leader
int fdSocketTurnLeader = net_client_connection(ipTurnLeader);
// Send the message to the turn leader
if (write(fdSocketTurnLeader, &message, sizeof(message_t)) <= 0)
{
perror("Error sending message to the turn leader");
fprintf(fLog, "Error sending message to the turn leader \n");
fflush(fLog);
exit(-1);
}
// Close the the communication with the next node
close(fdSocketNextNode);
printf("Message sent");
fflush(stdout);
}
}
}
// Change state
state = 1;
}
int main()
{
// Initialize variables
FILE *fLog; // Log file
// Create the log file
fLog = fopen("assignment3.log", "w");
// Control if there is an error in function fopen
if (fLog == NULL)
{
perror("Creating log file");
exit(-1);
}
// initialize the server
fdSocket = net_server_init();
// Control if there is an error initilizing the socket
if (fdSocket <= 0)
{
printf("Error initializing the server");
fflush(stdout);
fprintf(fLog, "Error initializing the server \n");
fflush(fLog);
fprintf(fLog, "Process exiting... \n");
fflush(fLog);
exit(-1);
}
// Obtain the id of our node
idNode = iptab_get_ID_of(ipAddr);
// Get lenght of the address table
lenghtTable = iptab_len();
// Set timeout time
timeout.tv_sec = 30;
timeout.tv_usec = 0;
while (voteCount < 10)
{
if (state == 0 && voteCount == 0)
{
printf("Handshake \n");
fflush(stdout);
handshake();
}
else if (state == 1)
{
printf("Vote \n");
fflush(stdout);
vote();
}
else if (state == 2)
{
printf("Turn \n");
fflush(stdout);
turn(NULL);
}
}
// Close log fie and control if there is an error in function fclose
if (fclose(fLog) < 0)
{
perror("Close log file");
exit(-1);
}
fprintf(fLog, "Master is exiting... \n");
fflush(fLog);
fprintf(fLog, "Master exited with return value: 0 \n");
fflush(fLog);
printf("\nMaster is exiting...");
fflush(stdout);
printf("\nReturn value: 0 \n\n");
fflush(stdout);
return 0;
}