From 8a25dcda67683817ddd55b669111f7dd8e3107ef Mon Sep 17 00:00:00 2001 From: sotech117 Date: Mon, 18 Sep 2023 18:23:35 -0400 Subject: decent connection, but not there yet --- l.dSYM/Contents/Info.plist | 20 ++++++++++++++++++++ l.dSYM/Contents/Resources/DWARF/l | Bin 0 -> 11745 bytes 2 files changed, 20 insertions(+) create mode 100644 l.dSYM/Contents/Info.plist create mode 100644 l.dSYM/Contents/Resources/DWARF/l (limited to 'l.dSYM/Contents') diff --git a/l.dSYM/Contents/Info.plist b/l.dSYM/Contents/Info.plist new file mode 100644 index 0000000..7025c85 --- /dev/null +++ b/l.dSYM/Contents/Info.plist @@ -0,0 +1,20 @@ + + + + + CFBundleDevelopmentRegion + English + CFBundleIdentifier + com.apple.xcode.dsym.l + CFBundleInfoDictionaryVersion + 6.0 + CFBundlePackageType + dSYM + CFBundleSignature + ???? + CFBundleShortVersionString + 1.0 + CFBundleVersion + 1 + + diff --git a/l.dSYM/Contents/Resources/DWARF/l b/l.dSYM/Contents/Resources/DWARF/l new file mode 100644 index 0000000..9843052 Binary files /dev/null and b/l.dSYM/Contents/Resources/DWARF/l differ -- cgit v1.2.3-70-g09d2 From 7db333857219362ba14dec132825debc0d940a6c Mon Sep 17 00:00:00 2001 From: sotech117 Date: Mon, 18 Sep 2023 23:58:47 -0400 Subject: cleanup some bugs. you can now listen to the music! good stopping point --- c | Bin 35294 -> 35294 bytes c.dSYM/Contents/Resources/DWARF/c | Bin 13866 -> 13886 bytes client.c | 2 + l | Bin 34654 -> 34638 bytes l.dSYM/Contents/Resources/DWARF/l | Bin 11745 -> 11651 bytes listener.c | 23 +++++--- s | Bin 54654 -> 55198 bytes s.dSYM/Contents/Resources/DWARF/s | Bin 21196 -> 23594 bytes snowcast_server_concurrent.c | 121 +++++++++++++++++++++++++++++++++----- 9 files changed, 124 insertions(+), 22 deletions(-) (limited to 'l.dSYM/Contents') diff --git a/c b/c index e73bb21..35031be 100755 Binary files a/c and b/c differ diff --git a/c.dSYM/Contents/Resources/DWARF/c b/c.dSYM/Contents/Resources/DWARF/c index 785b2db..4a8f904 100644 Binary files a/c.dSYM/Contents/Resources/DWARF/c and b/c.dSYM/Contents/Resources/DWARF/c differ diff --git a/client.c b/client.c index 29aad13..ecb0be9 100644 --- a/client.c +++ b/client.c @@ -106,6 +106,8 @@ int main(int argc, char *argv[]) perror("send"); exit(1); } + // print the amount of bytes sent + printf("client: sent %d bytes on a hello call \n", numbytessent); pthread_t reply_thread; pthread_create(&reply_thread, NULL, reply_thread_routine, (void*)sockfd); diff --git a/l b/l index dc80387..281f522 100755 Binary files a/l and b/l differ diff --git a/l.dSYM/Contents/Resources/DWARF/l b/l.dSYM/Contents/Resources/DWARF/l index 9843052..c55216a 100644 Binary files a/l.dSYM/Contents/Resources/DWARF/l and b/l.dSYM/Contents/Resources/DWARF/l differ diff --git a/listener.c b/listener.c index 2d46307..d5b4799 100644 --- a/listener.c +++ b/listener.c @@ -83,7 +83,7 @@ int main(int argc, char *argv[]) int count = 0; while(1) { - printf("\nlistener: waiting to recvfrom... %d times\n", count++); + // printf("\nlistener: waiting to recvfrom... %d times\n", count++); addr_len = sizeof their_addr; if ((numbytes = recvfrom(sockfd, buf, MAXBUFLEN , 0, @@ -91,14 +91,21 @@ int main(int argc, char *argv[]) perror("recvfrom"); exit(1); } + // buf[numbytes] = '\0'; - printf("listener: got packet from %s\n", - inet_ntop(their_addr.ss_family, - get_in_addr((struct sockaddr *)&their_addr), - s, sizeof s)); - printf("listener: packet is %d bytes long\n", numbytes); - buf[numbytes] = '\0'; - printf("listener: packet contains \"%s\"\n", buf); + //printf("listener: got packet from %s\n", + // inet_ntop(their_addr.ss_family, + // get_in_addr((struct sockaddr *)&their_addr), + // s, sizeof s)); + //printf("listener: packet is %d bytes long\n", numbytes); + //buf[numbytes] = '\0'; + //printf("listener: packet contains \"%s\"\n", buf); + + // print the size + + write(STDOUT_FILENO, buf, numbytes); + + memset(buf, 0, MAXBUFLEN); } close(sockfd); diff --git a/s b/s index 05b5da5..10bc785 100755 Binary files a/s and b/s differ diff --git a/s.dSYM/Contents/Resources/DWARF/s b/s.dSYM/Contents/Resources/DWARF/s index 74f9ec9..4db3832 100644 Binary files a/s.dSYM/Contents/Resources/DWARF/s and b/s.dSYM/Contents/Resources/DWARF/s differ diff --git a/snowcast_server_concurrent.c b/snowcast_server_concurrent.c index 03d6414..3b71156 100644 --- a/snowcast_server_concurrent.c +++ b/snowcast_server_concurrent.c @@ -10,13 +10,12 @@ #include "protocol.h" -#define NUM_STATIONS 2 #define LINE_MAX 1024 #define MAX_USERS 1000 #define MAX_PATH 50 typedef struct station { - int currentChunk; + int seekIndex; char* filePath; } station_t; @@ -34,7 +33,10 @@ int count = 0; pthread_cond_t cond = PTHREAD_COND_INITIALIZER; pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; +pthread_mutex_t station_mutex = PTHREAD_MUTEX_INITIALIZER; + const char *port; +int num_stations; int start_threads = 0; int max_active_users = 0; @@ -75,6 +77,10 @@ int main(int argc, char *argv[]) } port = argv[1]; + num_stations = argc - 2; + + printf("port: %s\n", port); + printf("num_stations: %d\n", num_stations); // init stations size_t totalSize = 0; @@ -92,7 +98,7 @@ int main(int argc, char *argv[]) } // print all indexes in station data - for (int i = 0; i < NUM_STATIONS; i++) + for (int i = 0; i < num_stations; i++) { printf("station %d: %s\n", i, station_data[i].filePath); } @@ -132,6 +138,27 @@ int main(int argc, char *argv[]) return 0; } +int sendall(int udp_sockfd, char *buf, int *len, struct addrinfo *thread_res) +{ + int MAX_PACKET_SIZE = 512; + int total = 0; // how many bytes we've sent + int bytesleft = *len; // how many we have left to send + int n; + + while(total < *len) { + n = sendto(udp_sockfd, buf+total, MAX_PACKET_SIZE, 0, thread_res->ai_addr, thread_res->ai_addrlen); + // thread_res->ai_addr, thread_res->ai_addrlen)) == -1; + if (n == -1) { break; } + total += n; + bytesleft -= n; + } + + *len = total; // return number actually sent here + + return n==-1?-1:0; // return -1 on failure, 0 on success +} + + /* Make the manager routine */ void *send_udp_packet_routine(void *arg) { // unpack args @@ -198,6 +225,7 @@ void *send_udp_packet_routine(void *arg) { { pthread_cond_wait(&cond, &m); } + int station_num = user_data[user_index].stationNum; if (station_num == -1) { did_work = 1; @@ -207,21 +235,52 @@ void *send_udp_packet_routine(void *arg) { // sendto a random string of data to the user 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; - if ((numbytes = sendto(udp_sockfd, data, strlen(data), 0, - thread_res->ai_addr, thread_res->ai_addrlen)) == -1) { - perror("talker: sendto"); + // printf("load data: thread %d \n", user_index); + + // get file path + char* file_path = station_data[station_num].filePath; + // get current seek chunk + int current_chunk = station_data[station_num].seekIndex; + FILE* file_stream = fopen(file_path, "r"); + if (fseek(file_stream, current_chunk, SEEK_SET) == -1) { + perror("fseek"); return (NULL); } - printf("send data: thread %d \n", user_index); + size_t BYTES_PER_SECOND = 16*1024; + // read 1000 bytes of the file + char file_buffer[BYTES_PER_SECOND]; + if (fread(file_buffer, BYTES_PER_SECOND, 1, file_stream) == -1) { + perror("fread"); + return (NULL); + } + // printf("send data: thread %d \n", user_index); + // int numbytes; + // if ((numbytes = sendto(udp_sockfd, data, strlen(data), 0, + // thread_res->ai_addr, thread_res->ai_addrlen)) == -1) { + // perror("talker: sendto"); + // return (NULL); + // } + // print the size of the file_buffer + // printf("size of file_buffer: %lu\n", sizeof(file_buffer)); + + int bytes_sent = sizeof(file_buffer); + if (sendall(udp_sockfd, file_buffer, &bytes_sent, thread_res) == -1) + { + perror("sendall"); + printf("We only sent %d bytes because of the error!\n", bytes_sent); + } + // printf("We sent all %d bytes!\n", bytes_sent); did_work = 1; + + close(file_stream); + + usleep(400000); } pthread_mutex_unlock(&m); - usleep(500000); + usleep(100000); } return NULL; } @@ -231,11 +290,39 @@ void *synchronization_thread(void *arg) { while (1) { start_threads = 1; - printf("\nbroadcast %d\n", c++); + // printf("\nbroadcast %d\n", c++); pthread_cond_broadcast(&cond); usleep(2000); start_threads = 0; - usleep(1000000-2000); + // printf("before loop"); + // update file seek index for each station + size_t BYTES_PER_SECOND = 16*1024; + // print num_stations + // printf("num_stations: %d\n", num_stations); + for (int i = 0; i < num_stations; i++) + { + // printf("checking station %d\n", i); + // get size of file + FILE* fp = fopen(station_data[i].filePath, "r"); + fseek(fp, 0L, SEEK_END); + size_t size = ftell(fp); + if (size == -1) { + perror("ftell"); + return (NULL); + } + station_data[i].seekIndex += BYTES_PER_SECOND; + // 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; + } + fclose(fp); + } + + + usleep(2000); + usleep(1000000-4000); } } @@ -348,9 +435,15 @@ void *select_thread(void *arg) { // send the welcome message to client struct Welcome welcome; welcome.replyType = 2; - welcome.numStations = htons(NUM_STATIONS); - if ((send(newfd, &welcome, sizeof(struct Welcome), 0)) == -1) + 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 -- cgit v1.2.3-70-g09d2 From c36846a96fd45e57f37ce73e49af8275a305d4b4 Mon Sep 17 00:00:00 2001 From: sotech117 Date: Tue, 19 Sep 2023 00:44:40 -0400 Subject: fix file descriptor leak --- c | Bin 35294 -> 35294 bytes l | Bin 34638 -> 34638 bytes l.dSYM/Contents/Resources/DWARF/l | Bin 11651 -> 11651 bytes listener.c | 1 - mp3/chipOffTheBlock.mp3 | Bin 0 -> 4477733 bytes s | Bin 55198 -> 55198 bytes s.dSYM/Contents/Resources/DWARF/s | Bin 23594 -> 23649 bytes snowcast_server_concurrent.c | 25 +++++++++++++++++-------- 8 files changed, 17 insertions(+), 9 deletions(-) create mode 100644 mp3/chipOffTheBlock.mp3 (limited to 'l.dSYM/Contents') diff --git a/c b/c index 35031be..20b2474 100755 Binary files a/c and b/c differ diff --git a/l b/l index 281f522..d6eec69 100755 Binary files a/l and b/l differ diff --git a/l.dSYM/Contents/Resources/DWARF/l b/l.dSYM/Contents/Resources/DWARF/l index c55216a..283ecc9 100644 Binary files a/l.dSYM/Contents/Resources/DWARF/l and b/l.dSYM/Contents/Resources/DWARF/l differ diff --git a/listener.c b/listener.c index d5b4799..7bb8afe 100644 --- a/listener.c +++ b/listener.c @@ -102,7 +102,6 @@ int main(int argc, char *argv[]) //printf("listener: packet contains \"%s\"\n", buf); // print the size - write(STDOUT_FILENO, buf, numbytes); memset(buf, 0, MAXBUFLEN); diff --git a/mp3/chipOffTheBlock.mp3 b/mp3/chipOffTheBlock.mp3 new file mode 100644 index 0000000..c06c05f Binary files /dev/null and b/mp3/chipOffTheBlock.mp3 differ diff --git a/s b/s index 10bc785..9f86c9b 100755 Binary files a/s and b/s differ diff --git a/s.dSYM/Contents/Resources/DWARF/s b/s.dSYM/Contents/Resources/DWARF/s index 4db3832..489e060 100644 Binary files a/s.dSYM/Contents/Resources/DWARF/s and b/s.dSYM/Contents/Resources/DWARF/s differ diff --git a/snowcast_server_concurrent.c b/snowcast_server_concurrent.c index 3b71156..47b112a 100644 --- a/snowcast_server_concurrent.c +++ b/snowcast_server_concurrent.c @@ -240,8 +240,8 @@ void *send_udp_packet_routine(void *arg) { // get file path char* file_path = station_data[station_num].filePath; // get current seek chunk - int current_chunk = station_data[station_num].seekIndex; FILE* file_stream = fopen(file_path, "r"); + int current_chunk = station_data[station_num].seekIndex; if (fseek(file_stream, current_chunk, SEEK_SET) == -1) { perror("fseek"); return (NULL); @@ -253,6 +253,7 @@ void *send_udp_packet_routine(void *arg) { perror("fread"); return (NULL); } + fclose(file_stream); // printf("send data: thread %d \n", user_index); // int numbytes; // if ((numbytes = sendto(udp_sockfd, data, strlen(data), 0, @@ -273,7 +274,6 @@ void *send_udp_packet_routine(void *arg) { did_work = 1; - close(file_stream); usleep(400000); } @@ -507,16 +507,24 @@ 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)/2].sockfd == -1) { - printf("reusing memory\n"); - user_index = (sockfd - 4)/2; - } else { - printf("making new memory\n"); - // have to make more memory + int running_index = 0; + while(running_index++ < max_active_users) + { + if (user_data[running_index].sockfd == -1) + { + user_index = running_index; + break; + } + // printf("reusing memory\n"); + } + // have to make more memory + if (user_index == max_active_users) { + ///printf("making new memory\n"); user_t *more_users = realloc(user_data, sizeof(user_t) * max_active_users); if (!more_users) { perror("realloc"); exit(1); } user_data = more_users; } + // map TCP sockfd to this user index user_data[user_index] = (user_t){-1, -1, sockfd, -1}; sockfd_to_user[sockfd] = user_index; @@ -585,4 +593,5 @@ void send_announce_message(int fd, int station_num) { printf("sent %d bytes\n", bytessent); free(send_buffer); + } \ No newline at end of file -- cgit v1.2.3-70-g09d2