diff options
author | sotech117 <michael_foiani@brown.edu> | 2023-09-23 17:30:45 -0400 |
---|---|---|
committer | sotech117 <michael_foiani@brown.edu> | 2023-09-23 17:30:45 -0400 |
commit | 3b2aa8c271bf5cd5497decb6577afe5fd7339f57 (patch) | |
tree | bc1d39ad76b15f58ddf61385645fa87a59fb1157 /client.c | |
parent | b417bcc57b9fd49f360087c32c97293a6bc7d2be (diff) | |
parent | 1e9ac5407ef4f2cddc745f35f33a860446526cea (diff) |
merge post-warmup with main
Diffstat (limited to 'client.c')
-rw-r--r-- | client.c | 125 |
1 files changed, 113 insertions, 12 deletions
@@ -11,13 +11,20 @@ #include <sys/types.h> #include <netinet/in.h> #include <sys/socket.h> +#include <ctype.h> +#include <pthread.h> #include <arpa/inet.h> -#include "protocol.h" +#include "protocol.c" #define MAXDATASIZE 100 // max number of bytes we can get at once +#define MAX_READ_SIZE 1024 +#define LINE_MAX 1024 + +void *reply_thread_routine(void *args); + // get sockaddr, IPv4 or IPv6: void *get_in_addr(struct sockaddr *sa) { @@ -77,30 +84,124 @@ int main(int argc, char *argv[]) inet_ntop(p->ai_family, get_in_addr((struct sockaddr *)p->ai_addr), s, sizeof s); - // printf("client: connecting to %s\n", s); freeaddrinfo(servinfo); // all done with this structure + + pthread_t reply_thread; + pthread_create(&reply_thread, NULL, reply_thread_routine, (void*)sockfd); + + usleep(1000); + + // sleep(1); + struct Hello hello; hello.commandType = 0; // convert updPort to an int int udpPortInt = atoi(udpPort); hello.udpPort = htons(udpPortInt); - if ((numbytessent = send(sockfd, &hello, sizeof(struct Hello), 0)) == -1) { perror("send"); exit(1); } - struct Welcome msg; - // recv the message, check for errors too - if ((recvbytes = recv(sockfd, (char*)&msg, sizeof(struct snowcast_message), 0)) == -1) { - perror("recv"); - exit(1); - } - msg.numStations = ntohs(msg.numStations); - printf("Welcome to Snowcast! The server has %d stations.\n", msg.numStations); + // CONSIDER: could recieve the welcome message here + + char input[LINE_MAX]; + printf("Enter a number to change to it's station. Click q to end stream.\n"); + while (1) { + char *line = fgets(input, LINE_MAX, stdin); - close(sockfd); + if (line == NULL) { + continue; + } else if (strncmp("q\n", input, LINE_MAX) == 0) { + // end code if type in q + printf("Exiting.\n"); + break; + } else { + // convert input to an int + int inputInt = atoi(input); + // printf("Changing to station %d.\n", inputInt); + + // send the command to change the station + struct SetStation setStation; + setStation.commandType = 1; + setStation.stationNumber = htons(inputInt); + int bytes_to_send = sizeof(struct SetStation); + if (send_all(sockfd, &setStation, &bytes_to_send) == -1) { + perror("send_all"); + exit(1); + } + } + } return 0; +} + +void *reply_thread_routine(void* args) { + int sockfd = (int)args; + // int recvbytes; + while (1) { + // recv the first byte of the message to get it's type + uint8_t reply_type = -1; + // print size of utin8 + if (recv(sockfd, &reply_type, 1, 0) == -1) { + perror("recv"); + exit(1); + } + + if (reply_type == 2) { // we have a welcome message + // recv the message, check for errors too + int16_t num_stations = -1; + int bytes_to_read = sizeof(uint16_t); + if (recv_all(sockfd, &num_stations, &bytes_to_read) == -1) { + perror("recv_all"); + exit(1); + } + num_stations = ntohs(num_stations); + printf("Welcome to Snowcast! The server has %d stations.\n", num_stations); + continue; + } + + if (reply_type == 3) { // we have an announce message + // get the string size + u_int8_t string_size = -1; + if (recv(sockfd, &string_size, 1, 0) == -1) { + perror("recv"); + exit(1); + } + char *song_name = malloc(string_size); + if(song_name == NULL) { perror("malloc in song name"); } + + int bytes_to_read = string_size; + if (recv_all(sockfd, song_name, &bytes_to_read) == -1) { + perror("recv_all"); + exit(1); + } + printf("New song announced: %s\n", song_name); + free(song_name); + continue; + } else if (reply_type == 4) { // we have an invalid command message + // get the string size + u_int8_t string_size = -1; + if (recv(sockfd, &string_size, 1, 0) == -1) { + perror("recv"); + exit(1); + } + char *message = malloc(string_size); + if(message == NULL) { perror("malloc in message"); } + int bytes_to_read = string_size; + if (recv_all(sockfd, message, &bytes_to_read) == -1) { + perror("recv_all"); + exit(1); + } + printf("Invalid protocol: %s. Exiting.\n", message); + free(message); + close(sockfd); + exit(1); + } + + printf("Lost connection to server. Exiting.\n"); + close(sockfd); + exit(1); + } }
\ No newline at end of file |