#include <stdlib.h>
#include <string.h>
#include <zmq.h>
#include <unistd.h>
#include <signal.h>

int running = 1;
static void stopHandler(int sign) {
  printf("received ctrl-c\n");
  running = 0;
}



int main() {
  signal(SIGINT, stopHandler); /* catches ctrl-c */

  void *context = zmq_ctx_new();
  void *publisher = zmq_socket(context, ZMQ_PUB);
  int rc = zmq_bind(publisher, "tcp://127.0.0.1:12345");
  if (rc != 0) {
    printf("something wrong with zmq_bind: %s\n", zmq_strerror(errno));
    exit(1);
  }

  double temperature = 15.0;
  while (running) {
    rc = zmq_send(publisher, &temperature, sizeof(double), 0);
    if (rc != -1) {
      printf("sent %f\n", temperature);
    } else {
      printf("something wrong with zmq_send: %s\n", zmq_strerror(errno));
    }
    
    temperature += 0.25;

    sleep(5);
  }

  printf("Terminating ...\n");
  zmq_close(publisher);
  zmq_ctx_destroy(context);
}
