diff options
author | sotech117 <michael_foiani@brown.edu> | 2023-09-23 23:10:12 -0400 |
---|---|---|
committer | sotech117 <michael_foiani@brown.edu> | 2023-09-23 23:10:12 -0400 |
commit | c055294629f1c3bb8843c65ce5ebff15efc94b7b (patch) | |
tree | a22057b62674feb4fe3a243b8cf61688a36f5747 /server.c | |
parent | 773f64c74c222d20cfb1183fcc235f429ce5370f (diff) |
implement extra credit
Diffstat (limited to 'server.c')
-rw-r--r-- | server.c | 33 |
1 files changed, 33 insertions, 0 deletions
@@ -92,6 +92,7 @@ void destroy_user(int sockfd); void send_announce_reply(int fd, int station_num); void send_invalid_command_reply(int fd, size_t message_size, char* message); +void send_stations_info_reply(int fd); // void *load_file(void* arg); @@ -711,6 +712,9 @@ void *select_routine(void *arg) { update_user_station(i, station_number); send_announce_reply(i, station_number); } + else if (command_type == 5) { // we got a ListStations command + send_stations_info_reply(i); + } else { // send back in invalid command char * message = "invalid command"; @@ -848,6 +852,35 @@ void send_invalid_command_reply(int fd, size_t message_size, char* message) { free(send_buffer); } +void send_stations_info_reply(int fd) { + printf("sending stations info reply\n"); + // get the size of the reply + + uint8_t reply_size = 0; + for (int i = 0; i < num_stations; i++) reply_size += snprintf(NULL, 0, "%d,%s\n", i, stations[i].filePath); + reply_size--; // don't want final \n + + // send type + uint8_t reply_num = 6; + if (send(fd, &reply_num, 1, 0) == -1) + perror("send in send stations info"); + // send payload size + printf("reply_size (server): %d\n", reply_size); + if (send(fd, &reply_size, 1, 0) == -1) + perror("send in send stations info"); + + char send_buffer[reply_size]; + int ptr = 0; + for (int i = 0; i < num_stations; i++) + ptr += sprintf(send_buffer + ptr, (i == num_stations - 1) ? "%d,%s" : "%d,%s\n", i, stations[i].filePath); + + printf("buffer: \n%s\n", send_buffer); + + int bytes_to_send = reply_size; // don't want final \n + if (send_all(fd, &send_buffer, &bytes_to_send) == -1) + perror("send_all"); +} + // 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"; |