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 --- c.dSYM/Contents/Resources/DWARF/c | Bin 0 -> 13761 bytes 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 c.dSYM/Contents/Resources/DWARF/c (limited to 'c.dSYM/Contents/Resources/DWARF/c') diff --git a/c.dSYM/Contents/Resources/DWARF/c b/c.dSYM/Contents/Resources/DWARF/c new file mode 100644 index 0000000..c325e9f Binary files /dev/null and b/c.dSYM/Contents/Resources/DWARF/c differ -- cgit v1.2.3-70-g09d2 From 186915db036f06a604883b644e40eaf377aedadf Mon Sep 17 00:00:00 2001 From: sotech117 Date: Mon, 18 Sep 2023 18:52:24 -0400 Subject: got it to work, not sure on protocol. --- c | Bin 35150 -> 35150 bytes c.dSYM/Contents/Resources/DWARF/c | Bin 13761 -> 13761 bytes client.c | 2 +- l | Bin 34654 -> 34654 bytes s | Bin 54558 -> 54494 bytes s.dSYM/Contents/Resources/DWARF/s | Bin 21520 -> 21128 bytes snowcast_control | Bin 20992 -> 35165 bytes snowcast_control.dSYM/Contents/Info.plist | 20 +++++++++++++++ .../Contents/Resources/DWARF/snowcast_control | Bin 0 -> 13761 bytes snowcast_server | Bin 25336 -> 35116 bytes snowcast_server.dSYM/Contents/Info.plist | 20 +++++++++++++++ .../Contents/Resources/DWARF/snowcast_server | Bin 0 -> 15573 bytes snowcast_server_concurrent.c | 27 +++++++-------------- 13 files changed, 50 insertions(+), 19 deletions(-) create mode 100644 snowcast_control.dSYM/Contents/Info.plist create mode 100644 snowcast_control.dSYM/Contents/Resources/DWARF/snowcast_control create mode 100644 snowcast_server.dSYM/Contents/Info.plist create mode 100644 snowcast_server.dSYM/Contents/Resources/DWARF/snowcast_server (limited to 'c.dSYM/Contents/Resources/DWARF/c') diff --git a/c b/c index a1f2a86..8e2e138 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 c325e9f..7af6442 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 6e6c95a..5edff63 100644 --- a/client.c +++ b/client.c @@ -156,7 +156,7 @@ void *reply_thread_routine(void* args) { printf("client: replyType: %d, stringSize: %d\n", buf[0], buf[1]); // print the while buffer by char for (int i = 0; i < recvbytes; i++) { - printf("%d", buf[i]); + printf("%c ", buf[i]); } struct Reply reply; diff --git a/l b/l index 265f692..52aca28 100755 Binary files a/l and b/l differ diff --git a/s b/s index 63b39b5..b664e12 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 09cab17..feff9cb 100644 Binary files a/s.dSYM/Contents/Resources/DWARF/s and b/s.dSYM/Contents/Resources/DWARF/s differ diff --git a/snowcast_control b/snowcast_control index 24fe551..1ab5778 100755 Binary files a/snowcast_control and b/snowcast_control differ diff --git a/snowcast_control.dSYM/Contents/Info.plist b/snowcast_control.dSYM/Contents/Info.plist new file mode 100644 index 0000000..afe5b44 --- /dev/null +++ b/snowcast_control.dSYM/Contents/Info.plist @@ -0,0 +1,20 @@ + + + + + CFBundleDevelopmentRegion + English + CFBundleIdentifier + com.apple.xcode.dsym.snowcast_control + CFBundleInfoDictionaryVersion + 6.0 + CFBundlePackageType + dSYM + CFBundleSignature + ???? + CFBundleShortVersionString + 1.0 + CFBundleVersion + 1 + + diff --git a/snowcast_control.dSYM/Contents/Resources/DWARF/snowcast_control b/snowcast_control.dSYM/Contents/Resources/DWARF/snowcast_control new file mode 100644 index 0000000..76fce35 Binary files /dev/null and b/snowcast_control.dSYM/Contents/Resources/DWARF/snowcast_control differ diff --git a/snowcast_server b/snowcast_server index 8a4c1cc..444d321 100755 Binary files a/snowcast_server and b/snowcast_server differ diff --git a/snowcast_server.dSYM/Contents/Info.plist b/snowcast_server.dSYM/Contents/Info.plist new file mode 100644 index 0000000..789e734 --- /dev/null +++ b/snowcast_server.dSYM/Contents/Info.plist @@ -0,0 +1,20 @@ + + + + + CFBundleDevelopmentRegion + English + CFBundleIdentifier + com.apple.xcode.dsym.snowcast_server + CFBundleInfoDictionaryVersion + 6.0 + CFBundlePackageType + dSYM + CFBundleSignature + ???? + CFBundleShortVersionString + 1.0 + CFBundleVersion + 1 + + diff --git a/snowcast_server.dSYM/Contents/Resources/DWARF/snowcast_server b/snowcast_server.dSYM/Contents/Resources/DWARF/snowcast_server new file mode 100644 index 0000000..0123b4a Binary files /dev/null and b/snowcast_server.dSYM/Contents/Resources/DWARF/snowcast_server differ diff --git a/snowcast_server_concurrent.c b/snowcast_server_concurrent.c index 4387407..110792d 100644 --- a/snowcast_server_concurrent.c +++ b/snowcast_server_concurrent.c @@ -379,33 +379,24 @@ void *select_thread(void *arg) { // 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); + char* file_path = station_data[station_num].filePath; + int len_file_path = strlen(file_path); - printf("song_name: %s\n", song_name); + char *send_buffer = malloc(len_file_path+2); + send_buffer[0] = 3; + send_buffer[1] = len_file_path; - announce->replyType = 3; - announce->songnameSize = song_name_size; + memcpy(send_buffer + 2, file_path, len_file_path); - 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); + printf("buffer: %s\n", send_buffer); int bytessent; - if ((bytessent = send(newfd, send_buffer, sizeof(struct Announce) + song_name_size, 0)) == -1) + if ((bytessent = send(newfd, send_buffer, len_file_path + 2, 0)) == -1) perror("send"); // print the number of bytes sent printf("sent %d bytes\n", bytessent); - free(announce->songname); - free(announce); + free(send_buffer); } else { // send back in invalid command -- cgit v1.2.3-70-g09d2 From 779ce6a6885346257b4d18f1efe205984cfff079 Mon Sep 17 00:00:00 2001 From: sotech117 Date: Mon, 18 Sep 2023 19:04:14 -0400 Subject: announce message basics done --- c | Bin 35150 -> 35294 bytes c.dSYM/Contents/Resources/DWARF/c | Bin 13761 -> 13866 bytes client.c | 15 +++++++- l | Bin 34654 -> 34654 bytes s | Bin 54494 -> 54654 bytes s.dSYM/Contents/Resources/DWARF/s | Bin 21128 -> 21196 bytes snowcast_control | Bin 35165 -> 35309 bytes .../Contents/Resources/DWARF/snowcast_control | Bin 13761 -> 13866 bytes snowcast_server | Bin 35116 -> 35116 bytes snowcast_server_concurrent.c | 42 ++++++++++++--------- 10 files changed, 37 insertions(+), 20 deletions(-) (limited to 'c.dSYM/Contents/Resources/DWARF/c') diff --git a/c b/c index 8e2e138..e73bb21 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 7af6442..785b2db 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 5edff63..29aad13 100644 --- a/client.c +++ b/client.c @@ -158,13 +158,24 @@ void *reply_thread_routine(void* args) { for (int i = 0; i < recvbytes; i++) { printf("%c ", buf[i]); } - struct Reply reply; - memcpy(&reply, buf, sizeof(struct Reply)); + memcpy(&reply, buf, 2); + + // print out the fields of reply on one line + printf("\nclient: replyType: %d, stringSize: %d\n", reply.replyType, reply.stringSize); + // print the size of reply if (reply.replyType == 3) { printf("client: received an announce message\n"); + char *song_name = malloc(reply.stringSize); + // printf(sizeof(struct Reply)); + memcpy(song_name, buf + 2, reply.stringSize); + + printf("client: song name: %s\n", song_name); + + free(song_name); + continue; } else if (reply.replyType == 4) { printf("client: received an invalid command message\n"); diff --git a/l b/l index 52aca28..dc80387 100755 Binary files a/l and b/l differ diff --git a/s b/s index b664e12..05b5da5 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 feff9cb..74f9ec9 100644 Binary files a/s.dSYM/Contents/Resources/DWARF/s and b/s.dSYM/Contents/Resources/DWARF/s differ diff --git a/snowcast_control b/snowcast_control index 1ab5778..fa08142 100755 Binary files a/snowcast_control and b/snowcast_control differ diff --git a/snowcast_control.dSYM/Contents/Resources/DWARF/snowcast_control b/snowcast_control.dSYM/Contents/Resources/DWARF/snowcast_control index 76fce35..774ea29 100644 Binary files a/snowcast_control.dSYM/Contents/Resources/DWARF/snowcast_control and b/snowcast_control.dSYM/Contents/Resources/DWARF/snowcast_control differ diff --git a/snowcast_server b/snowcast_server index 444d321..c194134 100755 Binary files a/snowcast_server and b/snowcast_server differ diff --git a/snowcast_server_concurrent.c b/snowcast_server_concurrent.c index 110792d..03d6414 100644 --- a/snowcast_server_concurrent.c +++ b/snowcast_server_concurrent.c @@ -59,6 +59,8 @@ void *update_user_station(int sockfd, int stationNum); void *print_user_data(int sockfd); void destroy_user(int sockfd); +void send_announce_message(int fd, int station_num); + // void *load_file(void* arg); @@ -379,24 +381,7 @@ void *select_thread(void *arg) { // update station of user update_user_station(i, ntohs(command.number)); - char* file_path = station_data[station_num].filePath; - int len_file_path = strlen(file_path); - - char *send_buffer = malloc(len_file_path+2); - send_buffer[0] = 3; - send_buffer[1] = len_file_path; - - memcpy(send_buffer + 2, file_path, len_file_path); - - printf("buffer: %s\n", send_buffer); - - int bytessent; - if ((bytessent = send(newfd, send_buffer, len_file_path + 2, 0)) == -1) - perror("send"); - // print the number of bytes sent - printf("sent %d bytes\n", bytessent); - - free(send_buffer); + send_announce_message(i, station_num); } else { // send back in invalid command @@ -486,4 +471,25 @@ void *get_in_addr(struct sockaddr *sa) } return &(((struct sockaddr_in6*)sa)->sin6_addr); +} + +void send_announce_message(int fd, int station_num) { + char* file_path = station_data[station_num].filePath; + int len_file_path = strlen(file_path); + + char *send_buffer = malloc(len_file_path+2); + send_buffer[0] = 3; + send_buffer[1] = len_file_path; + + memcpy(send_buffer + 2, file_path, len_file_path); + + printf("buffer: %s\n", send_buffer); + + int bytessent; + if ((bytessent = send(fd, send_buffer, len_file_path + 2, 0)) == -1) + perror("send"); + // print the number of bytes sent + printf("sent %d bytes\n", bytessent); + + free(send_buffer); } \ No newline at end of file -- 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 'c.dSYM/Contents/Resources/DWARF/c') 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