March 2013
Intermediate to advanced
516 pages
15h 11m
English
Because each socket flow has its own little traps for the unwary, we will test them in real code one by one, rather than trying to throw the whole lot into code in one go. When we’re happy with each flow, we can put them together into a full program. We’ll start with the state flow (Figure 3-20).

Figure 3-20. The state flow
Example 3-19 shows how this works in code.
Example 3-19. Prototype state flow (peering1.c)
//// Broker peering simulation (part 1)// Prototypes the state flow//#include "czmq.h"intmain(intargc,char*argv[]){// First argument is this broker's name// Other arguments are our peers' names//if(argc<2){printf("syntax: peering1 me {you}...\n");exit(EXIT_FAILURE);}char*self=argv[1];printf("I: preparing broker at %s...\n",self);srandom((unsigned)time(NULL));zctx_t*ctx=zctx_new();// Bind state backend to endpointvoid*statebe=zsocket_new(ctx,ZMQ_PUB);zsocket_bind(statebe,"ipc://%s-state.ipc",self);// Connect statefe to all peersvoid*statefe=zsocket_new(ctx,ZMQ_SUB);zsockopt_set_subscribe(statefe,"");intargn;for(argn=2;argn<argc;argn++){char*peer=argv[argn];printf("I: connecting to state backend at '%s'\n",peer);zsocket_connect(statefe,"ipc://%s-state.ipc",peer);}
The main loop (Example 3-20) sends out status messages to peers and collects status messages back from peers. ...
Read now
Unlock full access