From c5fcf7179a83ef65c86c6a4a390029149e518649 Mon Sep 17 00:00:00 2001 From: Franciszek Malinka Date: Tue, 5 Oct 2021 21:49:54 +0200 Subject: Duzy commit ze smieciami --- semestr-4/sieci/pracownia2/router/router.c | 60 ++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 semestr-4/sieci/pracownia2/router/router.c (limited to 'semestr-4/sieci/pracownia2/router/router.c') diff --git a/semestr-4/sieci/pracownia2/router/router.c b/semestr-4/sieci/pracownia2/router/router.c new file mode 100644 index 0000000..b5e732c --- /dev/null +++ b/semestr-4/sieci/pracownia2/router/router.c @@ -0,0 +1,60 @@ +/* + * Program: router + * Autor: Franciszek Malinka, 316093 + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "network_addr.h" +#include "utils.h" +#include "dist_vector.h" + +uint16_t read_configuration(struct network_addr** networks, uint16_t **dists) { + uint16_t n; + scanf("%hd", &n); + *networks = malloc(n * sizeof(struct network_addr)); + *dists = malloc(n * sizeof(uint16_t)); + for (int i = 0; i < n; i++) { + char addr[20]; + char _dist[10]; + uint16_t dist; + scanf(" %s %s %hd", addr, _dist, &dist); + (*networks)[i] = stona(addr); + (*dists)[i] = dist; + } + return n; +} + +void router_loop(int sockfd, int networks_number, struct network_addr *networks, uint16_t *dists) { + list_t dv = create_list(); + init_dv(&dv, networks_number, networks); + + printf("Starting the router loop...\n"); + for (;;) { + print_dv(&dv); + propagate_distance_vector(sockfd, networks_number, networks, dists, &dv); + listen_for_routers(sockfd, TURN_LEN_MS, networks_number, networks, dists, &dv); + } +} + +int main() { + struct network_addr* networks; + uint16_t *dists; + int n = read_configuration(&networks, &dists); + int sockfd = get_socket(); + bind_to_port(sockfd, SERVER_PORT); + + router_loop(sockfd, n, networks, dists); + + close(sockfd); + free(networks); + free(dists); +} \ No newline at end of file -- cgit v1.2.3