#include <stdlib.h>
#include <assert.h>
#include <signal.h>
#include <zmq.h>

double measuredTemperature = 0.0;


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



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

  /* prepare zmq context, connect to server and subscribe with empty filter */
  void *zmqContext = zmq_ctx_new();
  void *zmqSubscriber = zmq_socket(zmqContext, ZMQ_SUB);
  int rc = zmq_connect(zmqSubscriber, "tcp://127.0.0.1:12345");
  assert(rc == 0);
  rc = zmq_setsockopt(zmqSubscriber, ZMQ_SUBSCRIBE, 0, 0);
  assert(rc == 0);

  while (running) {
    double oldMeasuredTemperature = measuredTemperature;
    
    rc = zmq_recv(zmqSubscriber, &measuredTemperature, 
		  sizeof(double), 0);
    assert(rc != -1);
      
    if (oldMeasuredTemperature != measuredTemperature) {
      printf("temperature value changed: %f\n", measuredTemperature);
    }
  }

  printf("Terminating ...\n");
  zmq_close(zmqSubscriber);
  zmq_ctx_destroy(zmqContext);
}
