-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextra.c
2483 lines (2347 loc) · 99.1 KB
/
extra.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
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 "naming_server.h"
#include "functions.h"
#include <semaphore.h>
#include <stdlib.h>
#define CACHE_SIZE 4
#define TOTAL_SERVER 100
int server_socket;
struct sockaddr_in server_address;
int server_socket2;
struct sockaddr_in server_address2;
int server_socket3;
struct sockaddr_in server_address3;
pthread_t customer_intialize_thread;
pthread_t stserver_intialize_thread;
int port_number_for_client[TOTAL_SERVER];
char *ip_for_ss[TOTAL_SERVER];
int port_number_for_ss[TOTAL_SERVER];
int query_port_for_ss[TOTAL_SERVER];
pthread_t cli_thread;
TrieNode *Trie_for_ss;
TrieNode *Trie_for_ss1[TOTAL_SERVER];
// LRUCache *cache;
int current_cache_size = 0;
int cache_time = 0;
int socket_for_client[TOTAL_SERVER];
int working_server[TOTAL_SERVER];
int currently_working_storage_servers = 0;
int arrived_till_now = 0;
int getRandomNumber(int min, int max)
{
return min + rand() % (max - min + 1);
}
// cache
int mod = 100007;
int hash[100007];
int cache_size = 0;
int djb2Hash(const char *str)
{
int hash = 5381;
int c;
while ((c = *str++))
{
hash = ((hash << 5) + hash) + c; // hash * 33 + c
hash %= mod;
}
// printf("%d\n", hash);
return hash % mod;
}
typedef struct cache
{
char path[1024];
int server_num;
struct cache *next;
} cache;
cache *head = NULL;
// Function to create a new cache node
cache *createCacheNode(const char *path, int server_num)
{
cache *newNode = (cache *)malloc(sizeof(cache));
if (newNode != NULL)
{
strncpy(newNode->path, path, 1024 - 1);
newNode->path[1024 - 1] = '\0'; // Ensure null-terminated string
newNode->server_num = server_num;
newNode->next = NULL;
}
return newNode;
}
// Function to insert a new cache node at the beginning of the linked list
cache *insertCacheNode(const char *path, int server_num)
{
cache *newNode = createCacheNode(path, server_num);
if (newNode != NULL)
{
newNode->next = head;
head = newNode;
}
cache_size++;
return head;
}
cache *deleteLastElement()
{
if (head == NULL)
{
// List is empty
return NULL;
}
if (head->next == NULL)
{
// Only one element in the list
free(head);
return NULL;
}
cache *current = head;
while (current->next->next != NULL)
{
current = current->next;
}
hash[djb2Hash(current->next->path)] = -1;
cache_size--;
free(current->next);
current->next = NULL;
return head;
}
cache *insert_cache(const char *path, int server_num)
{
if (cache_size == CACHE_SIZE)
{
head = deleteLastElement(head);
}
head = insertCacheNode(path, server_num);
hash[djb2Hash(path)] = server_num;
return head;
}
cache *deleteNodeByPath(const char *pathToDelete)
{
cache *current = head;
cache *prev = NULL;
while (current != NULL)
{
if (strcmp(current->path, pathToDelete) == 0)
{
// Node with the specified path found
if (prev == NULL)
{
// Node to delete is the head
head = current->next;
}
else
{
// Update the previous node's next pointer
prev->next = current->next;
}
// Free the node
free(current);
return head;
}
// Move to the next node
prev = current;
current = current->next;
}
// Node with the specified path not found
return head;
}
void printCacheList()
{
cache *current = head;
while (current != NULL)
{
printf("Path: %s, Server Number: %d\n", current->path, current->server_num);
current = current->next;
}
}
int get_cache(char *path)
{
if (hash[djb2Hash(path)] != -1)
{
printf("Cache hit\n");
deleteNodeByPath(path);
head = insertCacheNode(path, hash[djb2Hash(path)]);
return hash[djb2Hash(path)];
}
else
{
printf("Cache miss\n");
return -1;
}
}
// storing the index of the temp servers corresponding to the server index
int first_temp_storage[TOTAL_SERVER] = {-1};
int second_temp_storage[TOTAL_SERVER] = {-1};
void copy_and_paste_server(int idx, int idx2, char argument[], char extra_argument[])
{
int client_socket1;
struct sockaddr_in server_addr;
client_socket1 = socket(AF_INET, SOCK_STREAM, 0);
if (client_socket1 == -1)
{
perror("Error creating socket");
exit(EXIT_FAILURE);
}
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(query_port_for_ss[idx]);
if (inet_pton(AF_INET, ip_for_ss[idx], &server_addr.sin_addr) <= 0)
{
perror("Invalid address/ Address not supported");
exit(EXIT_FAILURE);
}
if (connect(client_socket1, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0)
{
perror("Connection failed");
exit(EXIT_FAILURE);
}
// srand(time(NULL));
int random = getRandomNumber(25000, 45000);
printf("random number %d\n", random);
char message1[1024];
between_ss foridx1;
between_ss foridx2;
strcpy(foridx1.ip, ip_for_ss[idx2]);
strcpy(foridx1.path, argument);
foridx1.role = 1;
foridx1.port = random;
strcpy(message1, "copy");
printf("SENT TO 1ST SS\n");
send(client_socket1, &message1, sizeof(message1), 0);
send(client_socket1, &foridx1, sizeof(foridx1), 0);
char check1[1024];
printf("check1 : %s", check1);
// send(client_socket1, &foridx1, sizeof(foridx1), 0);
strcpy(foridx2.ip, ip_for_ss[idx]);
strcpy(foridx2.path, extra_argument);
foridx2.role = -1;
foridx2.port = random;
int client_socket2;
client_socket2 = socket(AF_INET, SOCK_STREAM, 0);
if (client_socket2 == -1)
{
perror("Error creating socket");
exit(EXIT_FAILURE);
}
struct sockaddr_in server_addr2;
server_addr2.sin_family = AF_INET;
server_addr2.sin_port = htons(query_port_for_ss[idx2]);
printf("sending to %d %s\n", query_port_for_ss[idx2], ip_for_ss[idx2]);
if (inet_pton(AF_INET, ip_for_ss[idx2], &server_addr2.sin_addr) <= 0)
{
perror("Invalid address/ Address not supported");
exit(EXIT_FAILURE);
}
// Connect to server
if (connect(client_socket2, (struct sockaddr *)&server_addr2, sizeof(server_addr2)) < 0)
{
perror("Connection failed");
exit(EXIT_FAILURE);
}
char message2[1024];
strcpy(message2, "paste");
printf("SENT TO 2nd SS\n");
send(client_socket2, &message2, sizeof(message2), 0);
send(client_socket2, &foridx2, sizeof(foridx2), 0);
char check2[20000];
recv(client_socket1, &check1, sizeof(check1), 0);
recv(client_socket2, &check2, sizeof(check2), 0);
printf("check2 : %s", check2);
// for (int i = 1; i <= currently_working_storage_servers; i++)
// {
// for (int j = 1; j <= currently_working_storage_servers; j++)
// {
// }
// }
// trie_add(idx2, check2);
close(client_socket1);
close(client_socket2);
// printf("message from SS%d = %s\n message from SS%d = %s\n ", idx, check1, idx2, check2);
}
void start_red_control(int index)
{
if (first_temp_storage[index] != -1)
{
printf("done\n");
return;
}
printf("reached\n");
int size_of_each_server[TOTAL_SERVER];
for (int idx = 1; idx <= currently_working_storage_servers; idx++)
{
int client_socket1;
struct sockaddr_in server_addr;
char message[100];
// Create socket
client_socket1 = socket(AF_INET, SOCK_STREAM, 0);
if (client_socket1 == -1)
{
perror("Error creating socket");
exit(EXIT_FAILURE);
}
// Set up server address
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(query_port_for_ss[idx]);
if (inet_pton(AF_INET, ip_for_ss[idx], &server_addr.sin_addr) <= 0)
{
perror("Invalid address/ Address not supported");
exit(EXIT_FAILURE);
}
// Connect to server
if (connect(client_socket1, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0)
{
perror("Connection failed");
exit(EXIT_FAILURE);
}
char temp[1024];
strcpy(temp, "retrievesize .");
send(client_socket1, temp, strlen(temp), 0);
long long int total_size = 0;
if (recv(client_socket1, &total_size, sizeof(total_size), 0) > 0)
{
}
printf("returned size : %lld\n", total_size);
size_of_each_server[idx] = total_size;
}
long long int min1 = 1e15;
long long int min2 = 1e15;
int indx1 = -1;
int indx2 = -1;
// finding the min 2 indexs in the array size_of_each_server
for (int i = 1; i <= currently_working_storage_servers; i++)
{
if (i == index || working_server[i] == 0)
continue;
if (size_of_each_server[i] < min1)
{
min2 = min1;
indx2 = indx1;
min1 = size_of_each_server[i];
indx1 = i;
}
else if (size_of_each_server[i] < min2)
{
min2 = size_of_each_server[i];
indx2 = i;
}
}
first_temp_storage[index] = indx1;
second_temp_storage[index] = indx2;
printf("storage server %d stores in %d %d\n", index, indx1, indx2);
// send()
int client_socket1;
struct sockaddr_in server_addr;
client_socket1 = socket(AF_INET, SOCK_STREAM, 0);
if (client_socket1 == -1)
{
perror("Error creating socket");
exit(EXIT_FAILURE);
}
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(query_port_for_ss[indx1]);
if (inet_pton(AF_INET, ip_for_ss[indx1], &server_addr.sin_addr) <= 0)
{
perror("Invalid address/ Address not supported");
exit(EXIT_FAILURE);
}
if (connect(client_socket1, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0)
{
perror("Connection failed");
exit(EXIT_FAILURE);
}
char message1[1024];
snprintf(message1, sizeof(message1), "createdir . SS%d", index);
send(client_socket1, &message1, sizeof(message1), 0);
bzero(message1, sizeof(message1));
recv(client_socket1, message1, sizeof(message1), 0);
int client_socket2;
struct sockaddr_in server_addr2;
client_socket2 = socket(AF_INET, SOCK_STREAM, 0);
if (client_socket2 == -1)
{
perror("Error creating socket");
exit(EXIT_FAILURE);
}
server_addr2.sin_family = AF_INET;
server_addr2.sin_port = htons(query_port_for_ss[indx2]);
if (inet_pton(AF_INET, ip_for_ss[indx2], &server_addr2.sin_addr) <= 0)
{
perror("Invalid address/ Address not supported");
exit(EXIT_FAILURE);
}
if (connect(client_socket2, (struct sockaddr *)&server_addr2, sizeof(server_addr2)) < 0)
{
perror("Connection failed");
exit(EXIT_FAILURE);
}
char message2[1024];
snprintf(message2, sizeof(message2), "createdir . SS%d", index);
send(client_socket2, &message2, sizeof(message2), 0);
// send()
bzero(message2, sizeof(message2));
recv(client_socket2, message2, sizeof(message2), 0);
printf("message from SS%d = %s\n message from SS%d = %s\n ", indx1, message1, indx2, message2);
close(client_socket1);
close(client_socket2);
// strcpy(message1)
char g1[1024];
char g2[1024];
// strcpy(g1,message1);
snprintf(g1, sizeof(g1), "./SS%d", index);
// snprintf(g2, sizeof(g2), "SS%d", indx2);
// printf("first temp storage %d\n", first_temp_storage[index]);
copy_and_paste_server(index, indx1, ".", g1);
sleep(2);
copy_and_paste_server(index, indx2, ".", g1);
sleep(2);
return;
}
int isPrefix(char *a, char *b)
{
while (*a && *b)
{
if (*a != *b)
{
return 0; // Not a prefix
}
a++;
b++;
}
return (*a == '\0'); // True if string a is fully traversed, i.e., it is a prefix
}
void trie_delete_dir(int index, char *dir2)
{
TrieNode *temp = Trie_for_ss1[index];
char dir[1024];
strcpy(dir, dir2);
// strcat(dir, "/");
printf("dir %s\n", dir);
TrieNode *temp1 = temp;
for (int i = 0; dir[i] != '\0'; i++)
{
int position = (int)dir[i];
temp1 = temp;
temp = temp->children[position];
}
temp->is_leaf = 1;
for (int i = 0; i < 256; i++)
{
temp->children[i] = NULL;
}
return;
}
void trie_add(int index, char *total)
{
char *token = strtok(total, "\n");
while (token != NULL)
{
insert_trie(Trie_for_ss1[index], token);
token = strtok(NULL, "\n");
}
return;
}
void *customer_handler(void *arg)
{
char buffer[BUFFER_SIZE];
ssize_t bytes_received;
int client_socket = *(int *)arg;
struct sockaddr_in client_address;
socklen_t client_address_len = sizeof(client_address);
int write_flag = 0;
int append_flag = 0;
while (1)
{
while ((bytes_received = recv(client_socket, buffer, sizeof(buffer), 0)) > 0)
{
// Process received data (you can replace this with your own logic)
buffer[bytes_received] = '\0'; // Null-terminate the received data
printf("Received data: %s", buffer);
char function_to_call[1024];
char argument[1024];
char message_store[1024];
char *temp = (char *)malloc(sizeof(char) * 1024);
strcpy(temp, buffer);
strcpy(message_store, buffer);
char *token = strtok(buffer, " \t\n");
char *extra_argument = (char *)malloc(sizeof(char) * 1024);
int counter1 = 0;
while (token != NULL)
{
if (counter1 == 0)
{
strcpy(function_to_call, token);
counter1++;
}
else if (counter1 == 1)
{
strcpy(argument, token);
counter1++;
}
else
{
strcpy(extra_argument, token);
counter1++;
}
token = strtok(NULL, " \t\n");
}
// printf("function to call %s\n", function_to_call);
// printf("equalise %d\n", strcmp(function_to_call, "read"));
if (counter1 == 2)
argument[strlen(argument)] = '\0';
else
extra_argument[strlen(extra_argument)] = '\0';
if (strcmp(function_to_call, "read") == 0)
{
printf("Client requested to read %s\n", argument);
int idx = -1;
idx = get_cache(argument);
if (idx == -1)
{
for (int i = 0; i < TOTAL_SERVER; i++)
{
if (search_trie(Trie_for_ss1[i], argument))
{
idx = i;
insert_cache(argument, idx);
break;
}
}
}
int f = 0;
int temp1 = idx;
printf("%d %d %d\n", idx, first_temp_storage[idx], second_temp_storage[idx]);
printf("%d\n", working_server[idx]);
if (first_temp_storage[idx] == -1 || second_temp_storage[idx] == -1)
{
}
else
printf("%d %d\n", working_server[first_temp_storage[idx]], working_server[second_temp_storage[idx]]);
if (working_server[idx] == 0)
{
if (working_server[first_temp_storage[idx]] == 1)
{
f = 1;
}
else if (working_server[second_temp_storage[idx]] == 1)
{
f = 2;
}
else
{
f = 3;
}
}
if (f == 1)
{
idx = first_temp_storage[idx];
}
else if (f == 2)
{
idx = second_temp_storage[idx];
}
else if (f == 3)
{
printf("All the servers related to this are not working\n");
// send(client_socket, "failed to read due to closed servers\n", sizeof("failed to read due to closed servers\n"), 0);
continue;
}
if (f != 0)
{
char argument2[1024];
// strcpy(argument2, "./SS");
snprintf(argument2, sizeof(argument2), "./SS%d", temp1);
for (int i = 1; i < strlen(argument); i++)
{
argument2[strlen(argument2)] = argument[i];
}
strcpy(argument, argument2);
printf("argument %s\n", argument);
return_struct return_struct_to_send;
return_struct_to_send.flag = 1;
return_struct_to_send.port = port_number_for_client[idx];
strcpy(return_struct_to_send.ip, ip_for_ss[idx]);
strcpy(return_struct_to_send.fullpath, argument);
send(client_socket, &return_struct_to_send, sizeof(return_struct_to_send), 0);
continue;
}
}
else if (strcmp(function_to_call, "write") == 0)
{
printf("Client requested to write %s\n", argument);
write_flag = 1;
}
else if (strcmp(function_to_call, "retrieve") == 0)
{
printf("Client requested to retrieve %s\n", argument);
}
else if (strcmp(function_to_call, "append") == 0)
{
printf("Client requested to append %s\n", argument);
append_flag = 1;
}
else if (strcmp(function_to_call, "createfile") == 0)
{
printf("Client requested to createfile at %s named %s\n", argument, extra_argument);
int idx = -1;
idx = get_cache(argument);
if (idx == -1)
{
for (int i = 0; i < TOTAL_SERVER; i++)
{
if (search_trie(Trie_for_ss1[i], argument))
{
idx = i;
insert_cache(argument, idx);
break;
}
}
}
if (idx == -1)
{
printf("the location you are finding does not exist\n");
send(client_socket, "failed to create file\n", sizeof("failed to create file\n"), 0);
continue;
}
int client_socket1;
struct sockaddr_in server_addr;
char message[100];
// Create socket
client_socket1 = socket(AF_INET, SOCK_STREAM, 0);
if (client_socket1 == -1)
{
perror("Error creating socket");
exit(EXIT_FAILURE);
}
// Set up server address
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(query_port_for_ss[idx]);
if (inet_pton(AF_INET, ip_for_ss[idx], &server_addr.sin_addr) <= 0)
{
perror("Invalid address/ Address not supported");
exit(EXIT_FAILURE);
}
// Connect to server
if (connect(client_socket1, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0)
{
perror("Connection failed");
exit(EXIT_FAILURE);
}
send(client_socket1, temp, strlen(temp), 0);
char response[1024];
recv(client_socket1, response, sizeof(response), 0);
int idff = idx;
int idff1 = first_temp_storage[idx];
int idff2 = second_temp_storage[idx];
/////////
printf("response %s\n", response);
if (strcmp(response, "fail") != 0)
{
insert_trie(Trie_for_ss1[idx], response);
send(client_socket, "file created\n", sizeof("file created\n"), 0);
}
else
{
send(client_socket, "failed to create file\n", sizeof("failed to create file\n"), 0);
}
if (idff1 != -1 && working_server[idff1] == 1)
{
// GOPAL
int client_socket2_1;
struct sockaddr_in server_addr2_1;
char message2_1[100];
// Create socket
client_socket2_1 = socket(AF_INET, SOCK_STREAM, 0);
if (client_socket2_1 == -1)
{
perror("Error creating socket");
exit(EXIT_FAILURE);
}
// Set up server address
server_addr2_1.sin_family = AF_INET;
server_addr2_1.sin_port = htons(query_port_for_ss[idff1]);
if (inet_pton(AF_INET, ip_for_ss[idff1], &server_addr2_1.sin_addr) <= 0)
{
perror("Invalid address/ Address not supported");
exit(EXIT_FAILURE);
}
// Connect to server
if (connect(client_socket2_1, (struct sockaddr *)&server_addr2_1, sizeof(server_addr2_1)) < 0)
{
perror("Connection failed");
exit(EXIT_FAILURE);
}
char temp2_1[1024];
strcpy(temp2_1, message_store);
char path2_1[1024];
char name2_1[1024];
char *token2_1 = strtok(temp2_1, " ");
for (int i = 0; i < 3 && token2_1 != NULL; ++i)
{
// Process or print the token
printf("Word %d: %s\n", i + 1, token2_1);
if (i == 1)
{
strcpy(path2_1, token2_1);
}
else if (i == 2)
{
strcpy(name2_1, token2_1);
}
// Move to the next token
token2_1 = strtok(NULL, " ");
}
// strcat()
char path2_1_1[2000];
for (int i = 2; i < strlen(path2_1); i++)
{
path2_1_1[i - 2] = path2_1[i];
}
path2_1_1[strlen(path2_1) - 2] = '\0';
char new_path2_1[4000];
snprintf(new_path2_1, sizeof(new_path2_1), "./SS%d/%s", idff, path2_1_1);
char new_command2_1[10000];
snprintf(new_command2_1, sizeof(new_command2_1), "createfile %s %s", new_path2_1, name2_1);
send(client_socket2_1, new_command2_1, strlen(new_command2_1), 0);
char response2_1[1024];
recv(client_socket2_1, response2_1, sizeof(response2_1), 0);
close(client_socket2_1);
}
if (idff2 != -1 && working_server[idff2] == 1)
{
int client_socket2_2;
struct sockaddr_in server_addr2_2;
char message2_2[100];
// Create socket
client_socket2_2 = socket(AF_INET, SOCK_STREAM, 0);
if (client_socket2_2 == -1)
{
perror("Error creating socket");
exit(EXIT_FAILURE);
}
// Set up server address
server_addr2_2.sin_family = AF_INET;
server_addr2_2.sin_port = htons(query_port_for_ss[idff2]);
if (inet_pton(AF_INET, ip_for_ss[idff2], &server_addr2_2.sin_addr) <= 0)
{
perror("Invalid address/ Address not supported");
exit(EXIT_FAILURE);
}
// Connect to server
if (connect(client_socket2_2, (struct sockaddr *)&server_addr2_2, sizeof(server_addr2_2)) < 0)
{
perror("Connection failed");
exit(EXIT_FAILURE);
}
char temp2_2[1024];
strcpy(temp2_2, message_store);
char path2_2[1024];
char name2_2[1024];
char *token2_2 = strtok(temp2_2, " ");
for (int i = 0; i < 3 && token2_2 != NULL; ++i)
{
// Process or print the token
printf("Word %d: %s\n", i + 1, token2_2);
if (i == 1)
{
strcpy(path2_2, token2_2);
}
else if (i == 2)
{
strcpy(name2_2, token2_2);
}
// Move to the next token
token2_2 = strtok(NULL, " ");
}
// strcat()
char path2_2_1[2000];
for (int i = 2; i < strlen(path2_2); i++)
{
path2_2_1[i - 2] = path2_2[i];
}
path2_2_1[strlen(path2_2) - 2] = '\0';
char new_path2_2[4000];
snprintf(new_path2_2, sizeof(new_path2_2), "./SS%d/%s", idff, path2_2_1);
char new_command2_2[10000];
snprintf(new_command2_2, sizeof(new_command2_2), "createfile %s %s", new_path2_2, name2_2);
send(client_socket2_2, new_command2_2, strlen(new_command2_2), 0);
char response2_2[1024];
recv(client_socket2_2, response2_2, sizeof(response2_2), 0);
close(client_socket2_2);
}
continue;
}
else if (strcmp(function_to_call, "createdir") == 0)
{
printf("Client requested to createdir at %s named %s\n", argument, extra_argument);
int idx = -1;
idx = get_cache(argument);
if (idx == -1)
{
for (int i = 0; i < TOTAL_SERVER; i++)
{
if (search_trie(Trie_for_ss1[i], argument))
{
idx = i;
insert_cache(argument, idx);
break;
}
}
}
if (idx == -1)
{
printf("the location you are finding does not exist\n");
send(client_socket, "failed to create dir\n", sizeof("failed to create dir\n"), 0);
continue;
}
int f = 0;
int client_socket1;
struct sockaddr_in server_addr;
char message[100];
// Create socket
client_socket1 = socket(AF_INET, SOCK_STREAM, 0);
if (client_socket1 == -1)
{
perror("Error creating socket");
exit(EXIT_FAILURE);
}
// Set up server address
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(query_port_for_ss[idx]);
if (inet_pton(AF_INET, ip_for_ss[idx], &server_addr.sin_addr) <= 0)
{
perror("Invalid address/ Address not supported");
exit(EXIT_FAILURE);
}
// Connect to server
if (connect(client_socket1, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0)
{
perror("Connection failed");
exit(EXIT_FAILURE);
}
send(client_socket1, temp, strlen(temp), 0);
char response[1024];
recv(client_socket1, response, sizeof(response), 0);
printf("response %s\n", response);
if (strcmp(response, "fail") != 0)
{
insert_trie(Trie_for_ss1[idx], response);
send(client_socket, "dir created\n", sizeof("dir created\n"), 0);
}
else
{
send(client_socket, "failed to create dir\n", sizeof("failed to create dir\n"), 0);
}
int idff = idx;
int idff1 = first_temp_storage[idx];
int idff2 = second_temp_storage[idx];
if (idff1 != -1 && working_server[idff1] == 1)
{
// GOPAL
int client_socket2_1;
struct sockaddr_in server_addr2_1;
char message2_1[100];
// Create socket
client_socket2_1 = socket(AF_INET, SOCK_STREAM, 0);
if (client_socket2_1 == -1)
{
perror("Error creating socket");
exit(EXIT_FAILURE);
}
// Set up server address
server_addr2_1.sin_family = AF_INET;
server_addr2_1.sin_port = htons(query_port_for_ss[idff1]);
if (inet_pton(AF_INET, ip_for_ss[idff1], &server_addr2_1.sin_addr) <= 0)
{
perror("Invalid address/ Address not supported");
exit(EXIT_FAILURE);
}
// Connect to server
if (connect(client_socket2_1, (struct sockaddr *)&server_addr2_1, sizeof(server_addr2_1)) < 0)
{
perror("Connection failed");
exit(EXIT_FAILURE);
}
char temp2_1[1024];
strcpy(temp2_1, message_store);
char path2_1[1024];
char name2_1[1024];
char *token2_1 = strtok(temp2_1, " ");
for (int i = 0; i < 3 && token2_1 != NULL; ++i)
{
// Process or print the token
// printf("Word %d: %s\n", i + 1, token);
if (i == 1)
{
strcpy(path2_1, token2_1);
}
else if (i == 2)
{
strcpy(name2_1, token2_1);
}
// Move to the next token
token2_1 = strtok(NULL, " ");
}
// strcat()
char path2_1_1[1024];
for (int i = 2; i < strlen(path2_1); i++)
{
path2_1_1[i - 2] = path2_1[i];
}
path2_1_1[strlen(path2_1) - 2] = '\0';
printf("path2_1 %s\n", path2_1_1);
char new_path2_1[2000];
snprintf(new_path2_1, sizeof(new_path2_1), "./SS%d/%s", idff, path2_1_1);
char new_command2_1[10000];
snprintf(new_command2_1, sizeof(new_command2_1), "createdir %s %s", new_path2_1, name2_1);
printf("new_command2_1 %s\n", new_command2_1);
send(client_socket2_1, new_command2_1, strlen(new_command2_1), 0);
char response2_1[1024];
recv(client_socket2_1, response2_1, sizeof(response2_1), 0);
printf("Response from temporary 1st storage %s\n", response2_1);
close(client_socket2_1);
// Create socket}
}
if (idff2 != -1 && working_server[idff2] == 1)
{
int client_socket2_2;
struct sockaddr_in server_addr2_2;
char message2_2[100];
client_socket2_2 = socket(AF_INET, SOCK_STREAM, 0);
if (client_socket2_2 == -1)
{
perror("Error creating socket");
exit(EXIT_FAILURE);
}
// Set up server address
server_addr2_2.sin_family = AF_INET;
server_addr2_2.sin_port = htons(query_port_for_ss[idff2]);
if (inet_pton(AF_INET, ip_for_ss[idff2], &server_addr2_2.sin_addr) <= 0)
{
perror("Invalid address/ Address not supported");
exit(EXIT_FAILURE);
}
// Connect to server
if (connect(client_socket2_2, (struct sockaddr *)&server_addr2_2, sizeof(server_addr2_2)) < 0)
{
perror("Connection failed");
exit(EXIT_FAILURE);
}
char temp2_2[1024];
strcpy(temp2_2, message_store);
char path2_2[1024];
char name2_2[1024];
char *token2_2 = strtok(temp2_2, " ");
for (int i = 0; i < 3 && token2_2 != NULL; ++i)
{
// Process or print the token
// printf("Word %d: %s\n", i + 1, token);
if (i == 1)
{
strcpy(path2_2, token2_2);
}
else if (i == 2)
{
strcpy(name2_2, token2_2);
}
// Move to the next token
token2_2 = strtok(NULL, " ");
}