本文整理汇总了C++中zmsg_destroy函数的典型用法代码示例。如果您正苦于以下问题:C++ zmsg_destroy函数的具体用法?C++ zmsg_destroy怎么用?C++ zmsg_destroy使用的例子?那么, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了zmsg_destroy函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: job_request_cb
static int job_request_cb (flux_t h, int typemask, zmsg_t **zmsg, void *arg)
{
const char *json_str;
json_object *o = NULL;
const char *topic;
if (flux_msg_get_topic (*zmsg, &topic) < 0)
goto out;
if (flux_msg_get_payload_json (*zmsg, &json_str) < 0)
goto out;
if (json_str && !(o = json_tokener_parse (json_str)))
goto out;
if (strcmp (topic, "job.shutdown") == 0) {
flux_reactor_stop (h);
}
if (strcmp (topic, "job.next-id") == 0) {
if (flux_rank (h) == 0) {
unsigned long id = lwj_next_id (h);
json_object *ox = json_id (id);
flux_json_respond (h, ox, zmsg);
json_object_put (o);
}
else {
fprintf (stderr, "%s: forwarding request\n", topic);
flux_json_request (h, FLUX_NODEID_ANY,
FLUX_MATCHTAG_NONE, topic, o);
}
}
if (strcmp (topic, "job.create") == 0) {
json_object *jobinfo = NULL;
unsigned long id = lwj_next_id (h);
bool should_workaround = false;
//"Fix" for Race Condition
if (util_json_object_get_boolean (o, "race_workaround",
&should_workaround) < 0) {
should_workaround = false;
} else if (should_workaround) {
if (wait_for_lwj_watch_init (h, id) < 0) {
flux_err_respond (h, errno, zmsg);
goto out;
}
}
int rc = kvs_job_new (h, id);
if (rc < 0) {
flux_err_respond (h, errno, zmsg);
goto out;
}
add_jobinfo (h, id, o);
kvs_commit (h);
/* Generate reply with new jobid */
jobinfo = util_json_object_new_object ();
util_json_object_add_int64 (jobinfo, "jobid", id);
flux_json_respond (h, jobinfo, zmsg);
json_object_put (jobinfo);
}
out:
if (o)
json_object_put (o);
zmsg_destroy (zmsg);
return 0;
}
示例2: s_agent_handle_pipe
static int
s_agent_handle_pipe (agent_t *self)
{
// Get the whole message off the pipe in one go
zmsg_t *request = zmsg_recv (self->pipe);
char *command = zmsg_popstr (request);
if (!command)
return -1; // Interrupted
if (streq (command, "ALLOW")) {
char *address = zmsg_popstr (request);
zhash_insert (self->whitelist, address, "OK");
zstr_free (&address);
zstr_send (self->pipe, "OK");
}
else
if (streq (command, "DENY")) {
char *address = zmsg_popstr (request);
zhash_insert (self->blacklist, address, "OK");
zstr_free (&address);
zstr_send (self->pipe, "OK");
}
else
if (streq (command, "PLAIN")) {
// For now we don't do anything with domains
char *domain = zmsg_popstr (request);
zstr_free (&domain);
// Get password file and load into zhash table
// If the file doesn't exist we'll get an empty table
char *filename = zmsg_popstr (request);
zhash_destroy (&self->passwords);
self->passwords = zhash_new ();
zhash_load (self->passwords, filename);
zstr_free (&filename);
zstr_send (self->pipe, "OK");
}
else
if (streq (command, "CURVE")) {
// For now we don't do anything with domains
char *domain = zmsg_popstr (request);
zstr_free (&domain);
// If location is CURVE_ALLOW_ANY, allow all clients. Otherwise
// treat location as a directory that holds the certificates.
char *location = zmsg_popstr (request);
if (streq (location, CURVE_ALLOW_ANY))
self->allow_any = true;
else {
zcertstore_destroy (&self->certstore);
self->certstore = zcertstore_new (location);
self->allow_any = false;
}
zstr_free (&location);
zstr_send (self->pipe, "OK");
}
else
if (streq (command, "VERBOSE")) {
char *verbose = zmsg_popstr (request);
self->verbose = *verbose == '1';
zstr_free (&verbose);
zstr_send (self->pipe, "OK");
}
else
if (streq (command, "TERMINATE")) {
self->terminated = true;
zstr_send (self->pipe, "OK");
}
else {
printf ("E: invalid command from API: %s\n", command);
assert (false);
}
zstr_free (&command);
zmsg_destroy (&request);
return 0;
}
示例3: main
int main (int argc, char *argv[])
{
// Prepare our context and sockets
void *context = zmq_init (1);
void *frontend = zmq_socket (context, ZMQ_XREP);
void *backend = zmq_socket (context, ZMQ_XREP);
zmq_bind (frontend, "ipc://frontend.ipc");
zmq_bind (backend, "ipc://backend.ipc");
int client_nbr;
for (client_nbr = 0; client_nbr < NBR_CLIENTS; client_nbr++) {
pthread_t client;
pthread_create (&client, NULL, client_thread, context);
}
int worker_nbr;
for (worker_nbr = 0; worker_nbr < NBR_WORKERS; worker_nbr++) {
pthread_t worker;
pthread_create (&worker, NULL, worker_thread, context);
}
// Logic of LRU loop
// - Poll backend always, frontend only if 1+ worker ready
// - If worker replies, queue worker as ready and forward reply
// to client if necessary
// - If client requests, pop next worker and send request to it
// Queue of available workers
int available_workers = 0;
char *worker_queue [NBR_WORKERS];
while (1) {
// Initialize poll set
zmq_pollitem_t items [] = {
// Always poll for worker activity on backend
{ backend, 0, ZMQ_POLLIN, 0 },
// Poll front-end only if we have available workers
{ frontend, 0, ZMQ_POLLIN, 0 }
};
if (available_workers)
zmq_poll (items, 2, -1);
else
zmq_poll (items, 1, -1);
// Handle worker activity on backend
if (items [0].revents & ZMQ_POLLIN) {
zmsg_t *zmsg = zmsg_recv (backend);
// Use worker address for LRU routing
assert (available_workers < NBR_WORKERS);
worker_queue [available_workers++] = zmsg_unwrap (zmsg);
// Forward message to client if it's not a READY
if (strcmp (zmsg_address (zmsg), "READY") == 0)
zmsg_destroy (&zmsg);
else {
zmsg_send (&zmsg, frontend);
if (--client_nbr == 0)
break; // Exit after N messages
}
}
if (items [1].revents & ZMQ_POLLIN) {
// Now get next client request, route to next worker
zmsg_t *zmsg = zmsg_recv (frontend);
zmsg_wrap (zmsg, worker_queue [0], "");
zmsg_send (&zmsg, backend);
// Dequeue and drop the next worker address
free (worker_queue [0]);
DEQUEUE (worker_queue);
available_workers--;
}
}
sleep (1);
zmq_term (context);
return 0;
}
示例4: zyre_event_new
zyre_event_t *
zyre_event_new (zyre_t *node)
{
zmsg_t *msg = zyre_recv (node);
if (!msg)
return NULL; // Interrupted
zyre_event_t *self = (zyre_event_t *) zmalloc (sizeof (zyre_event_t));
assert (self);
char *type = zmsg_popstr (msg);
self->sender = zmsg_popstr (msg);
self->name = zmsg_popstr (msg);
if (streq (type, "ENTER")) {
self->type = ZYRE_EVENT_ENTER;
zframe_t *headers = zmsg_pop (msg);
if (headers) {
self->headers = zhash_unpack (headers);
zframe_destroy (&headers);
}
self->address = zmsg_popstr (msg);
}
else
if (streq (type, "EXIT"))
self->type = ZYRE_EVENT_EXIT;
else
if (streq (type, "JOIN")) {
self->type = ZYRE_EVENT_JOIN;
self->group = zmsg_popstr (msg);
}
else
if (streq (type, "LEAVE")) {
self->type = ZYRE_EVENT_LEAVE;
self->group = zmsg_popstr (msg);
}
else
if (streq (type, "WHISPER")) {
self->type = ZYRE_EVENT_WHISPER;
self->msg = msg;
msg = NULL;
}
else
if (streq (type, "SHOUT")) {
self->type = ZYRE_EVENT_SHOUT;
self->group = zmsg_popstr (msg);
self->msg = msg;
msg = NULL;
}
else
if (streq (type, "STOP")) {
self->type = ZYRE_EVENT_STOP;
}
else
if (streq (type, "EVASIVE")) {
self->type = ZYRE_EVENT_EVASIVE;
}
else
zsys_warning ("bad message received from node: %s\n", type);
free (type);
zmsg_destroy (&msg);
return self;
}
示例5: watch_port
void watch_port(void *cvoid,
zctx_t * context,
void * pipe ) {
zclock_log("watch_port started!");
monitorconfig_t * config = (monitorconfig_t*) cvoid;
dump_monitorconfig(config);
void * linein = zsocket_new(context, ZMQ_SUB);
char * listen_socket = to_linesocket(config->line_id);
char line_id[16];
snprintf(line_id, 15, "%d", config->line_id);
zsocket_connect(linein, listen_socket);
zsockopt_set_unsubscribe(linein, "");
zsockopt_set_subscribe(linein, "CLEAR_MONITORS");
zsockopt_set_subscribe(linein, "VALUE");
// have set up subscription, can signal parent that we're ok.
child_handshake(pipe);
zsocket_destroy(context, pipe); // no longer require pipe
void * lineout = zsocket_new(context, ZMQ_PUB);
zsocket_connect(lineout, config->out_socket);
time_t until = time(NULL) + 60;
while(time(NULL)<until) {
zmsg_t * msg = zmsg_recv(linein);
if(!msg) {
zclock_log("monitor quitting!");
return;
}
zframe_t * cmd = zmsg_pop(msg);
if(zframe_streq(cmd, "CLEAR_MONITORS")) {
zclock_log("ephemeral monitor quitting");
zmsg_destroy(&msg);
zframe_destroy(&cmd);
break;
} else if (zframe_streq(cmd, "VALUE")) {
// TODO perhaps some rate limiting necessary
assert(zmsg_size(msg) == 2);
zframe_t * value = zmsg_pop(msg);
int res = *(int*)zframe_data(value);
char * new_channel = zmsg_popstr(msg);
if(strcmp(new_channel, config->channel)!=0) {
zclock_log("monitor on %d: listening for %s, channel changed to %s quitting",
config->line_id, config->channel, new_channel);
zmsg_destroy(&msg);
zframe_destroy(&cmd);
break;
}
zmsg_t * to_send = zmsg_new();
char buf[1024];
snprintf(buf,1023, "%d", res);
zmsg_pushstr(to_send, buf);
zmsg_pushstr(to_send, line_id);
zmsg_pushstr(to_send, config->source_worker);
zclock_log("%s sending line %s -> %s", config->source_worker, line_id, buf);
zmsg_send(&to_send, lineout);
// don't destroy value frame, now owned by zmsg
}
// else ignore
zmsg_destroy(&msg);
zframe_destroy(&cmd);
}
zclock_log("monitor on %d: listening for %s, expiring naturally",
config->line_id, config->channel);
//cleanup
zsocket_destroy(context, linein);
zsocket_destroy(context, lineout);
}
示例6: zyre_node_recv_api
static int
zyre_node_recv_api (zyre_node_t *self)
{
// Get the whole message off the pipe in one go
zmsg_t *request = zmsg_recv (self->pipe);
char *command = zmsg_popstr (request);
if (!command)
return -1; // Interrupted
if (streq (command, "SET")) {
char *name = zmsg_popstr (request);
char *value = zmsg_popstr (request);
zhash_update (self->headers, name, value);
zstr_free (&name);
zstr_free (&value);
}
else
if (streq (command, "START")) {
zyre_node_start (self);
zstr_send (self->pipe, "OK");
}
else
if (streq (command, "STOP")) {
zyre_node_stop (self);
zstr_send (self->pipe, "OK");
}
else
if (streq (command, "WHISPER")) {
// Get peer to send message to
char *identity = zmsg_popstr (request);
zyre_peer_t *peer = (zyre_peer_t *) zhash_lookup (self->peers, identity);
// Send frame on out to peer's mailbox, drop message
// if peer doesn't exist (may have been destroyed)
if (peer) {
zre_msg_t *msg = zre_msg_new (ZRE_MSG_WHISPER);
zre_msg_set_content (msg, request);
zyre_peer_send (peer, &msg);
request = NULL;
}
zstr_free (&identity);
}
else
if (streq (command, "SHOUT")) {
// Get group to send message to
char *name = zmsg_popstr (request);
zyre_group_t *group = (zyre_group_t *) zhash_lookup (self->peer_groups, name);
if (group) {
zre_msg_t *msg = zre_msg_new (ZRE_MSG_SHOUT);
zre_msg_set_group (msg, name);
zre_msg_set_content (msg, request);
zyre_group_send (group, &msg);
request = NULL;
}
zstr_free (&name);
}
else
if (streq (command, "JOIN")) {
char *name = zmsg_popstr (request);
zyre_group_t *group = (zyre_group_t *) zhash_lookup (self->own_groups, name);
if (!group) {
// Only send if we're not already in group
group = zyre_group_new (name, self->own_groups);
zre_msg_t *msg = zre_msg_new (ZRE_MSG_JOIN);
zre_msg_set_group (msg, name);
// Update status before sending command
zre_msg_set_status (msg, ++(self->status));
zhash_foreach (self->peers, zyre_node_send_peer, msg);
zre_msg_destroy (&msg);
zyre_log_info (self->log, ZRE_LOG_MSG_EVENT_JOIN, NULL, name);
}
zstr_free (&name);
}
else
if (streq (command, "LEAVE")) {
char *name = zmsg_popstr (request);
zyre_group_t *group = (zyre_group_t *) zhash_lookup (self->own_groups, name);
if (group) {
// Only send if we are actually in group
zre_msg_t *msg = zre_msg_new (ZRE_MSG_LEAVE);
zre_msg_set_group (msg, name);
// Update status before sending command
zre_msg_set_status (msg, ++(self->status));
zhash_foreach (self->peers, zyre_node_send_peer, msg);
zre_msg_destroy (&msg);
zhash_delete (self->own_groups, name);
zyre_log_info (self->log, ZRE_LOG_MSG_EVENT_LEAVE, NULL, name);
}
zstr_free (&name);
}
else
if (streq (command, "TERMINATE")) {
self->terminated = true;
zstr_send (self->pipe, "OK");
}
zstr_free (&command);
zmsg_destroy (&request);
return 0;
}
示例7: s_agent_handle_control
static int
s_agent_handle_control (agent_t *self)
{
// Get the whole message off the control socket in one go
zmsg_t *request = zmsg_recv (self->control);
char *command = zmsg_popstr (request);
if (!command)
return -1; // Interrupted
if (streq (command, "SET")) {
char *name = zmsg_popstr (request);
char *value = zmsg_popstr (request);
zhash_insert (self->metadata, name, value);
free (name);
free (value);
}
else
if (streq (command, "VERBOSE")) {
char *verbose = zmsg_popstr (request);
self->verbose = *verbose == '1';
free (verbose);
}
else
if (streq (command, "MAX CLIENTS")) {
char *limit = zmsg_popstr (request);
self->max_clients = atoi (limit);
free (limit);
}
else
if (streq (command, "MAX PENDING")) {
char *limit = zmsg_popstr (request);
self->max_pending = atoi (limit);
free (limit);
}
else
if (streq (command, "CLIENT TTL")) {
char *limit = zmsg_popstr (request);
self->client_ttl = atoi (limit);
free (limit);
}
else
if (streq (command, "PENDING TTL")) {
char *limit = zmsg_popstr (request);
self->pending_ttl = atoi (limit);
free (limit);
}
else
if (streq (command, "BIND")) {
char *endpoint = zmsg_popstr (request);
puts (endpoint);
int rc = zsocket_bind (self->router, "%s", endpoint);
assert (rc != -1);
free (endpoint);
}
else
if (streq (command, "UNBIND")) {
char *endpoint = zmsg_popstr (request);
int rc = zsocket_unbind (self->router, "%s", endpoint);
assert (rc != -1);
free (endpoint);
}
else
if (streq (command, "TERMINATE")) {
self->terminated = true;
zstr_send (self->control, "OK");
}
free (command);
zmsg_destroy (&request);
return 0;
}
示例8: zmsg_destroy
///
// Destroy a message object and all frames it contains
QZmsg::~QZmsg ()
{
zmsg_destroy (&self);
}
示例9: main
int main(int argc, char *argv[])
{
char *broker;
broker = strdup("tcp://localhost:5555");
char *msg_str;
msg_str = strdup("empty");
while ((argc > 1) && (argv[1][0] == '-')) {
switch (argv[1][1]) {
case 'b':
free(broker);
broker = strdup(argv[2]);
++argv;
--argc;
break;
case 'm':
free(msg_str);
msg_str = strdup(argv[2]);
++argv;
--argc;
break;
default:
printf("Wrong Argument: %s\n", argv[1]);
usage();
break;
}
++argv;
--argc;
}
printf("Connecting to broker %s ...\n",broker);
zctx_t *ctx = zctx_new();
void *b_sock = zsocket_new(ctx, ZMQ_PUSH);
assert(b_sock);
int rc;
/* connecting to master */
rc = zsocket_connect(b_sock, broker);
assert(!rc);
zmsg_t *msg = zmsg_new();;
/* send packet */
rc = zmsg_addstr(msg, msg_str);
assert(rc == 0);
printf ("Sending msg='%s' ...\n",msg_str);
/* zmsg_dump(msg); */
/* sending message */
rc = zmsg_send(&msg, b_sock);
assert(rc == 0);
/* wait for message to be sent */
zclock_sleep(100);
zmsg_destroy(&msg);
free(broker);
free(msg_str);
/* everything should be cleanly closed now */
zctx_destroy(&ctx);
return 0;
}
示例10: interface_task
static void
interface_task (void *args, zctx_t *ctx, void *pipe)
{
zre_interface_t *interface = zre_interface_new ();
int64_t counter = 0;
char *to_peer = NULL; // Either of these set,
char *to_group = NULL; // and we set a message
char *cookie = NULL; // received message
char *sending_cookie = NULL; // sending message
zmq_pollitem_t pollitems [] = {
{ pipe, 0, ZMQ_POLLIN, 0 },
{ zre_interface_handle (interface), 0, ZMQ_POLLIN, 0 }
};
// all interface joins GLOBAL
zre_interface_join (interface, "GLOBAL");
while (!zctx_interrupted) {
if (zmq_poll (pollitems, 2, randof (1000) * ZMQ_POLL_MSEC) == -1)
break; // Interrupted
if (pollitems [0].revents & ZMQ_POLLIN)
break; // Any command from parent means EXIT
// Process an event from interface
if (pollitems [1].revents & ZMQ_POLLIN) {
zmsg_t *incoming = zre_interface_recv (interface);
if (!incoming)
break; // Interrupted
char *event = zmsg_popstr (incoming);
if (streq (event, "ENTER")) {
// Always say hello to new peer
to_peer = zmsg_popstr (incoming);
sending_cookie = "R:HELLO";
}
else
if (streq (event, "EXIT")) {
// Do nothing
}
else
if (streq (event, "WHISPER")) {
to_peer = zmsg_popstr (incoming);
cookie = zmsg_popstr (incoming);
// if a message comes from zre_perf_local, send back a special response
if (streq (cookie, "S:WHISPER")) {
sending_cookie = "R:WHISPER";
}
else {
free (to_peer);
free (cookie);
to_peer = NULL;
cookie = NULL;
}
}
else
if (streq (event, "SHOUT")) {
to_peer = zmsg_popstr (incoming);
to_group = zmsg_popstr (incoming);
cookie = zmsg_popstr (incoming);
// if a message comes from zre_perf_local, send back a special response
if (streq (cookie, "S:SHOUT")) {
free (to_peer);
to_peer = NULL;
sending_cookie = "R:SHOUT";
}
else {
free (to_peer);
free (to_group);
to_peer = NULL;
to_group = NULL;
}
}
free (event);
zmsg_destroy (&incoming);
// Send outgoing messages if needed
if (to_peer) {
zmsg_t *outgoing = zmsg_new ();
zmsg_addstr (outgoing, to_peer);
zmsg_addstr (outgoing, sending_cookie);
zre_interface_whisper (interface, &outgoing);
free (to_peer);
to_peer = NULL;
}
if (to_group) {
zmsg_t *outgoing = zmsg_new ();
zmsg_addstr (outgoing, to_group);
zmsg_addstr (outgoing, sending_cookie);
zre_interface_shout (interface, &outgoing);
free (to_group);
to_group = NULL;
}
if (cookie) {
free (cookie);
cookie = NULL;
}
//.........这里部分代码省略.........
示例11: main
int main (int argc, char *argv [])
{
// Arguments can be either of:
// -p primary server, at tcp://localhost:5001
// -b backup server, at tcp://localhost:5002
zctx_t *ctx = zctx_new ();
void *statepub = zsocket_new (ctx, ZMQ_PUB);
void *statesub = zsocket_new (ctx, ZMQ_SUB);
zsockopt_set_subscribe (statesub, "");
void *frontend = zsocket_new (ctx, ZMQ_ROUTER);
bstar_t fsm = { 0 };
if (argc == 2 && streq (argv [1], "-p")) {
printf ("I: Primary active, waiting for backup (passive)\n");
zsocket_bind (frontend, "tcp://*:5001");
zsocket_bind (statepub, "tcp://*:5003");
zsocket_connect (statesub, "tcp://localhost:5004");
fsm.state = STATE_PRIMARY;
}
else if (argc == 2 && streq (argv [1], "-b")) {
printf ("I: Backup passive, waiting for primary (active)\n");
zsocket_bind (frontend, "tcp://*:5002");
zsocket_bind (statepub, "tcp://*:5004");
zsocket_connect (statesub, "tcp://localhost:5003");
fsm.state = STATE_BACKUP;
}
else {
printf ("Usage: bstarsrv { -p | -b }\n");
zctx_destroy (&ctx);
exit (0);
}
// .split handling socket input
// We now process events on our two input sockets, and process these
// events one at a time via our finite-state machine. Our "work" for
// a client request is simply to echo it back:
// Set timer for next outgoing state message
int64_t send_state_at = zclock_time () + HEARTBEAT;
while (!zctx_interrupted) {
zmq_pollitem_t items [] = {
{ frontend, 0, ZMQ_POLLIN, 0 },
{ statesub, 0, ZMQ_POLLIN, 0 }
};
int time_left = (int) ((send_state_at - zclock_time ()));
if (time_left < 0)
time_left = 0;
int rc = zmq_poll (items, 2, time_left * ZMQ_POLL_MSEC);
if (rc == -1)
break; // Context has been shut down
if (items [0].revents & ZMQ_POLLIN) {
// Have a client request
zmsg_t *msg = zmsg_recv (frontend);
fsm.event = CLIENT_REQUEST;
if (s_state_machine (&fsm) == false)
// Answer client by echoing request back
zmsg_send (&msg, frontend);
else
zmsg_destroy (&msg);
}
if (items [1].revents & ZMQ_POLLIN) {
// Have state from our peer, execute as event
char *message = zstr_recv (statesub);
fsm.event = atoi (message);
free (message);
if (s_state_machine (&fsm))
break; // Error, so exit
fsm.peer_expiry = zclock_time () + 2 * HEARTBEAT;
}
// If we timed out, send state to peer
if (zclock_time () >= send_state_at) {
char message [2];
sprintf (message, "%d", fsm.state);
zstr_send (statepub, message);
send_state_at = zclock_time () + HEARTBEAT;
}
}
if (zctx_interrupted)
printf ("W: interrupted\n");
// Shutdown sockets and context
zctx_destroy (&ctx);
return 0;
}
示例12: zyre_event_test
void
zyre_event_test (bool verbose)
{
printf (" * zyre_event: ");
// @selftest
// Create two nodes
zyre_t *node1 = zyre_new ("node1");
assert (node1);
zyre_set_header (node1, "X-HELLO", "World");
int rc = zyre_set_endpoint (node1, "inproc://zyre-node1");
assert (rc == 0);
// use gossiping instead of beaconing, suits Travis better
zyre_gossip_bind (node1, "inproc://gossip-hub");
if (verbose)
zyre_set_verbose (node1);
if (zyre_start (node1)) {
zyre_destroy (&node1);
printf ("OK (skipping test, no UDP discovery)\n");
return;
}
zyre_join (node1, "GLOBAL");
zyre_t *node2 = zyre_new ("node2");
assert (node2);
if (verbose)
zyre_set_verbose (node2);
rc = zyre_set_endpoint (node2, "inproc://zyre-node2");
assert (rc == 0);
// use gossiping instead of beaconing, suits Travis better
zyre_gossip_connect (node2, "inproc://gossip-hub");
rc = zyre_start (node2);
assert (rc == 0);
zyre_join (node2, "GLOBAL");
// Give time for them to interconnect
zclock_sleep (250);
// One node shouts to GLOBAL
zmsg_t *msg = zmsg_new ();
zmsg_addstr (msg, "Hello, World");
zyre_shout (node1, "GLOBAL", &msg);
zclock_sleep (100);
// Parse ENTER
zyre_event_t *event = zyre_event_new (node2);
assert (streq (zyre_event_type (event), "ENTER"));
const char *sender = zyre_event_peer_uuid (event);
assert (sender);
const char *name = zyre_event_peer_name (event);
assert (name);
assert (streq (name, "node1"));
const char *address = zyre_event_peer_addr (event);
assert (address);
const char *header = zyre_event_header (event, "X-HELLO");
assert (header);
zyre_event_destroy (&event);
// Parse JOIN
// We tolerate other events, which we can get if there are instances
// of Zyre running somewhere on the network.
event = zyre_event_new (node2);
if (streq (zyre_event_type (event), "JOIN")) {
// Parse SHOUT
zyre_event_destroy (&event);
event = zyre_event_new (node2);
if (streq (zyre_event_type (event), "SHOUT")) {
assert (streq (zyre_event_group (event), "GLOBAL"));
zmsg_t *msg = zyre_event_get_msg (event);
char *string = zmsg_popstr (msg);
zmsg_destroy (&msg);
assert (streq (string, "Hello, World"));
free (string);
}
zyre_event_destroy (&event);
}
zyre_destroy (&node1);
zyre_destroy (&node2);
// @end
printf ("OK\n");
}
示例13: mdp_worker_recv
zmsg_t *
mdp_worker_recv (mdp_worker_t *self, zframe_t **reply_to_p)
{
while (TRUE) {
zmq_pollitem_t items [] = {
{ self->worker, 0, ZMQ_POLLIN, 0 } };
int rc = zmq_poll (items, 1, self->heartbeat * ZMQ_POLL_MSEC);
if (rc == -1)
break; // Interrupted
if (items [0].revents & ZMQ_POLLIN) {
zmsg_t *msg = zmsg_recv (self->worker);
if (!msg)
break; // Interrupted
if (self->verbose) {
zclock_log ("I: received message from broker:");
zmsg_dump (msg);
}
self->liveness = HEARTBEAT_LIVENESS;
// Don't try to handle errors, just assert noisily
assert (zmsg_size (msg) >= 3);
zframe_t *empty = zmsg_pop (msg);
assert (zframe_streq (empty, ""));
zframe_destroy (&empty);
zframe_t *header = zmsg_pop (msg);
assert (zframe_streq (header, MDPW_WORKER));
zframe_destroy (&header);
zframe_t *command = zmsg_pop (msg);
if (zframe_streq (command, MDPW_REQUEST)) {
// We should pop and save as many addresses as there are
// up to a null part, but for now, just save one...
zframe_t *reply_to = zmsg_unwrap (msg);
if (reply_to_p)
*reply_to_p = reply_to;
else
zframe_destroy (&reply_to);
zframe_destroy (&command);
// Here is where we actually have a message to process; we
// return it to the caller application
return msg; // We have a request to process
}
else
if (zframe_streq (command, MDPW_HEARTBEAT))
; // Do nothing for heartbeats
else
if (zframe_streq (command, MDPW_DISCONNECT))
s_mdp_worker_connect_to_broker (self);
else {
zclock_log ("E: invalid input message");
zmsg_dump (msg);
}
zframe_destroy (&command);
zmsg_destroy (&msg);
}
else
if (--self->liveness == 0) {
if (self->verbose)
zclock_log ("W: disconnected from broker - retrying...");
zclock_sleep (self->reconnect);
s_mdp_worker_connect_to_broker (self);
}
// Send HEARTBEAT if it's time
if (zclock_time () > self->heartbeat_at) {
s_mdp_worker_send_to_broker (self, MDPW_HEARTBEAT, NULL, NULL);
self->heartbeat_at = zclock_time () + self->heartbeat;
}
}
if (zctx_interrupted)
printf ("W: interrupt received, killing worker...\n");
return NULL;
}
示例14: zmsg_test
void
zmsg_test (bool verbose)
{
printf (" * zmsg: ");
int rc = 0;
// @selftest
// Create two PAIR sockets and connect over inproc
zsock_t *output = zsock_new_pair ("@inproc://zmsg.test");
assert (output);
zsock_t *input = zsock_new_pair (">inproc://zmsg.test");
assert (input);
// Test send and receive of single-frame message
zmsg_t *msg = zmsg_new ();
assert (msg);
zframe_t *frame = zframe_new ("Hello", 5);
assert (frame);
zmsg_prepend (msg, &frame);
assert (zmsg_size (msg) == 1);
assert (zmsg_content_size (msg) == 5);
rc = zmsg_send (&msg, output);
assert (msg == NULL);
assert (rc == 0);
msg = zmsg_recv (input);
assert (msg);
assert (zmsg_size (msg) == 1);
assert (zmsg_content_size (msg) == 5);
zmsg_destroy (&msg);
// Test send and receive of multi-frame message
msg = zmsg_new ();
assert (msg);
rc = zmsg_addmem (msg, "Frame0", 6);
assert (rc == 0);
rc = zmsg_addmem (msg, "Frame1", 6);
assert (rc == 0);
rc = zmsg_addmem (msg, "Frame2", 6);
assert (rc == 0);
rc = zmsg_addmem (msg, "Frame3", 6);
assert (rc == 0);
rc = zmsg_addmem (msg, "Frame4", 6);
assert (rc == 0);
rc = zmsg_addmem (msg, "Frame5", 6);
assert (rc == 0);
rc = zmsg_addmem (msg, "Frame6", 6);
assert (rc == 0);
rc = zmsg_addmem (msg, "Frame7", 6);
assert (rc == 0);
rc = zmsg_addmem (msg, "Frame8", 6);
assert (rc == 0);
rc = zmsg_addmem (msg, "Frame9", 6);
assert (rc == 0);
zmsg_t *copy = zmsg_dup (msg);
assert (copy);
rc = zmsg_send (©, output);
assert (rc == 0);
rc = zmsg_send (&msg, output);
assert (rc == 0);
copy = zmsg_recv (input);
assert (copy);
assert (zmsg_size (copy) == 10);
assert (zmsg_content_size (copy) == 60);
zmsg_destroy (©);
msg = zmsg_recv (input);
assert (msg);
assert (zmsg_size (msg) == 10);
assert (zmsg_content_size (msg) == 60);
// create empty file for null test
FILE *file = fopen ("zmsg.test", "w");
assert (file);
fclose (file);
file = fopen ("zmsg.test", "r");
zmsg_t *null_msg = zmsg_load (NULL, file);
assert (null_msg == NULL);
fclose (file);
remove ("zmsg.test");
// Save to a file, read back
file = fopen ("zmsg.test", "w");
assert (file);
rc = zmsg_save (msg, file);
assert (rc == 0);
fclose (file);
file = fopen ("zmsg.test", "r");
rc = zmsg_save (msg, file);
assert (rc == -1);
fclose (file);
zmsg_destroy (&msg);
file = fopen ("zmsg.test", "r");
msg = zmsg_load (NULL, file);
assert (msg);
fclose (file);
//.........这里部分代码省略.........
示例15: main
int main (int argc, char *argv [])
{
bool verbose;
if (argc == 2 && streq (argv [1], "-v")) {
argc--;
verbose = true;
}
else
verbose = false;
// Do normal checks if run without arguments
if (argc < 2) {
printf ("Running self tests...\n");
fmq_patch_test (verbose);
fmq_dir_test (verbose);
fmq_msg_test (verbose);
fmq_sasl_test (verbose);
fmq_hash_test (verbose);
fmq_server_test (verbose);
fmq_client_test (verbose);
printf ("Tests passed OK\n");
return 0;
}
// Else run as FILEMQ server or client
if (streq (argv [1], "-s")) {
fmq_server_t *server = fmq_server_new ();
fmq_server_configure (server, "server_test.cfg");
fmq_server_publish (server, "./fmqroot/send", "/");
fmq_server_publish (server, "./fmqroot/logs", "/logs");
// We do this last
fmq_server_bind (server, "tcp://*:5670");
while (!zctx_interrupted)
zclock_sleep (1000);
fmq_server_destroy (&server);
}
else
if (streq (argv [1], "-c")) {
fmq_client_t *client = fmq_client_new ();
fmq_client_configure (client, "client_test.cfg");
fmq_client_setoption (client, "client/inbox", "./fmqroot/recv");
fmq_client_set_resync (client, true);
fmq_client_subscribe (client, "/photos");
fmq_client_subscribe (client, "/logs");
fmq_client_connect (client, "tcp://localhost:5670");
while (true) {
// Get message from fmq_client API
zmsg_t *msg = fmq_client_recv (client);
if (!msg)
break; // Interrupted
char *command = zmsg_popstr (msg);
if (streq (command, "DELIVER")) {
char *filename = zmsg_popstr (msg);
char *fullname = zmsg_popstr (msg);
printf ("I: received %s (%s)\n", filename, fullname);
free (filename);
free (fullname);
}
free (command);
zmsg_destroy (&msg);
}
fmq_client_destroy (&client);
}
return 0;
}