Example #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;
}
Example #2
0
static void
s_print_log_msg (void *collector)
{
    zre_log_msg_t *msg = zre_log_msg_recv (collector);
    if (!msg)
        return;                 //  Interrupted

    time_t curtime = zre_log_msg_time (msg);
    char *event = NULL;
    switch (zre_log_msg_event (msg)) {
    case ZRE_LOG_MSG_EVENT_JOIN:
        event = "Join group";
        break;
    case ZRE_LOG_MSG_EVENT_LEAVE:
        event = "Leave group";
        break;
    case ZRE_LOG_MSG_EVENT_ENTER:
        event = "Peer enters";
        break;
    case ZRE_LOG_MSG_EVENT_EXIT:
        event = "Peer exits";
        break;
    }
    struct tm *loctime = localtime (&curtime);
    char timestr [20];
    strftime (timestr, 20, "%y-%m-%d %H:%M:%S ", loctime);

    printf ("%s I: [%04X] [%04X] - %s %s\n",
            timestr,
            zre_log_msg_node (msg),
            zre_log_msg_peer (msg),
            event,
            zre_log_msg_data (msg));

    zre_log_msg_destroy (&msg);
}
Example #3
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;
}