From efa27dd7d84f43f02474b5d325fc9c9c4ea6f3fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C5=82awomir=20Mucha?= Date: Mon, 6 Jun 2016 18:15:27 +0200 Subject: [PATCH 1/8] Add box search function --- src/client.c | 41 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/src/client.c b/src/client.c index b34c432..ac827f1 100644 --- a/src/client.c +++ b/src/client.c @@ -166,6 +166,39 @@ stack_t* detect_local_changes(file_t *local_files, box_entry_t* box_entries) } +box_entry_t* find_in_box(box_entry_t *box_entry, char *name) +{ + box_entry_t *it; + + it = box_entry; + + while(it->next != NULL) { + if(strcmp(it->path, name) == 0) { + return it; + } + } + + return NULL; +} + + +void create_or_update(box_entry_t *box_entry, char *name, size_t size, time_t local_time, time_t server_time) +{ + box_entry_t *entry; + + entry = find_in_box(box_entry, name); + strcpy(box_entry->path, name); + + if(local_time != NULL) { + entry->local_timestamp = local_time; + } + + if(server_time != NULL) { + entry->global_timestamp = server_time; + } +} + + int push_local_changes(stack_t* changes) { message_info_t* message; @@ -174,7 +207,7 @@ int push_local_changes(stack_t* changes) switch(message->message_type) { case CLIENT_FILE: send_file(socket_fd, message->name, message->message_type); -// create_or_update(local_box, message->name, message->); + create_or_update(local_box, message->name, message->size, message->modification_time, NULL); break; case FILE_REMOVAL: printf("FILE REMOVAL; LET'S PRETEND IT'S REMOVED. TODO :-)\n"); @@ -266,6 +299,12 @@ void* track_directory(void *parameters) } +void init() +{ + +} + + int main(int argc, char *argv[]) { From bd4fef89bac7f1ab0721a725ae86ce436994ab68 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C5=82awomir=20Mucha?= Date: Mon, 6 Jun 2016 21:26:21 +0200 Subject: [PATCH 2/8] Add client init function --- src/client.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/client.c b/src/client.c index ac827f1..9dee570 100644 --- a/src/client.c +++ b/src/client.c @@ -2,6 +2,7 @@ #include #include #include +#include #include #include #include @@ -301,7 +302,19 @@ void* track_directory(void *parameters) void init() { + int fd; + file_t *local_files; + stack_t *changes; + + if(!file_exists(LOCAL_BOX_FILENAME)) { + fd = creat(LOCAL_BOX_FILENAME, 0666); + close(fd); + } + local_files = get_local_files_list("."); + changes = detect_local_changes(local_files, local_box); + push_local_changes(changes); + write_box(LOCAL_BOX_FILENAME, local_box); } From c702c7490bb27efcca52d9dfe0fcf057165e0805 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C5=82awomir=20Mucha?= Date: Mon, 6 Jun 2016 23:47:34 +0200 Subject: [PATCH 3/8] Pushing client local changes to server --- include/box_utils.h | 2 + src/box_utils.c | 45 +++++++++++- src/client.c | 140 +++++++++++++++++--------------------- src/communication_utils.c | 10 ++- src/server.c | 80 ++++++++++++++++++---- 5 files changed, 183 insertions(+), 94 deletions(-) diff --git a/include/box_utils.h b/include/box_utils.h index f232787..6364499 100644 --- a/include/box_utils.h +++ b/include/box_utils.h @@ -6,5 +6,7 @@ int insert_into_box(box_entry_t *entries, char* path, time_t global_time, time_t box_entry_t* read_box(char *path); int write_box(char *path, box_entry_t *entries); void print_box(box_entry_t* head); +box_entry_t* find_in_box(box_entry_t *box_entry, char *name); +void create_or_update(box_entry_t *box_entry, char *name, size_t size, time_t local_time, time_t server_time); #endif diff --git a/src/box_utils.c b/src/box_utils.c index 432cdce..7ddf455 100644 --- a/src/box_utils.c +++ b/src/box_utils.c @@ -56,6 +56,8 @@ int write_box(char *path, box_entry_t *entries) handler = fopen(path, "w"); it = entries; + printf("Writing to local box\n"); + while(it->next != NULL) { fwrite(it, sizeof(box_entry_t), 1, handler); it = it->next; @@ -72,4 +74,45 @@ void print_box(box_entry_t* head) printf("%s\n", head->path); head = head->next; } -} \ No newline at end of file +} + + +box_entry_t* find_in_box(box_entry_t *box_entry, char *name) +{ + box_entry_t *it; + + it = box_entry; + + while(it->next != NULL) { + if(strcmp(it->path, name) == 0) { + return it; + } + it = it->next; + } + + return NULL; +} + + +void create_or_update(box_entry_t *box_entry, char *name, size_t size, time_t local_time, time_t server_time) +{ + box_entry_t *entry; + + entry = find_in_box(box_entry, name); + + printf("Updating box... Name: %s, local time: %d\n", name, local_time); + + if(entry == NULL) { + insert_into_box(box_entry, name, server_time, local_time, -1); + } else { + strcpy(box_entry->path, name); + + if(local_time != 0) { + entry->local_timestamp = local_time; + } + + if(server_time != 0) { + entry->global_timestamp = server_time; + } + } +} diff --git a/src/client.c b/src/client.c index 9dee570..9ab9f8c 100644 --- a/src/client.c +++ b/src/client.c @@ -13,7 +13,7 @@ #include "box_utils.h" #define SERVER_BOX_FILENAME ".server_box" #define LOCAL_BOX_FILENAME ".box" -#define SYNC_TIME 10 +#define SYNC_TIME 1 typedef struct dirent dirent_t; typedef struct stat stat_t; @@ -43,8 +43,6 @@ int file_exists(char* filename) - - void print_files_list(file_t *head) { while(head->next != NULL) { @@ -79,7 +77,7 @@ file_t* get_local_files_list(char* name) // returns: number of entries, -1 on er sprintf(entry_path, "%s/%s", name, ent->d_name); stat(entry_path, &entry_stat); - if(S_ISREG(entry_stat.st_mode)) { + if(S_ISREG(entry_stat.st_mode) && ent->d_name[0] != '.') { file_it->size = (int) entry_stat.st_size; file_it->path = strdup(entry_path); file_it->is_directory = -1; @@ -116,15 +114,20 @@ stack_t* detect_local_changes(file_t *local_files, box_entry_t* box_entries) files_it = local_files; + printf("Printing local box in detect_local_changes\n"); + print_box(box_entries); + while(files_it->next != NULL) { box_it = box_entries; is_found = -1; while(box_it->next != NULL) { if(strcmp(files_it->path, box_it->path) == 0) { + is_found = 1; + if(files_it->modification_time > box_it->local_timestamp) { + printf("Modification time > local timestamp\n"); message = create_info_message(CLIENT_FILE, files_it->path, files_it->modification_time, (size_t)files_it->size); - is_found = 1; push(stack, message); break; } @@ -133,6 +136,7 @@ stack_t* detect_local_changes(file_t *local_files, box_entry_t* box_entries) } if(is_found < 0) { + printf("File is not found in the local box\n"); message = create_info_message(CLIENT_FILE, files_it->path, files_it->modification_time, (size_t)files_it->size); push(stack, message); } @@ -156,6 +160,7 @@ stack_t* detect_local_changes(file_t *local_files, box_entry_t* box_entries) } if(is_found < 0) { + printf("Box entry is not found in the files\n"); message = create_info_message(FILE_REMOVAL, box_it->path, time(NULL), 0); push(stack, message); } @@ -167,38 +172,6 @@ stack_t* detect_local_changes(file_t *local_files, box_entry_t* box_entries) } -box_entry_t* find_in_box(box_entry_t *box_entry, char *name) -{ - box_entry_t *it; - - it = box_entry; - - while(it->next != NULL) { - if(strcmp(it->path, name) == 0) { - return it; - } - } - - return NULL; -} - - -void create_or_update(box_entry_t *box_entry, char *name, size_t size, time_t local_time, time_t server_time) -{ - box_entry_t *entry; - - entry = find_in_box(box_entry, name); - strcpy(box_entry->path, name); - - if(local_time != NULL) { - entry->local_timestamp = local_time; - } - - if(server_time != NULL) { - entry->global_timestamp = server_time; - } -} - int push_local_changes(stack_t* changes) { @@ -207,8 +180,9 @@ int push_local_changes(stack_t* changes) while((message = (message_info_t *) pop(changes)) != NULL) { switch(message->message_type) { case CLIENT_FILE: + printf("CLIENT_FILE, sending file to server, filename: %s\n", message->name); send_file(socket_fd, message->name, message->message_type); - create_or_update(local_box, message->name, message->size, message->modification_time, NULL); + create_or_update(local_box, message->name, message->size, message->modification_time, message->modification_time); break; case FILE_REMOVAL: printf("FILE REMOVAL; LET'S PRETEND IT'S REMOVED. TODO :-)\n"); @@ -219,6 +193,8 @@ int push_local_changes(stack_t* changes) } } + printf("Local changes pushed\n"); + return 0; } @@ -243,30 +219,22 @@ int push_local_changes(stack_t* changes) -// -//box_entry_t* receive_server_box(int socket_fd) -//{ -// box_entry_t *box_entries; -// message_info_t message_info; -// -// message_info = receive_message_info(socket_fd); -// -// if(!message_info.message_type == MESSAGE_BOX) { -// printf("Message box expected\n"); -// exit(EXIT_FAILURE); -// } -// -// receive_file(socket_fd, SERVER_BOX_FILENAME, message_info.size); -// -// box_entries = malloc(message_info.size); -// -// if(read_box(SERVER_BOX_FILENAME, box_entries) < 0) { -// perror("reading server box failed\n"); -// return NULL; -// } -// -// return box_entries; -//} + +box_entry_t* receive_server_box(int socket_fd) +{ + message_info_t message_info; + + message_info = receive_message_info(socket_fd); + + if(!message_info.message_type == SERVER_BOX) { + printf("Message box expected\n"); + exit(EXIT_FAILURE); + } + + receive_file(socket_fd, SERVER_BOX_FILENAME, message_info.size); + + return read_box(SERVER_BOX_FILENAME); +} // //void* pull_changes(void *parameters) @@ -286,13 +254,21 @@ void* track_directory(void *parameters) stack_t *changes; file_t *local_files; + int i; + i = 0; while(1) { - // acquire lock + // acquire lock] local_files = get_local_files_list("."); + printf("Got local files list\n"); changes = detect_local_changes(local_files, local_box); + printf("Changes has been detected\n"); + printf("Pushing local changes\n"); push_local_changes(changes); + printf("Writing to local box\n"); write_box(LOCAL_BOX_FILENAME, local_box); // release lock + printf("Iteration: %d\n", i); + i += 1; sleep(SYNC_TIME); } @@ -306,11 +282,15 @@ void init() file_t *local_files; stack_t *changes; - if(!file_exists(LOCAL_BOX_FILENAME)) { + server_box = receive_server_box(socket_fd); + + if(file_exists(LOCAL_BOX_FILENAME) != 0) { fd = creat(LOCAL_BOX_FILENAME, 0666); close(fd); } + local_box = read_box(LOCAL_BOX_FILENAME); + local_files = get_local_files_list("."); changes = detect_local_changes(local_files, local_box); push_local_changes(changes); @@ -322,24 +302,26 @@ void init() int main(int argc, char *argv[]) { -// pthread_t tracker_id, listener_id; -// -// socket_fd = get_client_socket(argv[1], atoi(argv[2])); -// server_box = receive_server_box(socket_fd); -// -// -// if(pthread_create(&tracker_id, NULL, &track_directory, NULL) < 0) { -// perror("pthread_create failed"); -// return EXIT_FAILURE; -// } -// + pthread_t tracker_id, listener_id; + + chdir("./client_"); + + socket_fd = get_client_socket(argv[1], atoi(argv[2])); + + init(); + + if(pthread_create(&tracker_id, NULL, &track_directory, NULL) < 0) { + perror("pthread_create failed"); + return EXIT_FAILURE; + } + // if(pthread_create(&listener_id, NULL, &pull_changes, NULL) < 0) { // perror("pthread_create failed"); // return EXIT_FAILURE; // } -// -// if(pthread_join(tracker_id, NULL) < 0) { perror("pthread_join failed"); return EXIT_FAILURE; } + + if(pthread_join(tracker_id, NULL) < 0) { perror("pthread_join failed"); return EXIT_FAILURE; } // if(pthread_join(listener_id, NULL) < 0) { perror("pthread_join failed"); return EXIT_FAILURE; } -// -// return EXIT_SUCCESS; + + return EXIT_SUCCESS; } diff --git a/src/communication_utils.c b/src/communication_utils.c index 15ce5bd..ad0b55a 100644 --- a/src/communication_utils.c +++ b/src/communication_utils.c @@ -33,6 +33,8 @@ void send_file_data(int socket, int fd, size_t size) { remain_data -= sent_bytes; } + + printf("File has been sent\n"); } void send_file(int socket, const char* path, message_type_t type){ @@ -51,12 +53,15 @@ void send_file(int socket, const char* path, message_type_t type){ exit(EXIT_FAILURE); } + printf("Sending message info, size: %d\n", file_stat.st_size); send_message_info(socket, type, path, file_stat.st_size); + printf("Sending file data\n"); send_file_data(socket, fd, file_stat.st_size); } message_info_t receive_message_info(int socket){ message_info_t info; + printf("Receiving message info \n"); if(read(socket, &info, sizeof(info)) 0) && (remain_data > 0)) + printf("Receiving message file, size: %d\n", size); + while ((remain_data > 0) && ((len = read(socket, buffer, BUFSIZ)) > 0)) { + printf("Reading... %d\n", remain_data); fwrite(buffer, sizeof(char), len, file); remain_data -= len; } fclose(file); + printf("Received file\n"); } diff --git a/src/server.c b/src/server.c index b687e18..5ee00c7 100644 --- a/src/server.c +++ b/src/server.c @@ -1,21 +1,23 @@ #include +#include +#include +#include +#include #include "server.h" #include "communication_utils.h" +#include "box_utils.h" #include "socket_utils.h" -#define SERVER_BOX_FILENAME ".server_box" -#define LOCAL_BOX_FILENAME ".box" +#define SERVER_BOX_FILENAME ".origin_server_box" -int main(){ - struct sockaddr addr; - server_t server; - socklen_t len; +int max(int a, int b) { + return a > b ? a : b; +} - int socket = init_server_socket(9000); - server=get_server(socket); - run(server); - return EXIT_SUCCESS; +int file_exists(char* filename) +{ + return access(filename, F_OK); } @@ -60,11 +62,22 @@ server_t get_server(int socket){ void register_client(server_t* server){ int client_fd; client_fd = accept(server->socket, NULL, 0); + + printf("Registering new client, fd: %d \n", client_fd); + if(client_fd<0){ perror("accept error"); exit(EXIT_FAILURE); } + + server->highest_fd = max(server->highest_fd, client_fd); + FD_SET(client_fd, &server->server_fd_set); + add_client(client_fd, server->client_array, &server->client_num); + + printf("Sending server box to client\n"); + + send_file(client_fd, SERVER_BOX_FILENAME, SERVER_BOX); } void add_client(int socket, int* client_array, int* client_num){ @@ -80,21 +93,62 @@ void handle_file_request(int socket, message_info_t info){ send_file(socket, info.name, SERVER_FILE); } -void handle_client_file(int socket, message_info_t info){ +void handle_client_file(int socket, message_info_t *info){ box_entry_t* head; + + receive_file(socket, info->name, info->size); + head = read_box(SERVER_BOX_FILENAME); + create_or_update(head, info->name, info->size, 0, info->modification_time); + + + // broadcast } void handle_client_message(int socket){ + ssize_t read_bytes; message_info_t info; - read(socket, &info, sizeof(info)); + + read_bytes = read(socket, &info, sizeof(info)); + + printf("Handle client message, size: %d\n", read_bytes); switch(info.message_type){ case FILE_REQUEST: handle_file_request(socket, info); case CLIENT_FILE: - handle_client_file(socket, info); + printf("Received CLIENT_FILE %s\n", info.name); + handle_client_file(socket, &info); default: break; } +} + + +void init() +{ + int fd; + + if(file_exists(SERVER_BOX_FILENAME) != 0) { + printf("Creating server box\n"); + fd = creat(SERVER_BOX_FILENAME, 0666); + close(fd); + } +} + + +int main(int argc, char *argv[]){ + struct sockaddr addr; + server_t server; + socklen_t len; + + chdir("./server_"); + + init(); + + int socket = init_server_socket(atoi(argv[1])); + server=get_server(socket); + run(server); + + return EXIT_SUCCESS; } \ No newline at end of file From 170311f10c7d583deac805b397f845d169aae637 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C5=82awomir=20Mucha?= Date: Tue, 7 Jun 2016 00:24:31 +0200 Subject: [PATCH 4/8] Add client pulling functionality --- include/communication_utils.h | 2 + src/client.c | 95 +++++++++++++++++++++++++---------- 2 files changed, 71 insertions(+), 26 deletions(-) diff --git a/include/communication_utils.h b/include/communication_utils.h index c628bcf..514d25d 100644 --- a/include/communication_utils.h +++ b/include/communication_utils.h @@ -35,5 +35,7 @@ typedef struct BoxEntry { void send_file(int socket, const char* path, message_type_t type); message_info_t receive_message_info(int socket); void receive_file(int socket, const char* new_path, size_t size); +void send_message_info(int socket, message_type_t type, const char* path, size_t size); +message_info_t receive_message_info(int socket); #endif diff --git a/src/client.c b/src/client.c index 9ab9f8c..ee7a2a0 100644 --- a/src/client.c +++ b/src/client.c @@ -172,7 +172,6 @@ stack_t* detect_local_changes(file_t *local_files, box_entry_t* box_entries) } - int push_local_changes(stack_t* changes) { message_info_t* message; @@ -199,24 +198,61 @@ int push_local_changes(stack_t* changes) } +stack_t *detect_server_changes(box_entry_t* local_box_entries, box_entry_t* server_box_entries) +{ + stack_t *stack; + box_entry_t *local_it, *server_it; + message_info_t *message; + stack = init_stack(); -// -// -//int detect_server_changes(box_entry_t* local_box_entries, box_entry_t* server_box_entries, queue_t* changes) -//{ -// return -1; -//} -// -// -//int apply_server_changes(queue_t *changes) -//{ -// return -1; -//} + server_it = server_box_entries; + while(server_it->next != NULL) { + local_it = find_in_box(local_box_entries, server_it->path); + if(local_it == NULL || server_it->global_timestamp > local_it->global_timestamp) { + message = create_info_message(FILE_REQUEST, server_it->path, server_it->global_timestamp, 0); + push(stack, message); + } + server_it = server_it->next; + } + return stack; +} +int apply_server_changes(stack_t *changes) +{ + message_info_t *message, received_message; + struct stat st; + + while((message = (message_info_t *) pop(changes)) != NULL) { + switch(message->message_type) { + case FILE_REQUEST: + printf("FILE REQUEST, requesting file: %s\n", message->name); + send_message_info(socket_fd, FILE_REQUEST, message->name, 0); + received_message = receive_message_info(socket_fd); + + if(received_message.message_type == SERVER_FILE) { + printf("Receiving file from server...\n"); + receive_file(socket_fd, received_message.name, received_message.size); + printf("Server file has been received...\n"); + stat(received_message.name, &st); + create_or_update(local_box, received_message.name, received_message.size, st.st_mtime, received_message.modification_time); + } else if(received_message.message_type == SERVER_BOX) { + printf("Hey server, you fucked up\n"); + } + break; + default: + printf("I have no idea what is happening here - \n"); + break; + } + } + + printf("Server changes applied\n"); + + return 0; +} @@ -235,19 +271,26 @@ box_entry_t* receive_server_box(int socket_fd) return read_box(SERVER_BOX_FILENAME); } -// - -//void* pull_changes(void *parameters) -//{ -// while(1) { -// receive_server_box(socket_fd); -// -// -// } -// -// return NULL; -//} -// + +void* pull_changes(void *parameters) +{ + stack_t *changes; + + int i; + i = 0; + while(1) { + receive_server_box(socket_fd); + changes = detect_server_changes(local_box, server_box); + apply_server_changes(changes); + write_box(LOCAL_BOX_FILENAME, local_box); + + printf("Iteration: %d\n", i); + i += 1; + } + + return NULL; +} + void* track_directory(void *parameters) { From 0ac5f0e5e697e6f639a51f8135c3cc4d514857d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C5=82awomir=20Mucha?= Date: Tue, 7 Jun 2016 01:18:09 +0200 Subject: [PATCH 5/8] Add server broadcasting functions --- include/communication_utils.h | 4 ++-- include/server.h | 2 +- src/box_utils.c | 2 -- src/client.c | 38 ++++++++++++++++++++--------------- src/communication_utils.c | 8 +++++--- src/server.c | 36 +++++++++++++++++++++++++-------- 6 files changed, 58 insertions(+), 32 deletions(-) diff --git a/include/communication_utils.h b/include/communication_utils.h index 514d25d..d831985 100644 --- a/include/communication_utils.h +++ b/include/communication_utils.h @@ -32,10 +32,10 @@ typedef struct BoxEntry { } box_entry_t; -void send_file(int socket, const char* path, message_type_t type); +void send_file(int socket, const char* path, message_type_t type, time_t mod_time); message_info_t receive_message_info(int socket); void receive_file(int socket, const char* new_path, size_t size); -void send_message_info(int socket, message_type_t type, const char* path, size_t size); +void send_message_info(int socket, message_type_t type, const char* path, size_t size, time_t mod_time); message_info_t receive_message_info(int socket); #endif diff --git a/include/server.h b/include/server.h index 7ff6773..bdd879b 100644 --- a/include/server.h +++ b/include/server.h @@ -17,6 +17,6 @@ server_t get_server(int socket); void register_client(server_t* server); void add_client(int socket, int* client_array, int* client_num); void handle_file_request(int socket, message_info_t info); -void handle_client_message(int socket); +void handle_client_message(int socket, server_t* server); #endif diff --git a/src/box_utils.c b/src/box_utils.c index 7ddf455..d554d7c 100644 --- a/src/box_utils.c +++ b/src/box_utils.c @@ -56,8 +56,6 @@ int write_box(char *path, box_entry_t *entries) handler = fopen(path, "w"); it = entries; - printf("Writing to local box\n"); - while(it->next != NULL) { fwrite(it, sizeof(box_entry_t), 1, handler); it = it->next; diff --git a/src/client.c b/src/client.c index ee7a2a0..d271796 100644 --- a/src/client.c +++ b/src/client.c @@ -114,8 +114,8 @@ stack_t* detect_local_changes(file_t *local_files, box_entry_t* box_entries) files_it = local_files; - printf("Printing local box in detect_local_changes\n"); - print_box(box_entries); +// printf("Printing local box in detect_local_changes\n"); +// print_box(box_entries); while(files_it->next != NULL) { box_it = box_entries; @@ -180,7 +180,7 @@ int push_local_changes(stack_t* changes) switch(message->message_type) { case CLIENT_FILE: printf("CLIENT_FILE, sending file to server, filename: %s\n", message->name); - send_file(socket_fd, message->name, message->message_type); + send_file(socket_fd, message->name, message->message_type, message->modification_time); create_or_update(local_box, message->name, message->size, message->modification_time, message->modification_time); break; case FILE_REMOVAL: @@ -230,7 +230,7 @@ int apply_server_changes(stack_t *changes) switch(message->message_type) { case FILE_REQUEST: printf("FILE REQUEST, requesting file: %s\n", message->name); - send_message_info(socket_fd, FILE_REQUEST, message->name, 0); + send_message_info(socket_fd, FILE_REQUEST, message->name, 0, 0); received_message = receive_message_info(socket_fd); if(received_message.message_type == SERVER_FILE) { @@ -279,7 +279,7 @@ void* pull_changes(void *parameters) int i; i = 0; while(1) { - receive_server_box(socket_fd); + server_box = receive_server_box(socket_fd); changes = detect_server_changes(local_box, server_box); apply_server_changes(changes); write_box(LOCAL_BOX_FILENAME, local_box); @@ -302,15 +302,15 @@ void* track_directory(void *parameters) while(1) { // acquire lock] local_files = get_local_files_list("."); - printf("Got local files list\n"); +// printf("Got local files list\n"); changes = detect_local_changes(local_files, local_box); - printf("Changes has been detected\n"); - printf("Pushing local changes\n"); +// printf("Changes has been detected\n"); +// printf("Pushing local changes\n"); push_local_changes(changes); - printf("Writing to local box\n"); +// printf("Writing to local box\n"); write_box(LOCAL_BOX_FILENAME, local_box); // release lock - printf("Iteration: %d\n", i); +// printf("Iteration: %d\n", i); i += 1; sleep(SYNC_TIME); } @@ -334,6 +334,12 @@ void init() local_box = read_box(LOCAL_BOX_FILENAME); + // pull + changes = detect_server_changes(local_box, server_box); + apply_server_changes(changes); + write_box(LOCAL_BOX_FILENAME, local_box); + + // push local_files = get_local_files_list("."); changes = detect_local_changes(local_files, local_box); push_local_changes(changes); @@ -347,7 +353,7 @@ int main(int argc, char *argv[]) pthread_t tracker_id, listener_id; - chdir("./client_"); + chdir(argv[3]); socket_fd = get_client_socket(argv[1], atoi(argv[2])); @@ -358,13 +364,13 @@ int main(int argc, char *argv[]) return EXIT_FAILURE; } -// if(pthread_create(&listener_id, NULL, &pull_changes, NULL) < 0) { -// perror("pthread_create failed"); -// return EXIT_FAILURE; -// } + if(pthread_create(&listener_id, NULL, &pull_changes, NULL) < 0) { + perror("pthread_create failed"); + return EXIT_FAILURE; + } if(pthread_join(tracker_id, NULL) < 0) { perror("pthread_join failed"); return EXIT_FAILURE; } -// if(pthread_join(listener_id, NULL) < 0) { perror("pthread_join failed"); return EXIT_FAILURE; } + if(pthread_join(listener_id, NULL) < 0) { perror("pthread_join failed"); return EXIT_FAILURE; } return EXIT_SUCCESS; } diff --git a/src/communication_utils.c b/src/communication_utils.c index ad0b55a..b2e27d9 100644 --- a/src/communication_utils.c +++ b/src/communication_utils.c @@ -5,15 +5,17 @@ #include #include #include +#include #include "communication_utils.h" -void send_message_info(int socket, message_type_t type, const char* path, size_t size) +void send_message_info(int socket, message_type_t type, const char* path, size_t size, time_t mod_time) { message_info_t info; info.message_type = type; strncpy(info.name, path, MAX_PATHLEN); info.size = size; + info.modification_time = mod_time; if(write(socket, &info, sizeof(message_info_t)) < sizeof(info)) { @@ -37,7 +39,7 @@ void send_file_data(int socket, int fd, size_t size) printf("File has been sent\n"); } -void send_file(int socket, const char* path, message_type_t type){ +void send_file(int socket, const char* path, message_type_t type, time_t mod_time){ int fd; struct stat file_stat; @@ -54,7 +56,7 @@ void send_file(int socket, const char* path, message_type_t type){ } printf("Sending message info, size: %d\n", file_stat.st_size); - send_message_info(socket, type, path, file_stat.st_size); + send_message_info(socket, type, path, file_stat.st_size, mod_time); printf("Sending file data\n"); send_file_data(socket, fd, file_stat.st_size); } diff --git a/src/server.c b/src/server.c index 5ee00c7..ecc1493 100644 --- a/src/server.c +++ b/src/server.c @@ -40,7 +40,7 @@ void run(server_t server){ register_client(&server); } else{ - handle_client_message(fd); + handle_client_message(fd, &server); } } } @@ -77,7 +77,7 @@ void register_client(server_t* server){ printf("Sending server box to client\n"); - send_file(client_fd, SERVER_BOX_FILENAME, SERVER_BOX); + send_file(client_fd, SERVER_BOX_FILENAME, SERVER_BOX, 0); } void add_client(int socket, int* client_array, int* client_num){ @@ -90,22 +90,41 @@ void add_client(int socket, int* client_array, int* client_num){ } void handle_file_request(int socket, message_info_t info){ - send_file(socket, info.name, SERVER_FILE); + box_entry_t *entries, *entry; + + entries = read_box(SERVER_BOX_FILENAME); + entry = find_in_box(entries, info.name); + + send_file(socket, info.name, SERVER_FILE, entry->global_timestamp); } -void handle_client_file(int socket, message_info_t *info){ +void broadcast_box(int sender_socket_fd, server_t *server) +{ + int i; + + for(i=0; iclient_num; i+=1) { + if(sender_socket_fd != server->client_array[i]) { + printf("Brodacasting box to FD: %d \n", server->client_array[i]); + send_file(server->client_array[i], SERVER_BOX_FILENAME, SERVER_BOX, 0); + } + } +} + + +void handle_client_file(int socket, message_info_t *info, server_t *server){ box_entry_t* head; receive_file(socket, info->name, info->size); head = read_box(SERVER_BOX_FILENAME); create_or_update(head, info->name, info->size, 0, info->modification_time); - - // broadcast + write_box(SERVER_BOX_FILENAME, head); + + broadcast_box(socket, server); } -void handle_client_message(int socket){ +void handle_client_message(int socket, server_t *server){ ssize_t read_bytes; message_info_t info; @@ -118,13 +137,14 @@ void handle_client_message(int socket){ handle_file_request(socket, info); case CLIENT_FILE: printf("Received CLIENT_FILE %s\n", info.name); - handle_client_file(socket, &info); + handle_client_file(socket, &info, server); default: break; } } + void init() { int fd; From 7208b4e8e7adfe60a7d04814d58ebb37d363791e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Sadowski?= Date: Tue, 7 Jun 2016 13:27:06 +0200 Subject: [PATCH 6/8] fix bugs with push changes --- src/box_utils.c | 4 +++- src/communication_utils.c | 19 ++++++++++++++----- src/server.c | 10 +++++++++- 3 files changed, 26 insertions(+), 7 deletions(-) diff --git a/src/box_utils.c b/src/box_utils.c index d554d7c..c828856 100644 --- a/src/box_utils.c +++ b/src/box_utils.c @@ -101,9 +101,11 @@ void create_or_update(box_entry_t *box_entry, char *name, size_t size, time_t lo printf("Updating box... Name: %s, local time: %d\n", name, local_time); if(entry == NULL) { + printf("Entry not found create new\n"); insert_into_box(box_entry, name, server_time, local_time, -1); } else { - strcpy(box_entry->path, name); + printf("Entry found updating\n"); + strcpy(entry->path, name); if(local_time != 0) { entry->local_timestamp = local_time; diff --git a/src/communication_utils.c b/src/communication_utils.c index b2e27d9..50709e5 100644 --- a/src/communication_utils.c +++ b/src/communication_utils.c @@ -29,11 +29,18 @@ void send_file_data(int socket, int fd, size_t size) { off_t offset = 0; int remain_data = size; - int sent_bytes; + int sent_bytes, len; + char buffer[BUFSIZ]; + +// while (((sent_bytes = sendfile(socket, fd, &offset, BUFSIZ)) > 0) && (remain_data > 0)) +// { +// remain_data -= sent_bytes; +// } - while (((sent_bytes = sendfile(socket, fd, &offset, BUFSIZ)) > 0) && (remain_data > 0)) + while ((remain_data > 0) && ((len = read(fd, buffer, BUFSIZ)) > 0)) { - remain_data -= sent_bytes; + int num = write(socket, buffer, len); + remain_data -= len; } printf("File has been sent\n"); @@ -59,6 +66,7 @@ void send_file(int socket, const char* path, message_type_t type, time_t mod_tim send_message_info(socket, type, path, file_stat.st_size, mod_time); printf("Sending file data\n"); send_file_data(socket, fd, file_stat.st_size); + close(fd); } message_info_t receive_message_info(int socket){ @@ -83,8 +91,9 @@ void receive_file(int socket, const char* new_path, size_t size){ printf("Receiving message file, size: %d\n", size); while ((remain_data > 0) && ((len = read(socket, buffer, BUFSIZ)) > 0)) { - printf("Reading... %d\n", remain_data); - fwrite(buffer, sizeof(char), len, file); + printf("Reading... %d %c\n", remain_data, buffer[0]); + int num =fwrite(buffer, sizeof(char), len, file); + printf("writed %d bytes to file %s\n", num, new_path); remain_data -= len; } fclose(file); diff --git a/src/server.c b/src/server.c index ecc1493..de28c16 100644 --- a/src/server.c +++ b/src/server.c @@ -94,6 +94,10 @@ void handle_file_request(int socket, message_info_t info){ entries = read_box(SERVER_BOX_FILENAME); entry = find_in_box(entries, info.name); + if(entry==NULL){ + printf("Error finding file %s\n", info.name); + exit(0); + } send_file(socket, info.name, SERVER_FILE, entry->global_timestamp); } @@ -117,8 +121,12 @@ void handle_client_file(int socket, message_info_t *info, server_t *server){ receive_file(socket, info->name, info->size); head = read_box(SERVER_BOX_FILENAME); - create_or_update(head, info->name, info->size, 0, info->modification_time); + create_or_update(head, info->name, info->size, info->modification_time, info->modification_time); // broadcast + + printf("Printing box\n"); + print_box(head); + printf("\n"); write_box(SERVER_BOX_FILENAME, head); broadcast_box(socket, server); From 4eabaf73afe9b0615630dc1a898d43e9419c9c60 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Sadowski?= Date: Tue, 7 Jun 2016 14:58:54 +0200 Subject: [PATCH 7/8] fix switch --- src/box_utils.c | 5 +---- src/client.c | 20 +++++--------------- src/communication_utils.c | 13 ------------- src/server.c | 10 ++++------ 4 files changed, 10 insertions(+), 38 deletions(-) diff --git a/src/box_utils.c b/src/box_utils.c index c828856..91977db 100644 --- a/src/box_utils.c +++ b/src/box_utils.c @@ -69,7 +69,7 @@ int write_box(char *path, box_entry_t *entries) void print_box(box_entry_t* head) { while(head->next != NULL) { - printf("%s\n", head->path); + printf("%s %d %d\n", head->path, head->global_timestamp, head->local_timestamp); head = head->next; } } @@ -98,13 +98,10 @@ void create_or_update(box_entry_t *box_entry, char *name, size_t size, time_t lo entry = find_in_box(box_entry, name); - printf("Updating box... Name: %s, local time: %d\n", name, local_time); if(entry == NULL) { - printf("Entry not found create new\n"); insert_into_box(box_entry, name, server_time, local_time, -1); } else { - printf("Entry found updating\n"); strcpy(entry->path, name); if(local_time != 0) { diff --git a/src/client.c b/src/client.c index d271796..8e63d2f 100644 --- a/src/client.c +++ b/src/client.c @@ -114,8 +114,7 @@ stack_t* detect_local_changes(file_t *local_files, box_entry_t* box_entries) files_it = local_files; -// printf("Printing local box in detect_local_changes\n"); -// print_box(box_entries); + while(files_it->next != NULL) { box_it = box_entries; @@ -126,7 +125,6 @@ stack_t* detect_local_changes(file_t *local_files, box_entry_t* box_entries) is_found = 1; if(files_it->modification_time > box_it->local_timestamp) { - printf("Modification time > local timestamp\n"); message = create_info_message(CLIENT_FILE, files_it->path, files_it->modification_time, (size_t)files_it->size); push(stack, message); break; @@ -136,7 +134,6 @@ stack_t* detect_local_changes(file_t *local_files, box_entry_t* box_entries) } if(is_found < 0) { - printf("File is not found in the local box\n"); message = create_info_message(CLIENT_FILE, files_it->path, files_it->modification_time, (size_t)files_it->size); push(stack, message); } @@ -192,7 +189,6 @@ int push_local_changes(stack_t* changes) } } - printf("Local changes pushed\n"); return 0; } @@ -234,11 +230,10 @@ int apply_server_changes(stack_t *changes) received_message = receive_message_info(socket_fd); if(received_message.message_type == SERVER_FILE) { - printf("Receiving file from server...\n"); receive_file(socket_fd, received_message.name, received_message.size); - printf("Server file has been received...\n"); stat(received_message.name, &st); create_or_update(local_box, received_message.name, received_message.size, st.st_mtime, received_message.modification_time); + printf("Updated box\n"); } else if(received_message.message_type == SERVER_BOX) { printf("Hey server, you fucked up\n"); } @@ -249,7 +244,6 @@ int apply_server_changes(stack_t *changes) } } - printf("Server changes applied\n"); return 0; } @@ -284,8 +278,7 @@ void* pull_changes(void *parameters) apply_server_changes(changes); write_box(LOCAL_BOX_FILENAME, local_box); - printf("Iteration: %d\n", i); - i += 1; + } return NULL; @@ -302,15 +295,12 @@ void* track_directory(void *parameters) while(1) { // acquire lock] local_files = get_local_files_list("."); -// printf("Got local files list\n"); changes = detect_local_changes(local_files, local_box); -// printf("Changes has been detected\n"); -// printf("Pushing local changes\n"); + push_local_changes(changes); -// printf("Writing to local box\n"); + write_box(LOCAL_BOX_FILENAME, local_box); // release lock -// printf("Iteration: %d\n", i); i += 1; sleep(SYNC_TIME); } diff --git a/src/communication_utils.c b/src/communication_utils.c index 50709e5..7a61384 100644 --- a/src/communication_utils.c +++ b/src/communication_utils.c @@ -32,18 +32,12 @@ void send_file_data(int socket, int fd, size_t size) int sent_bytes, len; char buffer[BUFSIZ]; -// while (((sent_bytes = sendfile(socket, fd, &offset, BUFSIZ)) > 0) && (remain_data > 0)) -// { -// remain_data -= sent_bytes; -// } - while ((remain_data > 0) && ((len = read(fd, buffer, BUFSIZ)) > 0)) { int num = write(socket, buffer, len); remain_data -= len; } - printf("File has been sent\n"); } void send_file(int socket, const char* path, message_type_t type, time_t mod_time){ @@ -62,16 +56,13 @@ void send_file(int socket, const char* path, message_type_t type, time_t mod_tim exit(EXIT_FAILURE); } - printf("Sending message info, size: %d\n", file_stat.st_size); send_message_info(socket, type, path, file_stat.st_size, mod_time); - printf("Sending file data\n"); send_file_data(socket, fd, file_stat.st_size); close(fd); } message_info_t receive_message_info(int socket){ message_info_t info; - printf("Receiving message info \n"); if(read(socket, &info, sizeof(info)) 0) && ((len = read(socket, buffer, BUFSIZ)) > 0)) { - printf("Reading... %d %c\n", remain_data, buffer[0]); int num =fwrite(buffer, sizeof(char), len, file); - printf("writed %d bytes to file %s\n", num, new_path); remain_data -= len; } fclose(file); - printf("Received file\n"); } diff --git a/src/server.c b/src/server.c index de28c16..ca16b6c 100644 --- a/src/server.c +++ b/src/server.c @@ -124,9 +124,6 @@ void handle_client_file(int socket, message_info_t *info, server_t *server){ create_or_update(head, info->name, info->size, info->modification_time, info->modification_time); // broadcast - printf("Printing box\n"); - print_box(head); - printf("\n"); write_box(SERVER_BOX_FILENAME, head); broadcast_box(socket, server); @@ -138,14 +135,16 @@ void handle_client_message(int socket, server_t *server){ read_bytes = read(socket, &info, sizeof(info)); - printf("Handle client message, size: %d\n", read_bytes); switch(info.message_type){ case FILE_REQUEST: + printf("Received FILE_REQUEST %s\n", info.name); handle_file_request(socket, info); + break; case CLIENT_FILE: - printf("Received CLIENT_FILE %s\n", info.name); + printf("Received CLIENT_FILE %s from %d\n", info.name, socket); handle_client_file(socket, &info, server); + break; default: break; } @@ -158,7 +157,6 @@ void init() int fd; if(file_exists(SERVER_BOX_FILENAME) != 0) { - printf("Creating server box\n"); fd = creat(SERVER_BOX_FILENAME, 0666); close(fd); } From 27093ac66d863eb3b39fbc9d61cb083c01fc2d69 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Sadowski?= Date: Tue, 7 Jun 2016 18:59:33 +0200 Subject: [PATCH 8/8] add storing history --- src/client.c | 4 ++-- src/communication_utils.c | 27 +++++++++++++++++++++++++++ src/server.c | 13 ++++++++++++- 3 files changed, 41 insertions(+), 3 deletions(-) diff --git a/src/client.c b/src/client.c index 8e63d2f..c0a24cd 100644 --- a/src/client.c +++ b/src/client.c @@ -181,10 +181,10 @@ int push_local_changes(stack_t* changes) create_or_update(local_box, message->name, message->size, message->modification_time, message->modification_time); break; case FILE_REMOVAL: - printf("FILE REMOVAL; LET'S PRETEND IT'S REMOVED. TODO :-)\n"); + //printf("FILE REMOVAL; LET'S PRETEND IT'S REMOVED. TODO :-)\n"); break; default: - printf("I have no idea what is happening he;)\n"); + //printf("I have no idea what is happening he;)\n"); break; } } diff --git a/src/communication_utils.c b/src/communication_utils.c index 7a61384..97f2c65 100644 --- a/src/communication_utils.c +++ b/src/communication_utils.c @@ -7,6 +7,7 @@ #include #include #include "communication_utils.h" +#define HISTORY_DIR "history" void send_message_info(int socket, message_type_t type, const char* path, size_t size, time_t mod_time) @@ -86,3 +87,29 @@ void receive_file(int socket, const char* new_path, size_t size){ } fclose(file); } + + +void receive_file_history(int socket, const char* new_path, size_t size, time_t timestamp){ + int len; + FILE *file, *file_history; + char buffer[BUFSIZ]; + int remain_data = size; + char history_path[MAX_PATHLEN+50]=HISTORY_DIR; + char timestamp_s[15]=""; + sprintf(timestamp_s, "%d", (int)timestamp); + + strcat(history_path, "/"); + strcat(history_path, &new_path[2]); + strcat(history_path, "$"); + strcat(history_path, timestamp_s); + file = fopen(new_path, "w"); + file_history = fopen(history_path, "w"); + while ((remain_data > 0) && ((len = read(socket, buffer, BUFSIZ)) > 0)) + { + int num=fwrite(buffer, sizeof(char), len, file); + fwrite(buffer, sizeof(char), len, file_history); + remain_data -= len; + } + fclose(file); + fclose(file_history); + } \ No newline at end of file diff --git a/src/server.c b/src/server.c index ca16b6c..0fdc078 100644 --- a/src/server.c +++ b/src/server.c @@ -8,6 +8,7 @@ #include "box_utils.h" #include "socket_utils.h" #define SERVER_BOX_FILENAME ".origin_server_box" +#define HISTORY_DIR "history" int max(int a, int b) { @@ -118,7 +119,7 @@ void broadcast_box(int sender_socket_fd, server_t *server) void handle_client_file(int socket, message_info_t *info, server_t *server){ box_entry_t* head; - receive_file(socket, info->name, info->size); + receive_file_history(socket, info->name, info->size, info->modification_time); head = read_box(SERVER_BOX_FILENAME); create_or_update(head, info->name, info->size, info->modification_time, info->modification_time); @@ -168,6 +169,16 @@ int main(int argc, char *argv[]){ server_t server; socklen_t len; + if(argc!=2){ + printf("invalid number of arguments"); + exit(0); + } + + struct stat st = {0}; + if (stat(HISTORY_DIR, &st) == -1) { + mkdir(HISTORY_DIR, 0755); + } + chdir("./server_"); init();