aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorsotech117 <michael_foiani@brown.edu>2023-09-26 00:21:44 -0400
committersotech117 <michael_foiani@brown.edu>2023-09-26 00:21:44 -0400
commit172a46063a25b2b49072b3e74b4ce3fdb137032a (patch)
tree733a6554dc356285b9b1789d89dc6ef68a545e06
parentb0efac86fe8ef8c6265f73fca13cf5d331cec353 (diff)
parent45c29ba1ae0994f4e933d1f3f15df280306783c4 (diff)
Merge branch 'main' of https://github.com/brown-cs1680-f23/snowcast-sotech117
Revert server.
-rw-r--r--server.c94
1 files changed, 48 insertions, 46 deletions
diff --git a/server.c b/server.c
index b0ad3ed..feb62c9 100644
--- a/server.c
+++ b/server.c
@@ -48,7 +48,7 @@ pthread_mutex_t mutex_stations = PTHREAD_MUTEX_INITIALIZER;
station_t *stations;
int setup_stations(int argc, char *argv[]);
void *stream_routine(void *arg);
-void *stream_routine_cleanup(void *arg);
+void stream_routine_cleanup(void *arg);
int read_file(int fd, char buffer[FILE_READ_SIZE], int station_num);
// ----------------------------------------------------------------------------------------------------------
@@ -71,7 +71,7 @@ void destroy_user(int tcpfd);
// ----------------------------------------------------------------------------------------------------------
// 3) UDP STREAM DATA AND FUNCTIONS
// ----------------------------------------------------------------------------------------------------------
-typedef struct send_udp_packet_routine_args {
+struct send_udp_packet_routine_args {
int user_index;
int buffer_size;
char *file_buffer;
@@ -79,7 +79,7 @@ typedef struct send_udp_packet_routine_args {
int start_threads = 0; // for synchronization, with the cond
pthread_cond_t cond = PTHREAD_COND_INITIALIZER; // for synchronization, with the following routine
void *send_udp_packet_routine(void* arg);
-void *send_udp_packet_routine_cleanup(void *arg);
+void send_udp_packet_routine_cleanup(void *arg);
int setup_udp_connection(int udp_port, int *udp_sockfd, socklen_t *addrlen, struct sockaddr *addr);
int send_all_udp(int udp_sockfd, char *buf, int *len, struct sockaddr *addr, socklen_t addrlen);
@@ -107,12 +107,12 @@ void send_invalid_reply(int fd, size_t message_size, char* message);
// ----------------------------------------------------------------------------------------------------------
// 6) COMMAND LINE INTERFACE FUNCTIONS
// ----------------------------------------------------------------------------------------------------------
-uint8_t l = 0; // boolean for logging -> if (l) { printf("..."); }
+uint8_t l = 1; // boolean for logging -> if (l) { printf("..."); }
int parse(char buffer[COMMAND_LINE_MAX], char *tokens[COMMAND_LINE_MAX / 2]);
-void print_info_routine(int fd);
+void print_info(int fd);
void write_int_to_fd(int fd, int n); // helper to print_info_routine
-void *print_user_data(int index);
-void *print_station_data(int station);
+void print_user_data(int index);
+void print_station_data(int station);
void cleanup_readfds_and_sockets();
@@ -166,7 +166,7 @@ main(int argc, char *argv[]) // no int here for good luck :)
// make and start "select" thread that manages:
// 1) new connections, 2) requests from current connections, 3) closing connections
pthread_t select_thread;
- pthread_create(&select_thread, NULL, select_routine, listenerfd);
+ pthread_create(&select_thread, NULL, select_routine, (void *) &listenerfd);
// BELOW IS FOR THE COMMAND LINE INTERFACE
// -------------------------------------------------------------------------------------------------s
@@ -176,6 +176,7 @@ main(int argc, char *argv[]) // no int here for good luck :)
char *tokens[COMMAND_LINE_MAX / 2];
printf("snowcast_server> "); // very cute to have :)
fflush(stdout);
+
while (read(STDIN_FILENO, input, COMMAND_LINE_MAX) > 0) {
// init tokens
memset(tokens, 0, COMMAND_LINE_MAX / 2);
@@ -199,6 +200,7 @@ main(int argc, char *argv[]) // no int here for good luck :)
// if "p" command: print info
else if (!strcmp(command, "p")) {
+ // get the file path to print to
int print_fd;
// see if there is a file path to print to,
@@ -215,7 +217,7 @@ main(int argc, char *argv[]) // no int here for good luck :)
} else print_fd = STDOUT_FILENO;
// print the info
- print_info_routine(print_fd);
+ print_info(print_fd);
}
// if "u" command: print user data (debugging)
@@ -247,7 +249,7 @@ main(int argc, char *argv[]) // no int here for good luck :)
if (tokens[1] == NULL) {
printf("remove: must provide a station number to remove\n");
}
- else if ((tokens[1] != '\0' && atoi(tokens[1]) == 0)) {
+ else if ((tokens[1][0] != '0' && atoi(tokens[1]) == 0)) {
printf("remove: must provide a number\n");
} else destroy_station(atoi(tokens[1]));
}
@@ -292,7 +294,7 @@ int setup_stations(int argc, char *argv[]) {
stations[i].filePath = argv[i+2];
stations[i].readfd = open(argv[i+2], O_RDONLY);
if (stations[i].readfd < 0) { perror("read (from station file)"); return -1; }
- pthread_create(&stations[i].streamThread, NULL, stream_routine, i);
+ pthread_create(&stations[i].streamThread, NULL, stream_routine, (void *) &i);
}
return 1;
@@ -306,11 +308,11 @@ int setup_stations(int argc, char *argv[]) {
note: you can modify how often and how much is read off the file by changing the MACROS
*/
void *stream_routine(void *arg) {
- int station_num = (int) arg;
+ int station_num = * (int*) arg;
// printf("stream routine %d\n", station_num);
int read_fd = stations[station_num].readfd;
- pthread_cleanup_push(stream_routine_cleanup, read_fd);
+ pthread_cleanup_push(stream_routine_cleanup, (void *) &read_fd);
// make buffer for read_file
char buffer[FILE_READ_SIZE];
@@ -365,8 +367,8 @@ void *stream_routine(void *arg) {
pthread_cleanup_pop(1);
}
-void *stream_routine_cleanup(void *arg) { // closes the read fd if thread is cancelled
- int read_fd = (int) arg;
+void stream_routine_cleanup(void *arg) { // closes the read fd if thread is cancelled
+ int read_fd = * (int *) arg;
close(read_fd);
}
@@ -384,7 +386,7 @@ int read_file(int fd, char buffer[FILE_READ_SIZE], int station_num) {
if (bytes_read == 0) {
// printf("end of file, restarting\n");
pthread_t send_announce_thread;
- pthread_create(&send_announce_thread, NULL, send_announce_routine, station_num);
+ pthread_create(&send_announce_thread, NULL, send_announce_routine, (void *) &station_num);
if (lseek(fd, 0, SEEK_SET) == -1)
{
@@ -481,7 +483,7 @@ void *send_udp_packet_routine(void *arg) {
int *buf = arg;
int user_index = buf[0];
int buffer_size = buf[1];
- char *data_ptr = buf + 2; // add two will skip first ints, since int* buf
+ char *data_ptr = (char *) (buf + 2); // add two will skip first ints, since int* buf
// setup variables
int did_work = 1;
@@ -499,7 +501,7 @@ void *send_udp_packet_routine(void *arg) {
}
// setup cleanup for thread that closes the fd if closed
- pthread_cleanup_push(send_udp_packet_routine_cleanup, (void *)udp_sockfd);
+ pthread_cleanup_push(send_udp_packet_routine_cleanup, (void *)&udp_sockfd);
pthread_mutex_lock(&m);
// wait for threads
@@ -515,7 +517,7 @@ void *send_udp_packet_routine(void *arg) {
}
// send the data! (no error check since udp)
- send_all_udp(udp_sockfd, data_ptr, &buffer_size, &addr, addrlen);
+ send_all_udp(udp_sockfd, (char *) data_ptr, &buffer_size, &addr, addrlen);
// cleanup
close(udp_sockfd);
@@ -525,11 +527,10 @@ void *send_udp_packet_routine(void *arg) {
return (NULL);
}
-void *send_udp_packet_routine_cleanup(void *arg) // closes the udpfd if thread is cancelled
+void send_udp_packet_routine_cleanup(void *arg) // closes the udpfd if thread is cancelled
{
- int sockfd = (int) arg;
+ int sockfd = * (int *) arg;
close(sockfd);
- return (NULL);
}
/*
@@ -548,13 +549,13 @@ int setup_udp_connection(int udp_port, int *udp_sockfd, socklen_t *addrlen, stru
// convert port uint16 into a string for getaddrinfo
int length = snprintf( NULL, 0, "%d", udp_port );
char* port = malloc( length + 1 );
- if (!port) { perror("malloc on port"); return (NULL); }
+ if (!port) { perror("malloc on port"); return -1; }
snprintf(port, length + 1, "%d", udp_port);
sprintf(port, "%d", udp_port);
// resolve host
int error_code;
- if (error_code = getaddrinfo(NULL, port, &thread_hints, &thread_servinfo) != 0)
+ if ((error_code = getaddrinfo(NULL, port, &thread_hints, &thread_servinfo)) != 0)
{
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(error_code));
return -1;
@@ -571,7 +572,7 @@ int setup_udp_connection(int udp_port, int *udp_sockfd, socklen_t *addrlen, stru
}
break;
}
- if (sock == NULL) {
+ if (sock == -1) {
fprintf(stderr, "socket: failed to create udp socket(setup_udp_connection)\n");
return -1;
}
@@ -676,9 +677,7 @@ int handle_new_connection(int listener) {
// accept new connection
struct sockaddr_storage remoteaddr;
socklen_t addrlen = sizeof remoteaddr;
- int newfd = accept(listener,
- (struct sockaddr *)&remoteaddr,
- &addrlen);
+ int newfd = accept(listener, (struct sockaddr *)&remoteaddr, &addrlen);
// check if newfd is valid
if (newfd == -1) {
perror("accept");
@@ -710,7 +709,7 @@ int handle_new_connection(int listener) {
3) handles new connections or responds on new data from clients
*/
void *select_routine(void *arg) {
- int listener = (int) arg; // the listener fd
+ int listener = * (int *) arg; // the listener fd
fd_set read_fds; // temp file descriptor list for select()
FD_ZERO(&read_fds); // clear the temp set
@@ -777,8 +776,8 @@ int handle_client_command(int clientfd) {
}
if (command_type == SETSTATION) {
- if(!handle_setstation_command(clientfd)) { // failure
- // remove user
+ if(!handle_setstation_command(clientfd)) {
+ // on failure, remove user
destroy_user(clientfd);
return 0;
}
@@ -789,7 +788,7 @@ int handle_client_command(int clientfd) {
if (l) printf("received a LISTSTATIONS from socket %d\n", clientfd);
pthread_t send_thread; // send STATIONINFO from a different thread
- pthread_create(&send_thread, NULL, send_stationsinfo_routine, clientfd);
+ pthread_create(&send_thread, NULL, send_stationsinfo_routine, (void *) &clientfd);
return 1;
}
@@ -827,7 +826,7 @@ uint16_t handle_handshake(int newfd)
// get the udp port
uint16_t udp_port;
int bytes_to_read = sizeof(uint16_t);
- if (recv_all(newfd, &udp_port, &bytes_to_read) == -1) {
+ if (recv_all(newfd, (char *) &udp_port, &bytes_to_read) == -1) {
perror("recv_all");
close(newfd);
return 0;
@@ -847,7 +846,7 @@ uint8_t send_welcome_reply(int fd) { // returns 0 on failure
welcome.replyType = 2;
welcome.numStations = htons(num_stations);
int bytes_to_send = sizeof(struct Welcome);
- if (send_all(fd, &welcome, &bytes_to_send) == -1) {
+ if (send_all(fd, (char *) &welcome, &bytes_to_send) == -1) {
perror("send_all (in send_welcome_reply)");
return 0;
}
@@ -863,7 +862,7 @@ uint8_t handle_setstation_command(int tcpfd) {
// get the station number
uint16_t station_number;
int bytes_to_read = sizeof(uint16_t);
- if (recv_all(tcpfd, &station_number, &bytes_to_read) == -1) {
+ if (recv_all(tcpfd, (char *) &station_number, &bytes_to_read) == -1) {
perror("recv_all");
return 0;
}
@@ -913,7 +912,7 @@ void update_user_station(int tcpfd, int stationNum) {
*/
void *send_announce_routine(void *arg) {
// unpack arg
- int station_num = (int) arg;
+ int station_num = * (int *) arg;
// send the announce messages
for (int i = 0; i < max_active_users; i++)
{
@@ -923,6 +922,8 @@ void *send_announce_routine(void *arg) {
if (users[i].stationNum == station_num)
send_announce_reply(users[i].tcpfd, station_num);
}
+
+ return (NULL);
}
/*
@@ -1000,7 +1001,7 @@ int parse(char buffer[COMMAND_LINE_MAX], char *tokens[COMMAND_LINE_MAX / 2])
given a file descriptor to write to, prints the station/user info in the desired format
note: it will close the fd inputted, except for stdout
*/
-void print_info_routine(int fd) {
+void print_info(int fd) {
// for each station, print the info
for (int i = 0; i < num_stations; i++) {
@@ -1045,13 +1046,14 @@ void write_int_to_fd(int fd, int n) {
}
free(num);
}
-void *print_user_data(int index) {
+void print_user_data(int index) {
printf("user: %d -> udpPort: %d, stationNum: %d, tcpfd: %d\n", index,
users[index].udpPort, users[index].stationNum, users[index].tcpfd);
}
-void *print_station_data(int station) {
+void print_station_data(int station) {
printf("station: %d -> filePath: %s, readfd: %d\n",
station, stations[station].filePath, stations[station].readfd);
+
}
/*
to be called for a "graceful" exit
@@ -1074,7 +1076,7 @@ void cleanup_readfds_and_sockets() {
// 7) EXTRA CREDIT IMPLEMENTATIONS (no broad function comments below, but it is all pretty self explanatory)
// ----------------------------------------------------------------------------------------------------------
void *send_stationsinfo_routine(void * arg) {
- int fd = (int) arg;
+ int fd = * (int *) arg;
if (l) printf("sending STATIONSINFO reply to socket %d\n", fd);
@@ -1093,7 +1095,7 @@ void *send_stationsinfo_routine(void * arg) {
// send payload size
uint32_t reply_size_endian = htonl(reply_size);
int bytes = sizeof(uint32_t);
- if (send_all(fd, &reply_size_endian, &bytes) == -1)
+ if (send_all(fd, (char *) &reply_size_endian, &bytes) == -1)
perror("send_all in send stations info");
char send_buffer[reply_size];
@@ -1104,7 +1106,7 @@ void *send_stationsinfo_routine(void * arg) {
}
int bytes_to_send = reply_size; // don't want final \n
- if (send_all(fd, &send_buffer, &bytes_to_send) == -1)
+ if (send_all(fd, (char *) &send_buffer, &bytes_to_send) == -1)
perror("send_all buffer");
return (NULL);
@@ -1123,7 +1125,7 @@ void add_station(char *file_path) {
// reopen the file
stations[i].readfd = open(file_path, O_RDONLY);
if (stations[i].readfd < 0) { perror("read (from add station)"); return; }
- pthread_create(&stations[i].streamThread, NULL, stream_routine, i);
+ pthread_create(&stations[i].streamThread, NULL, stream_routine, (void *) &i);
send_newstation_reply(i);
return;
@@ -1140,7 +1142,7 @@ void add_station(char *file_path) {
totalSize += sizeof(pthread_t) + sizeof(int) + strlen(file_path);
// malloc the stations array
- char *more_stations = realloc(stations, totalSize);
+ station_t *more_stations = realloc(stations, totalSize);
if (!more_stations) { perror("malloc (stations pointer)"); return; }
stations = more_stations;
// assign the stations, and start the threads
@@ -1152,7 +1154,7 @@ void add_station(char *file_path) {
fdmax = stations[num_stations].readfd;
}
if (stations[num_stations].readfd < 0) { perror("read (from add station)"); return; }
- pthread_create(&stations[num_stations].streamThread, NULL, stream_routine, num_stations);
+ pthread_create(&stations[num_stations].streamThread, NULL, stream_routine, (void *) &num_stations);
send_newstation_reply(num_stations);
printf("add: successfully created station @ index %d\n", num_stations);
@@ -1202,7 +1204,7 @@ void send_newstation_reply(uint16_t station_num) {
if (!users[i].tcpfd || users[i].tcpfd == -1)
continue;
int bytes_to_send = sizeof(struct NewStation);
- if (send_all(users[i].tcpfd, &new_station, &bytes_to_send) == -1)
+ if (send_all(users[i].tcpfd, (char *) &new_station, &bytes_to_send) == -1)
perror("send_all in newstation reply");
}
}