aboutsummaryrefslogtreecommitdiff
path: root/server.c
diff options
context:
space:
mode:
Diffstat (limited to 'server.c')
-rw-r--r--server.c33
1 files changed, 33 insertions, 0 deletions
diff --git a/server.c b/server.c
index ccde0bb..d782bc0 100644
--- a/server.c
+++ b/server.c
@@ -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";