コード例 #1
0
ファイル: alerts.c プロジェクト: miska/mallory
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);
}
コード例 #2
0
ファイル: ui.c プロジェクト: miska/alice
int main(int argc, char **argv)
{
    if(argc != 2) {
        fprintf(stderr, "Usage: %s address\n", argv[0]);
        exit(1);
    }

    char *addr = argv[1];

    mlm_client_t *agent = mlm_client_new ();
    // mlm_client_connect () returns -1 on failure
    if ( mlm_client_connect (agent, addr, 1000, "UI") == -1 ) {
        zsys_error ("Cannot connect to the endpoint %s", addr);
        mlm_client_destroy (&agent);
        return EXIT_FAILURE;
    }
    zmsg_t *getmsg = zmsg_new();
    zmsg_addstr(getmsg, "GET");
    zmsg_addstr(getmsg, "temperature");
    // mlm_client_sendfor () returns zero on success
    if (mlm_client_sendfor (agent, "stats", "temperature", NULL, 0, &getmsg) !=0 ) {
        zsys_error ("Cannot send the message");
        mlm_client_destroy (&agent);
        return 1;
    }
    getmsg = zmsg_new();
    zmsg_addstr(getmsg, "GET");
    zmsg_addstr(getmsg, "ups.state");
    if (mlm_client_sendfor (agent, "stats", "ups.state", NULL, 0, &getmsg) != 0) {
        zsys_error ("Cannot send the message");
        mlm_client_destroy (&agent);
        return 1;
    }
    getmsg = zmsg_new();
    zmsg_addstr(getmsg, "GET");
    zmsg_addstr(getmsg, "ups.power");
    if (mlm_client_sendfor (agent, "stats", "ups.power", NULL, 0, &getmsg) != 0) {
        zsys_error ("Cannot send the message");
        mlm_client_destroy (&agent);
        return 1;
    }

    // need to read replies, waiying for 3 of them
    int i = 0;
    while ( !zsys_interrupted && i < 3 )
    {
        zsys_info ("waiting for %d reply message", i);
        zmsg_t *msg = mlm_client_recv (agent);
        if ( msg == NULL )
            continue;
        if ( streq (mlm_client_command (agent), "MAILBOX DELIVER") )
        {
            if ( ( strcmp(mlm_client_subject(agent), "temperature") == 0 ) ||
                 ( strcmp(mlm_client_subject(agent), "ups.power") == 0 ) ||
                 ( strcmp(mlm_client_subject(agent), "ups.temperature") == 0 ) )
            {
                char *key = zmsg_popstr (msg);
                char *result = zmsg_popstr (msg);
                zsys_info ("GOT: %s = %s", key , result);
                i++;
            }
        }
        zmsg_destroy (&msg);
    }
    mlm_client_destroy (&agent);
    return 0;
}
コード例 #3
0
ファイル: sender.c プロジェクト: thalman/mlmplaygroud
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;
}