From 831c3999b48613c61e15094bf0581cb38e1c3467 Mon Sep 17 00:00:00 2001 From: Loic Guegan Date: Sat, 15 Jul 2023 19:31:17 +0200 Subject: Minor changes --- .gitignore | 2 +- Makefile | 13 +++++---- README.md | 20 +++++++++----- src/client.c | 83 -------------------------------------------------------- src/logger.c | 3 -- src/publisher.c | 4 --- src/subscriber.c | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 102 insertions(+), 103 deletions(-) delete mode 100644 src/client.c create mode 100644 src/subscriber.c diff --git a/.gitignore b/.gitignore index 55af06b..dbc4b4c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,7 @@ # Binary /logger /publisher -/client +/subscriber # Caches /logs diff --git a/Makefile b/Makefile index a18b2fe..d12d046 100644 --- a/Makefile +++ b/Makefile @@ -3,21 +3,24 @@ include $(CONF) CC="gcc" CFLAGS= -all: publisher client logger +all: publisher subscriber logger publisher: src/publisher.c src/utils.c $(CC) -lzmq $^ -o $@ -client: src/client.c src/utils.c +subscriber: src/subscriber.c src/utils.c $(CC) -lzmq $^ -o $@ logger: src/logger.c src/utils.c $(CC) $^ -o $@ -start: publisher client +publish: publisher logger + $(error "Not yet implemented") + +subscribe: subscriber $(error "Not yet implemented") clean: - rm -f logger client publisher + rm -f logger subscriber publisher -.PHONY: clean publisher client logger start +.PHONY: clean publish subscribe diff --git a/README.md b/README.md index 30147ea..656b625 100644 --- a/README.md +++ b/README.md @@ -3,16 +3,22 @@ A ZeroMQ-based power monitoring publisher for the ina260 chip. This repository provides: - `logger`: that collects (as fast as possible) power measurements from the ina260 using the dedicated driver -- `publisher`: that continuously publish through the pub/sub communication pattern of ZeroMQ -- `client`: a sub client example that you can use for testing +- `publisher`: that continuously publish using the ZeroMQ pub/sub communication pattern +- `subscriber`: a client that collect the measurements from the publishers (broker, frontend etc...) *Note: Nothing is stored locally! Everything that is published by the publisher is either receive by a subscriber or lost.* ### Compilation -The only dependency is [ZeroMQ](https://zeromq.org/). -First, you need to setup the environment by editing `config.mk` according to your needs. Then, executing `make` is sufficient to generate *logger*, *publisher* and *client*. +The only dependency is [ZeroMQ](https://zeromq.org/). After installing the ZeroMQ library, the following will generate all the executables: -### Execution -Running `make start` will start the *logger* and *publisher* according to the settings you provided in `config.mk`. + $ make -Then, to access to the power measurements, simply execute the client on a machine where the publisher is reachable with `./client`. \ No newline at end of file +### Deployment +1. Update `config.mk` according to you needs +2. On the nodes equiped with the ina260 chips: + + $ make publish + +3. One the node that collect the measurements (broker, frontend etc..): + + $ make subscribe \ No newline at end of file diff --git a/src/client.c b/src/client.c deleted file mode 100644 index 32172fd..0000000 --- a/src/client.c +++ /dev/null @@ -1,83 +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 []) -{ - if(argc != 3){ - printf("Usage: %s ",argv[0]); - exit(1); - } - - //----- Arguments - int port=atoi(argv[1]); - char *cdatadir=argv[2]; - - //----- Various inits - mkdirp(cdatadir); - - //----- Init ZMQ - void *context = zmq_ctx_new (); - void *subscriber = zmq_socket (context, ZMQ_SUB); - char bindto[30]; - sprintf(bindto,"tcp://*:%d",port); - int rc = zmq_bind (subscriber, bindto); - if(rc!=0){ - printf("Failed to bind zmq on %s\n",bindto); - exit(1); - } - rc = zmq_setsockopt (subscriber, ZMQ_SUBSCRIBE, - ZMQ_TOKEN, strlen(ZMQ_TOKEN)); - - //----- Listen - char buffer[ZMQ_MSG_SIZE]; - int size; - while(1){ - size=zmq_recv (subscriber, buffer, ZMQ_MSG_SIZE-1, 0); - buffer[size < ZMQ_MSG_SIZE ? size : ZMQ_MSG_SIZE - 1] = '\0'; - //----- Read buffer - char *token = strtok(buffer, "\n"); - char key[255]; - char client[255]; - long int interval; - FILE *fptr; - int line=1; - while(token != NULL){ - if(line==2) - strcpy(key,token); - else if(line==3) - strcpy(client,token); - else if(line==4) - interval=atoi(token); - - if(line==4){ - printf("Data received with key=%s\n",key); - char path[255]=""; - sprintf(path,"%s/%s_%s_%ld",cdatadir,key,client,interval); - fptr=fopen(path,"a"); - } - - if(line>4){ - fwrite(token, strlen(token), 1, fptr); - fwrite("\n",2,1,fptr); - } - - token=strtok(NULL, "\n"); - line++; - } - fclose(fptr); - } - - - - 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 index 8798db4..0a3278e 100644 --- a/src/logger.c +++ b/src/logger.c @@ -1,6 +1,3 @@ -// Weather update client -// Connects SUB socket to tcp://localhost:5556 -// Collects weather updates and finds avg temp in zipcode #include #include #include diff --git a/src/publisher.c b/src/publisher.c index 8b0906a..e216232 100644 --- a/src/publisher.c +++ b/src/publisher.c @@ -1,7 +1,3 @@ -// Weather update server -// Binds PUB socket to tcp://*:5556 -// Publishes random weather updates - #include #include #include diff --git a/src/subscriber.c b/src/subscriber.c new file mode 100644 index 0000000..932a82d --- /dev/null +++ b/src/subscriber.c @@ -0,0 +1,80 @@ +#include +#include +#include +#include + +#include "utils.h" + +int main (int argc, char *argv []) +{ + if(argc != 3){ + printf("Usage: %s ",argv[0]); + exit(1); + } + + //----- Arguments + int port=atoi(argv[1]); + char *cdatadir=argv[2]; + + //----- Various inits + mkdirp(cdatadir); + + //----- Init ZMQ + void *context = zmq_ctx_new (); + void *subscriber = zmq_socket (context, ZMQ_SUB); + char bindto[30]; + sprintf(bindto,"tcp://*:%d",port); + int rc = zmq_bind (subscriber, bindto); + if(rc!=0){ + printf("Failed to bind zmq on %s\n",bindto); + exit(1); + } + rc = zmq_setsockopt (subscriber, ZMQ_SUBSCRIBE, + ZMQ_TOKEN, strlen(ZMQ_TOKEN)); + + //----- Listen + char buffer[ZMQ_MSG_SIZE]; + int size; + while(1){ + size=zmq_recv (subscriber, buffer, ZMQ_MSG_SIZE-1, 0); + buffer[size < ZMQ_MSG_SIZE ? size : ZMQ_MSG_SIZE - 1] = '\0'; + //----- Read buffer + char *token = strtok(buffer, "\n"); + char key[255]; + char client[255]; + long int interval; + FILE *fptr; + int line=1; + while(token != NULL){ + if(line==2) + strcpy(key,token); + else if(line==3) + strcpy(client,token); + else if(line==4) + interval=atoi(token); + + if(line==4){ + printf("Data received with key=%s\n",key); + char path[255]=""; + sprintf(path,"%s/%s_%s_%ld",cdatadir,key,client,interval); + fptr=fopen(path,"a"); + } + + if(line>4){ + fwrite(token, strlen(token), 1, fptr); + fwrite("\n",2,1,fptr); + } + + token=strtok(NULL, "\n"); + line++; + } + fclose(fptr); + } + + + + zmq_close (subscriber); + zmq_ctx_destroy (context); + + return 0; +} \ No newline at end of file -- cgit v1.2.3