From 92c813198a80de190d9a719cfc23383421c40098 Mon Sep 17 00:00:00 2001 From: Sergiusz Warga Date: Mon, 10 May 2021 20:00:31 +0200 Subject: [PATCH] Introduced Topic-11 --- Task-8/k_f0.c | 23 +++++++++++-- Topic-11/client.c | 46 ++++++++++++++++++++++++++ Topic-11/server.c | 84 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 150 insertions(+), 3 deletions(-) create mode 100644 Topic-11/client.c create mode 100644 Topic-11/server.c diff --git a/Task-8/k_f0.c b/Task-8/k_f0.c index b469ce3..150c8a9 100644 --- a/Task-8/k_f0.c +++ b/Task-8/k_f0.c @@ -14,6 +14,23 @@ #include "CircBuffer.h" #include "funs.h" +void handle_arguments(int argc, char *argv[]) { + if (argc != 3) { + fprintf(stderr, "Usage: %s SAMPLING_FREQUENCY REFRESHING_FREQUENCY\n", argv[0]); + exit(EXIT_FAILURE); + } + + if (strtol(argv[1], NULL, 10) < 2) { + fprintf(stderr, "Sampling frequency should be at least 400Hz!\n"); + exit(EXIT_FAILURE); + } + + if (strtol(argv[2], NULL, 10) > 1 || strtod(argv[2], NULL) < 0) { + fprintf(stderr, "Refreshing frequency should be at least 1Hz!\n"); + exit(EXIT_FAILURE); + } +} + int crosses_zero(double prev, double curr) { // let's just ignore 0 for now return (prev > 0 && curr < 0) || (prev < 0 && curr > 0); @@ -31,8 +48,6 @@ _Noreturn void k_f0(int fd, int sampling_frequency, int refreshing_frequency) { } struct timespec tic, toc; - // delay.tv_sec = 0; - // delay.tv_nsec = (1./refreshing_frequency * 1000000000L); // 1/f in nanoseconds unsigned long long delay = NSEC_PER_SEC*1./refreshing_frequency; clock_gettime(CLOCK_REALTIME, &tic); @@ -68,6 +83,8 @@ _Noreturn void k_f0(int fd, int sampling_frequency, int refreshing_frequency) { int main(int argc, char *argv[]) { + handle_arguments(argc, argv); + //----- Open a shared memory ----- const char *memory_name = "/dsp"; int fd = shm_open(memory_name, O_RDONLY, 0777); @@ -77,7 +94,7 @@ int main(int argc, char *argv[]) { } printf("Shared memory file descriptor: %d\n", fd); - k_f0(fd, 2000, 1); + k_f0(fd, strtol(argv[1], NULL, 10), strtol(argv[2], NULL, 10)); return 0; } \ No newline at end of file diff --git a/Topic-11/client.c b/Topic-11/client.c new file mode 100644 index 0000000..c24db6f --- /dev/null +++ b/Topic-11/client.c @@ -0,0 +1,46 @@ +#include +#include +#include +#include +#include +#include +#include + +void handle_arguments(int argc, char *argv[]) { + if (argc != 2) { + fprintf(stderr, "Usage: %s QUERRY\n", argv[0]); + exit(EXIT_FAILURE); + } + + if (strlen(argv[1]) > 100) { + fprintf(stderr, "QUERRY must not exceed 100 characters!\n"); + exit(EXIT_FAILURE); + } +} + +void run_client(char *msg) { + int sockfd = socket(AF_INET, SOCK_DGRAM, 0); + if (sockfd < 0) { + fprintf(stderr, "socket: "); + exit(EXIT_FAILURE); + } + + struct sockaddr_in serv_addr; + memset(&serv_addr, 0, sizeof(serv_addr)); //Clears the memory + serv_addr.sin_family = AF_INET; + serv_addr.sin_port = htons(2137); + serv_addr.sin_addr.s_addr = INADDR_ANY; + int rv = sendto(sockfd, msg, strlen(msg), 0, (const struct sockaddr *)&serv_addr, sizeof(serv_addr)); + if (rv < 0) { + fprintf(stderr, "sendto: "); + exit(EXIT_FAILURE); + } + char response[100]; + //rv = recvfrom(sockfd, response, sizeof(response), 0, NULL, NULL); +} + +int main(int argc, char *argv[]) { + handle_arguments(argc, argv); + run_client(argv[1]); + return 0; +} diff --git a/Topic-11/server.c b/Topic-11/server.c new file mode 100644 index 0000000..cdac6ce --- /dev/null +++ b/Topic-11/server.c @@ -0,0 +1,84 @@ +#include +#include +#include +#include +#include + +struct TokenizedQuerry { + int left; + char operand; + int right; +}; + +void handle_arguments(int argc, char *argv[]) { + if (argc != 2) { + fprintf(stderr, "Usage: %s PORT\n", argv[0]); + exit(EXIT_FAILURE); + } +} + +struct TokenizedQuerry tokenize(const char *querry) { + struct TokenizedQuerry tokenized_querry; + printf("QUERRY: %s\n", querry); + char left[50], *operand_ptr, right[50]; + char valid_operands[4] = {'+', '-', '/', '*'}; + for (int i = 0; i < sizeof(valid_operands); ++i) { + operand_ptr = strchr(querry, valid_operands[i]); + if (operand_ptr != NULL) break; + } + if (operand_ptr == NULL) { + fprintf(stderr, "Wrong operand!\n"); + exit(EXIT_FAILURE); + } + int left_length = operand_ptr - querry; + + strncpy(left, querry, left_length); + strcpy(right, querry + left_length + 1); + printf("Tokenized: %s %c %s\n", left, *operand_ptr, right); + tokenized_querry.left = strtol(left, NULL, 10); + tokenized_querry.operand = *operand_ptr; + + return tokenized_querry; +} + +void run_server(int port) { + int sockfd = socket(AF_INET, SOCK_DGRAM, 0); + if (sockfd < 0) { + fprintf(stderr, "socket: "); + exit(EXIT_FAILURE); + } + + struct sockaddr_in serv_addr, client_addr; + memset(&serv_addr, 0, sizeof(serv_addr)); + memset(&client_addr, 0, sizeof(client_addr)); + serv_addr.sin_family = AF_INET; + serv_addr.sin_port = htons(port); + serv_addr.sin_addr.s_addr = INADDR_ANY; + + int rv = bind(sockfd, (const struct sockaddr *)&serv_addr, sizeof(serv_addr)); + if (rv < 0) { + fprintf(stderr, "bind: "); + exit(EXIT_FAILURE); + } + + char buffer[100]; + memset(&buffer, 0, sizeof(buffer)); + socklen_t client_addr_len; + struct TokenizedQuerry tokenized_querry; + + while (1) { + rv = recvfrom(sockfd, buffer, sizeof(buffer), 0, (struct sockaddr *)&client_addr, &client_addr_len); + tokenized_querry = tokenize(buffer); + + //rv = sendto(sockfd, response, sizeof(response), 0, (const struct sockaddr *)&client_addr, sizeof(client_addr)); + memset(&buffer, 0, sizeof(buffer)); + } + + +} + +int main(int argc, char *argv[]) { + handle_arguments(argc, argv); + run_server(strtol(argv[1], NULL, 10)); + return 0; +}