Created
October 29, 2012 09:04
-
-
Save anonymous/3972502 to your computer and use it in GitHub Desktop.
Weather Update Server (with sleep)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // | |
| // 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