本文整理汇总了C++中zframe_destroy函数的典型用法代码示例。如果您正苦于以下问题:C++ zframe_destroy函数的具体用法?C++ zframe_destroy怎么用?C++ zframe_destroy使用的例子?那么, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了zframe_destroy函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: web_request
void web_request(void *sweb, req_store_t * req_store, void *spss, void *sgraph)
{
zmsg_t *msg = zmsg_recv(sweb);
zframe_t *address = zmsg_unwrap(msg);
json_t *req_json;
json_error_t error;
printf("\nbroker:sweb received: %s\n",
(const char *)zframe_data(zmsg_first(msg)));
const char *data;
size_t data_size = zframe_size(zmsg_first(msg));
data = zframe_data(zmsg_first(msg));
req_json = json_loadb(data, data_size, 0, &error);
zmsg_destroy(&msg);
int32_t requestId = request_store_add(req_store, address, req_json);
json_t *clientRequest = json_object_get(req_json, "clientRequest");
json_t *request = json_object_get(clientRequest, "request");
const char *type = json_string_value(json_object_get(request, "type"));
if (strcmp(type, "searchRequest") == 0)
web_request_searchRequest(requestId, request, spss);
else if (strcmp(type, "newNode") == 0)
web_request_newNode(requestId, request, sgraph);
else if (strcmp(type, "newPosition") == 0)
web_request_newPosition(requestId, request, spss);
else if (strcmp(type, "newLink") == 0)
web_request_newLink(requestId, request, sgraph);
else if (strcmp(type, "delLink") == 0)
web_request_delLink(requestId, request, sgraph);
else if (strcmp(type, "delNode") == 0)
web_request_delNode(requestId, request, sgraph);
else if (strcmp(type, "newNodeData") == 0)
web_request_newNodeData(requestId, request, sgraph);
else if (strcmp(type, "newLinkData") == 0)
web_request_newLinkData(requestId, request, sgraph);
else {
//TODO process request
//malformed request
printf("\ni received a malformed request : %s", type);
//delete request
zframe_destroy(&address);
request_store_delete(req_store, requestId);
}
}
示例2: client_task
static void *
client_task (void *args)
{
bool verbose = *((bool *) args);
char filename [256];
snprintf (filename, 255, TESTDIR "/client-%07d.cert", randof (10000000));
zcert_t *client_cert = zcert_new ();
zcert_save_public (client_cert, filename);
curve_client_t *client = curve_client_new (&client_cert);
curve_client_set_verbose (client, verbose);
zcert_t *server_cert = zcert_load (TESTDIR "/server.cert");
assert (server_cert);
curve_client_connect (client, "tcp://127.0.0.1:9006", zcert_public_key (server_cert));
zcert_destroy (&server_cert);
curve_client_sendstr (client, "Hello, World");
char *reply = curve_client_recvstr (client);
assert (streq (reply, "Hello, World"));
free (reply);
// Try a multipart message
zmsg_t *msg = zmsg_new ();
zmsg_addstr (msg, "Hello, World");
zmsg_addstr (msg, "Second frame");
curve_client_send (client, &msg);
msg = curve_client_recv (client);
assert (zmsg_size (msg) == 2);
zmsg_destroy (&msg);
// Now send messages of increasing size, check they work
int count;
int size = 0;
for (count = 0; count < 18; count++) {
zframe_t *data = zframe_new (NULL, size);
int byte_nbr;
// Set data to sequence 0...255 repeated
for (byte_nbr = 0; byte_nbr < size; byte_nbr++)
zframe_data (data)[byte_nbr] = (byte) byte_nbr;
msg = zmsg_new ();
zmsg_prepend (msg, &data);
curve_client_send (client, &msg);
msg = curve_client_recv (client);
data = zmsg_pop (msg);
assert (data);
assert (zframe_size (data) == size);
for (byte_nbr = 0; byte_nbr < size; byte_nbr++) {
assert (zframe_data (data)[byte_nbr] == (byte) byte_nbr);
}
zframe_destroy (&data);
zmsg_destroy (&msg);
size = size * 2 + 1;
}
// Signal end of test
curve_client_sendstr (client, "END");
reply = curve_client_recvstr (client);
free (reply);
curve_client_destroy (&client);
return NULL;
}
示例3: zhash_test
void
zhash_test (bool verbose)
{
printf (" * zhash: ");
// @selftest
zhash_t *hash = zhash_new ();
assert (hash);
assert (zhash_size (hash) == 0);
assert (zhash_first (hash) == NULL);
assert (zhash_cursor (hash) == NULL);
// Insert some items
int rc;
rc = zhash_insert (hash, "DEADBEEF", "dead beef");
char *item = (char *) zhash_first (hash);
assert (streq (zhash_cursor (hash), "DEADBEEF"));
assert (streq (item, "dead beef"));
assert (rc == 0);
rc = zhash_insert (hash, "ABADCAFE", "a bad cafe");
assert (rc == 0);
rc = zhash_insert (hash, "C0DEDBAD", "coded bad");
assert (rc == 0);
rc = zhash_insert (hash, "DEADF00D", "dead food");
assert (rc == 0);
assert (zhash_size (hash) == 4);
// Look for existing items
item = (char *) zhash_lookup (hash, "DEADBEEF");
assert (streq (item, "dead beef"));
item = (char *) zhash_lookup (hash, "ABADCAFE");
assert (streq (item, "a bad cafe"));
item = (char *) zhash_lookup (hash, "C0DEDBAD");
assert (streq (item, "coded bad"));
item = (char *) zhash_lookup (hash, "DEADF00D");
assert (streq (item, "dead food"));
// Look for non-existent items
item = (char *) zhash_lookup (hash, "foo");
assert (item == NULL);
// Try to insert duplicate items
rc = zhash_insert (hash, "DEADBEEF", "foo");
assert (rc == -1);
item = (char *) zhash_lookup (hash, "DEADBEEF");
assert (streq (item, "dead beef"));
// Some rename tests
// Valid rename, key is now LIVEBEEF
rc = zhash_rename (hash, "DEADBEEF", "LIVEBEEF");
assert (rc == 0);
item = (char *) zhash_lookup (hash, "LIVEBEEF");
assert (streq (item, "dead beef"));
// Trying to rename an unknown item to a non-existent key
rc = zhash_rename (hash, "WHATBEEF", "NONESUCH");
assert (rc == -1);
// Trying to rename an unknown item to an existing key
rc = zhash_rename (hash, "WHATBEEF", "LIVEBEEF");
assert (rc == -1);
item = (char *) zhash_lookup (hash, "LIVEBEEF");
assert (streq (item, "dead beef"));
// Trying to rename an existing item to another existing item
rc = zhash_rename (hash, "LIVEBEEF", "ABADCAFE");
assert (rc == -1);
item = (char *) zhash_lookup (hash, "LIVEBEEF");
assert (streq (item, "dead beef"));
item = (char *) zhash_lookup (hash, "ABADCAFE");
assert (streq (item, "a bad cafe"));
// Test keys method
zlist_t *keys = zhash_keys (hash);
assert (zlist_size (keys) == 4);
zlist_destroy (&keys);
// Test dup method
zhash_t *copy = zhash_dup (hash);
assert (zhash_size (copy) == 4);
item = (char *) zhash_lookup (copy, "LIVEBEEF");
assert (item);
assert (streq (item, "dead beef"));
zhash_destroy (©);
// Test pack/unpack methods
zframe_t *frame = zhash_pack (hash);
copy = zhash_unpack (frame);
zframe_destroy (&frame);
assert (zhash_size (copy) == 4);
item = (char *) zhash_lookup (copy, "LIVEBEEF");
assert (item);
assert (streq (item, "dead beef"));
zhash_destroy (©);
// Test save and load
zhash_comment (hash, "This is a test file");
zhash_comment (hash, "Created by %s", "czmq_selftest");
zhash_save (hash, ".cache");
//.........这里部分代码省略.........
示例4: zsocket_test
void
zsocket_test (bool verbose)
{
printf (" * zsocket (deprecated): ");
// @selftest
zctx_t *ctx = zctx_new ();
assert (ctx);
// Create a detached thread, let it run
char *interf = "127.0.0.1";
char *domain = "localhost";
int service = 5560;
void *writer = zsocket_new (ctx, ZMQ_PUSH);
assert (writer);
void *reader = zsocket_new (ctx, ZMQ_PULL);
assert (reader);
assert (streq (zsocket_type_str (writer), "PUSH"));
assert (streq (zsocket_type_str (reader), "PULL"));
int rc = zsocket_bind (writer, "tcp://%s:%d", interf, service);
assert (rc == service);
#if (ZMQ_VERSION >= ZMQ_MAKE_VERSION (3, 2, 0))
// Check unbind
rc = zsocket_unbind (writer, "tcp://%s:%d", interf, service);
assert (rc == 0);
// In some cases and especially when running under Valgrind, doing
// a bind immediately after an unbind causes an EADDRINUSE error.
// Even a short sleep allows the OS to release the port for reuse.
zclock_sleep (100);
// Bind again
rc = zsocket_bind (writer, "tcp://%s:%d", interf, service);
assert (rc == service);
#endif
rc = zsocket_connect (reader, "tcp://%s:%d", domain, service);
assert (rc == 0);
zstr_send (writer, "HELLO");
char *message = zstr_recv (reader);
assert (message);
assert (streq (message, "HELLO"));
free (message);
// Test binding to ports
int port = zsocket_bind (writer, "tcp://%s:*", interf);
assert (port >= ZSOCKET_DYNFROM && port <= ZSOCKET_DYNTO);
assert (zsocket_poll (writer, 100) == false);
// Test error state when connecting to an invalid socket type
// ('txp://' instead of 'tcp://', typo intentional)
rc = zsocket_connect (reader, "txp://%s:%d", domain, service);
assert (rc == -1);
// Test sending frames to socket
rc = zsocket_sendmem (writer, "ABC", 3, ZFRAME_MORE);
assert (rc == 0);
rc = zsocket_sendmem (writer, "DEFG", 4, 0);
assert (rc == 0);
zframe_t *frame = zframe_recv (reader);
assert (frame);
assert (zframe_streq (frame, "ABC"));
assert (zframe_more (frame));
zframe_destroy (&frame);
frame = zframe_recv (reader);
assert (frame);
assert (zframe_streq (frame, "DEFG"));
assert (!zframe_more (frame));
zframe_destroy (&frame);
rc = zsocket_signal (writer);
assert (rc == 0);
rc = zsocket_wait (reader);
assert (rc == 0);
zsocket_destroy (ctx, reader);
zsocket_destroy (ctx, writer);
zctx_destroy (&ctx);
// @end
printf ("OK\n");
}
示例5: zframe_destroy
///
// Destroy a frame
void QmlZframeAttached::destruct (QmlZframe *qmlSelf) {
zframe_destroy (&qmlSelf->self);
};
示例6: zgossip_msg_decode
zgossip_msg_t *
zgossip_msg_decode (zmsg_t **msg_p)
{
assert (msg_p);
zmsg_t *msg = *msg_p;
if (msg == NULL)
return NULL;
zgossip_msg_t *self = zgossip_msg_new (0);
// Read and parse command in frame
zframe_t *frame = zmsg_pop (msg);
if (!frame)
goto empty; // Malformed or empty
// Get and check protocol signature
self->needle = zframe_data (frame);
self->ceiling = self->needle + zframe_size (frame);
uint16_t signature;
GET_NUMBER2 (signature);
if (signature != (0xAAA0 | 0))
goto empty; // Invalid signature
// Get message id and parse per message type
GET_NUMBER1 (self->id);
switch (self->id) {
case ZGOSSIP_MSG_HELLO:
GET_NUMBER1 (self->version);
if (self->version != 1)
goto malformed;
break;
case ZGOSSIP_MSG_PUBLISH:
GET_NUMBER1 (self->version);
if (self->version != 1)
goto malformed;
GET_STRING (self->key);
GET_LONGSTR (self->value);
GET_NUMBER4 (self->ttl);
break;
case ZGOSSIP_MSG_PING:
GET_NUMBER1 (self->version);
if (self->version != 1)
goto malformed;
break;
case ZGOSSIP_MSG_PONG:
GET_NUMBER1 (self->version);
if (self->version != 1)
goto malformed;
break;
case ZGOSSIP_MSG_INVALID:
GET_NUMBER1 (self->version);
if (self->version != 1)
goto malformed;
break;
default:
goto malformed;
}
// Successful return
zframe_destroy (&frame);
zmsg_destroy (msg_p);
return self;
// Error returns
malformed:
zsys_error ("malformed message '%d'\n", self->id);
empty:
zframe_destroy (&frame);
zmsg_destroy (msg_p);
zgossip_msg_destroy (&self);
return (NULL);
}
示例7: s_self_handle_sink
static void
s_self_handle_sink (self_t *self)
{
#if defined (ZMQ_EVENT_ALL)
#if (ZMQ_VERSION_MAJOR == 4)
// First frame is event number and value
zframe_t *frame = zframe_recv (self->sink);
int event = *(uint16_t *) (zframe_data (frame));
int value = *(uint32_t *) (zframe_data (frame) + 2);
// Address is in second message frame
char *address = zstr_recv (self->sink);
zframe_destroy (&frame);
#elif (ZMQ_VERSION_MAJOR == 3 && ZMQ_VERSION_MINOR == 2)
// zmq_event_t is passed as-is in the frame
zframe_t *frame = zframe_recv (self->sink);
zmq_event_t *eptr = (zmq_event_t *) zframe_data (frame);
int event = eptr->event;
int value = eptr->data.listening.fd;
char *address = strdup (eptr->data.listening.addr);
zframe_destroy (&frame);
#else
// We can't plausibly be here with other versions of libzmq
assert (false);
#endif
// Now map event to text equivalent
char *name;
switch (event) {
case ZMQ_EVENT_ACCEPTED:
name = "ACCEPTED";
break;
case ZMQ_EVENT_ACCEPT_FAILED:
name = "ACCEPT_FAILED";
break;
case ZMQ_EVENT_BIND_FAILED:
name = "BIND_FAILED";
break;
case ZMQ_EVENT_CLOSED:
name = "CLOSED";
break;
case ZMQ_EVENT_CLOSE_FAILED:
name = "CLOSE_FAILED";
break;
case ZMQ_EVENT_DISCONNECTED:
name = "DISCONNECTED";
break;
case ZMQ_EVENT_CONNECTED:
name = "CONNECTED";
break;
case ZMQ_EVENT_CONNECT_DELAYED:
name = "CONNECT_DELAYED";
break;
case ZMQ_EVENT_CONNECT_RETRIED:
name = "CONNECT_RETRIED";
break;
case ZMQ_EVENT_LISTENING:
name = "LISTENING";
break;
#if (ZMQ_VERSION_MAJOR == 4)
case ZMQ_EVENT_MONITOR_STOPPED:
name = "MONITOR_STOPPED";
break;
#endif
default:
zsys_error ("illegal socket monitor event: %d", event);
name = "UNKNOWN";
break;
}
if (self->verbose)
zsys_info ("zmonitor: %s - %s", name, address);
zstr_sendfm (self->pipe, "%s", name);
zstr_sendfm (self->pipe, "%d", value);
zstr_send (self->pipe, address);
free (address);
#endif
}
示例8: mdp_client_msg_recv
int
mdp_client_msg_recv (mdp_client_msg_t *self, zsock_t *input)
{
assert (input);
if (zsock_type (input) == ZMQ_ROUTER) {
zframe_destroy (&self->routing_id);
self->routing_id = zframe_recv (input);
if (!self->routing_id || !zsock_rcvmore (input)) {
zsys_warning ("mdp_client_msg: no routing ID");
return -1; // Interrupted or malformed
}
}
zmq_msg_t frame;
zmq_msg_init (&frame);
int size = zmq_msg_recv (&frame, zsock_resolve (input), 0);
if (size == -1) {
zsys_warning ("mdp_client_msg: interrupted");
goto malformed; // Interrupted
}
// Get and check protocol signature
self->needle = (byte *) zmq_msg_data (&frame);
self->ceiling = self->needle + zmq_msg_size (&frame);
uint16_t signature;
GET_NUMBER2 (signature);
if (signature != (0xAAA0 | 4)) {
zsys_warning ("mdp_client_msg: invalid signature");
// TODO: discard invalid messages and loop, and return
// -1 only on interrupt
goto malformed; // Interrupted
}
// Get message id and parse per message type
GET_NUMBER1 (self->id);
switch (self->id) {
case MDP_CLIENT_MSG_CLIENT_REQUEST:
{
char version [256];
GET_STRING (version);
if (strneq (version, "MDPC02")) {
zsys_warning ("mdp_client_msg: version is invalid");
goto malformed;
}
}
{
byte messageid;
GET_NUMBER1 (messageid);
if (messageid != 1) {
zsys_warning ("mdp_client_msg: messageid is invalid");
goto malformed;
}
}
GET_STRING (self->service);
// Get zero or more remaining frames
zmsg_destroy (&self->body);
if (zsock_rcvmore (input))
self->body = zmsg_recv (input);
else
self->body = zmsg_new ();
break;
case MDP_CLIENT_MSG_CLIENT_PARTIAL:
{
char version [256];
GET_STRING (version);
if (strneq (version, "MDPC02")) {
zsys_warning ("mdp_client_msg: version is invalid");
goto malformed;
}
}
{
byte messageid;
GET_NUMBER1 (messageid);
if (messageid != 2) {
zsys_warning ("mdp_client_msg: messageid is invalid");
goto malformed;
}
}
GET_STRING (self->service);
// Get zero or more remaining frames
zmsg_destroy (&self->body);
if (zsock_rcvmore (input))
self->body = zmsg_recv (input);
else
self->body = zmsg_new ();
break;
case MDP_CLIENT_MSG_CLIENT_FINAL:
{
char version [256];
GET_STRING (version);
if (strneq (version, "MDPC02")) {
zsys_warning ("mdp_client_msg: version is invalid");
goto malformed;
}
}
{
byte messageid;
GET_NUMBER1 (messageid);
//.........这里部分代码省略.........
示例9: main
int main (void)
{
zctx_t *ctx = zctx_new ();
void *frontend = zsocket_new (ctx, ZMQ_ROUTER);
void *backend = zsocket_new (ctx, ZMQ_ROUTER);
zsocket_bind (frontend, "ipc://frontend.ipc");
zsocket_bind (backend, "ipc://backend.ipc");
int client_nbr;
for (client_nbr = 0; client_nbr < NBR_CLIENTS; client_nbr++)
zthread_new (client_task, NULL);
int worker_nbr;
for (worker_nbr = 0; worker_nbr < NBR_WORKERS; worker_nbr++)
zthread_new (worker_task, NULL);
// Queue of available workers
zlist_t *workers = zlist_new ();
// Here is the main loop for the load-balancer. It works the same way
// as the previous example, but is a lot shorter because CZMQ gives
// us an API that does more with fewer calls:
while (true) {
zmq_pollitem_t items [] = {
{ backend, 0, ZMQ_POLLIN, 0 },
{ frontend, 0, ZMQ_POLLIN, 0 }
};
// Poll frontend only if we have available workers
int rc = zmq_poll (items, zlist_size (workers)? 2: 1, -1);
if (rc == -1)
break; // Interrupted
// Handle worker activity on backend
if (items [0].revents & ZMQ_POLLIN) {
// Use worker identity for load-balancing
zmsg_t *msg = zmsg_recv (backend);
if (!msg)
break; // Interrupted
zframe_t *identity = zmsg_unwrap (msg);
zlist_append (workers, identity);
// Forward message to client if it's not a READY
zframe_t *frame = zmsg_first (msg);
if (memcmp (zframe_data (frame), WORKER_READY, 1) == 0)
zmsg_destroy (&msg);
else
zmsg_send (&msg, frontend);
}
if (items [1].revents & ZMQ_POLLIN) {
// Get client request, route to first available worker
zmsg_t *msg = zmsg_recv (frontend);
if (msg) {
zmsg_wrap (msg, (zframe_t *) zlist_pop (workers));
zmsg_send (&msg, backend);
}
}
}
// When we're done, clean up properly
while (zlist_size (workers)) {
zframe_t *frame = (zframe_t *) zlist_pop (workers);
zframe_destroy (&frame);
}
zlist_destroy (&workers);
zctx_destroy (&ctx);
return 0;
}
示例10: zyre_test
//.........这里部分代码省略.........
assert (zlist_size (peer_groups) == 2);
zlist_destroy (&peer_groups);
char *value = zyre_peer_header_value (node2, zyre_uuid (node1), "X-HELLO");
assert (streq (value, "World"));
zstr_free (&value);
// One node shouts to GLOBAL
zyre_shouts (node1, "GLOBAL", "Hello, World");
// Second node should receive ENTER, JOIN, and SHOUT
zmsg_t *msg = zyre_recv (node2);
assert (msg);
char *command = zmsg_popstr (msg);
assert (streq (command, "ENTER"));
zstr_free (&command);
assert (zmsg_size (msg) == 4);
char *peerid = zmsg_popstr (msg);
char *name = zmsg_popstr (msg);
assert (streq (name, "node1"));
zstr_free (&name);
zframe_t *headers_packed = zmsg_pop (msg);
char *address = zmsg_popstr (msg);
char *endpoint = zyre_peer_address (node2, peerid);
assert (streq (address, endpoint));
zstr_free (&peerid);
zstr_free (&endpoint);
zstr_free (&address);
assert (headers_packed);
zhash_t *headers = zhash_unpack (headers_packed);
assert (headers);
zframe_destroy (&headers_packed);
assert (streq ((char *) zhash_lookup (headers, "X-HELLO"), "World"));
zhash_destroy (&headers);
zmsg_destroy (&msg);
msg = zyre_recv (node2);
assert (msg);
command = zmsg_popstr (msg);
assert (streq (command, "JOIN"));
zstr_free (&command);
assert (zmsg_size (msg) == 3);
zmsg_destroy (&msg);
msg = zyre_recv (node2);
assert (msg);
command = zmsg_popstr (msg);
assert (streq (command, "JOIN"));
zstr_free (&command);
assert (zmsg_size (msg) == 3);
zmsg_destroy (&msg);
msg = zyre_recv (node2);
assert (msg);
command = zmsg_popstr (msg);
assert (streq (command, "SHOUT"));
zstr_free (&command);
zmsg_destroy (&msg);
zyre_stop (node2);
msg = zyre_recv (node2);
assert (msg);
command = zmsg_popstr (msg);
示例11: s_socket_event
static void
s_socket_event (agent_t *self)
{
zframe_t *frame;
zmq_event_t event;
char *description = "Unknown";
char address [1025];
// Copy event data into event struct
frame = zframe_recv (self->socket);
// Extract id of the event as bitfield
memcpy (&(event.event), zframe_data (frame), sizeof (event.event));
// Extract value which is either error code, fd, or reconnect interval
memcpy (&(event.value), zframe_data (frame) + sizeof (event.event),
sizeof (event.value));
zframe_destroy (&frame);
// Copy address part
frame = zframe_recv (self->socket);
memcpy (address, zframe_data (frame), zframe_size (frame));
address [zframe_size (frame)] = 0; // Terminate address string
zframe_destroy (&frame);
switch (event.event) {
case ZMQ_EVENT_ACCEPTED:
description = "Accepted";
break;
case ZMQ_EVENT_ACCEPT_FAILED:
description = "Accept failed";
break;
case ZMQ_EVENT_BIND_FAILED:
description = "Bind failed";
break;
case ZMQ_EVENT_CLOSED:
description = "Closed";
break;
case ZMQ_EVENT_CLOSE_FAILED:
description = "Close failed";
break;
case ZMQ_EVENT_DISCONNECTED:
description = "Disconnected";
break;
case ZMQ_EVENT_CONNECTED:
description = "Connected";
break;
case ZMQ_EVENT_CONNECT_DELAYED:
description = "Connect delayed";
break;
case ZMQ_EVENT_CONNECT_RETRIED:
description = "Connect retried";
break;
case ZMQ_EVENT_LISTENING:
description = "Listening";
break;
case ZMQ_EVENT_MONITOR_STOPPED:
description = "Monitor stopped";
break;
default:
if (self->verbose)
printf ("Unknown socket monitor event: %d", event.event);
break;
}
if (self->verbose)
printf ("I: zmonitor: %s - %s\n", description, address);
zmsg_t *msg = zmsg_new();
zmsg_addstrf (msg, "%d", (int) event.event);
zmsg_addstrf (msg, "%d", (int) event.value);
zmsg_addstrf (msg, "%s", address);
zmsg_addstrf (msg, "%s", description);
zmsg_send (&msg, self->pipe);
}
示例12: zbeacon_test
void
zbeacon_test (bool verbose)
{
printf (" * zbeacon: ");
if (verbose)
printf ("\n");
// @selftest
// Test 1 - two beacons, one speaking, one listening
// Create speaker beacon to broadcast our service
zactor_t *speaker = zactor_new (zbeacon, NULL);
assert (speaker);
if (verbose)
zstr_sendx (speaker, "VERBOSE", NULL);
zsock_send (speaker, "si", "CONFIGURE", 9999);
char *hostname = zstr_recv (speaker);
if (!*hostname) {
printf ("OK (skipping test, no UDP broadcasting)\n");
zactor_destroy (&speaker);
free (hostname);
return;
}
free (hostname);
// Create listener beacon on port 9999 to lookup service
zactor_t *listener = zactor_new (zbeacon, NULL);
assert (listener);
if (verbose)
zstr_sendx (listener, "VERBOSE", NULL);
zsock_send (listener, "si", "CONFIGURE", 9999);
hostname = zstr_recv (listener);
assert (*hostname);
free (hostname);
// We will broadcast the magic value 0xCAFE
byte announcement [2] = { 0xCA, 0xFE };
zsock_send (speaker, "sbi", "PUBLISH", announcement, 2, 100);
// We will listen to anything (empty subscription)
zsock_send (listener, "sb", "SUBSCRIBE", "", 0);
// Wait for at most 1/2 second if there's no broadcasting
zsock_set_rcvtimeo (listener, 500);
char *ipaddress = zstr_recv (listener);
if (ipaddress) {
zframe_t *content = zframe_recv (listener);
assert (zframe_size (content) == 2);
assert (zframe_data (content) [0] == 0xCA);
assert (zframe_data (content) [1] == 0xFE);
zframe_destroy (&content);
zstr_free (&ipaddress);
zstr_sendx (speaker, "SILENCE", NULL);
}
zactor_destroy (&listener);
zactor_destroy (&speaker);
// Test subscription filter using a 3-node setup
zactor_t *node1 = zactor_new (zbeacon, NULL);
assert (node1);
zsock_send (node1, "si", "CONFIGURE", 5670);
hostname = zstr_recv (node1);
assert (*hostname);
free (hostname);
zactor_t *node2 = zactor_new (zbeacon, NULL);
assert (node2);
zsock_send (node2, "si", "CONFIGURE", 5670);
hostname = zstr_recv (node2);
assert (*hostname);
free (hostname);
zactor_t *node3 = zactor_new (zbeacon, NULL);
assert (node3);
zsock_send (node3, "si", "CONFIGURE", 5670);
hostname = zstr_recv (node3);
assert (*hostname);
free (hostname);
zsock_send (node1, "sbi", "PUBLISH", "NODE/1", 6, 250);
zsock_send (node2, "sbi", "PUBLISH", "NODE/2", 6, 250);
zsock_send (node3, "sbi", "PUBLISH", "RANDOM", 6, 250);
zsock_send (node1, "sb", "SUBSCRIBE", "NODE", 4);
// Poll on three API sockets at once
zpoller_t *poller = zpoller_new (node1, node2, node3, NULL);
assert (poller);
int64_t stop_at = zclock_mono () + 1000;
while (zclock_mono () < stop_at) {
long timeout = (long) (stop_at - zclock_mono ());
if (timeout < 0)
timeout = 0;
void *which = zpoller_wait (poller, timeout * ZMQ_POLL_MSEC);
if (which) {
assert (which == node1);
char *ipaddress, *received;
zstr_recvx (node1, &ipaddress, &received, NULL);
assert (streq (received, "NODE/2"));
zstr_free (&ipaddress);
zstr_free (&received);
}
//.........这里部分代码省略.........
示例13: interface_task
//.........这里部分代码省略.........
} else if (streq (event, "EXIT")) {
peer = zmsg_popstr (incoming);
debugLog ("I: EXIT '%s'", peer);
if(self->callback) {
(*self->callback)(self,
LSD_EVENT_EXIT,
peer,
NULL,
NULL,
0,
self->class_ptr);
}
} else if (streq (event, "WHISPER")) {
peer = zmsg_popstr (incoming);
msg_frame = zmsg_pop (incoming);
debugLog ("I: WHISPER '%s' msglen %d", peer, (int)zframe_size(msg_frame));
if(self->callback) {
(*self->callback)(self,
LSD_EVENT_WHISPER,
peer,
NULL,
(const uint8_t*)zframe_data(msg_frame),
zframe_size(msg_frame),
self->class_ptr);
}
} else if (streq (event, "SHOUT")) {
peer = zmsg_popstr (incoming);
group = zmsg_popstr (incoming);
msg_frame = zmsg_pop (incoming);
debugLog ("I: SHOUT from '%s' group '%s' msglen %d", peer, group, (int)zframe_size(msg_frame));
if(self->callback) {
(*self->callback)(self,
LSD_EVENT_SHOUT,
peer,
group,
zframe_data(msg_frame),
zframe_size(msg_frame),
self->class_ptr);
}
} else if (streq (event, "DELIVER")) {
char *filename = zmsg_popstr (incoming);
char *fullname = zmsg_popstr (incoming);
debugLog ("I: DELIVER file %s", fullname);
if(self->callback) {
(*self->callback)(self,
LSD_EVENT_DELIVER,
NULL,
NULL,
(const uint8_t*)fullname,
strlen(fullname),
self->class_ptr);
}
free (fullname);
free (filename);
}else if (streq (event, "JOIN")) {
peer = zmsg_popstr (incoming);
group = zmsg_popstr (incoming);
debugLog ("I: JOIN '%s - %s'", peer, group);
if(self->callback) {
(*self->callback)(self,
LSD_EVENT_JOIN,
peer,
group,
NULL,
0,
self->class_ptr);
}
} else if (streq (event, "LEAVE")) {
peer = zmsg_popstr (incoming);
group = zmsg_popstr (incoming);
debugLog ("I: LEAVE '%s - %s'", peer, group);
if(self->callback) {
(*self->callback)(self,
LSD_EVENT_LEAVE,
peer,
group,
NULL,
0,
self->class_ptr);
}
}
if(peer) {
free(peer);
peer = NULL;
}
if(group) {
free(group);
group = NULL;
}
if(msg_frame) {
zframe_destroy(&msg_frame);
msg_frame = NULL;
}
free (event);
zmsg_destroy (&incoming);
}
}
}
示例14: graph_response
void graph_response(void *sgraph, req_store_t * req_store, void *sweb,
void *spss)
{
zmsg_t *msg = zmsg_recv(sgraph);
zframe_t *null = zmsg_unwrap(msg);
zframe_destroy(&null);
json_error_t error;
printf("\nbroker:sgraph received: %s\n",
(const char *)zframe_data(zmsg_first(msg)));
const char *data;
size_t data_size = zframe_size(zmsg_first(msg));
data = zframe_data(zmsg_first(msg));
json_t *graph_resp_json = json_loadb(data,
data_size, 0,
&error);
zmsg_destroy(&msg);
//identify the request
int32_t requestId =
json_integer_value(json_object_get(graph_resp_json, "requestId"));
req_t *req = request_store_req(req_store, requestId);
json_t *response = json_object_get(graph_resp_json, "response");
const char *resp_type =
json_string_value(json_object_get(response, "type"));
json_t *request =
json_object_get(json_object_get(req->request, "clientRequest"),
"request");
const char *req_type =
json_string_value(json_object_get(request, "type"));
if ((strcmp(resp_type, "retrieveResponse") == 0)
&& (strcmp(req_type, "searchRequest") == 0))
graph_response_retrieveResponse(req, response,
requestId, sweb, req_store);
else if ((strcmp(resp_type, "newNodeResponse") == 0)
&& (strcmp(req_type, "newNode") == 0))
graph_response_newNodeResponse(request,
response,
requestId, spss, req_store);
else if ((strcmp(resp_type, "newLinkResponse") == 0)
&& (strcmp(req_type, "newLink") == 0))
graph_response_newLinkResponse(req,
request,
response,
requestId, sweb, req_store);
else if ((strcmp(resp_type, "delLinkResponse")
== 0) && (strcmp(req_type, "delLink")
== 0))
graph_response_delLinkResponse(req,
request,
response,
requestId, sweb, req_store);
else if ((strcmp(resp_type, "delNode") == 0)
&& (strcmp(req_type, "delNode")
== 0))
graph_response_delNodeResponse(req,
request,
response,
requestId, spss, req_store);
else if ((strcmp(resp_type, "newNodeData")
== 0) && (strcmp(req_type, "newNodeData")
== 0))
graph_response_newNodeDataResponse(req,
request,
response,
requestId, sweb, req_store);
else if ((strcmp(resp_type, "newLinkData")
== 0) && (strcmp(req_type, "newLinkData")
== 0))
graph_response_newLinkDataResponse(req,
request,
response,
requestId, sweb, req_store);
json_decref(graph_resp_json);
}
示例15: mdcli_send
zmsg_t *
mdcli_send (mdcli_t *self, char *service, zmsg_t **request_p)
{
assert (self);
assert (request_p);
zmsg_t *request = *request_p;
// Prefix request with protocol frames
// Frame 1: "MDPCxy" (six bytes, MDP/Client x.y)
// Frame 2: Service name (printable string)
zmsg_pushstr (request, service);
zmsg_pushstr (request, MDPC_CLIENT);
if (self->verbose) {
zclock_log ("I: send request to '%s' service:", service);
zmsg_dump (request);
}
int retries_left = self->retries;
while (retries_left && !zctx_interrupted) {
zmsg_t *msg = zmsg_dup (request);
zmsg_send (&msg, self->client);
// Poll socket for a reply, with timeout
zmq_pollitem_t items [] = {
{ self->client, 0, ZMQ_POLLIN, 0 } };
int rc = zmq_poll (items, 1, self->timeout * ZMQ_POLL_MSEC);
if (rc == -1)
break; // Interrupted
// If we got a reply, process it
if (items [0].revents & ZMQ_POLLIN) {
zmsg_t *msg = zmsg_recv (self->client);
if (self->verbose) {
zclock_log ("I: received reply:");
zmsg_dump (msg);
}
// Don't try to handle errors, just assert noisily
assert (zmsg_size (msg) >= 3);
zframe_t *header = zmsg_pop (msg);
assert (zframe_streq (header, MDPC_CLIENT));
zframe_destroy (&header);
zframe_t *reply_service = zmsg_pop (msg);
assert (zframe_streq (reply_service, service));
zframe_destroy (&reply_service);
zmsg_destroy (&request);
return msg; // Success
}
else
if (--retries_left) {
if (self->verbose)
zclock_log ("W: no reply, reconnecting...");
// Reconnect socket
s_mdcli_connect_to_broker (self);
}
else {
if (self->verbose)
zclock_log ("W: permanent error, abandoning");
break; // Give up
}
}
if (zctx_interrupted)
printf ("W: interrupt received, killing client...\n");
zmsg_destroy (&request);
return NULL;
}