Exemple #1
0
static void
s_order_update (order_t *self, int volume)
{
    assert (self);
    assert (volume <= self->volume);
    self->volume -= volume;

    //  Prepare and send report to the client
    zmsg_t *report = zmsg_new ();
    zmsg_pushstrf (report, "%d", volume);

    if (self->volume == 0)
        zmsg_pushstr (report, "FILL");
    else
        zmsg_pushstr (report, "PARTIAL_FILL");

    mdp_worker_send (self->worker, &report, self->reply_to);
}
Exemple #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;
}
Exemple #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;
}