Skip to content

Commit

Permalink
Problem: no way to access tuple(s) in zgossip
Browse files Browse the repository at this point in the history
Solution: add new DRAFT APIs, KEYS and TUPLE, to respectively dump all
the keys and request the value associated with a key, and test them.
  • Loading branch information
bluca committed Jan 1, 2017
1 parent a57bd33 commit 17528ad
Showing 1 changed file with 65 additions and 0 deletions.
65 changes: 65 additions & 0 deletions src/zgossip.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,17 @@
* PUBLISH key value -- publish a key/value pair to the gossip cluster
* STATUS -- return number of key/value pairs held by gossip service
DRAFT commands:
* KEYS -- return number of keys followed by each key in its own frame
* TUPLE key -- return value or empty string if key not found
Returns these messages:
* PORT number -- reply to PORT command
* STATUS number -- reply to STATUS command
* DELIVER key value -- new tuple delivered from network
* KEYS size key1 ... keyn -- reply to KEYS command
* TUPLE value -- reply to TUPLE command
@discuss
The gossip protocol distributes information around a loosely-connected
network of gossip services. The information consists of name/value pairs
Expand Down Expand Up @@ -294,6 +300,36 @@ server_method (server_t *self, const char *method, zmsg_t *msg)
zmsg_addstr (reply, "STATUS");
zmsg_addstrf (reply, "%d", (int) zhashx_size (self->tuples));
}
#ifdef CZMQ_BUILD_DRAFT_API
else
if (streq (method, "KEYS")) {
// Dump the keys of all stored tuples
reply = zmsg_new ();
assert (reply);
zmsg_addstr (reply, "KEYS");

This comment has been minimized.

Copy link
@sappo

sappo Jan 2, 2017

Looks fine but I would remove the command from the answer. These calls should be synchronous. This is similar to what is done in zyre (https://github.com/zeromq/zyre/blob/master/src/zyre.c#L514)

This comment has been minimized.

Copy link
@bluca

bluca Jan 2, 2017

Author Owner

Good point, I had copied and pasted from the above STATUS command :-)

zlistx_t *keys = zhashx_keys (self->tuples);
zmsg_addstrf (reply, "%d", (int) zlistx_size (keys));
char *key = NULL;
for (key = (char *)zlistx_first (keys); key != NULL;
key = (char *)zlistx_next (keys))
zmsg_addstr (reply, key);
zlistx_destroy (&keys);
}
else
if (streq (method, "TUPLE")) {
// Return the requested value
reply = zmsg_new ();
assert (reply);
zmsg_addstr (reply, "TUPLE");
char *key = zmsg_popstr (msg);
char *value = (char *)zhashx_lookup (self->tuples, key);
if (value)
zmsg_addstr (reply, value);
else
zmsg_addstr (reply, "");
zstr_free (&key);
}
#endif
else
zsys_error ("unknown zgossip method '%s'", method);

Expand Down Expand Up @@ -517,6 +553,35 @@ zgossip_test (bool verbose)
zstr_free (&command);
zstr_free (&status);

#ifdef CZMQ_BUILD_DRAFT_API
zstr_send (alpha, "KEYS");

char *size_str;
command = zstr_recv (alpha);
assert (streq (command, "KEYS"));
zstr_free (&command);
size_str = zstr_recv (alpha);
int i, size = atoi (size_str);
assert (size == 4);
zstr_free (&size_str);

zlist_t *keys = zlist_new ();
for (i = 0; i < size; ++i) {
key = zstr_recv (alpha);
assert (!zlist_append (keys, key));
}
for (key = (char *)zlist_pop (keys); key != NULL; key = (char *) zlist_pop (keys)) {
zstr_sendx (alpha, "TUPLE", key, NULL);
zstr_recvx (alpha, &command, &value, NULL);
assert (streq (command, "TUPLE"));
assert (strneq (value, ""));
zstr_free (&command);
zstr_free (&value);
zstr_free (&key);
}
zlist_destroy (&keys);
#endif

zactor_destroy (&base);
zactor_destroy (&alpha);
zactor_destroy (&beta);
Expand Down

0 comments on commit 17528ad

Please sign in to comment.