diff options
author | sotech117 <michael_foiani@brown.edu> | 2023-09-20 01:19:39 -0400 |
---|---|---|
committer | sotech117 <michael_foiani@brown.edu> | 2023-09-20 01:19:39 -0400 |
commit | ea7aff51be44884e22c8bdabef917c77c291951e (patch) | |
tree | 7ecb12326287eb9c3799b047c36e555b5ccfbbaf | |
parent | 1263cbdbb6cf3ebbb157286b2bb2e488e4b931c8 (diff) |
add better command line interface to server repl. also, add the print function that can go to file.
-rw-r--r-- | client.c | 12 | ||||
-rwxr-xr-x | snowcast_control | bin | 21440 -> 35357 bytes | |||
-rw-r--r-- | snowcast_control.dSYM/Contents/Resources/DWARF/snowcast_control | bin | 13854 -> 13773 bytes | |||
-rwxr-xr-x | snowcast_listener | bin | 19128 -> 34654 bytes | |||
-rwxr-xr-x | snowcast_server | bin | 37128 -> 55724 bytes | |||
-rw-r--r-- | snowcast_server.dSYM/Contents/Resources/DWARF/snowcast_server | bin | 23723 -> 25277 bytes | |||
-rw-r--r-- | snowcast_server_concurrent.c | 117 |
7 files changed, 114 insertions, 15 deletions
@@ -144,9 +144,9 @@ void *reply_thread_routine(void* args) { exit(1); } buf[recvbytes] = '\0'; - printf("client: received %d bytes on a reply call \n", recvbytes); + // printf("client: received %d bytes on a reply call \n", recvbytes); // print the two first field of the call - printf("client: replyType: %d, stringSize: %d\n", buf[0], buf[1]); + // printf("client: replyType: %d, stringSize: %d\n", buf[0], buf[1]); // print the while buffer by char for (int i = 0; i < recvbytes; i++) { printf("%c ", buf[i]); @@ -155,8 +155,8 @@ void *reply_thread_routine(void* args) { memcpy(&reply, buf, 2); // print out the fields of reply on one line - printf("\nclient: replyType: %d, stringSize: %d\n", reply.replyType, reply.stringSize); - + // printf("\nclient: replyType: %d, stringSize: %d\n", reply.replyType, reply.stringSize); + if (reply.replyType == 2) { struct Welcome msg; // recv the message, check for errors too @@ -167,13 +167,13 @@ void *reply_thread_routine(void* args) { // print the size of reply if (reply.replyType == 3) { - printf("client: received an announce message\n"); + // printf("client: received an announce message\n"); char *song_name = malloc(reply.stringSize); // printf(sizeof(struct Reply)); memcpy(song_name, buf + 2, reply.stringSize); - printf("client: song name: %s\n", song_name); + printf("New song announced: %s\n", song_name); free(song_name); diff --git a/snowcast_control b/snowcast_control Binary files differindex 711e770..ad92c5e 100755 --- a/snowcast_control +++ b/snowcast_control diff --git a/snowcast_control.dSYM/Contents/Resources/DWARF/snowcast_control b/snowcast_control.dSYM/Contents/Resources/DWARF/snowcast_control Binary files differindex 4cd75ec..9af43c5 100644 --- a/snowcast_control.dSYM/Contents/Resources/DWARF/snowcast_control +++ b/snowcast_control.dSYM/Contents/Resources/DWARF/snowcast_control diff --git a/snowcast_listener b/snowcast_listener Binary files differindex 9f75571..9796cb9 100755 --- a/snowcast_listener +++ b/snowcast_listener diff --git a/snowcast_server b/snowcast_server Binary files differindex 8ef32a4..1c3a8fb 100755 --- a/snowcast_server +++ b/snowcast_server diff --git a/snowcast_server.dSYM/Contents/Resources/DWARF/snowcast_server b/snowcast_server.dSYM/Contents/Resources/DWARF/snowcast_server Binary files differindex a3f5493..a4a9da5 100644 --- a/snowcast_server.dSYM/Contents/Resources/DWARF/snowcast_server +++ b/snowcast_server.dSYM/Contents/Resources/DWARF/snowcast_server diff --git a/snowcast_server_concurrent.c b/snowcast_server_concurrent.c index d855bab..2c74cab 100644 --- a/snowcast_server_concurrent.c +++ b/snowcast_server_concurrent.c @@ -8,6 +8,9 @@ #include <netdb.h> #include <string.h> +#include <sys/stat.h> +#include <fcntl.h> + #include "protocol.h" #define LINE_MAX 1024 @@ -53,6 +56,9 @@ void *send_udp_packet_routine(void* arg); void *select_thread(void* arg); void *synchronization_thread(void* arg); +int parse(char buffer[LINE_MAX], char *tokens[LINE_MAX / 2]); +void *print_info_routine(void *arg); + void *get_in_addr(struct sockaddr *sa); void *init_user(int sockfd); @@ -118,27 +124,105 @@ int main(int argc, char *argv[]) // command line interface char input[LINE_MAX]; - while (1) { - char *line = fgets(input, LINE_MAX, stdin); + memset(input, 0, LINE_MAX); - if (line == NULL) { + char *tokens[LINE_MAX / 2]; + while (read(STDIN_FILENO, input, LINE_MAX) > 0) { + // init tokens + memset(tokens, 0, (LINE_MAX / 2) * sizeof(char *)); + + // if 0, all whitespace + if (!parse(input, tokens)) continue; - } else if (strncmp("q\n", input, LINE_MAX) == 0) { - // end code if type in q + + char *command = tokens[0]; + // if q, shutdown! + if (!strcmp(command, "q")) { printf("Exiting.\n"); + // TODO: exit better than break break; - } else if (strncmp("p\n", input, LINE_MAX) == 0) { + } + + // if p, print info + else if (!strcmp(command, "p")) { + // get the file descriptor + int print_fd = 0; + // see if there is a file path + char *output_file_path = tokens[1]; + if (output_file_path != NULL) + { + if ((print_fd = open(output_file_path, O_CREAT | O_WRONLY | O_TRUNC, S_IRWXU)) == -1) + { + perror("open"); + continue; + } + } else { + print_fd = STDOUT_FILENO; + } + printf("print_fd: %d\n", print_fd); + pthread_t print_info_thread; + pthread_create(&print_info_thread, NULL, print_info_routine, print_fd); + // note - this file descriptor is closed in the thread + } + else if (strncmp("u\n", input, LINE_MAX) == 0) + { // print all user data - for (int i = 0; i < max_active_users; i++) { + for (int i = 0; i < max_active_users; i++) + { print_user_data(i); } - } else if (strncmp("s\n", input, LINE_MAX) == 0) { } } return 0; } +void write_int_to_fd(int fd, int n) { + int l = snprintf(NULL, 0, "%d", n); + char *num = malloc(l + 1); + if (!num) { perror("malloc"); return; } + + snprintf(num, l + 1, "%d", n); + if (write(fd, num, strlen(num)) == -1) { + perror("write"); + } + + + free(num); +} + +void *print_info_routine(void *arg) { + int print_fd = (int) arg; + // printf("thread print_fd: %d\n", print_fd); + // printf("num_stations: %d\n", num_stations); + for (int i = 0; i < num_stations; i++) { + write_int_to_fd(print_fd, i); + char *comma = ","; + write(print_fd, comma, strlen(comma)); + + // write file path + char* file_path = station_data[i].filePath; + write(print_fd, file_path, strlen(file_path)); + + for (int j = 0; j < max_active_users; j++) { + if (user_data[j].sockfd == -1) + continue; + if (user_data[j].stationNum == i) { + char *localhost_ip = ",127.0.0.1:"; + write(print_fd, localhost_ip, strlen(localhost_ip)); + // write udpPort + write_int_to_fd(print_fd, user_data[j].udpPort); + } + } + // wrtie new line + char *newline = "\n"; + write(print_fd, newline, strlen(newline)); + } + + if (print_fd != STDOUT_FILENO) close(print_fd); + return (NULL); +} + int sendall(int udp_sockfd, char *buf, int *len, struct addrinfo *thread_res) { int MAX_PACKET_SIZE = 512; @@ -606,4 +690,19 @@ void send_announce_message(int fd, int station_num) { printf("sent %d bytes\n", bytessent); free(send_buffer); -}
\ No newline at end of file +} + + +// Parses a buffer into tokens, from cs33 :) +int parse(char buffer[LINE_MAX], char *tokens[LINE_MAX / 2]) { + const char *regex = " \n\t\f\r"; + char *current_token = strtok(buffer, regex); + if (current_token == NULL) return 0; + + for (int i = 0; current_token != NULL; i++) { + tokens[i] = current_token; + current_token = strtok(NULL, regex); + } + + return 1; +} |