示例#1
0
文件: engine.c 项目: lerwys/majordomo
int main(int argc, char *argv [])
{
    int verbose = (argc > 1 && streq (argv [1], "-v"));
    engine_t *engine = s_engine_new (
        "tcp://localhost:5555", "NYSE", verbose);

    while (1) {
        zframe_t *reply_to = NULL;
        zmsg_t *request = mdp_worker_recv (engine->worker, &reply_to, false);
        if (request == NULL)
            break;          // Worker has been interrupted
        s_engine_handle_request (engine, request, reply_to);
    }
    s_engine_destroy (&engine);
    return 0;
}
示例#2
0
int main (int argc, char *argv [])
{
    int verbose = (argc > 1 && streq (argv [1], "-v"));
    mdp_worker_t *session = mdp_worker_new (
        NULL, "tcp://localhost:5555", "echo", verbose);

    while (1) {
        zframe_t *reply_to;
        zmsg_t *request = mdp_worker_recv (session, &reply_to);
        if (request == NULL)
            break;              //  Worker was interrupted
        //  Echo message
        mdp_worker_send (session, &request, reply_to);
        zframe_destroy (&reply_to);
    }
    mdp_worker_destroy (&session);
    return 0;
}
示例#3
0
int
mdp_worker_test (bool verbose)
{
    printf (" * mdp_worker: ");

    //  Simple create/destroy test
    mdp_worker_t *self = mdp_worker_new (0);
    assert (self);
    mdp_worker_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 = mdp_worker_new (MDP_WORKER_READY);
    mdp_worker_service_set (self, "Life is short but Now lasts for ever");
    mdp_worker_send (&self, output);

    self = mdp_worker_recv (input);
    assert (self);
    assert (streq (mdp_worker_service (self), "Life is short but Now lasts for ever"));
    mdp_worker_destroy (&self);

    self = mdp_worker_new (MDP_WORKER_REQUEST);
    mdp_worker_client_set (self, zframe_new ("Captcha Diem", 12));
    mdp_worker_body_set (self, zframe_new ("Captcha Diem", 12));
    mdp_worker_send (&self, output);

    self = mdp_worker_recv (input);
    assert (self);
    assert (zframe_streq (mdp_worker_client (self), "Captcha Diem"));
    assert (zframe_streq (mdp_worker_body (self), "Captcha Diem"));
    mdp_worker_destroy (&self);

    self = mdp_worker_new (MDP_WORKER_REPLY);
    mdp_worker_client_set (self, zframe_new ("Captcha Diem", 12));
    mdp_worker_body_set (self, zframe_new ("Captcha Diem", 12));
    mdp_worker_send (&self, output);

    self = mdp_worker_recv (input);
    assert (self);
    assert (zframe_streq (mdp_worker_client (self), "Captcha Diem"));
    assert (zframe_streq (mdp_worker_body (self), "Captcha Diem"));
    mdp_worker_destroy (&self);

    self = mdp_worker_new (MDP_WORKER_HEARBEAT);
    mdp_worker_send (&self, output);

    self = mdp_worker_recv (input);
    assert (self);
    mdp_worker_destroy (&self);

    self = mdp_worker_new (MDP_WORKER_DISCONNECT);
    mdp_worker_send (&self, output);

    self = mdp_worker_recv (input);
    assert (self);
    mdp_worker_destroy (&self);

    zctx_destroy (&ctx);
    printf ("OK\n");
    return 0;
}