aboutsummaryrefslogtreecommitdiff
path: root/snowcast_server_concurrent.c
diff options
context:
space:
mode:
authorsotech117 <michael_foiani@brown.edu>2023-09-18 18:23:35 -0400
committersotech117 <michael_foiani@brown.edu>2023-09-18 18:23:35 -0400
commit8a25dcda67683817ddd55b669111f7dd8e3107ef (patch)
treeece530621214be8afdcaf312787cb04e343c563d /snowcast_server_concurrent.c
parentbc24590991cb27e8bd220fd6d0585e76f804601d (diff)
decent connection, but not there yet
Diffstat (limited to 'snowcast_server_concurrent.c')
-rw-r--r--snowcast_server_concurrent.c42
1 files changed, 35 insertions, 7 deletions
diff --git a/snowcast_server_concurrent.c b/snowcast_server_concurrent.c
index e09e398..4387407 100644
--- a/snowcast_server_concurrent.c
+++ b/snowcast_server_concurrent.c
@@ -62,7 +62,7 @@ void destroy_user(int sockfd);
// void *load_file(void* arg);
-main(int argc, char *argv[])
+int main(int argc, char *argv[])
{
// threads to control reading files at chunks while the other threads sleep
// station_data = malloc(sizeof(station_t) * NUM_STATIONS);
@@ -165,7 +165,7 @@ void *send_udp_packet_routine(void *arg) {
if (error_code = getaddrinfo(NULL, port, &thread_hints, &thread_servinfo) != 0)
{
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(error_code));
- return 1;
+ return (NULL);
}
free(port);
@@ -203,7 +203,7 @@ void *send_udp_packet_routine(void *arg) {
if (!did_work) {
// sendto a random string of data to the user
- int station_num = user_data[user_index].stationNum - 1;
+ int station_num = user_data[user_index].stationNum;
char *data = station_data[station_num].filePath;
printf("load data: thread %d \n", user_index);
int numbytes;
@@ -374,10 +374,38 @@ void *select_thread(void *arg) {
update_user_udpPort(i, ntohs(command.number));
}
else if (command.commandType == 1) {
- // setStation command for the user
- printf("TODO: set station to %d\n", ntohs(command.number));
+ int station_num = ntohs(command.number);
+ printf("setting station to %d\n", ntohs(command.number));
// update station of user
update_user_station(i, ntohs(command.number));
+
+ const char* song_name = station_data[station_num].filePath;
+ size_t song_name_size = strlen(song_name); // don't add 1, don't want to include the null terminator
+ struct Announce *announce = malloc(sizeof *announce + song_name_size);
+
+ printf("song_name: %s\n", song_name);
+
+ announce->replyType = 3;
+ announce->songnameSize = song_name_size;
+
+ announce->songname = malloc(song_name_size);
+ memcpy(announce->songname, song_name, song_name_size);
+
+ // print out all fields on announce on one line
+ printf("announce: %d %d %s\n", announce->replyType, announce->songnameSize, announce->songname);
+
+ char* send_buffer[sizeof(struct Announce) + song_name_size];
+ memcpy(send_buffer, announce, sizeof(struct Announce));
+ memcpy(send_buffer+sizeof(struct Announce), song_name, song_name_size);
+
+ int bytessent;
+ if ((bytessent = send(newfd, send_buffer, sizeof(struct Announce) + song_name_size, 0)) == -1)
+ perror("send");
+ // print the number of bytes sent
+ printf("sent %d bytes\n", bytessent);
+
+ free(announce->songname);
+ free(announce);
}
else {
// send back in invalid command
@@ -410,9 +438,9 @@ void *init_user(int sockfd) {
// this is to save memory space.
// in general, the displacement of 4 is where a user "used to be"
int user_index = max_active_users++;
- if(user_data[sockfd-4].sockfd == -1) {
+ if(user_data[(sockfd-4)/2].sockfd == -1) {
printf("reusing memory\n");
- user_index = sockfd - 4;
+ user_index = (sockfd - 4)/2;
} else {
printf("making new memory\n");
// have to make more memory