From 1eb51a665ab799fbdae784d6fee913c6e37f8ff9 Mon Sep 17 00:00:00 2001 From: Skylar Scorca <64870579+skylarscorca@users.noreply.github.com> Date: Wed, 22 Mar 2023 12:47:53 -0400 Subject: [PATCH 01/25] update readme --- README.md | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/README.md b/README.md index 47d612fe..dda79ae8 100644 --- a/README.md +++ b/README.md @@ -24,3 +24,29 @@ style CLI. Kilo was written by Salvatore Sanfilippo aka antirez and is released under the BSD 2 clause license. + +# cpd-term-project +Term Project for COP5570 +Concurrent Text File Editing (Google Docs redev) + +Contributers: +Skylar Scorca, +Tony Drouillard, +Jack Dewey + +Project Objectives (intended features) +- Allow users to update a single text file simultaneously +- Allow users to view the updates of other users in real-time +- Handle the movement of a user’s cursor after an update is made +- Handle transactions quickly and efficiently + +Optional Objectives: (if time allows) +- Allow users to create and delete text files in a greater system of files +- Allow users to modify the tree structure of the greater system of files +- Allow users to view the updates to the file system in real-time + +Due date countdown: +https://www.timeanddate.com/countdown/generic?iso=20230428T2359&p0=856&msg=cpd+term+project+due+date&font=cursive&csz=1 + +Google Drive Folder: +https://drive.google.com/drive/folders/1sZ5ulUizpBA6Rq9KOYdHxhaF4QC_5UAi?usp=share_link From d9eef172a2bf891176b6143aa869d947a35af249 Mon Sep 17 00:00:00 2001 From: Skylar Scorca Date: Wed, 22 Mar 2023 13:07:20 -0400 Subject: [PATCH 02/25] deleted TODO, changed makefile --- Makefile | 2 +- TODO | 10 ---------- 2 files changed, 1 insertion(+), 11 deletions(-) delete mode 100644 TODO diff --git a/Makefile b/Makefile index 13620cc1..1a4102b8 100644 --- a/Makefile +++ b/Makefile @@ -4,4 +4,4 @@ kilo: kilo.c $(CC) -o kilo kilo.c -Wall -W -pedantic -std=c99 clean: - rm kilo + rm -f kilo diff --git a/TODO b/TODO deleted file mode 100644 index 95ae28b9..00000000 --- a/TODO +++ /dev/null @@ -1,10 +0,0 @@ -IMPORTANT -=== - -* Testing and stability to reach "usable" level. - -MAYBE -=== - -* Send alternate screen sequences if TERM=xterm: "\033[?1049h" and "\033[?1049l" -* Improve internals to be more understandable. From 8909ab9612d0c5bd8186fb1caea3a4a381b286f8 Mon Sep 17 00:00:00 2001 From: Skylar Scorca Date: Wed, 22 Mar 2023 13:09:46 -0400 Subject: [PATCH 03/25] change makefile --- Makefile | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 1a4102b8..2e46ffe2 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,9 @@ +CC = gcc -g -Wall -W -ansi -pedantic -std=c99 -o + all: kilo kilo: kilo.c - $(CC) -o kilo kilo.c -Wall -W -pedantic -std=c99 + $(CC) kilo kilo.c clean: rm -f kilo From dc8334a6de46c0259a589584bf056b84867c2061 Mon Sep 17 00:00:00 2001 From: Skylar Scorca Date: Fri, 21 Apr 2023 18:39:06 -0400 Subject: [PATCH 04/25] fixed comments and added a few notes in the commends --- kilo.c | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/kilo.c b/kilo.c index 406eb7be..1be9e7b1 100644 --- a/kilo.c +++ b/kilo.c @@ -109,6 +109,8 @@ struct editorConfig { struct editorSyntax *syntax; /* Current syntax highlight, or NULL. */ }; +//Note: we may want to add a few fields to the erow and editorConfig structs + static struct editorConfig E; enum KEY_ACTION{ @@ -198,6 +200,7 @@ struct editorSyntax HLDB[] = { #define HLDB_ENTRIES (sizeof(HLDB)/sizeof(HLDB[0])) /* ======================= Low level terminal handling ====================== */ +//Note: probably don't need to edit these static struct termios orig_termios; /* In order to restore at exit.*/ @@ -214,7 +217,7 @@ void editorAtExit(void) { disableRawMode(STDIN_FILENO); } -/* Raw mode: 1960 magic shit. */ +/* Raw mode: 1960 magic*/ int enableRawMode(int fd) { struct termios raw; @@ -362,6 +365,7 @@ int getWindowSize(int ifd, int ofd, int *rows, int *cols) { } /* ====================== Syntax highlight color scheme ==================== */ +//Note: probably don't need to edit these int is_separator(int c) { return c == '\0' || isspace(c) || strchr(",.()+-/*=~%[];",c) != NULL; @@ -551,6 +555,10 @@ void editorSelectSyntaxHighlight(char *filename) { } /* ======================= Editor rows implementation ======================= */ +//Note: probably want to edit these. perhaps at the end of an update function, we call another +// function to send an update message to the server. +//Note: we can copy the logic from these functions to allow for editing after receuving an +// update message from the server. /* Update the rendered version and the syntax highlight of a row. */ void editorUpdateRow(erow *row) { @@ -616,7 +624,7 @@ void editorFreeRow(erow *row) { free(row->hl); } -/* Remove the row at the specified position, shifting the remainign on the +/* Remove the row at the specified position, shifting the remaining on the * top. */ void editorDelRow(int at) { erow *row; @@ -852,6 +860,7 @@ int editorSave(void) { } /* ============================= Terminal update ============================ */ +//Note: probably don't need to edit these /* We define a very simple "append buffer" structure, that is an heap * allocated string where we can append to. This is useful in order to @@ -1008,6 +1017,7 @@ void editorSetStatusMessage(const char *fmt, ...) { } /* =============================== Find mode ================================ */ +//Note: probably don't need to edit these #define KILO_QUERY_LEN 256 @@ -1107,6 +1117,7 @@ void editorFind(int fd) { } /* ========================= Editor events handling ======================== */ +//Note: we probably don't need to edit these /* Handle cursor position change because arrow keys were pressed. */ void editorMoveCursor(int key) { @@ -1288,7 +1299,10 @@ void initEditor(void) { signal(SIGWINCH, handleSigWinCh); } +//main program of text-editor client int main(int argc, char **argv) { + + //need to change this to be server ip instead if (argc != 2) { fprintf(stderr,"Usage: kilo \n"); exit(1); From 4c08df6d8b8467efffb7a3cfc7dc948c39929239 Mon Sep 17 00:00:00 2001 From: jdewey2023 Date: Fri, 21 Apr 2023 23:09:44 -0400 Subject: [PATCH 05/25] Initial server & client set up | Transfer a file between server and client --- Makefile | 10 +++-- kilo.c | 66 ++++++++++++++++++++++++++---- server.cpp | 115 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 181 insertions(+), 10 deletions(-) create mode 100755 server.cpp diff --git a/Makefile b/Makefile index 2e46ffe2..a32a9303 100644 --- a/Makefile +++ b/Makefile @@ -1,9 +1,13 @@ -CC = gcc -g -Wall -W -ansi -pedantic -std=c99 -o +CC = gcc -g -Wall -W -ansi -pedantic -std=c99 -pthread -o +C+ = g++ -g -Wall -pthread -o -all: kilo +all: clean kilo server kilo: kilo.c $(CC) kilo kilo.c +server: server.cpp + $(C+) server server.cpp + clean: - rm -f kilo + rm -f kilo kilo.exe server server.exe diff --git a/kilo.c b/kilo.c index 1be9e7b1..24f457c6 100644 --- a/kilo.c +++ b/kilo.c @@ -53,6 +53,7 @@ #include #include #include +#include /* Syntax highlight types */ #define HL_NORMAL 0 @@ -1299,17 +1300,68 @@ void initEditor(void) { signal(SIGWINCH, handleSigWinCh); } +void receiveFile(int fd){ + ssize_t n; + FILE *file = fopen("transfer", "w"); + char buffer[1024]; + n = read(fd, buffer, 1024); + buffer[n] = '\0'; + while (1){ + if ((n = read(fd, buffer, 1024)) > 0){ + // printf("Line: %s\n", buffer); + buffer[n] = '\0'; + if (!strcmp(buffer, "End Transfer")){ + // printf("Closing\n"); + fclose(file); + return; + } + fprintf(file, "%s\n", buffer); + send(fd, "ACK", 1024, 0); + } + } + //editorOpen("test"); +} + //main program of text-editor client int main(int argc, char **argv) { - - //need to change this to be server ip instead - if (argc != 2) { - fprintf(stderr,"Usage: kilo \n"); + //need to change this to be server ip instead + if (argc != 3) { + fprintf(stderr,"Usage: kilo \n"); exit(1); } - - initEditor(); - editorSelectSyntaxHighlight(argv[1]); + struct addrinfo hints, *res, *traverser; + memset(&hints, 0, sizeof(struct addrinfo)); + hints.ai_family = AF_UNSPEC; + hints.ai_socktype = SOCK_STREAM; + int r = getaddrinfo(argv[1], argv[2], &hints, &res); + if (r != 0){ + fprintf(stderr,"Error: Can't find server.\n"); + return 1; + } + // Try addresses until one is successful + int serverFd; + for (traverser = res; traverser; traverser = traverser->ai_next){ + if ((serverFd = socket(traverser->ai_family, traverser->ai_socktype, traverser->ai_protocol)) != -1){ + if ((connect(serverFd, traverser->ai_addr, traverser->ai_addrlen)) == 0){ + break; + } + } + } + printf("Connected\n"); + char buffer[1024]; + while (fgets(buffer, 1024, stdin)){ + buffer[strcspn(buffer, "\r\n")] = 0; + if (!strcmp(buffer, "get")){ + // printf("Get Received\n"); + send(serverFd, "get", 1024, 0); + receiveFile(serverFd); + } + // printf("%s\n", buffer); + } + close(serverFd); + // exit(0); + initEditor(); + editorSelectSyntaxHighlight("transfer"); editorOpen(argv[1]); enableRawMode(STDIN_FILENO); editorSetStatusMessage( diff --git a/server.cpp b/server.cpp new file mode 100755 index 00000000..a7f88dfc --- /dev/null +++ b/server.cpp @@ -0,0 +1,115 @@ +// C Headers +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +// C++ Headers +#include +#include +#include +#include +#include +#include +using namespace std; + +vector readFile(){ + string line; + vector lines; + ifstream file("test"); + while (getline(file, line)){ + lines.push_back(line); + } + return lines; +} + +void sendFile(int fd, vector lines){ + vector::iterator i; + send(fd, (void*)"Start Transfer", 1024, 0); + for (i = lines.begin(); i != lines.end(); i++){ + char buffer[1024]; + string msg = *i; + // cout << "Line: " << msg << endl; + send(fd, msg.c_str(), msg.length(), 0); + read(fd, buffer, 1024); + } + send(fd, (void*)"End Transfer", 1024, 0); +} + +void *threadFunc(void *args){ + ssize_t n; + int clientFd = *(int*)args; + char buffer[1024]; + pthread_detach(pthread_self()); + // Read until disconnection + while ((n = read(clientFd, buffer, 1024)) > 0){ + string line(buffer); + if (line == "exit"){ + close(clientFd); + } + if (line == "get"){ + // cout << "Get Received" << endl; + sendFile(clientFd, readFile()); + } + } + return NULL; +} + +void handleSigInt(int unused __attribute__((unused))){ + exit(0); +} + +int main(void){ + signal(SIGINT, handleSigInt); + // Create server socket + int serverFd; + if ((serverFd = socket(AF_INET, SOCK_STREAM, 0)) < 0){ + std::cerr << "Error: Can't create socket." << std::endl; + return 1; + } + // Set options for socket + int val; + if (setsockopt(serverFd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(int))){ + std::cerr << "Error: Can't reuse socket." << std::endl; + return 2; + } + // Configure addr and bind + struct sockaddr_in serverAddr; + serverAddr.sin_family = AF_INET; + serverAddr.sin_addr.s_addr = htonl(INADDR_ANY); + serverAddr.sin_port = htons(10001); + if (bind(serverFd, (struct sockaddr *)&serverAddr, sizeof(serverAddr)) == -1){ + cerr << "Error: Can't bind socket to port." << endl; + return 3; + } + // Listen for client connections + if (listen(serverFd, 20) < 0){ + cerr << "Error: Can't listen for clients." << endl; + return 4; + } + // Get name and port assigned to server + char *name = new char[1024]; + struct sockaddr_in infoAddr; + socklen_t len = sizeof(infoAddr); + gethostname(name, 1024); + getsockname(serverFd, (struct sockaddr *)&infoAddr, &len); + // Report name and port + cout << name << ":" << ntohs(serverAddr.sin_port)<< endl; + delete name; + // Connect a client + struct sockaddr_in cliAddr; + len = sizeof(cliAddr); + while (true){ + int clientFd = accept(serverFd, (struct sockaddr *)&serverAddr, &len); + // Create thread to deal with client + pthread_t thread; + pthread_create(&thread, NULL, threadFunc, (void *)&clientFd); + } + return 0; +} \ No newline at end of file From ccd310672a026e1fa89b7b1b2e6d4eb3b48c1d83 Mon Sep 17 00:00:00 2001 From: Skylar Scorca Date: Sat, 22 Apr 2023 17:42:55 -0400 Subject: [PATCH 06/25] cleaned up code --- kilo.c | 9 ++++++++- server.cpp | 15 +++++++++++++++ transfer | 0 3 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 transfer diff --git a/kilo.c b/kilo.c index 24f457c6..0c0bc6d6 100644 --- a/kilo.c +++ b/kilo.c @@ -1324,11 +1324,13 @@ void receiveFile(int fd){ //main program of text-editor client int main(int argc, char **argv) { - //need to change this to be server ip instead + //check command-line args if (argc != 3) { fprintf(stderr,"Usage: kilo \n"); exit(1); } + + //setup struct addrinfo hints, *res, *traverser; memset(&hints, 0, sizeof(struct addrinfo)); hints.ai_family = AF_UNSPEC; @@ -1338,6 +1340,7 @@ int main(int argc, char **argv) { fprintf(stderr,"Error: Can't find server.\n"); return 1; } + // Try addresses until one is successful int serverFd; for (traverser = res; traverser; traverser = traverser->ai_next){ @@ -1348,6 +1351,7 @@ int main(int argc, char **argv) { } } printf("Connected\n"); + char buffer[1024]; while (fgets(buffer, 1024, stdin)){ buffer[strcspn(buffer, "\r\n")] = 0; @@ -1358,8 +1362,11 @@ int main(int argc, char **argv) { } // printf("%s\n", buffer); } + close(serverFd); // exit(0); + + //start editor initEditor(); editorSelectSyntaxHighlight("transfer"); editorOpen(argv[1]); diff --git a/server.cpp b/server.cpp index a7f88dfc..f6c5a04a 100755 --- a/server.cpp +++ b/server.cpp @@ -19,6 +19,7 @@ #include using namespace std; +//readFile - reads lines in file into the data structure lines vector readFile(){ string line; vector lines; @@ -29,6 +30,7 @@ vector readFile(){ return lines; } +//sendFile - send file line-by-line to a client at fd void sendFile(int fd, vector lines){ vector::iterator i; send(fd, (void*)"Start Transfer", 1024, 0); @@ -42,6 +44,7 @@ void sendFile(int fd, vector lines){ send(fd, (void*)"End Transfer", 1024, 0); } +//threadFunc - thread function to read any messages from a client void *threadFunc(void *args){ ssize_t n; int clientFd = *(int*)args; @@ -61,24 +64,31 @@ void *threadFunc(void *args){ return NULL; } +//handleSigInt - action performed when user types ctrl-C void handleSigInt(int unused __attribute__((unused))){ exit(0); } +//main server program int main(void){ + + //setup SIGINT signal handler signal(SIGINT, handleSigInt); + // Create server socket int serverFd; if ((serverFd = socket(AF_INET, SOCK_STREAM, 0)) < 0){ std::cerr << "Error: Can't create socket." << std::endl; return 1; } + // Set options for socket int val; if (setsockopt(serverFd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(int))){ std::cerr << "Error: Can't reuse socket." << std::endl; return 2; } + // Configure addr and bind struct sockaddr_in serverAddr; serverAddr.sin_family = AF_INET; @@ -88,25 +98,30 @@ int main(void){ cerr << "Error: Can't bind socket to port." << endl; return 3; } + // Listen for client connections if (listen(serverFd, 20) < 0){ cerr << "Error: Can't listen for clients." << endl; return 4; } + // Get name and port assigned to server char *name = new char[1024]; struct sockaddr_in infoAddr; socklen_t len = sizeof(infoAddr); gethostname(name, 1024); getsockname(serverFd, (struct sockaddr *)&infoAddr, &len); + // Report name and port cout << name << ":" << ntohs(serverAddr.sin_port)<< endl; delete name; + // Connect a client struct sockaddr_in cliAddr; len = sizeof(cliAddr); while (true){ int clientFd = accept(serverFd, (struct sockaddr *)&serverAddr, &len); + // Create thread to deal with client pthread_t thread; pthread_create(&thread, NULL, threadFunc, (void *)&clientFd); diff --git a/transfer b/transfer new file mode 100644 index 00000000..e69de29b From 312aa8c3340a8596989b7fe54ed724ff30ca4838 Mon Sep 17 00:00:00 2001 From: Skylar Scorca Date: Sat, 22 Apr 2023 17:50:03 -0400 Subject: [PATCH 07/25] added comments --- kilo.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/kilo.c b/kilo.c index 0c0bc6d6..d17632bf 100644 --- a/kilo.c +++ b/kilo.c @@ -1300,6 +1300,8 @@ void initEditor(void) { signal(SIGWINCH, handleSigWinCh); } +/* ========================= Communication with Server ======================== */ + void receiveFile(int fd){ ssize_t n; FILE *file = fopen("transfer", "w"); @@ -1322,6 +1324,8 @@ void receiveFile(int fd){ //editorOpen("test"); } +/* ============================= Main Program ================================== */ + //main program of text-editor client int main(int argc, char **argv) { //check command-line args From 69eaef48a2413a985f8d1179ed18c95c3fd9f641 Mon Sep 17 00:00:00 2001 From: Skylar Scorca Date: Sat, 22 Apr 2023 18:26:37 -0400 Subject: [PATCH 08/25] fixed makefile and readme, copies test to transfer using get command --- Makefile | 6 +++--- README.md | 5 ++++- kilo.c | 6 ++++-- server.cpp | 21 +++++++++++++++++---- test | 4 ++++ 5 files changed, 32 insertions(+), 10 deletions(-) create mode 100644 test diff --git a/Makefile b/Makefile index a32a9303..e4b644fc 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ CC = gcc -g -Wall -W -ansi -pedantic -std=c99 -pthread -o -C+ = g++ -g -Wall -pthread -o +C+ = g++ -g -Wall -pthread -std=c++11 -o -all: clean kilo server +all: kilo server kilo: kilo.c $(CC) kilo kilo.c @@ -10,4 +10,4 @@ server: server.cpp $(C+) server server.cpp clean: - rm -f kilo kilo.exe server server.exe + rm -f kilo server transfer diff --git a/README.md b/README.md index dda79ae8..72998646 100644 --- a/README.md +++ b/README.md @@ -6,8 +6,11 @@ Kilo is a small text editor in less than 1K lines of code (counted with cloc). A screencast is available here: https://asciinema.org/a/90r2i9bq8po03nazhqtsifksb Usage: kilo `` +New Usage: kilo -Keys: +'get' to copy file from server + +Editor Keys: CTRL-S: Save CTRL-Q: Quit diff --git a/kilo.c b/kilo.c index d17632bf..02e5faf4 100644 --- a/kilo.c +++ b/kilo.c @@ -1306,8 +1306,10 @@ void receiveFile(int fd){ ssize_t n; FILE *file = fopen("transfer", "w"); char buffer[1024]; + n = read(fd, buffer, 1024); buffer[n] = '\0'; + while (1){ if ((n = read(fd, buffer, 1024)) > 0){ // printf("Line: %s\n", buffer); @@ -1318,7 +1320,7 @@ void receiveFile(int fd){ return; } fprintf(file, "%s\n", buffer); - send(fd, "ACK", 1024, 0); + send(fd, "ACK", 1024, 0); //why are we sending this? } } //editorOpen("test"); @@ -1360,7 +1362,7 @@ int main(int argc, char **argv) { while (fgets(buffer, 1024, stdin)){ buffer[strcspn(buffer, "\r\n")] = 0; if (!strcmp(buffer, "get")){ - // printf("Get Received\n"); + printf("sending get\n"); send(serverFd, "get", 1024, 0); receiveFile(serverFd); } diff --git a/server.cpp b/server.cpp index f6c5a04a..50f16ad2 100755 --- a/server.cpp +++ b/server.cpp @@ -33,14 +33,25 @@ vector readFile(){ //sendFile - send file line-by-line to a client at fd void sendFile(int fd, vector lines){ vector::iterator i; + send(fd, (void*)"Start Transfer", 1024, 0); - for (i = lines.begin(); i != lines.end(); i++){ + +/* for (i = lines.begin(); i != lines.end(); i++){ char buffer[1024]; string msg = *i; // cout << "Line: " << msg << endl; + send(fd, msg.c_str(), msg.length(), 0); + read(fd, buffer, 1024); + } */ + + //this is the same as the loop that is commented out + for(string msg : lines){ + char buffer[1024]; + send(fd, msg.c_str(), msg.length(), 0); read(fd, buffer, 1024); } + send(fd, (void*)"End Transfer", 1024, 0); } @@ -50,14 +61,16 @@ void *threadFunc(void *args){ int clientFd = *(int*)args; char buffer[1024]; pthread_detach(pthread_self()); + // Read until disconnection while ((n = read(clientFd, buffer, 1024)) > 0){ string line(buffer); + if (line == "exit"){ close(clientFd); } - if (line == "get"){ - // cout << "Get Received" << endl; + else if (line == "get"){ + cout << "Get Received" << endl; sendFile(clientFd, readFile()); } } @@ -121,7 +134,7 @@ int main(void){ len = sizeof(cliAddr); while (true){ int clientFd = accept(serverFd, (struct sockaddr *)&serverAddr, &len); - + // Create thread to deal with client pthread_t thread; pthread_create(&thread, NULL, threadFunc, (void *)&clientFd); diff --git a/test b/test new file mode 100644 index 00000000..57a9c9f5 --- /dev/null +++ b/test @@ -0,0 +1,4 @@ +this is a test file +these are some words +these are some more +bye! From a29e0bd1606743dad7b9d1fa8eb901f268717cb0 Mon Sep 17 00:00:00 2001 From: Skylar Scorca Date: Sat, 22 Apr 2023 18:36:47 -0400 Subject: [PATCH 09/25] remove transfer file --- transfer | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 transfer diff --git a/transfer b/transfer deleted file mode 100644 index e69de29b..00000000 From 9edaa489c69e81bfff7c3a6314a537f4265e73a4 Mon Sep 17 00:00:00 2001 From: Skylar Scorca Date: Sat, 22 Apr 2023 19:05:24 -0400 Subject: [PATCH 10/25] open editor after file transfer --- kilo.c | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/kilo.c b/kilo.c index 02e5faf4..108764c4 100644 --- a/kilo.c +++ b/kilo.c @@ -1312,10 +1312,9 @@ void receiveFile(int fd){ while (1){ if ((n = read(fd, buffer, 1024)) > 0){ - // printf("Line: %s\n", buffer); buffer[n] = '\0'; if (!strcmp(buffer, "End Transfer")){ - // printf("Closing\n"); + printf("Closing\n"); fclose(file); return; } @@ -1323,7 +1322,6 @@ void receiveFile(int fd){ send(fd, "ACK", 1024, 0); //why are we sending this? } } - //editorOpen("test"); } /* ============================= Main Program ================================== */ @@ -1358,6 +1356,7 @@ int main(int argc, char **argv) { } printf("Connected\n"); +/* char buffer[1024]; while (fgets(buffer, 1024, stdin)){ buffer[strcspn(buffer, "\r\n")] = 0; @@ -1368,14 +1367,25 @@ int main(int argc, char **argv) { } // printf("%s\n", buffer); } +*/ + + char buffer[1024]; + fgets(buffer, 1024, stdin); + buffer[strcspn(buffer, "\r\n")] = 0; + if (!strcmp(buffer, "get")){ + printf("sending get\n"); + send(serverFd, "get", 1024, 0); + receiveFile(serverFd); + } close(serverFd); // exit(0); + char filename[20] = "transfer"; //start editor initEditor(); - editorSelectSyntaxHighlight("transfer"); - editorOpen(argv[1]); + editorSelectSyntaxHighlight(filename); + editorOpen(filename); enableRawMode(STDIN_FILENO); editorSetStatusMessage( "HELP: Ctrl-S = save | Ctrl-Q = quit | Ctrl-F = find"); @@ -1383,5 +1393,6 @@ int main(int argc, char **argv) { editorRefreshScreen(); editorProcessKeypress(STDIN_FILENO); } + return 0; } From bef2304517ba86c75e09b0563a2169dc05242754 Mon Sep 17 00:00:00 2001 From: Skylar Scorca <64870579+skylarscorca@users.noreply.github.com> Date: Sun, 23 Apr 2023 13:43:09 -0400 Subject: [PATCH 11/25] update readme --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 72998646..78914942 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ Kilo is a small text editor in less than 1K lines of code (counted with cloc). A screencast is available here: https://asciinema.org/a/90r2i9bq8po03nazhqtsifksb Usage: kilo `` -New Usage: kilo +New Usage: kilo `` `` 'get' to copy file from server From b9aa87ac9fcfeeae8c8fa00887adaff096f7e629 Mon Sep 17 00:00:00 2001 From: Skylar Scorca <64870579+skylarscorca@users.noreply.github.com> Date: Sun, 23 Apr 2023 13:43:28 -0400 Subject: [PATCH 12/25] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 78914942..a5713011 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,7 @@ Kilo is a small text editor in less than 1K lines of code (counted with cloc). A screencast is available here: https://asciinema.org/a/90r2i9bq8po03nazhqtsifksb Usage: kilo `` + New Usage: kilo `` `` 'get' to copy file from server From 3872c620a931ceeb8db70c8a015b5dbd25ae69eb Mon Sep 17 00:00:00 2001 From: Skylar Scorca Date: Sun, 23 Apr 2023 14:23:36 -0400 Subject: [PATCH 13/25] started adding message sending on client side --- kilo.c | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/kilo.c b/kilo.c index 108764c4..3a3be609 100644 --- a/kilo.c +++ b/kilo.c @@ -55,6 +55,10 @@ #include #include +/* Server Communication */ +#define MSGSIZE 1024 +static int serverFd; + /* Syntax highlight types */ #define HL_NORMAL 0 #define HL_NONPRINT 1 @@ -616,6 +620,11 @@ void editorInsertRow(int at, char *s, size_t len) { editorUpdateRow(E.row+at); E.numrows++; E.dirty++; + + //setup server message + char msg[MSGSIZE]; + sprintf(msg, "ir:%d:%s", at, s); + send(serverFd, msg, MSGSIZE, 0); } /* Free row's heap allocated stuff. */ @@ -637,6 +646,8 @@ void editorDelRow(int at) { for (int j = at; j < E.numrows-1; j++) E.row[j].idx++; E.numrows--; E.dirty++; + + //setup server message here } /* Turn the editor rows into a single heap-allocated string. @@ -687,6 +698,8 @@ void editorRowInsertChar(erow *row, int at, int c) { row->chars[at] = c; editorUpdateRow(row); E.dirty++; + + //setup server message here } /* Append the string 's' at the end of a row */ @@ -697,6 +710,8 @@ void editorRowAppendString(erow *row, char *s, size_t len) { row->chars[row->size] = '\0'; editorUpdateRow(row); E.dirty++; + + //setup server message here } /* Delete the character at offset 'at' from the specified row. */ @@ -706,9 +721,12 @@ void editorRowDelChar(erow *row, int at) { editorUpdateRow(row); row->size--; E.dirty++; + + //setup server message here } /* Insert the specified char at the current prompt position. */ +//don't need to make a server message here since we call editorInsertRow and editorRowInsertChar void editorInsertChar(int c) { int filerow = E.rowoff+E.cy; int filecol = E.coloff+E.cx; @@ -731,6 +749,7 @@ void editorInsertChar(int c) { /* Inserting a newline is slightly complex as we have to handle inserting a * newline in the middle of a line, splitting the line as needed. */ +//don't need to make a server message here since we call editorInsertRow void editorInsertNewline(void) { int filerow = E.rowoff+E.cy; int filecol = E.coloff+E.cx; @@ -767,6 +786,7 @@ void editorInsertNewline(void) { } /* Delete the char at the current prompt position. */ +//don't need to make a server message here since we call editorDelRow and editorRowDelChar void editorDelChar() { int filerow = E.rowoff+E.cy; int filecol = E.coloff+E.cx; @@ -1302,16 +1322,16 @@ void initEditor(void) { /* ========================= Communication with Server ======================== */ -void receiveFile(int fd){ +void receiveFile(){ ssize_t n; FILE *file = fopen("transfer", "w"); char buffer[1024]; - n = read(fd, buffer, 1024); + n = read(serverFd, buffer, 1024); buffer[n] = '\0'; while (1){ - if ((n = read(fd, buffer, 1024)) > 0){ + if ((n = read(serverFd, buffer, 1024)) > 0){ buffer[n] = '\0'; if (!strcmp(buffer, "End Transfer")){ printf("Closing\n"); @@ -1319,7 +1339,7 @@ void receiveFile(int fd){ return; } fprintf(file, "%s\n", buffer); - send(fd, "ACK", 1024, 0); //why are we sending this? + send(serverFd, "ACK", 1024, 0); } } } @@ -1346,7 +1366,6 @@ int main(int argc, char **argv) { } // Try addresses until one is successful - int serverFd; for (traverser = res; traverser; traverser = traverser->ai_next){ if ((serverFd = socket(traverser->ai_family, traverser->ai_socktype, traverser->ai_protocol)) != -1){ if ((connect(serverFd, traverser->ai_addr, traverser->ai_addrlen)) == 0){ @@ -1375,7 +1394,7 @@ int main(int argc, char **argv) { if (!strcmp(buffer, "get")){ printf("sending get\n"); send(serverFd, "get", 1024, 0); - receiveFile(serverFd); + receiveFile(); } close(serverFd); From 2ad8a1650d755bcab01bc19edbdd3ff530552008 Mon Sep 17 00:00:00 2001 From: Skylar Scorca Date: Sun, 23 Apr 2023 14:43:41 -0400 Subject: [PATCH 14/25] send update messages to server --- kilo.c | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/kilo.c b/kilo.c index 3a3be609..2a3a5f62 100644 --- a/kilo.c +++ b/kilo.c @@ -647,7 +647,10 @@ void editorDelRow(int at) { E.numrows--; E.dirty++; - //setup server message here + //setup server message + char msg[MSGSIZE]; + sprintf(msg, "dr:%d", at); + send(serverFd, msg, MSGSIZE, 0); } /* Turn the editor rows into a single heap-allocated string. @@ -699,7 +702,10 @@ void editorRowInsertChar(erow *row, int at, int c) { editorUpdateRow(row); E.dirty++; - //setup server message here + //setup server message + char msg[MSGSIZE]; + sprintf(msg, "ic:%d:%d:%c", row->idx, at, c); + send(serverFd, msg, MSGSIZE, 0); } /* Append the string 's' at the end of a row */ @@ -711,7 +717,10 @@ void editorRowAppendString(erow *row, char *s, size_t len) { editorUpdateRow(row); E.dirty++; - //setup server message here + //setup server message + char msg[MSGSIZE]; + sprintf(msg, "as:%d:%s", row->idx, s); + send(serverFd, msg, MSGSIZE, 0); } /* Delete the character at offset 'at' from the specified row. */ @@ -722,7 +731,10 @@ void editorRowDelChar(erow *row, int at) { row->size--; E.dirty++; - //setup server message here + //setup server message + char msg[MSGSIZE]; + sprintf(msg, "dc:%d:%d", row->idx, at); + send(serverFd, msg, MSGSIZE, 0); } /* Insert the specified char at the current prompt position. */ From 74240290511dcd3d809f404597ee912d1af8923d Mon Sep 17 00:00:00 2001 From: Skylar Scorca Date: Sun, 23 Apr 2023 14:48:23 -0400 Subject: [PATCH 15/25] moved where we close serverFd, cleaned up code --- kilo.c | 17 +---------------- server.cpp | 8 -------- 2 files changed, 1 insertion(+), 24 deletions(-) diff --git a/kilo.c b/kilo.c index 2a3a5f62..00ec9f8b 100644 --- a/kilo.c +++ b/kilo.c @@ -1251,6 +1251,7 @@ void editorProcessKeypress(int fd) { quit_times--; return; } + close(serverFd); exit(0); break; case CTRL_S: /* Ctrl-s */ @@ -1387,19 +1388,6 @@ int main(int argc, char **argv) { } printf("Connected\n"); -/* - char buffer[1024]; - while (fgets(buffer, 1024, stdin)){ - buffer[strcspn(buffer, "\r\n")] = 0; - if (!strcmp(buffer, "get")){ - printf("sending get\n"); - send(serverFd, "get", 1024, 0); - receiveFile(serverFd); - } - // printf("%s\n", buffer); - } -*/ - char buffer[1024]; fgets(buffer, 1024, stdin); buffer[strcspn(buffer, "\r\n")] = 0; @@ -1409,9 +1397,6 @@ int main(int argc, char **argv) { receiveFile(); } - close(serverFd); - // exit(0); - char filename[20] = "transfer"; //start editor initEditor(); diff --git a/server.cpp b/server.cpp index 50f16ad2..68349adc 100755 --- a/server.cpp +++ b/server.cpp @@ -36,14 +36,6 @@ void sendFile(int fd, vector lines){ send(fd, (void*)"Start Transfer", 1024, 0); -/* for (i = lines.begin(); i != lines.end(); i++){ - char buffer[1024]; - string msg = *i; - // cout << "Line: " << msg << endl; - send(fd, msg.c_str(), msg.length(), 0); - read(fd, buffer, 1024); - } */ - //this is the same as the loop that is commented out for(string msg : lines){ char buffer[1024]; From f9b6f2099dd3e634e4f3c92578ba0b738986bf38 Mon Sep 17 00:00:00 2001 From: Skylar Scorca Date: Sun, 23 Apr 2023 15:22:05 -0400 Subject: [PATCH 16/25] moved ir to the right place --- kilo.c | 20 ++++++++++++++++++-- server.cpp | 30 ++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 2 deletions(-) diff --git a/kilo.c b/kilo.c index 00ec9f8b..fa143327 100644 --- a/kilo.c +++ b/kilo.c @@ -621,10 +621,10 @@ void editorInsertRow(int at, char *s, size_t len) { E.numrows++; E.dirty++; - //setup server message +/* //setup server message char msg[MSGSIZE]; sprintf(msg, "ir:%d:%s", at, s); - send(serverFd, msg, MSGSIZE, 0); + send(serverFd, msg, MSGSIZE, 0); */ } /* Free row's heap allocated stuff. */ @@ -766,10 +766,16 @@ void editorInsertNewline(void) { int filerow = E.rowoff+E.cy; int filecol = E.coloff+E.cx; erow *row = (filerow >= E.numrows) ? NULL : &E.row[filerow]; + char msg[MSGSIZE]; if (!row) { if (filerow == E.numrows) { editorInsertRow(filerow,"",0); + + //server message + sprintf(msg, "ir:%d:%s", filerow, ""); + send(serverFd, msg, MSGSIZE, 0); + goto fixcursor; } return; @@ -779,14 +785,24 @@ void editorInsertNewline(void) { if (filecol >= row->size) filecol = row->size; if (filecol == 0) { editorInsertRow(filerow,"",0); + + //server message + sprintf(msg, "ir:%d:%s", filerow, ""); + send(serverFd, msg, MSGSIZE, 0); } else { /* We are in the middle of a line. Split it between two rows. */ editorInsertRow(filerow+1,row->chars+filecol,row->size-filecol); + + //server message + sprintf(msg, "ir:%d:%s", filerow+1, row->chars+filecol); + send(serverFd, msg, MSGSIZE, 0); + row = &E.row[filerow]; row->chars[filecol] = '\0'; row->size = filecol; editorUpdateRow(row); } + fixcursor: if (E.cy == E.screenrows-1) { E.rowoff++; diff --git a/server.cpp b/server.cpp index 68349adc..3acc0727 100755 --- a/server.cpp +++ b/server.cpp @@ -17,6 +17,7 @@ #include #include #include +#include using namespace std; //readFile - reads lines in file into the data structure lines @@ -52,6 +53,8 @@ void *threadFunc(void *args){ ssize_t n; int clientFd = *(int*)args; char buffer[1024]; + bool copied = false; + pthread_detach(pthread_self()); // Read until disconnection @@ -64,6 +67,33 @@ void *threadFunc(void *args){ else if (line == "get"){ cout << "Get Received" << endl; sendFile(clientFd, readFile()); + copied = true; + } + else if(copied){ + stringstream ss(line); + + //get update type + string cmd; + getline(ss, cmd, ':'); + + if(cmd == "ir"){ + cout << "ir received\n"; + } + else if(cmd == "dr"){ + cout << "dr received\n"; + } + else if(cmd == "ic"){ + cout << "ic received\n"; + } + else if(cmd == "as"){ + cout << "as received\n"; + } + else if(cmd == "dc"){ + cout << "dc received\n"; + } + else{ + cout << "Error: " << cmd << " is not a valid update type\n"; + } } } return NULL; From b1b2bcc617b432e226b5f9366baa395a5142429c Mon Sep 17 00:00:00 2001 From: jdewey2023 Date: Sun, 23 Apr 2023 16:19:55 -0400 Subject: [PATCH 17/25] Initialize text editor with file from server and clear screen after closing kilo --- kilo.c | 19 ++++++++++--------- server.cpp | 10 +++++----- test | 9 +++++---- 3 files changed, 20 insertions(+), 18 deletions(-) mode change 100644 => 100755 test diff --git a/kilo.c b/kilo.c index fa143327..66f6969d 100644 --- a/kilo.c +++ b/kilo.c @@ -49,6 +49,7 @@ #include #include #include +#include #include #include #include @@ -1267,6 +1268,11 @@ void editorProcessKeypress(int fd) { quit_times--; return; } + if (!fork()){ + char *args[] = {"clear", NULL}; + execvp("clear", args); // Kills child + } + wait(NULL); // Wait on child close(serverFd); exit(0); break; @@ -1404,15 +1410,10 @@ int main(int argc, char **argv) { } printf("Connected\n"); - char buffer[1024]; - fgets(buffer, 1024, stdin); - buffer[strcspn(buffer, "\r\n")] = 0; - if (!strcmp(buffer, "get")){ - printf("sending get\n"); - send(serverFd, "get", 1024, 0); - receiveFile(); - } - + send(serverFd, "get", 1024, 0); + receiveFile(); + + // char buffer[1024] = {'g', 'e', 't', '\0'}; char filename[20] = "transfer"; //start editor initEditor(); diff --git a/server.cpp b/server.cpp index 3acc0727..546f1dad 100755 --- a/server.cpp +++ b/server.cpp @@ -77,19 +77,19 @@ void *threadFunc(void *args){ getline(ss, cmd, ':'); if(cmd == "ir"){ - cout << "ir received\n"; + cout << "ir received\n" << line << endl; } else if(cmd == "dr"){ - cout << "dr received\n"; + cout << "dr received\n" << line << endl; } else if(cmd == "ic"){ - cout << "ic received\n"; + cout << "ic received\n" << line << endl; } else if(cmd == "as"){ - cout << "as received\n"; + cout << "as received\n" << line << endl; } else if(cmd == "dc"){ - cout << "dc received\n"; + cout << "dc received\n" << line << endl; } else{ cout << "Error: " << cmd << " is not a valid update type\n"; diff --git a/test b/test old mode 100644 new mode 100755 index 57a9c9f5..1c7d86c3 --- a/test +++ b/test @@ -1,4 +1,5 @@ -this is a test file -these are some words -these are some more -bye! +test file +here is a test +this test is working +very nice +goodbye From 0a1c7f8776ad31ef2e86c7040c6c18f4ed170f2f Mon Sep 17 00:00:00 2001 From: jdewey2023 Date: Sun, 23 Apr 2023 16:35:08 -0400 Subject: [PATCH 18/25] Port in command line arguments for server --- server.cpp | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/server.cpp b/server.cpp index 546f1dad..7393c2bf 100755 --- a/server.cpp +++ b/server.cpp @@ -105,8 +105,14 @@ void handleSigInt(int unused __attribute__((unused))){ } //main server program -int main(void){ - +int main(int argc, char *argv[]){ + if (argc != 2){ + cerr << "Usage: \n"; + return 1; + } + stringstream stream(argv[1]); + int port; + stream >> port; //setup SIGINT signal handler signal(SIGINT, handleSigInt); @@ -128,7 +134,7 @@ int main(void){ struct sockaddr_in serverAddr; serverAddr.sin_family = AF_INET; serverAddr.sin_addr.s_addr = htonl(INADDR_ANY); - serverAddr.sin_port = htons(10001); + serverAddr.sin_port = htons(port); if (bind(serverFd, (struct sockaddr *)&serverAddr, sizeof(serverAddr)) == -1){ cerr << "Error: Can't bind socket to port." << endl; return 3; From b9217f568bd098ddb5c1b9e51324883cf1f8ab0b Mon Sep 17 00:00:00 2001 From: Skylar Scorca Date: Sun, 23 Apr 2023 16:47:08 -0400 Subject: [PATCH 19/25] kilo only needs one ctrl-q --- kilo.c | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/kilo.c b/kilo.c index 66f6969d..7abd7dc4 100644 --- a/kilo.c +++ b/kilo.c @@ -1245,12 +1245,7 @@ void editorMoveCursor(int key) { /* Process events arriving from the standard input, which is, the user * is typing stuff on the terminal. */ -#define KILO_QUIT_TIMES 3 void editorProcessKeypress(int fd) { - /* When the file is modified, requires Ctrl-q to be pressed N times - * before actually quitting. */ - static int quit_times = KILO_QUIT_TIMES; - int c = editorReadKey(fd); switch(c) { case ENTER: /* Enter */ @@ -1261,13 +1256,6 @@ void editorProcessKeypress(int fd) { * to the edited file. */ break; case CTRL_Q: /* Ctrl-q */ - /* Quit if the file was already saved. */ - if (E.dirty && quit_times) { - editorSetStatusMessage("WARNING!!! File has unsaved changes. " - "Press Ctrl-Q %d more times to quit.", quit_times); - quit_times--; - return; - } if (!fork()){ char *args[] = {"clear", NULL}; execvp("clear", args); // Kills child @@ -1317,8 +1305,6 @@ void editorProcessKeypress(int fd) { editorInsertChar(c); break; } - - quit_times = KILO_QUIT_TIMES; /* Reset it to the original value. */ } int editorFileWasModified(void) { From 2ebb4fc36ff93e041e82c5819142ffbe5453528c Mon Sep 17 00:00:00 2001 From: Skylar Scorca Date: Sun, 23 Apr 2023 16:59:42 -0400 Subject: [PATCH 20/25] took out save ctrl-s --- kilo.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/kilo.c b/kilo.c index 7abd7dc4..da3211b0 100644 --- a/kilo.c +++ b/kilo.c @@ -1265,7 +1265,7 @@ void editorProcessKeypress(int fd) { exit(0); break; case CTRL_S: /* Ctrl-s */ - editorSave(); + //editorSave(); break; case CTRL_F: editorFind(fd); @@ -1407,7 +1407,7 @@ int main(int argc, char **argv) { editorOpen(filename); enableRawMode(STDIN_FILENO); editorSetStatusMessage( - "HELP: Ctrl-S = save | Ctrl-Q = quit | Ctrl-F = find"); + "HELP: Ctrl-Q = quit | Ctrl-F = find"); while(1) { editorRefreshScreen(); editorProcessKeypress(STDIN_FILENO); From 9970d9778c24bd0d7e7b8be783d1ccf7ea54f94b Mon Sep 17 00:00:00 2001 From: jdewey2023 Date: Sun, 23 Apr 2023 17:04:01 -0400 Subject: [PATCH 21/25] Remove temporary transfer file --- kilo.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/kilo.c b/kilo.c index 7abd7dc4..c911aa4c 100644 --- a/kilo.c +++ b/kilo.c @@ -60,6 +60,9 @@ #define MSGSIZE 1024 static int serverFd; +/* Temporary File */ +char filename[20] = "transfer"; + /* Syntax highlight types */ #define HL_NORMAL 0 #define HL_NONPRINT 1 @@ -1262,6 +1265,7 @@ void editorProcessKeypress(int fd) { } wait(NULL); // Wait on child close(serverFd); + remove(filename); exit(0); break; case CTRL_S: /* Ctrl-s */ @@ -1400,7 +1404,6 @@ int main(int argc, char **argv) { receiveFile(); // char buffer[1024] = {'g', 'e', 't', '\0'}; - char filename[20] = "transfer"; //start editor initEditor(); editorSelectSyntaxHighlight(filename); From 54e32a345898921f6816e2b6e5413b8869a53f2d Mon Sep 17 00:00:00 2001 From: Tony Landis Drouillard Date: Sun, 23 Apr 2023 17:09:01 -0400 Subject: [PATCH 22/25] Starting server update client --- server.cpp | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/server.cpp b/server.cpp index 7393c2bf..21c07b06 100755 --- a/server.cpp +++ b/server.cpp @@ -73,7 +73,8 @@ void *threadFunc(void *args){ stringstream ss(line); //get update type - string cmd; + bool validCMD = true; + string cmd; getline(ss, cmd, ':'); if(cmd == "ir"){ @@ -93,7 +94,12 @@ void *threadFunc(void *args){ } else{ cout << "Error: " << cmd << " is not a valid update type\n"; + validCMD = false; } + + if(validCMD){ + // Send update messages + } } } return NULL; @@ -168,4 +174,4 @@ int main(int argc, char *argv[]){ pthread_create(&thread, NULL, threadFunc, (void *)&clientFd); } return 0; -} \ No newline at end of file +} From 9e1c06db2c66865fd8b04a5d4bd5b9ebb501162b Mon Sep 17 00:00:00 2001 From: Skylar Scorca Date: Sun, 23 Apr 2023 17:24:57 -0400 Subject: [PATCH 23/25] added thread function and create thread --- kilo.c | 33 +++++++++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/kilo.c b/kilo.c index da3211b0..e4b07a29 100644 --- a/kilo.c +++ b/kilo.c @@ -55,8 +55,10 @@ #include #include #include +#include /* Server Communication */ +#define _POSIX_SOURCE #define MSGSIZE 1024 static int serverFd; @@ -1346,13 +1348,13 @@ void initEditor(void) { void receiveFile(){ ssize_t n; FILE *file = fopen("transfer", "w"); - char buffer[1024]; + char buffer[MSGSIZE]; - n = read(serverFd, buffer, 1024); + n = read(serverFd, buffer, MSGSIZE); buffer[n] = '\0'; while (1){ - if ((n = read(serverFd, buffer, 1024)) > 0){ + if ((n = read(serverFd, buffer, MSGSIZE)) > 0){ buffer[n] = '\0'; if (!strcmp(buffer, "End Transfer")){ printf("Closing\n"); @@ -1360,15 +1362,32 @@ void receiveFile(){ return; } fprintf(file, "%s\n", buffer); - send(serverFd, "ACK", 1024, 0); + send(serverFd, "ACK", MSGSIZE, 0); } } } +void *read_server_messages(){ + char buffer[MSGSIZE]; + int n; + + pthread_detach(pthread_self()); + + if ((n = read(serverFd, buffer, MSGSIZE)) == 0) { + printf("server crashed\n"); + exit(0); + } + buffer[n] = '\0'; + + return NULL; +} + /* ============================= Main Program ================================== */ //main program of text-editor client int main(int argc, char **argv) { + pthread_t read_thread; + //check command-line args if (argc != 3) { fprintf(stderr,"Usage: kilo \n"); @@ -1398,6 +1417,12 @@ int main(int argc, char **argv) { send(serverFd, "get", 1024, 0); receiveFile(); + + //create thread for reading server messages + int i; + if ((i = pthread_create(&read_thread, NULL, read_server_messages, (void*)NULL)) != 0) { + printf("thread creation failed\n"); + } // char buffer[1024] = {'g', 'e', 't', '\0'}; char filename[20] = "transfer"; From 2fa5e53663d7a5346fd88ed4cd27a65efcd4a0b3 Mon Sep 17 00:00:00 2001 From: Tony Landis Drouillard Date: Sun, 23 Apr 2023 17:32:52 -0400 Subject: [PATCH 24/25] Added multiple clients and distribution of update messages --- server.cpp | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/server.cpp b/server.cpp index 21c07b06..d4b54248 100755 --- a/server.cpp +++ b/server.cpp @@ -20,6 +20,8 @@ #include using namespace std; +vector users; + //readFile - reads lines in file into the data structure lines vector readFile(){ string line; @@ -99,6 +101,10 @@ void *threadFunc(void *args){ if(validCMD){ // Send update messages + for(auto user : users){ + if(user == clientFd) continue; + write(user, buffer, 1024); + } } } } @@ -167,11 +173,11 @@ int main(int argc, char *argv[]){ struct sockaddr_in cliAddr; len = sizeof(cliAddr); while (true){ - int clientFd = accept(serverFd, (struct sockaddr *)&serverAddr, &len); + users.push_back(accept(serverFd, (struct sockaddr *)&serverAddr, &len)); // Create thread to deal with client pthread_t thread; - pthread_create(&thread, NULL, threadFunc, (void *)&clientFd); + pthread_create(&thread, NULL, threadFunc, (void *)&users[users.size()-1]); } return 0; } From b0e4f484dae24ab00fcf682fb1f1a101c0acacfd Mon Sep 17 00:00:00 2001 From: Skylar Scorca Date: Sun, 23 Apr 2023 17:57:41 -0400 Subject: [PATCH 25/25] get update command --- kilo.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/kilo.c b/kilo.c index e4b07a29..659dd90c 100644 --- a/kilo.c +++ b/kilo.c @@ -1367,6 +1367,17 @@ void receiveFile(){ } } +void handle_server_message(char *msg){ + char cmd[MSGSIZE]; + int i; + + //get command + for(i = 0; i < MSGSIZE; ++i){ + if(msg[i] == ':'){break;} //stop reading when we encounter colon + cmd[i] = msg[i]; + } +} + void *read_server_messages(){ char buffer[MSGSIZE]; int n; @@ -1379,6 +1390,8 @@ void *read_server_messages(){ } buffer[n] = '\0'; + handle_server_message(buffer); + return NULL; }