Model One: Simple Retry and Failover
So, our menu appears to offer the following choices: simple, brutal, complex, or nasty. Letâs start with simple and then work out the kinks. Weâll take Lazy Pirate and rewrite it to work with multiple server endpoints. Weâll start one or several servers first, specifying a bind endpoint as the argument (Example 4-77).
Example 4-77. Freelance server, Model One (flserver1.c)
//
// Freelance server - Model One
// Trivial echo service
//
#include "czmq.h"
int
main
(
int
argc
,
char
*
argv
[])
{
if
(
argc
<
2
)
{
printf
(
"I: syntax: %s <endpoint>
\n
"
,
argv
[
0
]);
exit
(
EXIT_SUCCESS
);
}
zctx_t
*
ctx
=
zctx_new
();
void
*
server
=
zsocket_new
(
ctx
,
ZMQ_REP
);
zsocket_bind
(
server
,
argv
[
1
]);
printf
(
"I: echo service is ready at %s
\n
"
,
argv
[
1
]);
while
(
true
)
{
zmsg_t
*
msg
=
zmsg_recv
(
server
);
if
(
!
msg
)
break
;
// Interrupted
zmsg_send
(
&
msg
,
server
);
}
if
(
zctx_interrupted
)
printf
(
"W: interrupted
\n
"
);
zctx_destroy
(
&
ctx
);
return
0
;
}
Then weâll start the client (Example 4-78), specifying one or more connect endpoints as arguments.
Example 4-78. Freelance client, Model One (flclient1.c)
//
// Freelance client - Model One
// Uses REQ socket to query one or more services
//
#include "czmq.h"
#define REQUEST_TIMEOUT 1000
#define MAX_RETRIES 3
// Before we abandon
static
zmsg_t
*
s_try_request
(
zctx_t
*
ctx
,
char
*
endpoint
,
zmsg_t
*
request
)
{
printf
(
"I: trying echo service at %s...
\n
"
,
endpoint
);
void
*
client
=
zsocket_new
(
ctx
,
ZMQ_REQ
);
zsocket_connect ...
Get ZeroMQ now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.