コード例 #1
0
ファイル: fmq_server.c プロジェクト: JuanCerezuela/filemq
//  Execute state machine as long as we have events
static void
server_client_execute (server_t *self, client_t *client, int event)
{
    client->next_event = event;
    while (client->next_event) {
        client->event = client->next_event;
        client->next_event = 0;
        switch (client->state) {
            case start_state:
                if (client->event == ohai_event) {
                    try_anonymous_access (self, client);
                    client->state = checking_client_state;
                }
                else
                if (client->event == heartbeat_event) {
                }
                else
                if (client->event == expired_event) {
                    terminate_the_client (self, client);
                }
                else {
                    //  Process all other events
                    fmq_msg_set_id (client->reply, FMQ_MSG_RTFM);
                    fmq_msg_send (&client->reply, client->router);
                    client->reply = fmq_msg_new (0);
                    fmq_msg_set_address (client->reply, client->address);
                    terminate_the_client (self, client);
                }
                break;

            case checking_client_state:
                if (client->event == friend_event) {
                    fmq_msg_set_id (client->reply, FMQ_MSG_OHAI_OK);
                    fmq_msg_send (&client->reply, client->router);
                    client->reply = fmq_msg_new (0);
                    fmq_msg_set_address (client->reply, client->address);
                    client->state = ready_state;
                }
                else
                if (client->event == foe_event) {
                    fmq_msg_set_id (client->reply, FMQ_MSG_SRSLY);
                    fmq_msg_send (&client->reply, client->router);
                    client->reply = fmq_msg_new (0);
                    fmq_msg_set_address (client->reply, client->address);
                    terminate_the_client (self, client);
                }
                else
                if (client->event == maybe_event) {
                    list_security_mechanisms (self, client);
                    fmq_msg_set_id (client->reply, FMQ_MSG_ORLY);
                    fmq_msg_send (&client->reply, client->router);
                    client->reply = fmq_msg_new (0);
                    fmq_msg_set_address (client->reply, client->address);
                    client->state = challenging_client_state;
                }
                else
                if (client->event == heartbeat_event) {
                }
                else
                if (client->event == expired_event) {
                    terminate_the_client (self, client);
                }
                else
                if (client->event == ohai_event) {
                    try_anonymous_access (self, client);
                    client->state = checking_client_state;
                }
                else {
                    //  Process all other events
                    fmq_msg_set_id (client->reply, FMQ_MSG_RTFM);
                    fmq_msg_send (&client->reply, client->router);
                    client->reply = fmq_msg_new (0);
                    fmq_msg_set_address (client->reply, client->address);
                    terminate_the_client (self, client);
                }
                break;

            case challenging_client_state:
                if (client->event == yarly_event) {
                    try_security_mechanism (self, client);
                    client->state = checking_client_state;
                }
                else
                if (client->event == heartbeat_event) {
                }
                else
                if (client->event == expired_event) {
                    terminate_the_client (self, client);
                }
                else
                if (client->event == ohai_event) {
                    try_anonymous_access (self, client);
                    client->state = checking_client_state;
                }
                else {
                    //  Process all other events
                    fmq_msg_set_id (client->reply, FMQ_MSG_RTFM);
                    fmq_msg_send (&client->reply, client->router);
                    client->reply = fmq_msg_new (0);
                    fmq_msg_set_address (client->reply, client->address);
                    terminate_the_client (self, client);
                }
                break;

            case ready_state:
                if (client->event == icanhaz_event) {
                    store_client_subscription (self, client);
                    fmq_msg_set_id (client->reply, FMQ_MSG_ICANHAZ_OK);
                    fmq_msg_send (&client->reply, client->router);
                    client->reply = fmq_msg_new (0);
                    fmq_msg_set_address (client->reply, client->address);
                }
                else
                if (client->event == nom_event) {
                    store_client_credit (self, client);
                    get_next_patch_for_client (self, client);
                    client->state = dispatching_state;
                }
                else
                if (client->event == hugz_event) {
                    fmq_msg_set_id (client->reply, FMQ_MSG_HUGZ_OK);
                    fmq_msg_send (&client->reply, client->router);
                    client->reply = fmq_msg_new (0);
                    fmq_msg_set_address (client->reply, client->address);
                }
                else
                if (client->event == kthxbai_event) {
                    terminate_the_client (self, client);
                }
                else
                if (client->event == dispatch_event) {
                    get_next_patch_for_client (self, client);
                    client->state = dispatching_state;
                }
                else
                if (client->event == heartbeat_event) {
                    fmq_msg_set_id (client->reply, FMQ_MSG_HUGZ);
                    fmq_msg_send (&client->reply, client->router);
                    client->reply = fmq_msg_new (0);
                    fmq_msg_set_address (client->reply, client->address);
                }
                else
                if (client->event == expired_event) {
                    terminate_the_client (self, client);
                }
                else
                if (client->event == ohai_event) {
                    try_anonymous_access (self, client);
                    client->state = checking_client_state;
                }
                else {
                    //  Process all other events
                    fmq_msg_set_id (client->reply, FMQ_MSG_RTFM);
                    fmq_msg_send (&client->reply, client->router);
                    client->reply = fmq_msg_new (0);
                    fmq_msg_set_address (client->reply, client->address);
                    terminate_the_client (self, client);
                }
                break;

            case dispatching_state:
                if (client->event == send_chunk_event) {
                    fmq_msg_set_id (client->reply, FMQ_MSG_CHEEZBURGER);
                    fmq_msg_send (&client->reply, client->router);
                    client->reply = fmq_msg_new (0);
                    fmq_msg_set_address (client->reply, client->address);
                    get_next_patch_for_client (self, client);
                }
                else
                if (client->event == send_delete_event) {
                    fmq_msg_set_id (client->reply, FMQ_MSG_CHEEZBURGER);
                    fmq_msg_send (&client->reply, client->router);
                    client->reply = fmq_msg_new (0);
                    fmq_msg_set_address (client->reply, client->address);
                    get_next_patch_for_client (self, client);
                }
                else
                if (client->event == next_patch_event) {
                    get_next_patch_for_client (self, client);
                }
                else
                if (client->event == no_credit_event) {
                    client->state = ready_state;
                }
                else
                if (client->event == finished_event) {
                    client->state = ready_state;
                }
                else
                if (client->event == heartbeat_event) {
                }
                else
                if (client->event == expired_event) {
                    terminate_the_client (self, client);
                }
                else
                if (client->event == ohai_event) {
                    try_anonymous_access (self, client);
                    client->state = checking_client_state;
                }
                else {
                    //  Process all other events
                    fmq_msg_set_id (client->reply, FMQ_MSG_RTFM);
                    fmq_msg_send (&client->reply, client->router);
                    client->reply = fmq_msg_new (0);
                    fmq_msg_set_address (client->reply, client->address);
                    terminate_the_client (self, client);
                }
                break;

        }
        if (client->next_event == terminate_event) {
            //  Automatically calls client_destroy
            zhash_delete (self->clients, client->hashkey);
            break;
        }
    }
}
コード例 #2
0
ファイル: fmq_msg.c プロジェクト: hintjens/filemq
void
fmq_msg_test (bool verbose)
{
    printf (" * fmq_msg:");

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

    //  @selftest
    //  Simple create/destroy test
    fmq_msg_t *self = fmq_msg_new ();
    assert (self);
    fmq_msg_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-fmq_msg");
    assert (rc == 0);

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


    //  Encode/send/decode and verify each message type
    int instance;
    self = fmq_msg_new ();
    fmq_msg_set_id (self, FMQ_MSG_OHAI);

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

    for (instance = 0; instance < 2; instance++) {
        fmq_msg_recv (self, input);
        assert (fmq_msg_routing_id (self));
    }
    fmq_msg_set_id (self, FMQ_MSG_OHAI_OK);

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

    for (instance = 0; instance < 2; instance++) {
        fmq_msg_recv (self, input);
        assert (fmq_msg_routing_id (self));
    }
    fmq_msg_set_id (self, FMQ_MSG_ICANHAZ);

    fmq_msg_set_path (self, "Life is short but Now lasts for ever");
    zhash_t *icanhaz_options = zhash_new ();
    zhash_insert (icanhaz_options, "Name", "Brutus");
    fmq_msg_set_options (self, &icanhaz_options);
    zhash_t *icanhaz_cache = zhash_new ();
    zhash_insert (icanhaz_cache, "Name", "Brutus");
    fmq_msg_set_cache (self, &icanhaz_cache);
    //  Send twice
    fmq_msg_send (self, output);
    fmq_msg_send (self, output);

    for (instance = 0; instance < 2; instance++) {
        fmq_msg_recv (self, input);
        assert (fmq_msg_routing_id (self));
        assert (streq (fmq_msg_path (self), "Life is short but Now lasts for ever"));
        zhash_t *options = fmq_msg_get_options (self);
        assert (zhash_size (options) == 2);
        assert (streq ((char *) zhash_first (options), "Brutus"));
        assert (streq ((char *) zhash_cursor (options), "Name"));
        zhash_destroy (&options);
        if (instance == 1)
            zhash_destroy (&icanhaz_options);
        zhash_t *cache = fmq_msg_get_cache (self);
        assert (zhash_size (cache) == 2);
        assert (streq ((char *) zhash_first (cache), "Brutus"));
        assert (streq ((char *) zhash_cursor (cache), "Name"));
        zhash_destroy (&cache);
        if (instance == 1)
            zhash_destroy (&icanhaz_cache);
    }
    fmq_msg_set_id (self, FMQ_MSG_ICANHAZ_OK);

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

    for (instance = 0; instance < 2; instance++) {
        fmq_msg_recv (self, input);
        assert (fmq_msg_routing_id (self));
    }
    fmq_msg_set_id (self, FMQ_MSG_NOM);

    fmq_msg_set_credit (self, 123);
    fmq_msg_set_sequence (self, 123);
    //  Send twice
    fmq_msg_send (self, output);
    fmq_msg_send (self, output);

    for (instance = 0; instance < 2; instance++) {
        fmq_msg_recv (self, input);
        assert (fmq_msg_routing_id (self));
        assert (fmq_msg_credit (self) == 123);
        assert (fmq_msg_sequence (self) == 123);
    }
    fmq_msg_set_id (self, FMQ_MSG_CHEEZBURGER);

    fmq_msg_set_sequence (self, 123);
    fmq_msg_set_operation (self, 123);
    fmq_msg_set_filename (self, "Life is short but Now lasts for ever");
    fmq_msg_set_offset (self, 123);
    fmq_msg_set_eof (self, 123);
    zhash_t *cheezburger_headers = zhash_new ();
    zhash_insert (cheezburger_headers, "Name", "Brutus");
    fmq_msg_set_headers (self, &cheezburger_headers);
    zchunk_t *cheezburger_chunk = zchunk_new ("Captcha Diem", 12);
    fmq_msg_set_chunk (self, &cheezburger_chunk);
    //  Send twice
    fmq_msg_send (self, output);
    fmq_msg_send (self, output);

    for (instance = 0; instance < 2; instance++) {
        fmq_msg_recv (self, input);
        assert (fmq_msg_routing_id (self));
        assert (fmq_msg_sequence (self) == 123);
        assert (fmq_msg_operation (self) == 123);
        assert (streq (fmq_msg_filename (self), "Life is short but Now lasts for ever"));
        assert (fmq_msg_offset (self) == 123);
        assert (fmq_msg_eof (self) == 123);
        zhash_t *headers = fmq_msg_get_headers (self);
        assert (zhash_size (headers) == 2);
        assert (streq ((char *) zhash_first (headers), "Brutus"));
        assert (streq ((char *) zhash_cursor (headers), "Name"));
        zhash_destroy (&headers);
        if (instance == 1)
            zhash_destroy (&cheezburger_headers);
        assert (memcmp (zchunk_data (fmq_msg_chunk (self)), "Captcha Diem", 12) == 0);
        if (instance == 1)
            zchunk_destroy (&cheezburger_chunk);
    }
    fmq_msg_set_id (self, FMQ_MSG_HUGZ);

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

    for (instance = 0; instance < 2; instance++) {
        fmq_msg_recv (self, input);
        assert (fmq_msg_routing_id (self));
    }
    fmq_msg_set_id (self, FMQ_MSG_HUGZ_OK);

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

    for (instance = 0; instance < 2; instance++) {
        fmq_msg_recv (self, input);
        assert (fmq_msg_routing_id (self));
    }
    fmq_msg_set_id (self, FMQ_MSG_KTHXBAI);

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

    for (instance = 0; instance < 2; instance++) {
        fmq_msg_recv (self, input);
        assert (fmq_msg_routing_id (self));
    }
    fmq_msg_set_id (self, FMQ_MSG_SRSLY);

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

    for (instance = 0; instance < 2; instance++) {
        fmq_msg_recv (self, input);
        assert (fmq_msg_routing_id (self));
        assert (streq (fmq_msg_reason (self), "Life is short but Now lasts for ever"));
    }
    fmq_msg_set_id (self, FMQ_MSG_RTFM);

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

    for (instance = 0; instance < 2; instance++) {
        fmq_msg_recv (self, input);
        assert (fmq_msg_routing_id (self));
        assert (streq (fmq_msg_reason (self), "Life is short but Now lasts for ever"));
    }

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

    printf ("OK\n");
}