summaryrefslogtreecommitdiff
path: root/src/client/main.c
blob: e7ff4a5107554a1ecf421b2b086f25ce5b2192d9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
//  Weather update client
//  Connects SUB socket to tcp://localhost:5556
//  Collects weather updates and finds avg temp in zipcode
#include <zmq.h>
#include <assert.h>
#include <time.h>
#include <string.h>

#define STR(symbol) #symbol
#define STRINGIFY(symbol) STR(symbol)

#ifndef PUBLISHER_PORT 
#error PUBLISHER_PORT macro undefined
#endif

int main (int argc, char *argv [])
{
    //  Socket to talk to server
    printf ("Collecting updates from weather server...\n");
    void *context = zmq_ctx_new ();
    void *subscriber = zmq_socket (context, ZMQ_SUB);
    int rc = zmq_connect (subscriber, "tcp://localhost:"STRINGIFY(PUBLISHER_PORT));
    assert (rc == 0);

    //  Subscribe to zipcode, default is NYC, 10001
 //  Subscribe to zipcode, default is NYC, 10001
    const char *filter = "Hello";
    rc = zmq_setsockopt (subscriber, ZMQ_SUBSCRIBE,
                         filter, strlen(filter));
    assert (rc == 0);

    //  Process 100 updates
    int update_nbr;
    char buffer[10];
    for (update_nbr = 0; update_nbr < 100; update_nbr++) {
      zmq_recv (subscriber, buffer, 10, 0);
      printf("Received!");
    }

    zmq_close (subscriber);
    zmq_ctx_destroy (context);
    return 0;
}