Exemplo n.º 1
0
int main (int argc, char *argv [])
{
    puts (PRODUCT);
    puts (COPYRIGHT);
    puts (NOWARRANTY);

    fmq_server_t *server = NULL;
    fmq_client_t *client = NULL;

    if (argc < 2 || streq (argv [1], "-s")) {
        server = fmq_server_new ();
        fmq_server_configure (server, "server_test.cfg");
        fmq_server_publish (server, "./fmqroot/send", "/");
        fmq_server_publish (server, "./fmqroot/logs", "/logs");
        //  We do this last
        fmq_server_bind (server, "tcp://*:6000");
    }
    if (argc < 2 || streq (argv [1], "-c")) {
        client = fmq_client_new ();
        fmq_client_configure (client, "client_test.cfg");
        fmq_client_setoption (client, "client/inbox", "./fmqroot/recv");
        fmq_client_connect   (client, "tcp://localhost:6000");
        fmq_client_subscribe (client, "/");
        fmq_client_subscribe (client, "/photos");
        fmq_client_subscribe (client, "/logs");
    }
    while (!zctx_interrupted)
        sleep (1);
    puts ("interrupted");

    fmq_server_destroy (&server);
    fmq_client_destroy (&client);
    return 0;
}
Exemplo n.º 2
0
static agent_t *
agent_new (zctx_t *ctx, void *pipe)
{
    void *inbox = zsocket_new (ctx, ZMQ_ROUTER);
    if (!inbox)                 //  Interrupted
        return NULL;

    agent_t *self = (agent_t *) zmalloc (sizeof (agent_t));
    self->ctx = ctx;
    self->pipe = pipe;
    self->udp = zre_udp_new (ZRE_DISCOVERY_PORT);
    self->inbox = inbox;
    self->host = zre_udp_host (self->udp);
    self->port = zsocket_bind (self->inbox, "tcp://*:*");
    sprintf (self->endpoint, "%s:%d", self->host, self->port);
    if (self->port < 0) {       //  Interrupted
        zre_udp_destroy (&self->udp);
        free (self);
        return NULL;
    }
    self->uuid = zre_uuid_new ();
    self->identity = strdup (zre_uuid_str (self->uuid));
    self->peers = zhash_new ();
    self->peer_groups = zhash_new ();
    self->own_groups = zhash_new ();
    self->headers = zhash_new ();
    zhash_autofree (self->headers);
    self->log = zre_log_new (self->endpoint);

    //  Set up content distribution network: Each server binds to an
    //  ephemeral port and publishes a temporary directory that acts
    //  as the outbox for this node.
    //
    sprintf (self->fmq_outbox, "%s/send/%s", s_tmpdir (), self->identity);
    zfile_mkdir (self->fmq_outbox);
    sprintf (self->fmq_inbox, "%s/recv/%s", s_tmpdir (), self->identity);
    zfile_mkdir (self->fmq_inbox);

    self->fmq_server = fmq_server_new ();
    self->fmq_service = fmq_server_bind (self->fmq_server, "tcp://*:*");
    fmq_server_publish (self->fmq_server, self->fmq_outbox, "/");
    fmq_server_set_anonymous (self->fmq_server, true);
    char publisher [32];
    sprintf (publisher, "tcp://%s:%d", self->host, self->fmq_service);
    zhash_update (self->headers, "X-FILEMQ", publisher);

    //  Client will connect as it discovers new nodes
    self->fmq_client = fmq_client_new ();
    fmq_client_set_inbox (self->fmq_client, self->fmq_inbox);
    fmq_client_set_resync (self->fmq_client, true);
    fmq_client_subscribe (self->fmq_client, "/");
    
    return self;
}
Exemplo n.º 3
0
int main (int argc, char *argv [])
{
    if (argc < 3) {
        puts ("usage: track original-directory tracking-directory");
        return 0;
    }
    fmq_server_t *server = fmq_server_new ();
    fmq_server_configure (server, "anonymous.cfg");
    fmq_server_publish (server, argv [1], "/");
    fmq_server_set_anonymous (server, true);
    fmq_server_bind (server, "tcp://*:5670");

    fmq_client_t *client = fmq_client_new ();
    fmq_client_set_inbox (client, argv [2]);
    fmq_client_set_resync (client, true);
    fmq_client_subscribe (client, "/");
    fmq_client_connect (client, "tcp://localhost:5670");

    while (true) {
        //  Get message from fmq_client API
        zmsg_t *msg = fmq_client_recv (client);
        if (!msg)
            break;              //  Interrupted
        char *command = zmsg_popstr (msg);
        if (streq (command, "DELIVER")) {
            char *filename = zmsg_popstr (msg);
            char *fullname = zmsg_popstr (msg);
            printf ("I: received %s (%s)\n", filename, fullname);
            free (filename);
            free (fullname);
        }
        free (command);
        zmsg_destroy (&msg);
    }
    fmq_server_destroy (&server);
    fmq_client_destroy (&client);
    return 0;
}
Exemplo n.º 4
0
int
fmq_server_test (bool verbose)
{
    printf (" * fmq_server: ");
    fflush (stdout);
    zctx_t *ctx = zctx_new ();
    
    fmq_server_t *self;
    void *dealer = zsocket_new (ctx, ZMQ_DEALER);
    zsocket_set_rcvtimeo (dealer, 2000);
    zsocket_connect (dealer, "tcp://localhost:5670");
    
    fmq_msg_t *request, *reply;
    
    //  Run selftest using '' configuration
    self = fmq_server_new ();
    assert (self);
    int port = fmq_server_bind (self, "tcp://*:5670");
    assert (port == 5670);                            
    request = fmq_msg_new (FMQ_MSG_OHAI);
    fmq_msg_send (&request, dealer);
    reply = fmq_msg_recv (dealer);
    assert (reply);
    assert (fmq_msg_id (reply) == FMQ_MSG_SRSLY);
    fmq_msg_destroy (&reply);

    request = fmq_msg_new (FMQ_MSG_ICANHAZ);
    fmq_msg_send (&request, dealer);
    reply = fmq_msg_recv (dealer);
    assert (reply);
    assert (fmq_msg_id (reply) == FMQ_MSG_RTFM);
    fmq_msg_destroy (&reply);

    request = fmq_msg_new (FMQ_MSG_NOM);
    fmq_msg_send (&request, dealer);
    reply = fmq_msg_recv (dealer);
    assert (reply);
    assert (fmq_msg_id (reply) == FMQ_MSG_RTFM);
    fmq_msg_destroy (&reply);

    request = fmq_msg_new (FMQ_MSG_HUGZ);
    fmq_msg_send (&request, dealer);
    reply = fmq_msg_recv (dealer);
    assert (reply);
    assert (fmq_msg_id (reply) == FMQ_MSG_RTFM);
    fmq_msg_destroy (&reply);

    fmq_server_destroy (&self);
    //  Run selftest using 'anonymous.cfg' configuration
    self = fmq_server_new ();
    assert (self);
    fmq_server_configure (self, "anonymous.cfg");
    port = fmq_server_bind (self, "tcp://*:5670");
    assert (port == 5670);                        
    request = fmq_msg_new (FMQ_MSG_OHAI);
    fmq_msg_send (&request, dealer);
    reply = fmq_msg_recv (dealer);
    assert (reply);
    assert (fmq_msg_id (reply) == FMQ_MSG_OHAI_OK);
    fmq_msg_destroy (&reply);

    request = fmq_msg_new (FMQ_MSG_NOM);
    fmq_msg_send (&request, dealer);

    request = fmq_msg_new (FMQ_MSG_HUGZ);
    fmq_msg_send (&request, dealer);
    reply = fmq_msg_recv (dealer);
    assert (reply);
    assert (fmq_msg_id (reply) == FMQ_MSG_HUGZ_OK);
    fmq_msg_destroy (&reply);

    request = fmq_msg_new (FMQ_MSG_YARLY);
    fmq_msg_send (&request, dealer);
    reply = fmq_msg_recv (dealer);
    assert (reply);
    assert (fmq_msg_id (reply) == FMQ_MSG_RTFM);
    fmq_msg_destroy (&reply);

    fmq_server_destroy (&self);
    //  Run selftest using 'server_test.cfg' configuration
    self = fmq_server_new ();
    assert (self);
    fmq_server_configure (self, "server_test.cfg");
    port = fmq_server_bind (self, "tcp://*:5670");
    assert (port == 5670);                        
    request = fmq_msg_new (FMQ_MSG_OHAI);
    fmq_msg_send (&request, dealer);
    reply = fmq_msg_recv (dealer);
    assert (reply);
    assert (fmq_msg_id (reply) == FMQ_MSG_ORLY);
    fmq_msg_destroy (&reply);

    request = fmq_msg_new (FMQ_MSG_YARLY);
    fmq_msg_set_mechanism (request, "PLAIN");                                
    fmq_msg_set_response (request, fmq_sasl_plain_encode ("guest", "guest"));
    fmq_msg_send (&request, dealer);
    reply = fmq_msg_recv (dealer);
    assert (reply);
    assert (fmq_msg_id (reply) == FMQ_MSG_OHAI_OK);
    fmq_msg_destroy (&reply);

    request = fmq_msg_new (FMQ_MSG_NOM);
    fmq_msg_send (&request, dealer);

    request = fmq_msg_new (FMQ_MSG_HUGZ);
    fmq_msg_send (&request, dealer);
    reply = fmq_msg_recv (dealer);
    assert (reply);
    assert (fmq_msg_id (reply) == FMQ_MSG_HUGZ_OK);
    fmq_msg_destroy (&reply);

    reply = fmq_msg_recv (dealer);
    assert (reply);
    assert (fmq_msg_id (reply) == FMQ_MSG_HUGZ);
    fmq_msg_destroy (&reply);

    fmq_server_destroy (&self);

    zctx_destroy (&ctx);
    //  @end

    //  No clean way to wait for a background thread to exit
    //  Under valgrind this will randomly show as leakage
    //  Reduce this by giving server thread time to exit
    zclock_sleep (200);
    printf ("OK\n");
    return 0;
}
Exemplo n.º 5
0
int main (int argc, char *argv [])
{
    bool verbose;
    if (argc == 2 && streq (argv [1], "-v")) {
        argc--;
        verbose = true;
    }
    else
        verbose = false;

    //  Do normal checks if run without arguments
    if (argc < 2) {
        printf ("Running self tests...\n");
        fmq_patch_test (verbose);
        fmq_dir_test (verbose);
        fmq_msg_test (verbose);
        fmq_sasl_test (verbose);
        fmq_hash_test (verbose);
        fmq_server_test (verbose);
        fmq_client_test (verbose);
        printf ("Tests passed OK\n");
        return 0;
    }

    //  Else run as FILEMQ server or client
    if (streq (argv [1], "-s")) {
        fmq_server_t *server = fmq_server_new ();
        fmq_server_configure (server, "server_test.cfg");
        fmq_server_publish (server, "./fmqroot/send", "/");
        fmq_server_publish (server, "./fmqroot/logs", "/logs");
        //  We do this last
        fmq_server_bind (server, "tcp://*:5670");
        while (!zctx_interrupted)
            zclock_sleep (1000);
        fmq_server_destroy (&server);
    }
    else
    if (streq (argv [1], "-c")) {
        fmq_client_t *client = fmq_client_new ();
        fmq_client_configure (client, "client_test.cfg");
        fmq_client_setoption (client, "client/inbox", "./fmqroot/recv");
        fmq_client_set_resync (client, true);
        fmq_client_subscribe (client, "/photos");
        fmq_client_subscribe (client, "/logs");
        fmq_client_connect (client, "tcp://localhost:5670");

        while (true) {
            //  Get message from fmq_client API
            zmsg_t *msg = fmq_client_recv (client);
            if (!msg)
                break;              //  Interrupted
            char *command = zmsg_popstr (msg);
            if (streq (command, "DELIVER")) {
                char *filename = zmsg_popstr (msg);
                char *fullname = zmsg_popstr (msg);
                printf ("I: received %s (%s)\n", filename, fullname);
                free (filename);
                free (fullname);
            }
            free (command);
            zmsg_destroy (&msg);
        }
        fmq_client_destroy (&client);
    }
    return 0;
}