예제 #1
0
파일: lsd.c 프로젝트: vperron/lsd
//  ---------------------------------------------------------------------
//  Leave group
void lsd_leave(lsd_handle_t* self, const char* group)
{
	assert(self);
	assert(group);
	zre_node_leave(self->interface, group);
}
예제 #2
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);
}