From f89a58860e4e3c76ef3c2e85d47ff38eeaaabb55 Mon Sep 17 00:00:00 2001 From: Loic Guegan Date: Sat, 15 Jul 2023 08:38:33 +0200 Subject: Minor changes --- .gitignore | 4 +--- Makefile | 26 +++++++++--------------- src/client.c | 39 +++++++++++++++++++++++++++++++++++ src/client/Makefile | 15 -------------- src/client/main.c | 39 ----------------------------------- src/logger.c | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/logger/Makefile | 13 ------------ src/logger/main.c | 55 -------------------------------------------------- src/publisher.c | 27 +++++++++++++++++++++++++ src/publisher/Makefile | 15 -------------- src/publisher/main.c | 27 ------------------------- 11 files changed, 132 insertions(+), 183 deletions(-) create mode 100644 src/client.c delete mode 100644 src/client/Makefile delete mode 100644 src/client/main.c create mode 100644 src/logger.c delete mode 100644 src/logger/Makefile delete mode 100644 src/logger/main.c create mode 100644 src/publisher.c delete mode 100644 src/publisher/Makefile delete mode 100644 src/publisher/main.c diff --git a/.gitignore b/.gitignore index 69e4961..0588eac 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,5 @@ # Binary -/client -/publisher -/logger +/bin # Logs /logs \ No newline at end of file diff --git a/Makefile b/Makefile index fc5c2bf..c830530 100644 --- a/Makefile +++ b/Makefile @@ -3,27 +3,21 @@ include $(CONF) CC="gcc" CFLAGS= -all: publisher client logger +all: pre-build bin/publisher bin/client bin/logger -publisher: - @echo "---------- Building publisher..." - CFLAGS=$(CFLAGS) \ - $(MAKE) -C src/publisher/ CC=$(CC) OUT=$(PWD) CONF=$(CONF) +pre-build: + mkdir -p bin/ -client: - @echo "---------- Building client..." - CFLAGS=$(CFLAGS) \ - $(MAKE) -C src/client/ CC=$(CC) OUT=$(PWD) CONF=$(CONF) +bin/publisher: src/publisher.c + $(CC) -lzmq $^ -o $@ -logger: - @echo "---------- Building logger..." - CFLAGS=$(CFLAGS) \ - $(MAKE) -C src/logger/ CC=$(CC) OUT=$(PWD) CONF=$(CONF) +bin/client: src/client.c + $(CC) -lzmq $^ -o $@ +bin/logger: src/logger.c + $(CC) $^ -o $@ clean: - $(MAKE) clean -C src/publisher/ OUT=$(PWD) - $(MAKE) clean -C src/client/ OUT=$(PWD) - $(MAKE) clean -C src/logger/ OUT=$(PWD) + rm -rf bin/ .PHONY: clean publisher client logger diff --git a/src/client.c b/src/client.c new file mode 100644 index 0000000..d4a6c5c --- /dev/null +++ b/src/client.c @@ -0,0 +1,39 @@ +// Weather update client +// Connects SUB socket to tcp://localhost:5556 +// Collects weather updates and finds avg temp in zipcode +#include +#include +#include +#include + +#include "utils.h" + + +int main (int argc, char *argv []) +{ + // Socket to talk to server + printf ("Collecting updates from weather server...\n"); + void *context = zmq_ctx_new (); + void *subscriber = zmq_socket (context, ZMQ_SUB); + int rc = zmq_connect (subscriber, "tcp://"STRINGIFY(PUBLISHER_ADDR)":"STRINGIFY(PUBLISHER_PORT)); + assert (rc == 0); + + // Subscribe to zipcode, default is NYC, 10001 + // Subscribe to zipcode, default is NYC, 10001 + const char *filter = "Hello"; + rc = zmq_setsockopt (subscriber, ZMQ_SUBSCRIBE, + filter, strlen(filter)); + assert (rc == 0); + + // Process 100 updates + int update_nbr; + char buffer[10]; + for (update_nbr = 0; update_nbr < 100; update_nbr++) { + zmq_recv (subscriber, buffer, 10, 0); + printf("Received!"); + } + + zmq_close (subscriber); + zmq_ctx_destroy (context); + return 0; +} \ No newline at end of file diff --git a/src/client/Makefile b/src/client/Makefile deleted file mode 100644 index 1afa44c..0000000 --- a/src/client/Makefile +++ /dev/null @@ -1,15 +0,0 @@ -include $(CONF) -EXEC=client -CFLAGS+=-lzmq - -DEFINES=\ --DPUBLISHER_PORT=$(PUBLISHER_PORT) \ --DPUBLISHER_ADDR=$(PUBLISHER_ADDR) - -all: main.c - $(CC) $^ -o $(OUT)/$(EXEC) $(CFLAGS) $(DEFINES) - -clean: - rm -f $(OUT)/$(EXEC) - -.PHONY: clean diff --git a/src/client/main.c b/src/client/main.c deleted file mode 100644 index e4b3498..0000000 --- a/src/client/main.c +++ /dev/null @@ -1,39 +0,0 @@ -// Weather update client -// Connects SUB socket to tcp://localhost:5556 -// Collects weather updates and finds avg temp in zipcode -#include -#include -#include -#include - -#include "../utils.h" - - -int main (int argc, char *argv []) -{ - // Socket to talk to server - printf ("Collecting updates from weather server...\n"); - void *context = zmq_ctx_new (); - void *subscriber = zmq_socket (context, ZMQ_SUB); - int rc = zmq_connect (subscriber, "tcp://"STRINGIFY(PUBLISHER_ADDR)":"STRINGIFY(PUBLISHER_PORT)); - assert (rc == 0); - - // Subscribe to zipcode, default is NYC, 10001 - // Subscribe to zipcode, default is NYC, 10001 - const char *filter = "Hello"; - rc = zmq_setsockopt (subscriber, ZMQ_SUBSCRIBE, - filter, strlen(filter)); - assert (rc == 0); - - // Process 100 updates - int update_nbr; - char buffer[10]; - for (update_nbr = 0; update_nbr < 100; update_nbr++) { - zmq_recv (subscriber, buffer, 10, 0); - printf("Received!"); - } - - zmq_close (subscriber); - zmq_ctx_destroy (context); - return 0; -} \ No newline at end of file diff --git a/src/logger.c b/src/logger.c new file mode 100644 index 0000000..2245887 --- /dev/null +++ b/src/logger.c @@ -0,0 +1,55 @@ +// Weather update client +// Connects SUB socket to tcp://localhost:5556 +// Collects weather updates and finds avg temp in zipcode +#include +#include +#include +#include +#include +#include +#include +#include +#include "utils.h" +#include + +void start(char *power_path, char *busid, char *chipaddr); + +int main (int argc, char *argv []) +{ + if(argc != 2){ + printf("Usage: %s ",argv[0]); + exit(1); + } + + // Extract bus id and ina260 chip address + char busid[10]; + char chipaddr[10]; + char *base=basename(argv[1]); + sscanf(base,"%[^-]-%[^-]",busid,chipaddr); + + start("/home/loic/out.txt", busid,chipaddr); + + return 0; +} + +void start(char *power_path, char *busid, char *chipaddr){ + if (access(power_path, F_OK) != 0){ + printf("Could not read %s\n",power_path); + exit(2); + } + char outdir[255]; + mkdir(STRINGIFY(LOGGER_DIR),0755); + sprintf(outdir,"%s/%s-%s/",STRINGIFY(LOGGER_DIR), busid,chipaddr); + mkdir(outdir,0755); + + char outfile[255]; + time_t timestamp; + timestamp = time(NULL); + sprintf(outfile,"%s/%d",outdir,timestamp); + printf("aa %s\n",outfile); + + FILE *f; + f=fopen(outfile, "w"); + fclose(f); + +} \ No newline at end of file diff --git a/src/logger/Makefile b/src/logger/Makefile deleted file mode 100644 index fc04b6d..0000000 --- a/src/logger/Makefile +++ /dev/null @@ -1,13 +0,0 @@ -include $(CONF) -EXEC=logger - -DEFINES=\ --DLOGGER_DIR=$(LOGGER_DIR) - -all: main.c - $(CC) $^ -o $(OUT)/$(EXEC) $(CFLAGS) $(DEFINES) - -clean: - rm -f $(OUT)/$(EXEC) - -.PHONY: clean diff --git a/src/logger/main.c b/src/logger/main.c deleted file mode 100644 index 04781dc..0000000 --- a/src/logger/main.c +++ /dev/null @@ -1,55 +0,0 @@ -// Weather update client -// Connects SUB socket to tcp://localhost:5556 -// Collects weather updates and finds avg temp in zipcode -#include -#include -#include -#include -#include -#include -#include -#include -#include "../utils.h" -#include - -void start(char *power_path, char *busid, char *chipaddr); - -int main (int argc, char *argv []) -{ - if(argc != 2){ - printf("Usage: %s ",argv[0]); - exit(1); - } - - // Extract bus id and ina260 chip address - char busid[10]; - char chipaddr[10]; - char *base=basename(argv[1]); - sscanf(base,"%[^-]-%[^-]",busid,chipaddr); - - start("/home/loic/out.txt", busid,chipaddr); - - return 0; -} - -void start(char *power_path, char *busid, char *chipaddr){ - if (access(power_path, F_OK) != 0){ - printf("Could not read %s\n",power_path); - exit(2); - } - char outdir[255]; - mkdir(STRINGIFY(LOGGER_DIR),0755); - sprintf(outdir,"%s/%s-%s/",STRINGIFY(LOGGER_DIR), busid,chipaddr); - mkdir(outdir,0755); - - char outfile[255]; - time_t timestamp; - timestamp = time(NULL); - sprintf(outfile,"%s/%d",outdir,timestamp); - printf("aa %s\n",outfile); - - FILE *f; - f=fopen(outfile, "w"); - fclose(f); - -} \ No newline at end of file diff --git a/src/publisher.c b/src/publisher.c new file mode 100644 index 0000000..134d678 --- /dev/null +++ b/src/publisher.c @@ -0,0 +1,27 @@ +// Weather update server +// Binds PUB socket to tcp://*:5556 +// Publishes random weather updates + +#include +#include +#include + +#include "utils.h" + +int main (void) +{ + // Prepare our context and publisher + void *context = zmq_ctx_new (); + void *publisher = zmq_socket (context, ZMQ_PUB); + int rc = zmq_bind (publisher, "tcp://*:"STRINGIFY(PUBLISHER_PORT)); + assert (rc == 0); + + // Initialize random number generator + while (1) { + zmq_send (publisher, "Hello World", 5, 0); + printf("AA\n"); + } + zmq_close (publisher); + zmq_ctx_destroy (context); + return 0; +} \ No newline at end of file diff --git a/src/publisher/Makefile b/src/publisher/Makefile deleted file mode 100644 index 49bf5ea..0000000 --- a/src/publisher/Makefile +++ /dev/null @@ -1,15 +0,0 @@ -include $(CONF) -EXEC=publisher -CFLAGS+=-lzmq - -DEFINES=\ --DPUBLISHER_PORT=$(PUBLISHER_PORT) \ --DPUBLISHER_ADDR=$(PUBLISHER_ADDR) - -all: main.c - $(CC) $^ -o $(OUT)/$(EXEC) $(CFLAGS) $(DEFINES) - -clean: - rm -f $(OUT)/$(EXEC) - -.PHONY: clean diff --git a/src/publisher/main.c b/src/publisher/main.c deleted file mode 100644 index 001df66..0000000 --- a/src/publisher/main.c +++ /dev/null @@ -1,27 +0,0 @@ -// Weather update server -// Binds PUB socket to tcp://*:5556 -// Publishes random weather updates - -#include -#include -#include - -#include "../utils.h" - -int main (void) -{ - // Prepare our context and publisher - void *context = zmq_ctx_new (); - void *publisher = zmq_socket (context, ZMQ_PUB); - int rc = zmq_bind (publisher, "tcp://*:"STRINGIFY(PUBLISHER_PORT)); - assert (rc == 0); - - // Initialize random number generator - while (1) { - zmq_send (publisher, "Hello World", 5, 0); - printf("AA\n"); - } - zmq_close (publisher); - zmq_ctx_destroy (context); - return 0; -} \ No newline at end of file -- cgit v1.2.3