JNIEXPORT jint JNICALL
Java_org_zeromq_zebra_XrapTraffic__1_1recv (JNIEnv *env, jclass c, jlong self, jlong input)
{
    jint recv_ = (jint) xrap_traffic_recv ((xrap_traffic_t *) (intptr_t) self, (zsock_t *) (intptr_t) input);
    return recv_;
}
Ejemplo n.º 2
0
///
//  Receive a xrap_traffic from the socket. Returns 0 if OK, -1 if
//  there was an error. Blocks if there is no message waiting.    
int QmlXrapTraffic::recv (zsock_t *input) {
    return xrap_traffic_recv (self, input);
};
Ejemplo n.º 3
0
void
zeb_broker_test (bool verbose)
{
    printf (" * zeb_broker: ");
    if (verbose)
        printf ("\n");

    //  @selftest
    zactor_t *server = zactor_new (zeb_broker, "broker");
    if (verbose)
        zstr_send (server, "VERBOSE");

    zstr_sendx (server, "BIND", "tcp://127.0.0.1:9999", NULL);

    zsock_t* client = zsock_new_dealer (">tcp://127.0.0.1:9999");
    zsock_t* worker = zsock_new_dealer (">tcp://127.0.0.1:9999");
    assert (client);
    assert (worker);

    xrap_traffic_t *traffic = xrap_traffic_new ();

    //  Invalid Command
    xrap_traffic_set_id (traffic, XRAP_TRAFFIC_XRAP_OFFER);
    xrap_traffic_send (traffic, client);
    xrap_traffic_recv (traffic, client);
    assert (xrap_traffic_id (traffic) == XRAP_TRAFFIC_ERROR);
    assert (xrap_traffic_status_code (traffic) == XRAP_TRAFFIC_COMMAND_INVALID);

    //  Open Connections for client & worker
    xrap_traffic_set_id (traffic, XRAP_TRAFFIC_CONNECTION_OPEN);
    xrap_traffic_send (traffic, client);
    xrap_traffic_recv (traffic, client);
    assert (xrap_traffic_id (traffic) == XRAP_TRAFFIC_OK);

    xrap_traffic_set_id (traffic, XRAP_TRAFFIC_CONNECTION_OPEN);
    xrap_traffic_send (traffic, worker);
    xrap_traffic_recv (traffic, worker);
    assert (xrap_traffic_id (traffic) == XRAP_TRAFFIC_OK);

    //  Provide Rubish Offering
    xrap_traffic_set_id (traffic, XRAP_TRAFFIC_XRAP_OFFER);
    xrap_traffic_set_route (traffic, "///");
    xrap_traffic_set_method (traffic, "GET");
    xrap_traffic_send (traffic, worker);
    xrap_traffic_recv (traffic, worker);
    assert (xrap_traffic_id (traffic) == XRAP_TRAFFIC_FAIL);
    assert (xrap_traffic_status_code (traffic) == XRAP_TRAFFIC_CONFLICT);

    //  Provide Offering
    xrap_traffic_set_id (traffic, XRAP_TRAFFIC_XRAP_OFFER);
    xrap_traffic_set_route (traffic, "/foo/{[^/]}");
    xrap_traffic_set_method (traffic, "GET");
    xrap_traffic_send (traffic, worker);
    xrap_traffic_recv (traffic, worker);
    assert (xrap_traffic_id (traffic) == XRAP_TRAFFIC_OK);

    //  Send Request
    xrap_msg_t *xmsg = xrap_msg_new (XRAP_MSG_GET);
    xrap_msg_set_resource (xmsg, "%s", "/foo/bar");
    zmsg_t *msg = xrap_msg_encode (&xmsg);
    xrap_traffic_set_id (traffic, XRAP_TRAFFIC_XRAP_SEND);
    xrap_traffic_set_content (traffic, &msg);
    xrap_traffic_send (traffic, client);
    xrap_traffic_recv (traffic, client);
    assert (xrap_traffic_id (traffic) == XRAP_TRAFFIC_OK);

    //  Receive Request
    xrap_traffic_recv (traffic, worker);
    assert (xrap_traffic_id (traffic) == XRAP_TRAFFIC_XRAP_DELIVER);
    msg = zmsg_dup (xrap_traffic_content (traffic));
    xmsg = xrap_msg_decode (&msg);
    assert (xrap_msg_id (xmsg) == XRAP_MSG_GET);
    assert (streq ("/foo/bar", xrap_msg_resource (xmsg)));
    xrap_msg_destroy (&xmsg);

    //  Send Response
    xmsg = xrap_msg_new (XRAP_MSG_GET_OK);
    xrap_msg_set_status_code (xmsg, 200);
    xrap_msg_set_content_type (xmsg, "text/hello");
    xrap_msg_set_content_body (xmsg, "Hello World!");
    msg = xrap_msg_encode (&xmsg);
    xrap_traffic_set_id (traffic, XRAP_TRAFFIC_XRAP_DELIVER);
    xrap_traffic_set_content (traffic, &msg);
    xrap_traffic_send (traffic, worker);

    //  Receive Response
    xrap_traffic_recv (traffic, client);
    assert (xrap_traffic_id (traffic) == XRAP_TRAFFIC_XRAP_DELIVER);
    msg = zmsg_dup (xrap_traffic_content (traffic));
    xmsg = xrap_msg_decode (&msg);
    assert (xrap_msg_id (xmsg) == XRAP_MSG_GET_OK);
    assert (xrap_msg_status_code (xmsg) == 200);
    assert (streq ("text/hello", xrap_msg_content_type (xmsg)));
    assert (streq ("Hello World!", xrap_msg_content_body (xmsg)));
    xrap_msg_destroy (&xmsg);

    //  Send Request 2
    xmsg = xrap_msg_new (XRAP_MSG_GET);
    xrap_msg_set_resource (xmsg, "%s", "/fou/baz");
    msg = xrap_msg_encode (&xmsg);
    xrap_traffic_set_id (traffic, XRAP_TRAFFIC_XRAP_SEND);
    xrap_traffic_set_content (traffic, &msg);
    xrap_traffic_send (traffic, client);
    xrap_traffic_recv (traffic, client);
    assert (xrap_traffic_id (traffic) == XRAP_TRAFFIC_FAIL);
    assert (xrap_traffic_status_code (traffic) == XRAP_TRAFFIC_NOT_FOUND);

    xrap_traffic_destroy (&traffic);

    //  Finished, we can clean up
    zsock_destroy (&client);
    zsock_destroy (&worker);
    zactor_destroy (&server);
    //  @end
    printf ("OK\n");
}
Ejemplo n.º 4
0
void
xrap_traffic_test (bool verbose)
{
    printf (" * xrap_traffic:");

    if (verbose)
        printf ("\n");

    //  @selftest
    //  Simple create/destroy test
    xrap_traffic_t *self = xrap_traffic_new ();
    assert (self);
    xrap_traffic_destroy (&self);
    //  Create pair of sockets we can send through
    //  We must bind before connect if we wish to remain compatible with ZeroMQ < v4
    zsock_t *output = zsock_new (ZMQ_DEALER);
    assert (output);
    int rc = zsock_bind (output, "inproc://selftest-xrap_traffic");
    assert (rc == 0);

    zsock_t *input = zsock_new (ZMQ_ROUTER);
    assert (input);
    rc = zsock_connect (input, "inproc://selftest-xrap_traffic");
    assert (rc == 0);


    //  Encode/send/decode and verify each message type
    int instance;
    self = xrap_traffic_new ();
    xrap_traffic_set_id (self, XRAP_TRAFFIC_CONNECTION_OPEN);

    xrap_traffic_set_address (self, "Life is short but Now lasts for ever");
    //  Send twice
    xrap_traffic_send (self, output);
    xrap_traffic_send (self, output);

    for (instance = 0; instance < 2; instance++) {
        xrap_traffic_recv (self, input);
        assert (xrap_traffic_routing_id (self));
        assert (streq (xrap_traffic_address (self), "Life is short but Now lasts for ever"));
    }
    xrap_traffic_set_id (self, XRAP_TRAFFIC_CONNECTION_PING);

    //  Send twice
    xrap_traffic_send (self, output);
    xrap_traffic_send (self, output);

    for (instance = 0; instance < 2; instance++) {
        xrap_traffic_recv (self, input);
        assert (xrap_traffic_routing_id (self));
    }
    xrap_traffic_set_id (self, XRAP_TRAFFIC_CONNECTION_PONG);

    //  Send twice
    xrap_traffic_send (self, output);
    xrap_traffic_send (self, output);

    for (instance = 0; instance < 2; instance++) {
        xrap_traffic_recv (self, input);
        assert (xrap_traffic_routing_id (self));
    }
    xrap_traffic_set_id (self, XRAP_TRAFFIC_CONNECTION_CLOSE);

    //  Send twice
    xrap_traffic_send (self, output);
    xrap_traffic_send (self, output);

    for (instance = 0; instance < 2; instance++) {
        xrap_traffic_recv (self, input);
        assert (xrap_traffic_routing_id (self));
    }
    xrap_traffic_set_id (self, XRAP_TRAFFIC_XRAP_SEND);

    xrap_traffic_set_timeout (self, 123);
    zmsg_t *xrap_send_content = zmsg_new ();
    xrap_traffic_set_content (self, &xrap_send_content);
    zmsg_addstr (xrap_traffic_content (self), "Captcha Diem");
    //  Send twice
    xrap_traffic_send (self, output);
    xrap_traffic_send (self, output);

    for (instance = 0; instance < 2; instance++) {
        xrap_traffic_recv (self, input);
        assert (xrap_traffic_routing_id (self));
        assert (xrap_traffic_timeout (self) == 123);
        assert (zmsg_size (xrap_traffic_content (self)) == 1);
        char *content = zmsg_popstr (xrap_traffic_content (self));
        assert (streq (content, "Captcha Diem"));
        zstr_free (&content);
        if (instance == 1)
            zmsg_destroy (&xrap_send_content);
    }
    xrap_traffic_set_id (self, XRAP_TRAFFIC_XRAP_OFFER);

    xrap_traffic_set_route (self, "Life is short but Now lasts for ever");
    xrap_traffic_set_method (self, "Life is short but Now lasts for ever");
    //  Send twice
    xrap_traffic_send (self, output);
    xrap_traffic_send (self, output);

    for (instance = 0; instance < 2; instance++) {
        xrap_traffic_recv (self, input);
        assert (xrap_traffic_routing_id (self));
        assert (streq (xrap_traffic_route (self), "Life is short but Now lasts for ever"));
        assert (streq (xrap_traffic_method (self), "Life is short but Now lasts for ever"));
    }
    xrap_traffic_set_id (self, XRAP_TRAFFIC_XRAP_DELIVER);

    zuuid_t *xrap_deliver_sender = zuuid_new ();
    xrap_traffic_set_sender (self, xrap_deliver_sender);
    zmsg_t *xrap_deliver_content = zmsg_new ();
    xrap_traffic_set_content (self, &xrap_deliver_content);
    zmsg_addstr (xrap_traffic_content (self), "Captcha Diem");
    //  Send twice
    xrap_traffic_send (self, output);
    xrap_traffic_send (self, output);

    for (instance = 0; instance < 2; instance++) {
        xrap_traffic_recv (self, input);
        assert (xrap_traffic_routing_id (self));
        assert (zuuid_eq (xrap_deliver_sender, zuuid_data (xrap_traffic_sender (self))));
        if (instance == 1)
            zuuid_destroy (&xrap_deliver_sender);
        assert (zmsg_size (xrap_traffic_content (self)) == 1);
        char *content = zmsg_popstr (xrap_traffic_content (self));
        assert (streq (content, "Captcha Diem"));
        zstr_free (&content);
        if (instance == 1)
            zmsg_destroy (&xrap_deliver_content);
    }
    xrap_traffic_set_id (self, XRAP_TRAFFIC_OK);

    xrap_traffic_set_status_code (self, 123);
    xrap_traffic_set_status_reason (self, "Life is short but Now lasts for ever");
    //  Send twice
    xrap_traffic_send (self, output);
    xrap_traffic_send (self, output);

    for (instance = 0; instance < 2; instance++) {
        xrap_traffic_recv (self, input);
        assert (xrap_traffic_routing_id (self));
        assert (xrap_traffic_status_code (self) == 123);
        assert (streq (xrap_traffic_status_reason (self), "Life is short but Now lasts for ever"));
    }
    xrap_traffic_set_id (self, XRAP_TRAFFIC_FAIL);

    xrap_traffic_set_status_code (self, 123);
    xrap_traffic_set_status_reason (self, "Life is short but Now lasts for ever");
    //  Send twice
    xrap_traffic_send (self, output);
    xrap_traffic_send (self, output);

    for (instance = 0; instance < 2; instance++) {
        xrap_traffic_recv (self, input);
        assert (xrap_traffic_routing_id (self));
        assert (xrap_traffic_status_code (self) == 123);
        assert (streq (xrap_traffic_status_reason (self), "Life is short but Now lasts for ever"));
    }
    xrap_traffic_set_id (self, XRAP_TRAFFIC_ERROR);

    xrap_traffic_set_status_code (self, 123);
    xrap_traffic_set_status_reason (self, "Life is short but Now lasts for ever");
    //  Send twice
    xrap_traffic_send (self, output);
    xrap_traffic_send (self, output);

    for (instance = 0; instance < 2; instance++) {
        xrap_traffic_recv (self, input);
        assert (xrap_traffic_routing_id (self));
        assert (xrap_traffic_status_code (self) == 123);
        assert (streq (xrap_traffic_status_reason (self), "Life is short but Now lasts for ever"));
    }

    xrap_traffic_destroy (&self);
    zsock_destroy (&input);
    zsock_destroy (&output);
    //  @end

    printf ("OK\n");
}