-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.c
732 lines (648 loc) · 28.5 KB
/
client.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
//
// Created by barry on 9/29/15.
//
#include "list.h"
#include "socketUtils.h"
#include "client.h"
#include "packetUtils.h"
#include "main.h"
#include "server.h"
#include <errno.h>
#include <stdio.h>
#include <dirent.h>
fd_set masterFDList, tempFDList; //Master file descriptor list to add all sockets and stdin
int fdmax; //to hold the max file descriptor value
struct host *masterServer; //details about the master server
int connectionIdGenerator; //index for connections
int runClient(char *port) {
char *name = "Client";
//initialize masterServer, peerList and hostlist
peerList = NULL;
connectionList = NULL;
connectionIdGenerator = 2; //1 is received for the server
masterServer = NULL;
int listernerSockfd = -1;
struct connectionInfo *serverInfo = startServer(port, "CLIENT");
if (serverInfo == NULL) {
fprintf(stderr, "Did not get serverInfo from startServer()\n");
return -1;
}
listernerSockfd = serverInfo->listernerSockfd;
int STDIN = 0; //0 represents STDIN
FD_ZERO(&masterFDList); // clear the master and temp sets
FD_ZERO(&tempFDList);
FD_SET(STDIN, &masterFDList); // add STDIN to master FD list
fdmax = STDIN;
FD_SET(listernerSockfd, &masterFDList); //add the listener to master FD list and update fdmax
if (listernerSockfd > fdmax)
fdmax = listernerSockfd;
int actionComplete = 1;
while (1) //keep waiting for input, connections and data
{
//this is ti identify is an activity is in progress
if (actionComplete == 1) {
printf("$");
fflush(stdout); //print the terminal symbol
}
actionComplete = 1;
tempFDList = masterFDList; //make a copy of masterFDList and use it as select() modifies the list
//int select(int numfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout);
if (select(fdmax + 1, &tempFDList, NULL, NULL, NULL) ==
-1) //select waits till it gets data in an fd in the list
{
fprintf(stderr, "Error in select\n");
return -1;
}
// an FD has data so iterate through the list to find the fd with data
int fd;
for (fd = 0; fd <= 20; fd++) {
if (FD_ISSET(fd, &tempFDList)) //found the FD with data
{
if (fd == STDIN) //data from commandLine(STDIN)
{
size_t commandLength = 50;
char *command = (char *) malloc(commandLength);
getline(&command, &commandLength, stdin); //get line the variable if space is not sufficient
if (stringEquals(command, "\n")) //to handle the stray \n s
continue;
//printf("--Got data: %s--\n",command);
handleCommands(command, "CLIENT");
}
else if (fd == listernerSockfd) //new client trying to connect to listener
{
struct sockaddr_storage clientaddr;
socklen_t addrlen = sizeof clientaddr;
int clientsockfd;
if ((clientsockfd = accept(listernerSockfd, (struct sockaddr *) &clientaddr, &addrlen)) == -1) {
fprintf(stderr, "Error accepting connection: %d %s\n", clientsockfd,
gai_strerror(clientsockfd));
return -1;
}
else {
//accept connection from client add it to the connectionList
struct host *client = (struct host *) malloc(sizeof(struct host));
struct sockaddr *hostAddress = (struct sockaddr *) &clientaddr;
client->id = connectionIdGenerator++;
client->sockfd = clientsockfd;
client->ipAddress = getIPAddress(hostAddress);
client->hostName = getHostFromIp(client->ipAddress);
client->port = getPort(hostAddress);
addNode(&connectionList, client);
FD_SET(client->sockfd, &masterFDList); // add fd to fdlist
if (client->sockfd > fdmax)
fdmax = client->sockfd;
printf("Accepted client: %s/%s.\n", client->hostName, client->port);
}
}
else if (masterServer != NULL && fd == masterServer->sockfd)// handle data from server
{
struct packet *recvPacket = readPacket(fd);
if (recvPacket == NULL) {
printf("Master Server has terminated unexpectedly.\n");
int connectionId = getIDForFD(connectionList, fd);
terminateConnection(connectionId);
masterServer = NULL;
peerList = NULL;
continue;
}
// printf("Received packet: ");
// printPacket(recvPacket);
// printf("\n");
//received terminate from server
if (recvPacket->header->messageType == terminate) {
printf("Received TERMINATE command from server.\n");
int connectionId = getIDForFD(connectionList, fd);
terminateConnection(connectionId);
peerList = NULL;
masterServer = NULL;
continue;
}
if (recvPacket->header->messageType == hostList) {
//split the hostlist
int length = 0;
char **hostinfo = splitString(recvPacket->message, ' ', &length);
free(peerList);
peerList = NULL; //destroy the old peerList
int i;
for (i = 0; i < length; i = i + 2) {
if (i + 1 >= length)
fprintf(stderr, "Disproportionate terms in hostList sent by server.\n");
if (stringEquals(myIpAddress, hostinfo[i]) && stringEquals(myListenerPort, port)) {
//this is so that the client doesn't add itself in the peerList
continue;
}
//add all nodes
struct host *peer = (struct host *) malloc(sizeof(struct host));
peer->sockfd = -1; // we do not have a connection with it yet
peer->ipAddress = hostinfo[i];
peer->hostName = getHostFromIp(peer->ipAddress);
peer->port = hostinfo[i + 1];
addNode(&peerList, peer);
}
printf("New peerList received from server:\n");
printPeerList(peerList);
continue;
}
if (recvPacket->header->messageType == syncFiles) {
printf("Received a sync initiate request from Server.\n");
syncHostnameFiles();
continue;
}
}
else {
//handle data from the peers
struct packet *recvPacket = readPacket(fd);
if (recvPacket == NULL) {
//one of the clients terminated unexpectedly
int id = getIDForFD(connectionList, fd);
struct host *host = getHostByID(connectionList, id);
printf("%s/%s terminated unexpectedly. Removing it from the list.\n",
host->hostName, host->port, host->sockfd);
terminateConnection(id);
continue;
}
// printf("Received packet: ");
// printPacket(recvPacket);
// printf("\n");
//received terminate
if (recvPacket->header->messageType == terminate) {
int id = getIDForFD(connectionList, fd);
struct host *source = getHostByID(connectionList, id);
printf("Received TERMINATE command from %s/%s. Removing it.\n", source->hostName, source->port);
terminateConnection(id);
continue;
}//handle error message
else if (recvPacket->header->messageType == error) {
printf("Received error message: %s\n", recvPacket->message);
continue;
}//handle get request
else if (recvPacket->header->messageType == get) {
int connectionId = getIDForFD(connectionList, fd);
struct host *destination = getHostByID(connectionList, connectionId);
if (destination == NULL) {
fprintf(stderr, "Coudn't find the connection id.\n");
continue;
}
printf("Received a get request for file %s from client: %s/%s.\n",
recvPacket->header->fileName, destination->hostName, destination->port);
sendFile(connectionId, recvPacket->header->fileName);
}//hand a put packet
else if (recvPacket->header->messageType == put) {
int connectionId = getIDForFD(connectionList, fd);
receiveFileAsynchronously(connectionId, recvPacket);
actionComplete = 0; //action is not complete until a ok packet is received
continue;
}//handle a ok packet
else if (recvPacket->header->messageType == ok) {
int connectionId = getIDForFD(connectionList, fd);
okPacketHandler(connectionId, recvPacket);
actionComplete = 1; // activity completed
continue;
}
}
}
}
}
}
int connectToHost(char *hostName, char *port) //connects to give ipaddress or hostname/portnumber
// adds the sock fd masterFDList and returns the sockfd
{
struct addrinfo *serverAddressInfo = getAddressInfo(hostName, port);
if (serverAddressInfo == NULL) {
return -1;
}
// printf("Server IP Address: %s Port: %s\n",
// getIPAddress(serverAddressInfo->ai_addr), getPort(serverAddressInfo->ai_addr));
int clientSocketfd;
if ((clientSocketfd = socket(serverAddressInfo->ai_family, serverAddressInfo->ai_socktype,
serverAddressInfo->ai_protocol)) == -1) {
fprintf(stderr, "Error Creating socket: %d %s\n", clientSocketfd, gai_strerror(clientSocketfd));
return -1;
} else {
//printf("Created Socket.\n");
}
int connectStatus;
if ((connectStatus = connect(clientSocketfd, serverAddressInfo->ai_addr, serverAddressInfo->ai_addrlen)) == -1) {
fprintf(stderr, "Error connecting to host %d %s\n", connectStatus, gai_strerror(connectStatus));
freeaddrinfo(serverAddressInfo); // free the address list
return -1;
}
else {
//printf("Connected to %s.\n", hostName); //#needtomodify this looks like a hack
FD_SET(clientSocketfd, &masterFDList); // add clientSocketfd to master FD list
if (clientSocketfd > fdmax)
fdmax = clientSocketfd;
freeaddrinfo(serverAddressInfo); // free the address list
return clientSocketfd;
}
}
int registerToServer(char *hostName, char *port) {
//hostName = "localhost"; //for testing on local machine #needtomodify
printf("Registering to Server %s/%s.\n", hostName, port);
if (masterServer != NULL) {
printf("The client is already registered to the master server.\n");
return 0;
}
int serversockfd = connectToHost(hostName, port); //connect and get sockfd
if (serversockfd == -1) {
fprintf(stderr, "There is no server running on given hostname/port\n");
}
else {
//add server to the connectionList
masterServer = (struct host *) malloc(sizeof(struct host));
masterServer->id = 1;
masterServer->hostName = hostName;
masterServer->ipAddress = getIpfromHost(hostName); // change to hostName
masterServer->port = port;
masterServer->sockfd = serversockfd;
addNode(&connectionList, masterServer);
//once registered send the listener port of the client to the server
char *message = myListenerPort;
struct packet *pckt = packetBuilder(registerHost, NULL, strlen(message), message);
char *packetString = packetDecoder(pckt);
//printf("Port Packet: %s\n", packetString);
int bytes_sent = send(masterServer->sockfd, packetString, strlen(packetString), 0);
if (bytes_sent != -1) {
//printf("Sent the listerner port %s to the server.\n", message);
}
free(pckt);
}
return 0;
}
int connectToClient(char *hostName, char *port) {
//check if the host is trying to connect to itself
if ((stringEquals(hostName, myHostName) || stringEquals(hostName, myIpAddress))
&& (stringEquals(port, myListenerPort))) {
printf("You can't connect to yourself.\n");
return -1;
}
//Check if hostname/ip and port is registered to server
if (!isHostPresent(peerList, hostName, port)) {
printf("The client you are trying to connect to is not registered with the server.\n");
return -1;
}
//duplicate connection
if (isHostPresent(connectionList, hostName, port)) {
printf("You are already connected to %s/%s.\n", hostName, port);
return -1;
}
//check if the user entered a ipAddress or hostName
if (stringEquals(getIpfromHost(hostName), hostName)) {
//if true the user entered an ipaddress
hostName = getHostFromIp(hostName);
}
//printf("Connecting to %s/%s.\n", hostName, port);
int clientSockfd = connectToHost(hostName, port); //"localhost" should be hostName #needtomodify
if (clientSockfd == -1) {
fprintf(stderr, "Error connecting to client\n");
}
else {
//add the client to the connectionList
struct host *client = (struct host *) malloc(sizeof(struct host));
client->id = connectionIdGenerator++;
client->sockfd = clientSockfd;
client->ipAddress = getIpfromHost(hostName);
client->hostName = hostName;
client->port = port;
addNode(&connectionList, client);
printf("Connected to %s/%s\n", client->hostName, client->port);
}
return 0;
}
int printConnectionList(struct list *head) {
struct list *current = head;
if (current == NULL) {
fprintf(stdout, "There are no connections at the moment.\n");
return 0;
}
else {
printf("id:\tHostname\t\t\tIP Address\t\tPort No.\n");
do {
struct host *currenthost = (struct host *) current->value;
printf("%d:\t%s\t%s\t\t%s\n", currenthost->id, currenthost->hostName,
currenthost->ipAddress, currenthost->port);
current = current->next;
} while (current != NULL);
}
return 0;
}
int printPeerList(struct list *head) {
struct list *current = head;
if (current == NULL) {
fprintf(stdout, "There are no other clients registered to the server at the moment.\n");
return 0;
}
else {
printf("Hostname\t\t\tIP Address\tPort No.\n");
do {
struct host *currenthost = (struct host *) current->value;
printf("%s\t%s\t%s\n", currenthost->hostName,
currenthost->ipAddress, currenthost->port);
current = current->next;
} while (current != NULL);
}
return 0;
}
int terminateConnection(int connectionId) {
struct host *host = getHostByID(connectionList, connectionId);
if (host == NULL) {
printf("Connection %d not found.\n", connectionId);
return 0;
}
//send terminate message to the corresponding peer
char *message = "";
struct packet *pckt = packetBuilder(terminate, NULL, strlen(message), message);
char *packetString = packetDecoder(pckt);
//printf("TERMINATE Packet: %s\n", packetString);
int bytes_sent = send(host->sockfd, packetString, strlen(packetString), 0);
//printf("Sent Terminate: %d\n", bytes_sent);
//close sock and remove it from fdlist and connection list
close(host->sockfd);
FD_CLR(host->sockfd, &masterFDList);
connectionList = removeNodeById(connectionList, connectionId);
masterServer = NULL;
// if (connectionList == NULL) {
// printf("Got empty connection list\n");
// }
printf("Terminated Connection %d with %s/%s.\n", connectionId, host->hostName, host->port);
return 0;
}
void quitClient() {
if (connectionList == NULL) {
printf("Bye!\n");
exit(0);
}
//Build terminate packet
char *message = "";
struct packet *pckt = packetBuilder(terminate, NULL, strlen(message), message);
char *packetString = packetDecoder(pckt);
//printf("TERMINATE Packet: %s\n", packetString);
//send terminate message all connected peers
int bytes_sent;
struct list *current = connectionList;
do {
struct host *currenthost = (struct host *) current->value;
//printf("FD: %d\n", currenthost->sockfd);
bytes_sent = send(currenthost->sockfd, packetString, strlen(packetString), 0);
//printf("Sent Terminate: %d to FD: %d\n", bytes_sent, currenthost->sockfd);
close(currenthost->sockfd);
FD_CLR(currenthost->sockfd, &masterFDList);
//printf("Closed SockFd: %d\n", currenthost->sockfd);
current = current->next;
} while (current != NULL);
free(connectionList);
printf("Bye!\n");
exit(0);
}
int getFile(int connectionId, char *filename) {
//get the destination host
struct host *destination = getHostByID(connectionList, connectionId);
if (destination == NULL) {
printf("There is no connection with connection id: %d\n", connectionId);
return -1;
}
//build a get request
char *message = "";
struct packet *pckt = packetBuilder(get, filename, strlen(message), message);
char *packetString = packetDecoder(pckt);
//printf("Get Request Packet: %s\n", packetString);
//send the get request
int bytes_sent = send(destination->sockfd, packetString, strlen(packetString), 0);
if (bytes_sent != strlen(packetString)) {
fprintf(stderr, "Get Request not fully sent.Bytes sent: %d, Packet Length: %d\n", bytes_sent,
strlen(packetString));
return -1;
}
return 0;
}
int putFile(int connectionId, char *filename) {
//get the destination
struct host *destination = getHostByID(connectionList, connectionId);
if (destination == NULL) {
printf("Count't find connection id to send file.\n");
return -1;
}
//get the filename removing the directory names
int filenamePartLength;
char **filenameParts = splitString(strdup(filename), '/', &filenamePartLength);
char *justFilename = filenameParts[filenamePartLength - 1];
//open file
FILE *fp = fopen(filename, "rb"); //rb is for opening binary files
if (fp == NULL) {
printf("Filename: %s, %s\n", filename, strerror(errno));
return -2;
}
printf("Starting to send file: %s at %s.\n", justFilename, printCurrentTime());
//Read data for first packet
char buff[PACKET_SIZE];
int data_read = 0;
int bytes_read = fread(buff, 1, PACKET_SIZE, fp);
if (bytes_read < PACKET_SIZE)
buff[bytes_read] = 0;
data_read += bytes_read;
//printf("Bytes read %d \n", bytes_read);
if (bytes_read <= 0 && ferror(fp)) {
printf("Error reading file: %s\n", filename);
fclose(fp);
return -3;
}
struct packet *pckt = packetBuilder(put, justFilename, bytes_read, "");
char *packetString = packetDecoder(pckt);
// printf("Packet String: %s\n", packetString);
// printPacket(pckt);
int header_bytes_sent = send(destination->sockfd, packetString, strlen(packetString), 0);
//send the data separately as is it is not text
int data_bytes_sent = send(destination->sockfd, buff, bytes_read, 0);
//printf("Number of bytes sent: %d\n", header_bytes_sent + data_bytes_sent);
free(pckt);
pckt = NULL;
//check if there is more data if so send them in packets
while (true) {
if (bytes_read < PACKET_SIZE) {
if (feof(fp))
//reached EOF no need to print anything just beak
if (ferror(fp))
printf("Error reading file: %s\n", filename);
break;
}
bytes_read = fread(buff, 1, PACKET_SIZE, fp);
if (bytes_read < PACKET_SIZE)
buff[bytes_read] = 0;
data_read += bytes_read;
pckt = packetBuilder(put, justFilename, bytes_read, "");
packetString = packetDecoder(pckt);
//printf("Packet String: %s\n", packetString);
// printPacket(pckt);
header_bytes_sent = send(destination->sockfd, packetString, strlen(packetString), 0);
//send the data separately as it is not text
data_bytes_sent = send(destination->sockfd, buff, bytes_read, 0);
//printf("Number of bytes sent: %d\n", data_bytes_sent);
free(pckt);
pckt = NULL;
}
printf("Completed sending file: %s at %s.\n", justFilename, printCurrentTime());
//send a ok message to indicate completion.
pckt = packetBuilder(ok, justFilename, 0, "");
packetString = packetDecoder(pckt);
// printf("OK Packet String: %s\n", packetString);
// printPacket(pckt);
int bytes_sent = send(destination->sockfd, packetString, strlen(packetString), 0);
//printf("ok packet Number of bytes sent: %d\n", bytes_sent);
return 0;
}
int sendFile(int connectionId, char *filename) // this is include the error message that need to be sent
{
//get the filename removing the directory names
int filenamePartLength;
char **filenameParts = splitString(filename, '/', &filenamePartLength);
char *justFilename = filenameParts[filenamePartLength - 1];
//get the destination
struct host *destination = getHostByID(connectionList, connectionId);
if (destination == NULL) {
printf("Count't find connection id to send file.\n");
return -1;
}
//call putfile()
int status = putFile(connectionId, filename);
//incase of error sent error message to requestor
if (status == -2) {
//unable to open file send an error message to the requester.
char *errorMessage;
asprintf(&errorMessage, "%s", strerror(errno));
//printf("%s:%s:%d\n", errorMessage, strerror(errno), strlen(errorMessage));
struct packet *pckt = packetBuilder(error, justFilename, strlen(errorMessage), errorMessage);
char *packetString = packetDecoder(pckt);
//printf("Packet String: %s\n", packetString);
// printPacket(pckt);
int bytes_sent = send(destination->sockfd, packetString, strlen(packetString), 0);
return -2;
}
else if (status == -3) {
char *errorMessage;
asprintf(&errorMessage, "Error reading file: %s", filename);
struct packet *pckt = packetBuilder(error, filename, strlen(errorMessage), errorMessage);
char *packetString = packetDecoder(pckt);
//printf("Packet String: %s\n", packetString);
//printPacket(pckt);
int bytes_sent = send(destination->sockfd, packetString, strlen(packetString), 0);
return -3;
}
return 0;
}
int receiveFileAsynchronously(int connectionId, struct packet *recvPacket)
{
struct list *connection = getNodeByID(connectionList, connectionId);
struct host *source = (struct host *) connection->value;
if (connection->filePointer == NULL) {
printf("Started receiving file: %s at %s from %s/%s.\n$",
recvPacket->header->fileName, printCurrentTime(), source->hostName, source->port);
fflush(stdout);
// Create file where data will be stored
char *filename = recvPacket->header->fileName;
char *tempfilename = stringConcat("./", filename);
FILE *fp = fopen(tempfilename, "wb"); //for testing it should be filename
if (fp == NULL) {
printf("Error opening file.\n");
deletePacketAndMessage(recvPacket);
return -1;
}
//update the File pointer to the connection
connection->filePointer = fp;
//write the packet received
int written_bytes = fwrite(recvPacket->message, 1, recvPacket->header->length, connection->filePointer);
deletePacketAndMessage(recvPacket);
}
else {
//write the packet received
int written_bytes = fwrite(recvPacket->message, 1, recvPacket->header->length, connection->filePointer);
deletePacketAndMessage(recvPacket);
}
return 0;
}
int okPacketHandler(int connectionId, struct packet *recvPacket)
{
struct list *connection = getNodeByID(connectionList, connectionId);
struct host *source = (struct host *) connection->value;
if (connection->filePointer != NULL) {
//print completion of file download
printf("Finished receiving file: %s at %s from %s/%s.\n",
recvPacket->header->fileName, printCurrentTime(), source->hostName, source->port);
//close the file pointer
fclose(connection->filePointer);
//make the file pointer in the connection to indicate that no file operation is in progress
connection->filePointer = NULL;
}
return 0;
}
int startSync() {
//check if the client is registered to the server
if (masterServer == NULL) {
printf("Client is not registered to the server. Please register first to proceed.\n");
return 0;
}
//build a sync packet
struct packet *packet = packetBuilder(syncFiles, NULL, 0, NULL);
char *packetString = packetDecoder(packet);
//printf("Sync Packet: %s\n", packetString);
//send sync message to the server for broadcast
int bytes_sent = send(masterServer->sockfd, packetString, strlen(packetString), 0);
printf("Sync request sent to server.\n");
return 0;
}
int syncHostnameFiles()
{
//open current directory and send all files to all the connected peers
char *directory = "./";
char *filename = stringConcat(myHostName, ".txt");
//send file for all peers in connectionList
struct list *listIterator = connectionList;
struct host *destination;
while (listIterator != NULL) {
destination = (struct host *) listIterator->value;
//if destination is the master server do not send
if (masterServer != NULL && destination->sockfd == masterServer->sockfd) {
listIterator = listIterator->next;
continue;
}
filename = stringConcat(directory, filename);
putFile(destination->id, filename);
listIterator = listIterator->next;
}
//printf("Sent file: %s\n", filename);
return 0;
}
int syncAll() {
//open current directory and send all files to all the connected peers
DIR *d;
struct dirent *dir;
char *directory = "./";
char *filename = "";
d = opendir(directory);
if (d != NULL) {
while ((dir = readdir(d)) != NULL) {
//for each file
if (dir->d_type == DT_REG) {
//for all peers in connectionList
struct list *listIterator = connectionList;
struct host *destination;
while (listIterator != NULL) {
destination = (struct host *) listIterator->value;
//if destination is the master server do not send
if (masterServer != NULL && destination->sockfd == masterServer->sockfd) {
listIterator = listIterator->next;
continue;
}
filename = stringConcat(directory, dir->d_name);
putFile(destination->id, filename);
listIterator = listIterator->next;
}
printf("Sent file: %s\n", filename);
}
}
closedir(d);
}
else {
printf("Unable to open directory.\n");
}
return 0;
}