ina260-zmq-publisher/src/publisher.c

94 lines
2.7 KiB
C
Raw Normal View History

2023-07-14 18:49:03 +02:00
// Weather update server
// Binds PUB socket to tcp://*:5556
// Publishes random weather updates
2023-07-14 18:26:31 +02:00
2023-07-14 18:49:03 +02:00
#include <zmq.h>
#include <assert.h>
#include <time.h>
2023-07-15 12:14:22 +02:00
#include <dirent.h>
2023-07-14 18:26:31 +02:00
2023-07-15 08:38:33 +02:00
#include "utils.h"
2023-07-14 19:29:18 +02:00
2023-07-15 12:14:22 +02:00
// Global:
char *__logdir;
int __loginterval;
2023-07-15 12:45:13 +02:00
int __port;
2023-07-15 14:02:43 +02:00
void publish(void *publisher, char *filepath, char* client, int interval);
2023-07-15 12:14:22 +02:00
2023-07-15 11:49:17 +02:00
int main (int argc, char *argv [])
2023-07-14 18:49:03 +02:00
{
2023-07-15 12:45:13 +02:00
if(argc != 4){
printf("Usage: %s <abslogdir> <loginterval> <port>",argv[0]);
2023-07-15 12:14:22 +02:00
exit(1);
}
//----- Init global variables
__logdir=argv[1];
__loginterval=atoi(argv[2]);
2023-07-15 12:45:13 +02:00
__port=atoi(argv[3]);
2023-07-15 12:14:22 +02:00
2023-07-15 12:45:13 +02:00
//----- Prepare our context and publisher
void *context = zmq_ctx_new ();
void *publisher = zmq_socket (context, ZMQ_PUB);
char bindto[30];
sprintf(bindto,"tcp://*:%d",__port);
int rc = zmq_bind (publisher, bindto);
if(rc!=0){
printf("Failed to bind zmq on %s\n",bindto);
exit(1);
}
2023-07-15 12:26:46 +02:00
//----- Start publisher
2023-07-15 12:14:22 +02:00
struct dirent *de; // Pointer for directory entry
2023-07-15 13:54:03 +02:00
while(1){
2023-07-15 12:14:22 +02:00
int interval=INTERVAL(__loginterval);
int interval_next=INTERVAL_NEXT(__loginterval);
2023-07-15 13:54:03 +02:00
DIR *dr = opendir(__logdir);
2023-07-15 12:14:22 +02:00
while ((de = readdir(dr)) != NULL){
if(strcmp(de->d_name,".") && strcmp(de->d_name,"..")){
char *client=de->d_name;
2023-07-15 12:26:46 +02:00
char logfile[255];
char logfile_next[255];
sprintf(logfile,"%s/%s/%ld",__logdir,client,interval);
sprintf(logfile_next,"%s/%s/%ld",__logdir,client,interval_next);
// As long as next logfile is not available, we should wait
// for sending the current one
printf("Waiting for %s...%s\n",client,logfile_next);
while(!FILE_EXISTS(logfile_next)){
sleep(1);
}
// Send current one
if(FILE_EXISTS(logfile)){
2023-07-15 14:02:43 +02:00
publish(publisher,logfile,client,interval);
2023-07-15 13:54:03 +02:00
remove(logfile);
2023-07-15 12:26:46 +02:00
}
2023-07-15 12:14:22 +02:00
}
}
2023-07-15 13:54:03 +02:00
closedir(dr);
2023-07-15 12:14:22 +02:00
}
2023-07-15 12:45:13 +02:00
zmq_close (publisher);
zmq_ctx_destroy (context);
2023-07-15 12:14:22 +02:00
return 0;
2023-07-15 12:45:13 +02:00
// // 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);
2023-07-14 18:26:31 +02:00
2023-07-15 12:45:13 +02:00
// // Initialize random number generator
// while (1) {
// zmq_send (publisher, "Hello World", 5, 0);
// printf("AA\n");
// }
// zmq_close (publisher);
// zmq_ctx_destroy (context);
2023-07-14 18:49:03 +02:00
return 0;
2023-07-15 12:45:13 +02:00
}
2023-07-15 14:02:43 +02:00
void publish(void *publisher, char *filepath, char* client, int interval){
2023-07-15 13:54:03 +02:00
printf("Publish!\n");
2023-07-15 14:02:43 +02:00
zmq_send (publisher, ZMQ_TOKEN, strlen(ZMQ_TOKEN), 0);
2023-07-14 18:49:03 +02:00
}