-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnaming_server.c
More file actions
1228 lines (969 loc) · 36.3 KB
/
naming_server.c
File metadata and controls
1228 lines (969 loc) · 36.3 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
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
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <ifaddrs.h>
#include <net/if.h>
#include <netinet/in.h>
#include <sys/select.h>
#include <sys/time.h>
#include <sys/types.h>
#include <stdbool.h>
#include <time.h>
#include <stdarg.h>
#include <pthread.h>
#include <dirent.h>
#include <sys/stat.h>
#define PORT 0
#define MAX_LENGTH 1024
#define MAX_STORAGE_SERVERS 100
#define MAX_PATHS 100
#define MAX_PATH_ENTRIES 1000
typedef struct StorageServer {
int id;
char ip[INET_ADDRSTRLEN];
int client_port; // Port for client communication
int ss_socket; // Socket descriptor for NM-SS communication
int nm_port;
int num_paths;
char paths[MAX_PATH_ENTRIES][MAX_LENGTH];
bool running;
time_t last_heartbeat;
int beatcount;
} StorageServer;
#define MAX_CACHE_SIZE 100 // Fixed maximum cache size
typedef struct {
char path[MAX_LENGTH];
StorageServer* ss;
} CacheEntry;
#define MAX_SUB_DIR 100
#define MAX_DIR_NAME 256
typedef struct TrieNode {
char name[MAX_DIR_NAME];
struct TrieNode* children[MAX_SUB_DIR];
int storage_server_id;
} TrieNode;
TrieNode* root;
// FUNCTION DEFINITIONS
void add_storage_server(StorageServer* ss);
void close_storage_server(StorageServer* ss);
void add_path(char* file_path, int storage_server_id);
StorageServer* get_ss_for_path(char* file_path);
void delete_paths_with_ss_id(TrieNode* node, int storage_server_id);
void remove_storage_server_paths(int storage_server_id);
void print_paths(TrieNode* node, char* buffer, int depth);
void log_message(const char *format, ...);
int create_socket();
void close_connection(int sock_fd, fd_set *master_set);
void setup_server_socket(int sock_fd, int port);
void print_ip_addresses();
void send_data(int sock_fd, char *data, size_t data_size);
void receive_data(int sock_fd, char *buffer, size_t buffer_size);
int cache_lookup(const char* path, StorageServer** ss);
void cache_add(const char* path, StorageServer* ss);
char* get_file_name(char *path);
bool copy_directory(char *src_path, char *dest_path, int client_sock_fd);
bool copy_file(char *src_path, char *dest_path, int client_sock_fd);
void process_client_request(int sock_fd, char *message);
void process_storage_server_message(int sock_fd, char *message, StorageServer **storage_server);
void *monitor_storage_servers(void *arg);
void* listener(void* args);
// STORAGE SERVER
StorageServer* storage_servers[MAX_PATH_ENTRIES];
int ss_count = 0;
void add_storage_server(StorageServer* ss) {
for (int i = 0; i < ss->num_paths; i++) {
char* pass = strdup(ss->paths[i]);
add_path(pass, ss->id);
printf("Added path: %s\n", ss->paths[i]);
}
ss_count++;
printf("Added storage server %d with IP %s, client port %d with %d paths\n", ss->id, ss->ip, ss->client_port, ss->num_paths);
log_message("Added storage server %d with IP %s, client port %d with %d paths", ss->id, ss->ip, ss->client_port, ss->num_paths);
printf("Paths available now are:\n");
char buffer[1024];
bzero(buffer, sizeof(buffer));
print_paths(root, buffer, 0);
}
void close_storage_server(StorageServer* ss) {
storage_servers[ss->id]->running = false;
remove_storage_server_paths(ss->id);
ss_count--;
printf("Storage server %d is no longer running\n", ss->id);
log_message("Storage server %d is no longer running", ss->id);
printf("Paths available now are:\n");
char buffer[1024];
bzero(buffer, sizeof(buffer));
print_paths(root, buffer, 0);
}
int connect_to_ss(StorageServer* ss) {
int ss_sock_fd = create_socket();
// Set up the storage server address
struct sockaddr_in ss_addr;
memset(&ss_addr, 0, sizeof(ss_addr));
ss_addr.sin_family = AF_INET;
ss_addr.sin_port = htons(ss->nm_port);
if (inet_pton(AF_INET, ss->ip, &ss_addr.sin_addr) <= 0) {
perror("Invalid storage server address");
return -1;
}
// Connect to the storage server
if (connect(ss_sock_fd, (struct sockaddr *)&ss_addr, sizeof(ss_addr)) < 0) {
perror("Connection to storage server failed");
close(ss_sock_fd);
return -1;
}
return ss_sock_fd;
}
// CACHE
CacheEntry cache[MAX_CACHE_SIZE];
int cache_size = 0;
int cache_start = 0; // Points to the oldest entry
// TRIE
void add_path(char* file_path, int storage_server_id) {
char* token = strtok(file_path, "/");
TrieNode* current = root;
while (token != NULL) {
bool found = false;
for (int i = 0; i < MAX_SUB_DIR; i++) {
if (current->children[i] != NULL && strcmp(current->children[i]->name, token) == 0) {
current = current->children[i];
found = true;
break;
}
}
if (!found) {
TrieNode* new_node = (TrieNode*) malloc(sizeof(TrieNode));
strcpy(new_node->name, token);
for (int i = 0; i < MAX_SUB_DIR; i++) {
new_node->children[i] = NULL;
}
new_node->storage_server_id = -1;
for (int i = 0; i < MAX_SUB_DIR; i++) {
if (current->children[i] == NULL) {
current->children[i] = new_node;
current = new_node;
break;
}
}
}
token = strtok(NULL, "/");
}
current->storage_server_id = storage_server_id;
}
StorageServer* get_ss_for_path(char* fp) {
char* file_path = strdup(fp);
char* token = strtok(file_path, "/");
TrieNode* current = root;
while (token != NULL) {
bool found = false;
for (int i = 0; i < MAX_SUB_DIR; i++) {
if (current->children[i] != NULL && strcmp(current->children[i]->name, token) == 0) {
current = current->children[i];
found = true;
break;
}
}
token = strtok(NULL, "/");
}
if (current->storage_server_id == -1) return NULL;
return storage_servers[current->storage_server_id];
}
void delete_paths_with_ss_id(TrieNode* node, int storage_server_id) {
if (node == NULL) return;
for (int i = 0; i < MAX_SUB_DIR; i++) {
if (node->children[i] != NULL) {
delete_paths_with_ss_id(node->children[i], storage_server_id);
if (node->children[i]->storage_server_id == storage_server_id) {
free(node->children[i]);
node->children[i] = NULL;
}
}
}
}
void remove_storage_server_paths(int storage_server_id) {
TrieNode* current = root;
delete_paths_with_ss_id(current, storage_server_id);
}
void print_paths(TrieNode* node, char* buffer, int depth) {
if (!node) return;
// Append the current node's name to the path buffer
if (depth > 0) {
strcat(buffer, "/");
}
strcat(buffer, node->name);
// If this node marks the end of a storage server path, print the path
if (node->storage_server_id != -1) {
printf("%s (Storage Server ID: %d)\n", buffer, node->storage_server_id);
}
// Traverse all children
for (int i = 0; i < MAX_SUB_DIR; i++) {
if (node->children[i]) {
// Save the current state of the buffer before recursion
char temp[MAX_LENGTH];
strcpy(temp, buffer);
// Recurse to the child
print_paths(node->children[i], buffer, depth + 1);
// Restore the buffer
strcpy(buffer, temp);
}
}
// Remove the current node's name from the buffer for backtracking
if (depth > 0) {
char* last_slash = strrchr(buffer, '/');
if (last_slash) {
*last_slash = '\0';
}
} else {
buffer[0] = '\0'; // Reset the buffer if at the root
}
}
// Logs a message to log.txt with a timestamp
void log_message(const char *format, ...) {
// Prepare the message
va_list args;
va_start(args, format);
char log_buffer[MAX_LENGTH];
vsnprintf(log_buffer, sizeof(log_buffer), format, args);
va_end(args);
// Open log.txt in append mode
FILE *log_file = fopen("log.txt", "a");
if (log_file == NULL) {
perror("Could not open log.txt");
return;
}
// Write the log message without timestamp
fprintf(log_file, "%s\n", log_buffer);
fclose(log_file);
}
// SERVER
int create_socket() {
int sock_fd = socket(AF_INET, SOCK_STREAM, 0);
if (sock_fd == -1) {
perror("Socket creation failed");
exit(EXIT_FAILURE);
}
return sock_fd;
}
void close_connection(int sock_fd, fd_set *master_set) {
close(sock_fd);
FD_CLR(sock_fd, master_set);
}
void setup_server_socket(int sock_fd, int port) {
struct sockaddr_in server_addr;
socklen_t addrlen = sizeof(server_addr);
// Zero out the structure
memset(&server_addr, 0, sizeof(server_addr));
// Assign IP and PORT
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = INADDR_ANY; // Accept connections from any IP
server_addr.sin_port = htons(port);
// Bind the socket
if (bind(sock_fd, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
perror("Bind failed");
exit(EXIT_FAILURE);
}
if (getsockname(sock_fd, (struct sockaddr *)&server_addr, &addrlen) == -1) {
perror("getsockname");
exit(EXIT_FAILURE);
}
int portnum = ntohs(server_addr.sin_port);
printf("Port: %i\n", portnum);
// Listen
if (listen(sock_fd, SOMAXCONN) < 0) {
perror("Listen failed");
exit(EXIT_FAILURE);
}
}
void print_ip_addresses() {
struct ifaddrs *ifaddr, *ifa;
int family;
char host[NI_MAXHOST];
if (getifaddrs(&ifaddr) == -1) {
perror("getifaddrs");
exit(EXIT_FAILURE);
}
printf("Server IP addresses:\n");
// Walk through linked list of interfaces
for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
if (ifa->ifa_addr == NULL)
continue;
family = ifa->ifa_addr->sa_family;
// Check for IPv4 addresses (AF_INET) and exclude loopback interface
if (family == AF_INET && (ifa->ifa_flags & IFF_LOOPBACK) == 0) {
// Get the IP address
if (getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in),
host, NI_MAXHOST,
NULL, 0, NI_NUMERICHOST) == 0) {
printf("\tInterface : %s\n", ifa->ifa_name);
printf("\t Address : %s\n", host);
}
}
}
freeifaddrs(ifaddr);
}
// HELPERS
void send_data(int sock_fd, char *data, size_t data_size) {
if (sock_fd == -1) {
return;
}
if (send(sock_fd, data, data_size, 0) != data_size) {
perror("Send failed");
exit(EXIT_FAILURE);
}
}
void receive_data(int sock_fd, char *buffer, size_t buffer_size) {
bzero(buffer, buffer_size);
ssize_t bytes_received = recv(sock_fd, buffer, buffer_size, 0);
if (bytes_received < 0) {
perror("Receive failed");
exit(EXIT_FAILURE);
}
// Null-terminate if necessary
buffer[bytes_received] = '\0';
}
// CACHE
int cache_lookup(const char* path, StorageServer** ss) {
int index = -1;
// Search the cache
for (int i = 0; i < cache_size; i++) {
int pos = (cache_start + i) % MAX_CACHE_SIZE;
if (strcmp(path, cache[pos].path) == 0) {
// Found matching entry
index = pos;
break;
}
}
if (index != -1) {
// Move entry to the most recently used position
CacheEntry temp = cache[index];
for (int i = index; i != (cache_start + cache_size - 1) % MAX_CACHE_SIZE; i = (i + 1) % MAX_CACHE_SIZE) {
int next = (i + 1) % MAX_CACHE_SIZE;
cache[i] = cache[next];
}
int end = (cache_start + cache_size - 1) % MAX_CACHE_SIZE;
cache[end] = temp;
*ss = cache[end].ss;
// *ss = cache[index].ss;
return 1; // Cache hit
}
return 0; // Cache miss
}
void cache_add(const char* path, StorageServer* ss) {
if (cache_size < MAX_CACHE_SIZE) {
// Add new entry at the end
int pos = (cache_start + cache_size) % MAX_CACHE_SIZE;
strcpy(cache[pos].path, path);
cache[pos].ss = ss;
cache_size++;
} else {
// Overwrite oldest entry
cache_start = (cache_start + 1) % MAX_CACHE_SIZE;
int pos = (cache_start + cache_size - 1) % MAX_CACHE_SIZE;
strcpy(cache[pos].path, path);
cache[pos].ss = ss;
}
printf("Added to cache: %s\n", path);
}
// COPYING
#define WRITE_PACKET_SIZE 32
// Helper function to get the file name from a given path
char* get_file_name(char *path) {
char *file_name = strrchr(path, '/');
return (file_name) ? file_name + 1 : path;
}
// Function to copy a file, ensuring the destination path includes the file name
bool copy_file(char *src_path, char *dest_path, int client_sock_fd) {
// Send write confirmation to destination storage server
char* extension = strrchr(dest_path, '.');
StorageServer* dest_ss = get_ss_for_path(dest_path);
if (dest_ss == NULL) {
printf("ERR100: Destination path doesn't exist\n");
send_data(client_sock_fd, "ERR100: Destination path doesn't exist\n", 31);
return false;
}
int dest_ss_sock_fd = connect_to_ss(dest_ss);
if (dest_ss_sock_fd == -1) {
printf("Error connecting to storage server\n");
return false;
}
char* message = (char*)malloc(MAX_LENGTH);
snprintf(message, MAX_LENGTH, "WRITE %s", dest_path);
send_data(dest_ss_sock_fd, message, strlen(message));
printf("Sent operation to destination SS: %s\n", message);
StorageServer* src_ss = get_ss_for_path(src_path);
if (src_ss == NULL) {
printf("ERR100: Source path doesn't exist\n");
send_data(client_sock_fd, "ERR100: Source path doesn't exist\n", 31);
send_data(dest_ss_sock_fd, "exit", 4);
return false;
}
int src_ss_sock_fd = connect_to_ss(src_ss);
if (src_ss_sock_fd == -1) {
printf("Error connecting to storage server\n");
send_data(dest_ss_sock_fd, "exit", 4);
return false;
}
snprintf(message, MAX_LENGTH, "READ %s", src_path);
send_data(src_ss_sock_fd, message, strlen(message));
printf("Sent operation to source SS: %s\n", message);
printf("Starting reading.\n");
// Keep receiving data until an acknowledgment is received
char* temp_file = (char*)malloc(MAX_LENGTH);
snprintf(temp_file, MAX_LENGTH, "tmp%s", extension);
FILE *tmp_file = fopen(temp_file, "wb");
while (true) {
char buffer[MAX_LENGTH];
receive_data(src_ss_sock_fd, buffer, MAX_LENGTH);
if (strncmp(buffer, "ACK", 3) == 0) {
break;
}
if (strncmp(buffer, "ERR", 3) == 0) {
printf("Error reading file\n");
send_data(client_sock_fd, buffer, strlen(buffer));
send_data(dest_ss_sock_fd, "exit", 4);
return false;
}
fprintf(tmp_file, "%s", buffer);
}
fclose(tmp_file);
close(src_ss_sock_fd);
printf("File read successfully.\n");
send_data(dest_ss_sock_fd, "ASYNC", 5);
printf("Sent ASYNC command to destination SS %d\n", dest_ss->id);
char* buffer = (char*) malloc(MAX_LENGTH);
receive_data(dest_ss_sock_fd, buffer, MAX_LENGTH);
printf("Recieved ACK: %s\n", buffer);
if (strstr(buffer, "async_ack") == NULL){
printf("ERROR: Writing ASYNC data failed\n");
return false;
}
char write_buffer[WRITE_PACKET_SIZE];
char response_buffer[MAX_LENGTH];
FILE *file = fopen(temp_file, "rb");
if (file == NULL) {
printf("Error opening file\n");
return false;
}
int count = 0;
size_t bytes_read = fread(write_buffer, 1, WRITE_PACKET_SIZE, file);
while (bytes_read > 0) {
write_buffer[bytes_read] = '\0'; // Ensure null termination for safe string handling
// Send the chunk
send_data(dest_ss_sock_fd, write_buffer, strlen(write_buffer));
// Wait for acknowledgment
receive_data(dest_ss_sock_fd, response_buffer, MAX_LENGTH);
if (strncmp(response_buffer, "ERR", 3) == 0) {
printf("Error writing to file\n");
send_data(client_sock_fd, response_buffer, strlen(response_buffer));
return false;
}
if(strcmp(response_buffer, "rec_ack")!=0){
printf("Failed to send packet\n");
continue;
}
bytes_read = fread(write_buffer, 1, WRITE_PACKET_SIZE, file);
}
while (true) {
send_data(dest_ss_sock_fd, "STOP", 4);
bzero(response_buffer, MAX_LENGTH);
receive_data(dest_ss_sock_fd, response_buffer, MAX_LENGTH);
if (strcmp(response_buffer, "stop_ack") == 0) break;
}
fclose(file);
printf("File copied successfully.\n");
return true;
}
// Function to copy a directory recursively, ensuring proper path handling
bool copy_directory(char *src_path, char *dest_path, int client_sock_fd) {
// Get SS for source path
StorageServer* src_ss = get_ss_for_path(src_path);
if (src_ss == NULL) {
printf("ERR100: Source path doesn't exist\n");
send_data(client_sock_fd, "ERR100: Source path doesn't exist\n", 31);
return false;
}
// Connect to source storage server
int src_ss_sock_fd = connect_to_ss(src_ss);
if (src_ss_sock_fd == -1) {
printf("Error connecting to storage server\n");
return false;
}
// Get SS for destination path
StorageServer* dest_ss = get_ss_for_path(dest_path);
if (dest_ss == NULL) {
printf("ERR100: Destination path doesn't exist\n");
send_data(client_sock_fd, "ERR100: Destination path doesn't exist\n", 34);
return false;
}
// Send TREE command to source storage server
char command[MAX_LENGTH];
char buffer[MAX_LENGTH];
// Prepare and send the TREE command
snprintf(command, MAX_LENGTH, "TREE %s", src_path);
send_data(src_ss_sock_fd, command, strlen(command));
usleep(1000000);
char path[MAX_LENGTH];
bzero(path, sizeof(path));
path[0] = '/';
int depth = 0;
// Receive the directory tree from the server
while (true) {
send_data(src_ss_sock_fd, "NEXT", 4);
ssize_t bytes_received = recv(src_ss_sock_fd, buffer, MAX_LENGTH - 1, 0);
if (bytes_received <= 0) {
perror("Receive error or connection closed");
break;
}
buffer[bytes_received - 1] = '\0';
// Check for "END_OF_TREE" message
if (strncmp(buffer, "ACK", 3) == 0) {
printf("Directory listing completed.\n");
break;
}
int cur_depth = strstr(buffer, "|-- ") - buffer;
while (cur_depth <= depth) {
char* last_slash = strrchr(path, '/');
if (last_slash) {
*last_slash = '\0';
}
depth -= 4;
}
// Append the current directory to the path
snprintf(path + strlen(path), MAX_LENGTH - strlen(path), "/%s", strstr(buffer, "|-- ") + 4);
printf("%s\n", path);
// Connect to destination storage server
// Send the COPY command to the destination storage server, if the file is a path
char* filename = get_file_name(path);
if (strstr(buffer, ".") == NULL) {
printf("Creating directory: %s%s\n", dest_path, path);
// Connect to the destination storage server
int dest_ss_sock_fd = connect_to_ss(dest_ss);
if (dest_ss_sock_fd == -1) {
send_data(client_sock_fd, "ERR002: Invalid storage server address\n", 41);
return false;
}
char* command = (char*)malloc(MAX_LENGTH);
snprintf(command, MAX_LENGTH, "CREATE %s%s", dest_path, path);
send_data(dest_ss_sock_fd, command, strlen(command));
// Receive acknowledgment from the storage server
char* ack_buffer = (char*)malloc(MAX_LENGTH);
bzero(ack_buffer, MAX_LENGTH);
receive_data(dest_ss_sock_fd, ack_buffer, MAX_LENGTH);
if (strncmp(ack_buffer, "ACK", 3) == 0 || strncmp(ack_buffer, "ERR101", 6) == 0) {
printf("Directory creation success.\n");
} else {
printf("Directory creation failed: %s\n", ack_buffer);
send_data(client_sock_fd, ack_buffer, strlen(ack_buffer));
close(dest_ss_sock_fd);
return false;
}
close(dest_ss_sock_fd);
} else {
// Copy the file
char f_src_path[MAX_LENGTH];
char d_src_path[MAX_LENGTH];
snprintf(f_src_path, MAX_LENGTH, "%s%s", src_path, path);
snprintf(d_src_path, MAX_LENGTH, "%s%s", dest_path, path);
printf("Copying file: %s to %s\n", f_src_path, d_src_path);
copy_file(f_src_path, d_src_path, client_sock_fd);
}
depth = cur_depth;
}
return true;
}
bool backup(StorageServer* src, StorageServer* dest) {
printf("Backing up storage server %d in SS %d\n", src->id, dest->id);
log_message("Backing up storage server %d in SS %d and %d", src->id, dest->id);
// if (dest == NULL || !dest->running) {
// printf("Destination storage server not available.\n");
// log_message("Destination storage server not available.");
// return false;
// }
// for (int i = 0; i < src->num_paths; i++) {
// char* path = src->paths[i];
// char* dest_path = (char*) malloc(MAX_LENGTH);
// snprintf(dest_path, MAX_LENGTH, "backup_%d/%s", src->id, path);
// copy_directory(path, dest_path, -1);
// }
return true;
}
// PROCESSING
void process_client_request(int sock_fd, char *message) {
// Parse and process the client request
printf("Processing client request: %s\n", message);
// Log the client request
log_message("Processing client request on socket %d: %s", sock_fd, message);
// Parse the message
char* operation = (char*) malloc(MAX_LENGTH);
char* path = (char*) malloc(MAX_LENGTH);
char* dest = (char*) malloc(MAX_LENGTH);
char* message_dup = (char*) malloc(MAX_LENGTH);
strcpy(message_dup, message);
char *token = strtok(message, " ");
if (token == NULL) {
printf("Invalid client request.\n");
send_data(sock_fd, "ERR001: Invalid request\n", 25);
return;
}
strcpy(operation, token);
printf("Operation: %s\n", operation);
token = strtok(NULL, " ");
if (token == NULL) {
printf("Invalid client request: no path specified.\n");
send_data(sock_fd, "ERR007: No path specified\n", 27);
return;
}
strcpy(path, token);
printf("Path: %s\n", path);
if (strcmp(operation, "COPY") == 0) {
token = strtok(NULL, " ");
if (token == NULL) {
printf("Invalid client request: no destination specified.\n");
send_data(sock_fd, "ERR007: No destination specified\n", 34);
return;
}
strcpy(dest, token);
}
// Check LRU cache for the path
StorageServer *ss = NULL;
char* lookup = strdup(path);
char* last = strrchr(lookup, '/');
if (last != NULL && strchr(last, '.') != NULL) {
*last = '\0';
}
if (cache_lookup(lookup, &ss)) {
printf("Cache hit for path: %s\n", path);
}
if (ss == NULL) {
printf("Cache miss for path: %s\n", path);
ss = get_ss_for_path(lookup);
if(ss == NULL) {
printf("Path not found: %s\n", lookup);
log_message("Path not found for request on socket %d: %s", sock_fd, lookup);
send_data(sock_fd, "ERR100: Path not found\n", 24);
if (strcmp(operation, "COPY") == 0) {
send_data(sock_fd, "ERR107: Copy operation failed\n", 31);
free(path);
free(operation);
free(dest);
}
return;
}
cache_add(lookup, ss);
printf("Path: %s SS:%d\n", path, ss->id);
}
if (strcmp(operation, "READ") == 0 || strcmp(operation, "WRITE") == 0 ||
strcmp(operation, "INFO") == 0 || strcmp(operation, "STREAM") == 0 ||
strcmp(operation, "TREE") == 0) {
// Send information to the client about the storage server
printf("Client command.\n");
char response[MAX_LENGTH];
snprintf(response, sizeof(response), "IP: %s Port: %d", ss->ip, ss->client_port);
send_data(sock_fd, response, strlen(response));
printf("Response sent to client on socket %d: %s\n", sock_fd, response);
}
// Send the operation message to the storage server
if (strcmp(operation, "CREATE") == 0 || strcmp(operation, "DELETE") == 0) {
int ss_sock_fd = connect_to_ss(ss);
if (ss_sock_fd == -1) {
send_data(sock_fd, "ERR002: Invalid storage server address\n", 41);
return;
}
// Send the operation message to the storage server
send_data(ss_sock_fd, message_dup, strlen(message_dup));
// Receive acknowledgment from the storage server
char* ack_buffer = (char*)malloc(MAX_LENGTH);
bzero(ack_buffer, MAX_LENGTH);
receive_data(ss_sock_fd, ack_buffer, MAX_LENGTH);
// Send acknowledgment back to the client
send_data(sock_fd, ack_buffer, strlen(ack_buffer));
close(ss_sock_fd);
}
else if (strcmp(operation, "COPY") == 0) {
printf("Destination: %s\n", dest);
// copy_directory(path, dest, sock_fd);
// send_data(sock_fd, "ERR107: Copy operation failed\n", 31);
char* filename = get_file_name(path);
bool result;
if (strstr(filename, ".") != NULL) {
// Copy file
printf("Copying file\n");
result = copy_file(path, dest, sock_fd);
}
else {
// Copy directory
result = copy_directory(path, dest, sock_fd);
}
if (result) {
send_data(sock_fd, "ACK: COPY Successful\n", 23);
} else {
send_data(sock_fd, "ERR107: Copy operation failed\n", 31);
}
}
// After sending response
log_message("Response sent to client on socket %d", sock_fd);
free(path);
free(operation);
}
void process_storage_server_message(int sock_fd, char *message, StorageServer **storage_server) {
// Parse and process the storage server message
log_message("Processing storage server message on socket %d: %s", sock_fd, message);
if (strncmp(message, "REGISTER", 8) == 0) { // Registration message
char ip[INET_ADDRSTRLEN];
int client_port;
int nm_port;
int id;
char *token = strtok(message, " "); // "REGISTER"
token = strtok(NULL, " "); // ID
if (token == NULL) {
printf("ERR007: Invalid registration message from storage server (no ID).\n");
return;
}
id = atoi(token);
token = strtok(NULL, " "); // IP
if (token == NULL) {
printf("ERR007: Invalid registration message from storage server (no IP).\n");
return;
}
strcpy(ip, token);
token = strtok(NULL, " "); // nm_port
if (token == NULL) {
printf("ERR007: Invalid registration message from storage server (no naming server port).\n");
return;
}
nm_port = atoi(token);
token = strtok(NULL, " "); // cl_port
if (token == NULL) {
printf("ERR007: Invalid registration message from storage server (no client port).\n");
return;
}
client_port = atoi(token);
if (storage_servers[id] != NULL) {
printf("Storage server with that IP has already been initialised.\n");
if (storage_servers[id]->running) {
printf("Storage server is already running.\n");
send_data(sock_fd, "BLOCKED", 7);
return;
}
StorageServer *ss = storage_servers[id];
printf("Attempting to restart storage server...\n");
ss->client_port = client_port;
ss->nm_port = nm_port;
ss->ss_socket = sock_fd;
ss->running = true;
add_storage_server(ss);
// Send confirmation
send_data(sock_fd, "REGISTRATION_SUCCESSFUL", 23);
*storage_server = ss;
return;
}
token = strtok(NULL, " "); // path
if (token == NULL) {
printf("ERR007: Invalid registration message from storage server (no paths).\n");
return;
}
char* paths = (char*)malloc(MAX_LENGTH);
strcpy(paths, token);
StorageServer *ss = (StorageServer*) malloc(sizeof(StorageServer));
strcpy(ss->ip, ip);
ss->id = id;
ss->client_port = client_port;
ss->ss_socket = sock_fd;
ss->nm_port = nm_port;
ss->running = true;
int num_paths = 0;
token = strtok(paths, ",");
while(token != NULL) {
strcpy(ss->paths[num_paths++], token);
token = strtok(NULL, ",");
}
ss->num_paths = num_paths;
storage_servers[id] = ss;
add_storage_server(ss);
// Send acknowledgment
send_data(sock_fd, "REGISTRATION_SUCCESSFUL", 23);
free(paths);
*storage_server = ss;
}
else if (strncmp(message, "HEARTBEAT", 9) == 0) {
int ss_id;
sscanf(message, "HEARTBEAT %d", &ss_id);
StorageServer *ss = storage_servers[ss_id];
if (ss != NULL) {
ss->last_heartbeat = time(NULL);
ss->running = true;
ss->beatcount++;