Example #1
0
static void
write_message_to_xrap_handler (client_t *self)
{
    service_t *service = self->server->xrap;
    assert (service);
    ztrie_t *routes = NULL;

    zmsg_t *content = zmsg_dup (xrap_traffic_content (self->message));
    xrap_msg_t *msg = xrap_msg_decode (&content);
    char *route;
    int method = xrap_msg_id (msg);
    if (method == XRAP_MSG_GET) {
        route = (char *) xrap_msg_resource (msg);
        routes = service->get_routes;
    }
    else
    if (method == XRAP_MSG_POST) {
        route = (char *) xrap_msg_parent (msg);
        routes = service->post_routes;
    }
    else
    if (method == XRAP_MSG_PUT) {
        route = (char *) xrap_msg_resource (msg);
        routes = service->put_routes;
    }
    else
    if (method == XRAP_MSG_DELETE) {
        route = (char *) xrap_msg_resource (msg);
        routes = service->delete_routes;
    }
    else {
        xrap_traffic_set_status_code (self->message, XRAP_TRAFFIC_BAD_REQUEST);
        engine_set_exception (self, fail_event);
    }

    client_t *target;
    if (routes && ztrie_matches (routes, route)) {
        target = (client_t *) ztrie_hit_data (routes);
        //  Save message for broker
        target->msg = zmsg_dup (xrap_traffic_content (self->message));
        //  Trigger dispatch event
        target->callee = self;
        engine_send_event (target, xrap_message_event);
    }
    else {
        xrap_traffic_set_status_code (self->message, XRAP_TRAFFIC_NOT_FOUND);
        self->rc = -1;
    }

    //  Clean up
    xrap_msg_destroy (&msg);
}
Example #2
0
JNIEXPORT jint JNICALL
Java_org_zeromq_zebra_XrapMsg__1_1id (JNIEnv *env, jclass c, jlong self)
{
    jint id_ = (jint) xrap_msg_id ((xrap_msg_t *) (intptr_t) self);
    return id_;
}
Example #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");
}
Example #4
0
///
//  Get the xrap_msg id and printable command
int QXrapMsg::id ()
{
    int rv = xrap_msg_id (self);
    return rv;
}
Example #5
0
void
zwr_client_test (bool verbose)
{
    printf (" * zwr_client: ");
    if (verbose)
        printf ("\n");

    //  @selftest
    zwr_client_verbose = verbose;

    //  Start a server to test against, and bind to endpoint
    zactor_t *server = zactor_new (zwr_server, "zwr_client_test");
    if (verbose)
        zstr_send (server, "VERBOSE");
    zstr_sendx (server, "LOAD", "src/zwr_client.cfg", NULL);

    //  Create clients for testing
    zwr_client_t *client = zwr_client_new ();
    zwr_client_t *handler = zwr_client_new ();
    assert (client);
    assert (handler);

    //  Connect clients to server
    int rc = zwr_client_connect (client, "tcp://127.0.0.1:9999", 1000, "client");
    assert (rc == 0);
    assert (zwr_client_connected (client) == true);

    rc = zwr_client_connect (handler, "tcp://127.0.0.1:9999", 1000, "handler");
    assert (rc == 0);
    assert (zwr_client_connected (handler) == true);

    //  Provide Rubbish Offering
    rc = zwr_client_set_handler (handler, "GET", "///");
    assert (rc != 0);
    assert (zwr_client_status (handler) == XRAP_TRAFFIC_CONFLICT);

    //  Provide Offering
    rc = zwr_client_set_handler (handler, "GET", "/foo/{[^/]}");
    assert (rc == 0);

    //  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);
    rc = zwr_client_request (client, 0, &msg);
    assert (rc == 0);

    //  Receive Request
    msg = zwr_client_recv (handler);
    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);
    zwr_client_deliver (handler, handler->sender, &msg);
    zuuid_t *sender = zwr_client_sender (handler);
    zuuid_destroy (&sender);

    //  Receive Response
    msg = zwr_client_recv (client);
    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);
    sender = zwr_client_sender (client);
    zuuid_destroy (&sender);

    //  Send Request 2
    xmsg = xrap_msg_new (XRAP_MSG_GET);
    xrap_msg_set_resource (xmsg, "%s", "/fou/baz");
    msg = xrap_msg_encode (&xmsg);
    rc = zwr_client_request (client, 0, &msg);
    assert (rc == XRAP_TRAFFIC_NOT_FOUND);

    zwr_client_destroy (&client);
    zwr_client_destroy (&handler);

    //  Done, shut down
    zactor_destroy (&server);
    //  @end
    printf ("OK\n");
}