ROUTER-to-DEALER in C

//
// Custom routing Router to Dealer
//
#include "zhelpers.h"
#include <pthread.h>

// We have two workers, here we copy the code, normally these would
// run on different boxes…
//

static void *
worker_task_a (void *args)
{
void *context = zmq_ctx_new ();
void *worker = zmq_socket (context, ZMQ_DEALER);
zmq_setsockopt (worker, ZMQ_IDENTITY, "A", 1);
zmq_connect (worker, "ipc://routing.ipc");

int total = 0;
while (true) {
// We receive one part, with the workload
char *request = s_recv (worker);
int finished = (strcmp (request, "END") == 0);
free (request);
if (finished) {
printf ("A received: %d\n", total);
break;
}
total++;
}
zmq_close (worker);
zmq_ctx_destroy (context);
return NULL;
}

static void *
worker_task_b (void *args)
{
void *context = zmq_ctx_new ();
void *worker = zmq_socket (context, ZMQ_DEALER);
zmq_setsockopt (worker, ZMQ_IDENTITY, "B", 1);
zmq_connect (worker, "ipc://routing.ipc");

int total = 0;
while (true) {
// We receive one part, with the workload
char *request = s_recv (worker);
int finished = (strcmp (request, "END") == 0);
free (request);
if (finished) {
printf ("B received: %d\n", total);
break;
}
total++;
}
zmq_close (worker);
zmq_ctx_destroy (context);
return NULL;
}

// After we've defined the two worker tasks, we have the main task.
// Recall that these three tasks could be in separate processes, even
// running on different boxes. It's just easier to start by writing
// these in a single program. The main task starts the two workers,
// then scatters tasks to the workers. It sends an END message to each
// worker to tell them to exit:

int main (void)
{
void *context = zmq_ctx_new ();
void *client = zmq_socket (context, ZMQ_ROUTER);
zmq_bind (client, "ipc://routing.ipc");

pthread_t worker;
pthread_create (&worker, NULL, worker_task_a, NULL);
pthread_create (&worker, NULL, worker_task_b, NULL);

// Wait for threads to connect, since otherwise the messages
// we send won't be routable.
sleep (1);

// Send 10 tasks scattered to A twice as often as B
int task_nbr;
srandom ((unsigned) time (NULL));
for (task_nbr = 0; task_nbr < 10; task_nbr++) {
// Send two message parts, first the address…
if (randof (3) > 0)
s_sendmore (client, "A");
else
s_sendmore (client, "B");

// And then the workload
s_send (client, "This is the workload");
}
s_sendmore (client, "A");
s_send (client, "END");

s_sendmore (client, "B");
s_send (client, "END");

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