Skip to content

Instantly share code, notes, and snippets.

Created October 29, 2012 09:04
Show Gist options
  • Select an option

  • Save anonymous/3972502 to your computer and use it in GitHub Desktop.

Select an option

Save anonymous/3972502 to your computer and use it in GitHub Desktop.
Weather Update Server (with sleep)
//
// Weather update server in C++
// Binds PUB socket to tcp://*:5556
// Publishes random weather updates
//
// Olivier Chamoux <olivier.chamoux@fr.thalesgroup.com>
//
#include <zmq.hpp>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <iostream>
#define within(num) (int) ((float) num * random () / (RAND_MAX + 1.0))
int main () {
// Prepare our context and publisher
zmq::context_t context( 4 );
zmq::socket_t publisher( context, ZMQ_PUB );
publisher.bind( "tcp://*:5556" );
//publisher.bind( "ipc://weather.ipc" );
// Initialize random number generator
srandom ((unsigned) time (NULL));
while (1) {
int zipcode, temperature, relhumidity;
// Get values that will fool the boss
zipcode = within (100000);
temperature = within (215) - 80;
relhumidity = within (50) + 10;
// Send message to all subscribers
zmq::message_t message( 20 );
snprintf ((char *) message.data(), 20, "%05d %d %d", zipcode, temperature, relhumidity);
bool result = publisher.send( message );
if( !result )
std::cout << "SendErr" << std::endl;
sleep(1);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment