Esempio n. 1
0
static void
s_alerts (
    zsock_t *pipe,
    void *args) {

    const char *name = "ALERT";

    mlm_client_t *cl = mlm_client_new ();
    mlm_client_connect (cl, endpoint, 5000, __PRETTY_FUNCTION__);
    mlm_client_set_producer (cl, stream);

    zsock_t *msgpipe = mlm_client_msgpipe (cl);

    zpoller_t *poller = zpoller_new (pipe, msgpipe, NULL);

    char *alert_state = strdup ("NEW");

    zsock_signal (pipe, 0);
    while (!zsys_interrupted) {
        zsock_t *which = zpoller_wait (poller, 1000);

        if (!which) {
            mlm_client_sendx (cl, "alert://upsonbattery@ups1", alert_state, NULL);
            continue;
        }

        if (which == pipe)
            break;

        //which == msgpipe
        zmsg_t *msg = mlm_client_recv (cl);
        if (!streq (mlm_client_command (cl), "MAILBOX DELIVER"))
            goto msg_destroy;

        char *alert_name = zmsg_popstr (msg);

        zstr_free (&alert_state);
        alert_state = zmsg_popstr (msg);

        zsys_info ("%s: Alert '%s' new state is '%s'", name, alert_name, alert_state);
        zstr_free (&alert_name);
msg_destroy:
        zmsg_destroy (&msg);
    }

    zstr_free (&alert_state);
    zpoller_destroy (&poller);
    mlm_client_destroy (&cl);
}
Esempio n. 2
0
int main (int argc, char **argv) {
    unsigned int count = 0;

    parse_args(argc, argv);
    if (!endpoint || !name) {
        zsys_error("endpoint or name not specified.");
        usage();
    }

    mlm_client_t *client = mlm_client_new ();
    assert(client);

    int rv;
    rv = mlm_client_connect(client, endpoint, 5000, name);
    if (rv == -1) {
        zsys_error("connection failed.");
        mlm_client_destroy (&client);
        return -1;
    }

    rv = mlm_client_set_producer (client, "stream");
    if (rv == -1) {
        zsys_error("set_producer failed.");
        mlm_client_destroy (&client);
        return -2;
    }

    zsock_t *pipe = mlm_client_msgpipe (client);
    if (!pipe) {
        zsys_error ("mlm_client_msgpipe() failed.");
        mlm_client_destroy (&client);
        return -3;
    }

    zpoller_t *poller = zpoller_new (pipe, NULL);
    if (!poller) {
        zsys_error("zpoller_new() failed.");
        mlm_client_destroy (&client);
        return -4;
    }

    while ( !zsys_interrupted && ( !num_messages || count < num_messages) ) {
        zsock_t *which = zpoller_wait (poller, interval);
        if ( which != NULL ) {
            // so we have something to receive
            zmsg_t *recv_msg = mlm_client_recv (client);
            zmsg_destroy (&recv_msg);
        }
        // in any case we are going to send something
//        zclock_sleep(interval);
        zmsg_t *msg = zmsg_new();
        assert (msg);
        if ( count % 10 == 0) { 
            zmsg_pushstr (msg, "exit");
        } else {
            zmsg_pushstr (msg, "hello");
        }
        zmsg_print(msg);
        mlm_client_send (client, "testing message", &msg);
        zmsg_destroy (&msg);
        ++count;
    }
    mlm_client_destroy(&client);
    zpoller_destroy(&poller);
    free(endpoint);
    free(name);
    zsys_info ("finished, sent: %u.", count);
    return 0;
}