Пример #1
0
Файл: lsd.c Проект: vperron/lsd
//  ---------------------------------------------------------------------
//  Whisper to peer
void lsd_whisper(lsd_handle_t* self, const char* peer, const uint8_t * msg, size_t len) 
{
	assert(self);
	assert(peer);

	zmsg_t* outgoing = zmsg_new();
	zmsg_addstr (outgoing, peer);
	if(msg && len) {
		zmsg_addmem(outgoing, msg, len);
	}
	zre_node_shout(self->interface, &outgoing);
	zmsg_destroy(&outgoing);
}
Пример #2
0
int
main (int argc, char *argv [])
{
    //  Get number of remote nodes to simulate, default 100
    //  If we run multiple zre_perf_remote on multiple machines,
    //  max_node must be sum of all the remote node counts.
    int max_node = 100;
    int max_message = 10000;
    int nbr_node = 0;
    int nbr_hello_response = 0;
    int nbr_message = 0;
    int nbr_message_response = 0;

    if (argc > 1)
        max_node = atoi (argv [1]);
    if (argc > 2)
        max_message = atoi (argv [2]);

    zre_node_t *node = zre_node_new ();
    zre_node_join (node, "GLOBAL");

    int64_t start = zclock_time ();
    int64_t elapse;

    char **peers = zmalloc (sizeof (char *) * max_node);

    while (true) {
        zmsg_t *incoming = zre_node_recv (node);
        if (!incoming)
            break;              //  Interrupted

        //  If new peer, say hello to it and wait for it to answer us
        char *event = zmsg_popstr (incoming);
        if (streq (event, "ENTER")) {
            char *peer = zmsg_popstr (incoming);
            peers[nbr_node++] = peer;

            if (nbr_node == max_node) {
                // got HELLO from the all remote nodes
                elapse = zclock_time () - start;
                printf ("Took %ld ms to coordinate with all remote\n", (long)elapse);
            }
        }
        else
        if (streq (event, "WHISPER")) {
            char *peer = zmsg_popstr (incoming);
            char *cookie = zmsg_popstr (incoming);

            if (streq (cookie, "R:HELLO")) {
                if (++nbr_hello_response == max_node) {
                    // got HELLO from the all remote nodes
                    elapse = zclock_time () - start;
                    printf ("Took %ld ms to get greeting from all remote\n", (long)elapse);
                }
            }
            free (peer);
            free (cookie);
        }
        free (event);
        zmsg_destroy (&incoming);

        if (nbr_node == max_node && nbr_hello_response == max_node)
            break;
    }

    zmq_pollitem_t pollitems [] = {
        { zre_node_handle (node), 0, ZMQ_POLLIN, 0 }
    };

    //  send WHISPER message
    start = zclock_time ();
    for (nbr_message = 0; nbr_message < max_message; nbr_message++) {
        zmsg_t *outgoing = zmsg_new ();
        zmsg_addstr (outgoing, peers [nbr_message % max_node]);
        zmsg_addstr (outgoing, "S:WHISPER");
        zre_node_whisper (node, &outgoing);

        while (zmq_poll (pollitems, 1, 0) > 0) {
            if (s_node_recv (node, "WHISPER", "R:WHISPER"))
                nbr_message_response++;
        }
    }

    while (nbr_message_response < max_message)
        if (s_node_recv (node, "WHISPER", "R:WHISPER"))
            nbr_message_response++;

    // got WHISPER response from the all remote nodes
    elapse = zclock_time () - start;
    printf ("Took %ld ms to send/receive %d message. %.2f msg/s \n", (long)elapse, max_message, (float) max_message * 1000 / elapse);

    //  send SPOUT message
    start = zclock_time ();
    nbr_message = 0;
    nbr_message_response = 0;

    max_message = max_message / max_node;

    for (nbr_message = 0; nbr_message < max_message; nbr_message++) {
        zmsg_t *outgoing = zmsg_new ();
        zmsg_addstr (outgoing, "GLOBAL");
        zmsg_addstr (outgoing, "S:SHOUT");
        zre_node_shout (node, &outgoing);

        while (zmq_poll (pollitems, 1, 0) > 0) {
            if (s_node_recv (node, "SHOUT", "R:SHOUT"))
                nbr_message_response++;
        }
    }

    while (nbr_message_response < max_message * max_node)
        if (s_node_recv (node, "SHOUT", "R:SHOUT"))
            nbr_message_response++;

    // got SHOUT response from the all remote nodes
    elapse = zclock_time () - start;
    printf ("Took %ld ms to send %d, recv %d GROUP message. %.2f msg/s \n",
            (long) elapse, max_message, max_node * max_message,
            (float) max_node * max_message * 1000 / elapse);


    zre_node_destroy (&node);
    for (nbr_node = 0; nbr_node < max_node; nbr_node++) {
        free (peers[nbr_node]);
    }
    free (peers);
    return 0;
}
Пример #3
0
static void
node_task (void *args, zctx_t *ctx, void *pipe)
{
    zre_node_t *node = zre_node_new ();
    int64_t counter = 0;
    char *to_peer = NULL;        //  Either of these set,
    char *to_group = NULL;       //    and we set a message
    char *cookie = NULL;

    zmq_pollitem_t pollitems [] = {
        { pipe,                             0, ZMQ_POLLIN, 0 },
        { zre_node_handle (node), 0, ZMQ_POLLIN, 0 }
    };
    //  Do something once a second
    int64_t trigger = zclock_time () + 1000;
    while (!zctx_interrupted) {
        if (zmq_poll (pollitems, 2, randof (1000) * ZMQ_POLL_MSEC) == -1)
            break;              //  Interrupted

        if (pollitems [0].revents & ZMQ_POLLIN)
            break;              //  Any command from parent means EXIT

        //  Process an event from node
        if (pollitems [1].revents & ZMQ_POLLIN) {
            zmsg_t *incoming = zre_node_recv (node);
            if (!incoming)
                break;              //  Interrupted

            char *event = zmsg_popstr (incoming);
            if (streq (event, "ENTER")) {
                //  Always say hello to new peer
                to_peer = zmsg_popstr (incoming);
            }
            else if (streq (event, "EXIT")) {
                //  Always try talk to departed peer
                to_peer = zmsg_popstr (incoming);
            }
            else if (streq (event, "WHISPER")) {
                //  Send back response 1/2 the time
                if (randof (2) == 0) {
                    to_peer = zmsg_popstr (incoming);
                    cookie = zmsg_popstr (incoming);
                }
            }
            else if (streq (event, "SHOUT")) {
                to_peer = zmsg_popstr (incoming);
                to_group = zmsg_popstr (incoming);
                cookie = zmsg_popstr (incoming);
                //  Send peer response 1/3rd the time
                if (randof (3) > 0) {
                    free (to_peer);
                    to_peer = NULL;
                }
                //  Send group response 1/3rd the time
                if (randof (3) > 0) {
                    free (to_group);
                    to_group = NULL;
                }
            }
            else if (streq (event, "JOIN")) {
                char *from_peer = zmsg_popstr (incoming);
                char *group = zmsg_popstr (incoming);
                if (randof (3) > 0) {
                    zre_node_join (node, group);
                }
                free (from_peer);
                free (group);
            }
            else if (streq (event, "LEAVE")) {
                char *from_peer = zmsg_popstr (incoming);
                char *group = zmsg_popstr (incoming);
                if (randof (3) > 0) {
                    zre_node_leave (node, group);
                }
                free (from_peer);
                free (group);
            }
            else if (streq (event, "DELIVER")) {
                char *filename = zmsg_popstr (incoming);
                char *fullname = zmsg_popstr (incoming);
                printf ("I: received file %s\n", fullname);
                free (fullname);
                free (filename);
            }
            free (event);
            zmsg_destroy (&incoming);

            //  Send outgoing messages if needed
            if (to_peer) {
                zmsg_t *outgoing = zmsg_new ();
                zmsg_addstr (outgoing, to_peer);
                zmsg_addstr (outgoing, "%lu", counter++);
                zre_node_whisper (node, &outgoing);
                free (to_peer);
                to_peer = NULL;
            }
            if (to_group) {
                zmsg_t *outgoing = zmsg_new ();
                zmsg_addstr (outgoing, to_group);
                zmsg_addstr (outgoing, "%lu", counter++);
                zre_node_shout (node, &outgoing);
                free (to_group);
                to_group = NULL;
            }
            if (cookie) {
                free (cookie);
                cookie = NULL;
            }
        }
        if (zclock_time () >= trigger) {
            trigger = zclock_time () + 1000;
            char group [10];
            sprintf (group, "GROUP%03d", randof (MAX_GROUP));
            if (randof (4) == 0)
                zre_node_join (node, group);
            else if (randof (3) == 0)
                zre_node_leave (node, group);
        }
    }
    zre_node_destroy (&node);
}