aboutsummaryrefslogtreecommitdiff
path: root/snowcast_server_concurrent.c
diff options
context:
space:
mode:
authorsotech117 <michael_foiani@brown.edu>2023-09-20 04:04:22 +0000
committersotech117 <michael_foiani@brown.edu>2023-09-20 04:04:22 +0000
commit1263cbdbb6cf3ebbb157286b2bb2e488e4b931c8 (patch)
tree0538cd930e5afd840f0edef42f448d1933b03b59 /snowcast_server_concurrent.c
parent5eb24f6063515e86e3e87e6fb91e6672dc14de34 (diff)
fix handshake protocol
Diffstat (limited to 'snowcast_server_concurrent.c')
-rw-r--r--snowcast_server_concurrent.c57
1 files changed, 34 insertions, 23 deletions
diff --git a/snowcast_server_concurrent.c b/snowcast_server_concurrent.c
index d0e6aa5..d855bab 100644
--- a/snowcast_server_concurrent.c
+++ b/snowcast_server_concurrent.c
@@ -114,7 +114,7 @@ int main(int argc, char *argv[])
// start syncchronization thread to broadcast stations
pthread_t sync_thread;
-pthread_create(&sync_thread, NULL, synchronization_thread, NULL);
+ pthread_create(&sync_thread, NULL, synchronization_thread, NULL);
// command line interface
char input[LINE_MAX];
@@ -307,6 +307,7 @@ void *synchronization_thread(void *arg) {
FILE* fp = fopen(station_data[i].filePath, "r");
fseek(fp, 0L, SEEK_END);
size_t size = ftell(fp);
+ fclose(fp);
if (size == -1) {
perror("ftell");
return (NULL);
@@ -315,10 +316,25 @@ void *synchronization_thread(void *arg) {
// if the seek index is greater than the size of the file, reset it
if (station_data[i].seekIndex >= size)
{
- // printf("resetting seek index for station %d\n", i);
station_data[i].seekIndex = 0;
+
+
+ // send the announce messages
+ for (int i = 0; i < max_active_users; i++)
+ {
+ // if (user_data[i].streamThread == NULL) {
+ // break;
+ // }
+ if (user_data[i].sockfd == -1)
+ continue;
+ // print_user_data(i);
+ // update the station of each user
+ if (user_data[i].stationNum == i)
+ {
+ send_announce_message(user_data[i].sockfd, i);
+ }
+ }
}
- fclose(fp);
}
@@ -424,27 +440,14 @@ void *select_thread(void *arg) {
if (newfd > fdmax) { // keep track of the max
fdmax = newfd;
}
- printf("selectserver: new connection from %s on "
- "socket %d\n.",
- inet_ntop(remoteaddr.ss_family,
- get_in_addr((struct sockaddr*)&remoteaddr),
- remoteIP, INET6_ADDRSTRLEN),
- newfd);
+ // printf("selectserver: new connection from %s on "
+ // "socket %d\n.",
+ // inet_ntop(remoteaddr.ss_family,
+ // get_in_addr((struct sockaddr*)&remoteaddr),
+ // remoteIP, INET6_ADDRSTRLEN),
+ // newfd);
// init user with this newfd
init_user(newfd);
-
- // send the welcome message to client
- struct Welcome welcome;
- welcome.replyType = 2;
- welcome.numStations = htons(num_stations);
- int numbytes;
- if ((numbytes=send(newfd, &welcome, sizeof(struct Welcome), 0)) == -1)
- perror("send");
-
- //print the num bytes
- // print the size of the struct welcome
- printf("size of welcome struct: %lu\n", sizeof(struct Welcome));
- printf("sent %d bytes\n", numbytes);
}
} else {
// handle data from a client
@@ -468,6 +471,14 @@ void *select_thread(void *arg) {
printf("udpPort (from Hello) for new connection is %d.\n", ntohs(command.number));
// update udp port of user
update_user_udpPort(i, ntohs(command.number));
+
+ // send the welcome message to client
+ struct Welcome welcome;
+ welcome.replyType = 2;
+ welcome.numStations = htons(num_stations);
+ int numbytes;
+ if ((numbytes=send(i, &welcome, sizeof(struct Welcome), 0)) == -1)
+ perror("send");
}
else if (command.commandType == 1) {
int station_num = ntohs(command.number);
@@ -490,6 +501,7 @@ void *select_thread(void *arg) {
// drop connection upon invalid command
close(i);
FD_CLR(i, &master);
+ destroy_user(i);
}
}
} // END handle data from client
@@ -594,5 +606,4 @@ void send_announce_message(int fd, int station_num) {
printf("sent %d bytes\n", bytessent);
free(send_buffer);
-
} \ No newline at end of file