Introduced Topic-11

This commit is contained in:
Sergiusz Warga 2021-05-10 20:00:31 +02:00
parent fdc21217ee
commit 92c813198a
3 changed files with 150 additions and 3 deletions

View File

@ -14,6 +14,23 @@
#include "CircBuffer.h" #include "CircBuffer.h"
#include "funs.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) { int crosses_zero(double prev, double curr) {
// let's just ignore 0 for now // let's just ignore 0 for now
return (prev > 0 && curr < 0) || (prev < 0 && curr > 0); 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; 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; unsigned long long delay = NSEC_PER_SEC*1./refreshing_frequency;
clock_gettime(CLOCK_REALTIME, &tic); 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[]) { int main(int argc, char *argv[]) {
handle_arguments(argc, argv);
//----- Open a shared memory ----- //----- Open a shared memory -----
const char *memory_name = "/dsp"; const char *memory_name = "/dsp";
int fd = shm_open(memory_name, O_RDONLY, 0777); 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); 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; return 0;
} }

46
Topic-11/client.c Normal file
View File

@ -0,0 +1,46 @@
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
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;
}

84
Topic-11/server.c Normal file
View File

@ -0,0 +1,84 @@
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
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;
}