Ejemplo n.º 1
0
static void
store_tuple_if_new (client_t *self)
{
    server_accept (self->server,
                   zgossip_msg_key (self->message),
                   zgossip_msg_value (self->message));
}
Ejemplo n.º 2
0
static int
remote_handler (zloop_t *loop, zsock_t *remote, void *argument)
{
    zgossip_msg_t *msg = zgossip_msg_recv (remote);
    if (!msg)
        return -1;              //  Interrupted

    if (zgossip_msg_id (msg) == ZGOSSIP_MSG_PUBLISH)
        server_accept ((server_t *) argument,
                       zgossip_msg_key (msg),
                       zgossip_msg_value (msg));
    else
    if (zgossip_msg_id (msg) == ZGOSSIP_MSG_INVALID)
        //  Connection was reset, so send HELLO again
        zgossip_msg_send_hello (remote);
    else
    if (zgossip_msg_id (msg) == ZGOSSIP_MSG_PONG)
        assert (true);   //  Do nothing with PONGs

    zgossip_msg_destroy (&msg);
    return 0;
}
Ejemplo n.º 3
0
static int
remote_handler (zloop_t *loop, zsock_t *remote, void *argument)
{
    server_t *self = (server_t *) argument;
    if (zgossip_msg_recv (self->message, remote))
        return -1;          //  Interrupted

    if (zgossip_msg_id (self->message) == ZGOSSIP_MSG_PUBLISH)
        server_accept (self,
                       zgossip_msg_key (self->message),
                       zgossip_msg_value (self->message));
    else
    if (zgossip_msg_id (self->message) == ZGOSSIP_MSG_INVALID) {
        //  Connection was reset, so send HELLO again
        zgossip_msg_set_id (self->message, ZGOSSIP_MSG_HELLO);
        zgossip_msg_send (self->message, remote);
    }
    else
    if (zgossip_msg_id (self->message) == ZGOSSIP_MSG_PONG)
        assert (true);   //  Do nothing with PONGs

    return 0;
}
Ejemplo n.º 4
0
int
zgossip_msg_test (bool verbose)
{
    printf (" * zgossip_msg: ");

    //  @selftest
    //  Simple create/destroy test
    zgossip_msg_t *self = zgossip_msg_new (0);
    assert (self);
    zgossip_msg_destroy (&self);

    //  Create pair of sockets we can send through
    zsock_t *input = zsock_new (ZMQ_ROUTER);
    assert (input);
    zsock_connect (input, "inproc://selftest-zgossip_msg");

    zsock_t *output = zsock_new (ZMQ_DEALER);
    assert (output);
    zsock_bind (output, "inproc://selftest-zgossip_msg");

    //  Encode/send/decode and verify each message type
    int instance;
    zgossip_msg_t *copy;
    self = zgossip_msg_new (ZGOSSIP_MSG_HELLO);
    
    //  Check that _dup works on empty message
    copy = zgossip_msg_dup (self);
    assert (copy);
    zgossip_msg_destroy (&copy);

    //  Send twice from same object
    zgossip_msg_send_again (self, output);
    zgossip_msg_send (&self, output);

    for (instance = 0; instance < 2; instance++) {
        self = zgossip_msg_recv (input);
        assert (self);
        assert (zgossip_msg_routing_id (self));
        
        zgossip_msg_destroy (&self);
    }
    self = zgossip_msg_new (ZGOSSIP_MSG_PUBLISH);
    
    //  Check that _dup works on empty message
    copy = zgossip_msg_dup (self);
    assert (copy);
    zgossip_msg_destroy (&copy);

    zgossip_msg_set_key (self, "Life is short but Now lasts for ever");
    zgossip_msg_set_value (self, "Life is short but Now lasts for ever");
    //  Send twice from same object
    zgossip_msg_send_again (self, output);
    zgossip_msg_send (&self, output);

    for (instance = 0; instance < 2; instance++) {
        self = zgossip_msg_recv (input);
        assert (self);
        assert (zgossip_msg_routing_id (self));
        
        assert (streq (zgossip_msg_key (self), "Life is short but Now lasts for ever"));
        assert (streq (zgossip_msg_value (self), "Life is short but Now lasts for ever"));
        zgossip_msg_destroy (&self);
    }
    self = zgossip_msg_new (ZGOSSIP_MSG_PING);
    
    //  Check that _dup works on empty message
    copy = zgossip_msg_dup (self);
    assert (copy);
    zgossip_msg_destroy (&copy);

    //  Send twice from same object
    zgossip_msg_send_again (self, output);
    zgossip_msg_send (&self, output);

    for (instance = 0; instance < 2; instance++) {
        self = zgossip_msg_recv (input);
        assert (self);
        assert (zgossip_msg_routing_id (self));
        
        zgossip_msg_destroy (&self);
    }
    self = zgossip_msg_new (ZGOSSIP_MSG_PONG);
    
    //  Check that _dup works on empty message
    copy = zgossip_msg_dup (self);
    assert (copy);
    zgossip_msg_destroy (&copy);

    //  Send twice from same object
    zgossip_msg_send_again (self, output);
    zgossip_msg_send (&self, output);

    for (instance = 0; instance < 2; instance++) {
        self = zgossip_msg_recv (input);
        assert (self);
        assert (zgossip_msg_routing_id (self));
        
        zgossip_msg_destroy (&self);
    }
    self = zgossip_msg_new (ZGOSSIP_MSG_INVALID);
    
    //  Check that _dup works on empty message
    copy = zgossip_msg_dup (self);
    assert (copy);
    zgossip_msg_destroy (&copy);

    //  Send twice from same object
    zgossip_msg_send_again (self, output);
    zgossip_msg_send (&self, output);

    for (instance = 0; instance < 2; instance++) {
        self = zgossip_msg_recv (input);
        assert (self);
        assert (zgossip_msg_routing_id (self));
        
        zgossip_msg_destroy (&self);
    }

    zsock_destroy (&input);
    zsock_destroy (&output);
    //  @end

    printf ("OK\n");
    return 0;
}
Ejemplo n.º 5
0
void
zgossip_msg_test (bool verbose)
{
    printf (" * zgossip_msg:");

    if (verbose)
        printf ("\n");

    //  @selftest
    //  Simple create/destroy test
    zgossip_msg_t *self = zgossip_msg_new ();
    assert (self);
    zgossip_msg_destroy (&self);
    //  Create pair of sockets we can send through
    //  We must bind before connect if we wish to remain compatible with ZeroMQ < v4
    zsock_t *output = zsock_new (ZMQ_DEALER);
    assert (output);
    int rc = zsock_bind (output, "inproc://selftest-zgossip_msg");
    assert (rc == 0);

    zsock_t *input = zsock_new (ZMQ_ROUTER);
    assert (input);
    rc = zsock_connect (input, "inproc://selftest-zgossip_msg");
    assert (rc == 0);


    //  Encode/send/decode and verify each message type
    int instance;
    self = zgossip_msg_new ();
    zgossip_msg_set_id (self, ZGOSSIP_MSG_HELLO);

    //  Send twice
    zgossip_msg_send (self, output);
    zgossip_msg_send (self, output);

    for (instance = 0; instance < 2; instance++) {
        zgossip_msg_recv (self, input);
        assert (zgossip_msg_routing_id (self));
    }
    zgossip_msg_set_id (self, ZGOSSIP_MSG_PUBLISH);

    zgossip_msg_set_key (self, "Life is short but Now lasts for ever");
    zgossip_msg_set_value (self, "Life is short but Now lasts for ever");
    zgossip_msg_set_ttl (self, 123);
    //  Send twice
    zgossip_msg_send (self, output);
    zgossip_msg_send (self, output);

    for (instance = 0; instance < 2; instance++) {
        zgossip_msg_recv (self, input);
        assert (zgossip_msg_routing_id (self));
        assert (streq (zgossip_msg_key (self), "Life is short but Now lasts for ever"));
        assert (streq (zgossip_msg_value (self), "Life is short but Now lasts for ever"));
        assert (zgossip_msg_ttl (self) == 123);
    }
    zgossip_msg_set_id (self, ZGOSSIP_MSG_PING);

    //  Send twice
    zgossip_msg_send (self, output);
    zgossip_msg_send (self, output);

    for (instance = 0; instance < 2; instance++) {
        zgossip_msg_recv (self, input);
        assert (zgossip_msg_routing_id (self));
    }
    zgossip_msg_set_id (self, ZGOSSIP_MSG_PONG);

    //  Send twice
    zgossip_msg_send (self, output);
    zgossip_msg_send (self, output);

    for (instance = 0; instance < 2; instance++) {
        zgossip_msg_recv (self, input);
        assert (zgossip_msg_routing_id (self));
    }
    zgossip_msg_set_id (self, ZGOSSIP_MSG_INVALID);

    //  Send twice
    zgossip_msg_send (self, output);
    zgossip_msg_send (self, output);

    for (instance = 0; instance < 2; instance++) {
        zgossip_msg_recv (self, input);
        assert (zgossip_msg_routing_id (self));
    }

    zgossip_msg_destroy (&self);
    zsock_destroy (&input);
    zsock_destroy (&output);
    //  @end

    printf ("OK\n");
}