Пример #1
0
/* ================ handle_pullin_on_client_pipe() ================ */
int handle_pullin_on_client_pipe(zloop_t *loop, zsock_t *pipe, void *user_data)
{
    client_t *client = (client_t*)user_data;

    if ( over_actors >= total_actors ){
        zloop_reader_end(loop, pipe);
        return -1;
    }

    zmsg_t *msg = zmsg_recv(pipe);
    if ( msg == NULL ){
        zloop_reader_end(loop, pipe);
        return -1;
    }

    /*zmsg_print(msg);*/

    if ( message_check_status(msg, MSG_STATUS_ACTOR_OVER) == 0 ){
        over_actors++;
        info_log("Actor %d over! (%d/%d)", client->id, over_actors, total_actors);
    }

    zmsg_destroy(&msg);

    return 0;
}
Пример #2
0
void client_proxy(zsock_t *pipe, void *arg) {
    client_proxy_t *self = s_new(pipe);
    assert(self);
    zsock_signal(self->pipe, 0);
    zloop_reader(self->loop, self->pipe, pipe_loop_handler, self);
    zloop_start(self->loop);
    zloop_reader_end(self->loop, self->pipe);
    if (self->rep != NULL)
        zloop_reader_end(self->loop, self->rep);
    s_destroy(&self);
}
Пример #3
0
Файл: zloop.c Проект: skaes/czmq
static int
s_timer_event5 (zloop_t *loop, int timer_id, void *arg)
{
    //  remove reader from loop
    zloop_reader_end(loop, (zsock_t *) arg);

    //  end reactor on next run
    zloop_timer(loop, 1, 1, s_timer_event4, NULL);

    return 0;
}
Пример #4
0
void ticket_hid_printer(zsock_t *pipe, void *arg) {
    char *device_path = (char *) arg;
    ticket_printer_t *self = s_new(pipe, device_path);
    zsock_signal(self->pipe, 0);

    int main_timer_id = zloop_timer(self->loop, 300, 0, timer_main_loop, self);
    zloop_reader(self->loop, self->pipe, reader_pipe_event, self);
    zloop_start(self->loop);
    zloop_timer_end(self->loop, main_timer_id);
    zloop_reader_end(self->loop, self->pipe);
    s_destroy(&self);
}
Пример #5
0
void ticket_serial_printer(zsock_t *pipe, void *arg) {
    char *port = (char *) arg;
    ticket_printer_t *self = s_new_serial(pipe, port);
    zsock_signal(self->pipe, 0);

    int main_serial_id = zloop_timer(self->loop, 300, 0, timer_main_serial_loop, self);
    zloop_reader(self->loop, self->pipe, reader_pipe_event, self);
    zloop_start(self->loop);
    zloop_reader_end(self->loop, self->pipe);
    zloop_timer_end(self->loop, main_serial_id);
    s_destroy(&self);
}
Пример #6
0
int
zloop_start (zloop_t *self)
{
    assert (self);
    int rc = 0;

    //  Main reactor loop
    while (!zsys_interrupted) {
        if (self->need_rebuild) {
            //  If s_rebuild_pollset() fails, break out of the loop and
            //  return its error
            rc = s_rebuild_pollset (self);
            if (rc)
                break;
        }
        rc = zmq_poll (self->pollset, (int) self->poll_size, s_tickless (self));
        if (rc == -1 || zsys_interrupted) {
            if (self->verbose)
                zsys_debug ("zloop: interrupted");
            rc = 0;
            break;              //  Context has been shut down
        }
        
        //  Handle any timers that have now expired
        int64_t time_now = zclock_mono ();
        s_timer_t *timer = (s_timer_t *) zlistx_first (self->timers);
        while (timer) {
            if (time_now >= timer->when) {
                if (self->verbose)
                    zsys_debug ("zloop: call timer handler id=%d", timer->timer_id);
                rc = timer->handler (self, timer->timer_id, timer->arg);
                if (rc == -1)
                    break;      //  Timer handler signaled break
                if (timer->times && --timer->times == 0)
                    zlistx_delete (self->timers, timer->list_handle);
                else
                    timer->when += timer->delay;
            }
            timer = (s_timer_t *) zlistx_next (self->timers);
        }
        
        //  Handle any tickets that have now expired
        s_ticket_t *ticket = (s_ticket_t *) zlistx_first (self->tickets);
        while (ticket && time_now >= ticket->when) {
            if (self->verbose)
                zsys_debug ("zloop: call ticket handler");
            rc = ticket->handler (self, 0, ticket->arg);
            if (rc == -1)
                break;      //  Timer handler signaled break
            zlistx_delete (self->tickets, ticket->list_handle);
            ticket = (s_ticket_t *) zlistx_next (self->tickets);
        }
        
        //  Handle any readers and pollers that are ready
        size_t item_nbr;
        for (item_nbr = 0; item_nbr < self->poll_size && rc >= 0; item_nbr++) {
            s_reader_t *reader = &self->readact [item_nbr];
            if (reader->handler) {
                if ((self->pollset [item_nbr].revents & ZMQ_POLLERR)
                && !reader->tolerant) {
                    if (self->verbose)
                        zsys_warning ("zloop: can't read %s socket: %s",
                                      zsock_type_str (reader->sock),
                                      zmq_strerror (zmq_errno ()));
                    //  Give handler one chance to handle error, then kill
                    //  reader because it'll disrupt the reactor otherwise.
                    if (reader->errors++) {
                        zloop_reader_end (self, reader->sock);
                        self->pollset [item_nbr].revents = 0;
                    }
                }
                else
                    reader->errors = 0;     //  A non-error happened

                if (self->pollset [item_nbr].revents) {
                    if (self->verbose)
                        zsys_debug ("zloop: call %s socket handler",
                                    zsock_type_str (reader->sock));
                    rc = reader->handler (self, reader->sock, reader->arg);
                    if (rc == -1 || self->need_rebuild)
                        break;
                }
            }
            else {
                s_poller_t *poller = &self->pollact [item_nbr];
                assert (self->pollset [item_nbr].socket == poller->item.socket);

                if ((self->pollset [item_nbr].revents & ZMQ_POLLERR)
                && !poller->tolerant) {
                    if (self->verbose)
                        zsys_warning ("zloop: can't poll %s socket (%p, %d): %s",
                                      poller->item.socket ?
                                      zsys_sockname (zsock_type (poller->item.socket)) : "FD",
                                      poller->item.socket, poller->item.fd,
                                      zmq_strerror (zmq_errno ()));
                    //  Give handler one chance to handle error, then kill
                    //  poller because it'll disrupt the reactor otherwise.
                    if (poller->errors++) {
                        zloop_poller_end (self, &poller->item);
                        self->pollset [item_nbr].revents = 0;
                    }
                }
                else
                    poller->errors = 0;     //  A non-error happened

                if (self->pollset [item_nbr].revents) {
                    if (self->verbose)
                        zsys_debug ("zloop: call %s socket handler (%p, %d)",
                                    poller->item.socket ?
                                    zsys_sockname (zsock_type (poller->item.socket)) : "FD",
                                    poller->item.socket, poller->item.fd);
                    rc = poller->handler (self, &self->pollset [item_nbr], poller->arg);
                    if (rc == -1 || self->need_rebuild)
                        break;
                }
            }
        }
        //  Now handle any timer zombies
        //  This is going to be slow if we have many timers; we might use
        //  a faster lookup on the timer list.
        while (zlistx_first (self->zombies)) {
            //  Get timer_id back from pointer
            int timer_id = (byte *) zlistx_detach (self->zombies, NULL) - (byte *) NULL;
            s_timer_remove (self, timer_id);
        }
        if (rc == -1)
            break;
    }
    self->terminated = true;
    return rc;
}
Пример #7
0
///
//  Cancel a socket reader from the reactor. If multiple readers exist for
//  same socket, cancels ALL of them.                                     
void QmlZloop::readerEnd (QmlZsock *sock) {
    zloop_reader_end (self, sock->self);
};
Пример #8
0
JNIEXPORT void JNICALL
Java_org_zeromq_czmq_Zloop__1_1readerEnd (JNIEnv *env, jclass c, jlong self, jlong sock)
{
    zloop_reader_end ((zloop_t *) (intptr_t) self, (zsock_t *) (intptr_t) sock);
}