Beispiel #1
0
int
zre_log_msg_test (bool verbose)
{
    printf (" * zre_log_msg: ");

    //  @selftest
    //  Simple create/destroy test
    zre_log_msg_t *self = zre_log_msg_new (0);
    assert (self);
    zre_log_msg_destroy (&self);

    //  Create pair of sockets we can send through
    zctx_t *ctx = zctx_new ();
    assert (ctx);

    void *output = zsocket_new (ctx, ZMQ_DEALER);
    assert (output);
    zsocket_bind (output, "inproc://selftest");
    void *input = zsocket_new (ctx, ZMQ_ROUTER);
    assert (input);
    zsocket_connect (input, "inproc://selftest");
    
    //  Encode/send/decode and verify each message type

    self = zre_log_msg_new (ZRE_LOG_MSG_LOG);
    zre_log_msg_set_level (self, 123);
    zre_log_msg_set_event (self, 123);
    zre_log_msg_set_node (self, 123);
    zre_log_msg_set_peer (self, 123);
    zre_log_msg_set_time (self, 123);
    zre_log_msg_set_data (self, "Life is short but Now lasts for ever");
    zre_log_msg_send (&self, output);
    
    self = zre_log_msg_recv (input);
    assert (self);
    assert (zre_log_msg_level (self) == 123);
    assert (zre_log_msg_event (self) == 123);
    assert (zre_log_msg_node (self) == 123);
    assert (zre_log_msg_peer (self) == 123);
    assert (zre_log_msg_time (self) == 123);
    assert (streq (zre_log_msg_data (self), "Life is short but Now lasts for ever"));
    zre_log_msg_destroy (&self);

    zctx_destroy (&ctx);
    //  @end

    printf ("OK\n");
    return 0;
}
Beispiel #2
0
zre_log_msg_t *
zre_log_msg_decode (zmsg_t **msg_p)
{
    assert (msg_p);
    zmsg_t *msg = *msg_p;
    if (msg == NULL)
        return NULL;
        
    zre_log_msg_t *self = zre_log_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 | 2))
        goto empty;             //  Invalid signature

    //  Get message id and parse per message type
    GET_NUMBER1 (self->id);

    switch (self->id) {
        case ZRE_LOG_MSG_LOG:
            GET_NUMBER1 (self->level);
            GET_NUMBER1 (self->event);
            GET_NUMBER2 (self->node);
            GET_NUMBER2 (self->peer);
            GET_NUMBER8 (self->time);
            GET_STRING (self->data);
            break;

        default:
            goto malformed;
    }
    //  Successful return
    zframe_destroy (&frame);
    zmsg_destroy (msg_p);
    return self;

    //  Error returns
    malformed:
        printf ("E: malformed message '%d'\n", self->id);
    empty:
        zframe_destroy (&frame);
        zmsg_destroy (msg_p);
        zre_log_msg_destroy (&self);
        return (NULL);
}
Beispiel #3
0
zmsg_t * 
zre_log_msg_encode_log (
    byte level,
    byte event,
    uint16_t node,
    uint16_t peer,
    uint64_t time,
    const char *data)
{
    zre_log_msg_t *self = zre_log_msg_new (ZRE_LOG_MSG_LOG);
    zre_log_msg_set_level (self, level);
    zre_log_msg_set_event (self, event);
    zre_log_msg_set_node (self, node);
    zre_log_msg_set_peer (self, peer);
    zre_log_msg_set_time (self, time);
    zre_log_msg_set_data (self, data);
    return zre_log_msg_encode (&self);
}
Beispiel #4
0
int
zre_log_msg_send_log (
    void *output,
    byte level,
    byte event,
    uint16_t node,
    uint16_t peer,
    uint64_t time,
    char *data)
{
    zre_log_msg_t *self = zre_log_msg_new (ZRE_LOG_MSG_LOG);
    zre_log_msg_set_level (self, level);
    zre_log_msg_set_event (self, event);
    zre_log_msg_set_node (self, node);
    zre_log_msg_set_peer (self, peer);
    zre_log_msg_set_time (self, time);
    zre_log_msg_set_data (self, data);
    return zre_log_msg_send (&self, output);
}
Beispiel #5
0
zre_log_msg_t *
zre_log_msg_dup (zre_log_msg_t *self)
{
    if (!self)
        return NULL;
        
    zre_log_msg_t *copy = zre_log_msg_new (self->id);
    if (self->address)
        copy->address = zframe_dup (self->address);
    switch (self->id) {
        case ZRE_LOG_MSG_LOG:
            copy->level = self->level;
            copy->event = self->event;
            copy->node = self->node;
            copy->peer = self->peer;
            copy->time = self->time;
            copy->data = strdup (self->data);
            break;

    }
    return copy;
}
Beispiel #6
0
zre_log_msg_t *
zre_log_msg_recv (void *input)
{
    assert (input);
    zre_log_msg_t *self = zre_log_msg_new (0);
    zframe_t *frame = NULL;
    size_t string_size;
    size_t list_size;
    size_t hash_size;

    //  Read valid message frame from socket; we loop over any
    //  garbage data we might receive from badly-connected peers
    while (true) {
        //  If we're reading from a ROUTER socket, get address
        if (zsocket_type (input) == ZMQ_ROUTER) {
            zframe_destroy (&self->address);
            self->address = zframe_recv (input);
            if (!self->address)
                goto empty;         //  Interrupted
            if (!zsocket_rcvmore (input))
                goto malformed;
        }
        //  Read and parse command in frame
        frame = zframe_recv (input);
        if (!frame)
            goto empty;             //  Interrupted

        //  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 | 2))
            break;                  //  Valid signature

        //  Protocol assertion, drop message
        while (zsocket_rcvmore (input)) {
            zframe_destroy (&frame);
            frame = zframe_recv (input);
        }
        zframe_destroy (&frame);
    }
    //  Get message id and parse per message type
    GET_NUMBER1 (self->id);

    switch (self->id) {
        case ZRE_LOG_MSG_LOG:
            GET_NUMBER1 (self->level);
            GET_NUMBER1 (self->event);
            GET_NUMBER2 (self->node);
            GET_NUMBER2 (self->peer);
            GET_NUMBER8 (self->time);
            free (self->data);
            GET_STRING (self->data);
            break;

        default:
            goto malformed;
    }
    //  Successful return
    zframe_destroy (&frame);
    return self;

    //  Error returns
    malformed:
        printf ("E: malformed message '%d'\n", self->id);
    empty:
        zframe_destroy (&frame);
        zre_log_msg_destroy (&self);
        return (NULL);
}
Beispiel #7
0
int
zre_log_msg_test (bool verbose)
{
    printf (" * zre_log_msg: ");

    //  @selftest
    //  Simple create/destroy test
    zre_log_msg_t *self = zre_log_msg_new (0);
    assert (self);
    zre_log_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");

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

    //  Encode/send/decode and verify each message type
    int instance;
    zre_log_msg_t *copy;
    self = zre_log_msg_new (ZRE_LOG_MSG_LOG);
    
    //  Check that _dup works on empty message
    copy = zre_log_msg_dup (self);
    assert (copy);
    zre_log_msg_destroy (&copy);

    zre_log_msg_set_level (self, 123);
    zre_log_msg_set_event (self, 123);
    zre_log_msg_set_node (self, 123);
    zre_log_msg_set_peer (self, 123);
    zre_log_msg_set_time (self, 123);
    zre_log_msg_set_data (self, "Life is short but Now lasts for ever");
    //  Send twice from same object
    zre_log_msg_send_again (self, output);
    zre_log_msg_send (&self, output);

    for (instance = 0; instance < 2; instance++) {
        self = zre_log_msg_recv (input);
        assert (self);
        assert (zre_log_msg_routing_id (self));
        
        assert (zre_log_msg_level (self) == 123);
        assert (zre_log_msg_event (self) == 123);
        assert (zre_log_msg_node (self) == 123);
        assert (zre_log_msg_peer (self) == 123);
        assert (zre_log_msg_time (self) == 123);
        assert (streq (zre_log_msg_data (self), "Life is short but Now lasts for ever"));
        zre_log_msg_destroy (&self);
    }

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

    printf ("OK\n");
    return 0;
}