Example #1
0
void
zmailer_msg_print (zmailer_msg_t *self)
{
    assert (self);
    switch (self->id) {
        case ZMAILER_MSG_MAIL:
            zsys_debug ("ZMAILER_MSG_MAIL:");
            zsys_debug ("    version=1");
            zsys_debug ("    from='%s'", self->from);
            if (self->to)
                zsys_debug ("    to='%s'", self->to);
            else
                zsys_debug ("    to=");
            if (self->subject)
                zsys_debug ("    subject='%s'", self->subject);
            else
                zsys_debug ("    subject=");
            if (self->request)
                zsys_debug ("    request='%s'", self->request);
            else
                zsys_debug ("    request=");
            break;

    }
}
Example #2
0
static long
s_tickless (zloop_t *self)
{
    //  Calculate tickless timer, up to 1 hour
    int64_t tickless = zclock_mono () + 1000 * 3600;
    
    //  Scan timers, which are not sorted
    //  TODO: sort timers properly on insertion
    s_timer_t *timer = (s_timer_t *) zlistx_first (self->timers);
    while (timer) {
        //  Find earliest timer
        if (tickless > timer->when)
            tickless = timer->when;
        timer = (s_timer_t *) zlistx_next (self->timers);
    }
    //  Tickets are sorted, so check first ticket
    s_ticket_t *ticket = (s_ticket_t *) zlistx_first (self->tickets);
    if (ticket && tickless > ticket->when)
        tickless = ticket->when;

    long timeout = (long) (tickless - zclock_mono ());
    if (timeout < 0)
        timeout = 0;
    if (self->verbose)
        zsys_debug ("zloop polling for %d msec", (int) timeout);
    
    return timeout * ZMQ_POLL_MSEC;
}
Example #3
0
void
zloop_poller_end (zloop_t *self, zmq_pollitem_t *item)
{
    assert (self);

    s_poller_t *poller = (s_poller_t *) zlistx_first (self->pollers);
    while (poller) {
        bool match = false;
        if (item->socket) {
            if (item->socket == poller->item.socket)
                match = true;
        }
        else {
            if (item->fd == poller->item.fd)
                match = true;
        }
        if (match) {
            zlistx_delete (self->pollers, poller->list_handle);
            //  Force rebuild to avoid reading from freed poller
            self->need_rebuild = true;
        }
        poller = (s_poller_t *) zlistx_next (self->pollers);
    }
    if (self->verbose)
        zsys_debug ("zloop: cancel %s poller (%p, %d)",
                    item->socket ? zsys_sockname (zsock_type (item->socket)) : "FD",
                    item->socket, item->fd);
}
Example #4
0
int
zloop_timer (zloop_t *self, size_t delay, size_t times, zloop_timer_fn handler, void *arg)
{
    assert (self);
    //  Catch excessive use of timers
    if (self->max_timers && zlistx_size (self->timers) == self->max_timers) {
        zsys_error ("zloop: timer limit reached (max=%d)", self->max_timers);
        return -1;
    }
    int timer_id = s_next_timer_id (self);
    s_timer_t *timer = s_timer_new (timer_id, delay, times, handler, arg);
    if (timer) {
        timer->list_handle = zlistx_add_end (self->timers, timer);
        if (!timer->list_handle) {
            s_timer_destroy (&timer);
            return -1;
        }
        if (self->verbose)
            zsys_debug ("zloop: register timer id=%d delay=%d times=%d",
                        timer_id, (int) delay, (int) times);
        return timer_id;
    }
    else
        return -1;
}
JNIEXPORT void JNICALL
Java_org_zeromq_czmq_Zsys__1_1debug (JNIEnv *env, jclass c, jstring format)
{
    char *format_ = (char *) (*env)->GetStringUTFChars (env, format, NULL);
    zsys_debug (format_);
    (*env)->ReleaseStringUTFChars (env, format, format_);
}
Example #6
0
int
zloop_poller (zloop_t *self, zmq_pollitem_t *item, zloop_fn handler, void *arg)
{
    assert (self);

    if (item->socket
    &&  streq (zsys_sockname (zsock_type (item->socket)), "UNKNOWN"))
        return -1;

    s_poller_t *poller = s_poller_new (item, handler, arg);
    if (poller) {
        poller->list_handle = zlistx_add_end (self->pollers, poller);
        if (!poller->list_handle) {
            s_poller_destroy (&poller);
            return -1;
        }
        self->need_rebuild = true;
        if (self->verbose)
            zsys_debug ("zloop: register %s poller (%p, %d)",
                        item->socket ? zsys_sockname (zsock_type (item->socket)) : "FD",
                        item->socket, item->fd);
        return 0;
    }
    else
        return -1;
}
Example #7
0
int main(int argc, char** argv) {
    if(argc != 2) {
        fprintf(stderr, "Usage: %s ups_name\n", argv[0]);
        exit(1);
    }

    char *addr = NULL;
    zyre_t *n = zyre_new(argv[1]);
    zyre_start(n);
    zyre_join(n, "BIOS");
    while(!zsys_interrupted && addr == NULL) {
        zyre_event_t *e = zyre_event_new(n);
        if(!e)
            break;
        if(zyre_event_headers(e) && zyre_event_header(e, "HAP_SERVER") != NULL) {
            addr = strdup(zyre_event_header(e, "HAP_SERVER"));
            printf("Address: %s\n", addr);
        }
        zyre_event_destroy(&e);
    }
    zyre_destroy(&n);

    if(addr == NULL)
        exit(1);

    zsock_t * sc = zsock_new(ZMQ_PUB);
    zsock_connect(sc, "%s", addr);
    bool state = random()%2;
    int timeout = 0;
    while(!zsys_interrupted) {
        if(timeout == 0) {
            state = !state;
            timeout = 5 + random()%20;
        }
        timeout--;
        if(state) {
            zstr_sendx(sc, argv[1], "ON", NULL);
            zsys_debug("UPS %s ON", argv[1]);
        } else {
            zstr_sendx(sc, argv[1], "OFF", NULL);
            zsys_debug("UPS %s OFF", argv[1]);
        }
        sleep(1);
    }
    zsock_destroy(&sc);
}
Example #8
0
int
zloop_timer (zloop_t *self, size_t delay, size_t times, zloop_timer_fn handler, void *arg)
{
    assert (self);
    int timer_id = s_next_timer_id (self);
    s_timer_t *timer = s_timer_new (timer_id, delay, times, handler, arg);
    if (!timer)
        return -1;
    if (zlist_append (self->timers, timer))
        return -1;
    if (self->verbose)
#ifdef __WINDOWS__
		zsys_debug ("zloop: register timer id=%d delay=%u times=%u",
                    timer_id, delay, times);
#else
        zsys_debug ("zloop: register timer id=%d delay=%zd times=%zd",
                    timer_id, delay, times);
#endif
    
    return timer_id;
}
Example #9
0
static void
connect_to_server (client_t *self)
{
    if (zsock_connect(self->dealer, "%s", self->args->endpoint)) {
        engine_set_exception(self, connect_error_event);
        zsys_warning("could not connect to %s", self->args->endpoint);
        zsock_send(self->cmdpipe, "si", "FAILURE", 0);
    }
    else {
        zsys_debug("connected to %s", self->args->endpoint);
        zsock_send(self->cmdpipe, "si", "SUCCESS", 0);
    }
}
Example #10
0
static int reader_rep_event(zloop_t *loop, zsock_t *sink, void *arg) {
    client_proxy_t *self = arg;
    zmsg_t *request = zmsg_recv(self->rep);
    char *correlation_id = zmsg_popstr(request);
    zmsg_t *dup = zmsg_dup(request);
    if (!request) {
        return 0;
    }
    char *command = zmsg_popstr(request);
    bool is_ticket_command =
            streq(command, "PRINT") || streq(command, "GETTICKETINFO") || streq(command, "DETAILREPORT") ||
            streq(command, "REPORT") || streq(command, "SIMPLEREPORT") || streq(command, "SUMMARY") || streq(command,"UNPRINT");
    if (self->ticket_store_req != NULL && is_ticket_command) {
        zmsg_send(&dup, self->ticket_store_req);
        zmsg_t *resp = zmsg_recv(self->ticket_store_req);
        zmsg_pushstr(resp, correlation_id);
        if (self->verbose) {
            zsys_debug("client proxy: sending response from ticket store for command %s", command);
            zmsg_print(resp);
        }
        zmsg_send(&resp, self->rep);
    }
    bool is_printer_store_command = streq(command, "GETPRINTERS") || streq(command, "GETPRINTER") || streq(command, "SCANPRINTERS");
    if (self->printer_store_req != NULL && is_printer_store_command) {
        zmsg_send(&dup, self->printer_store_req);
        zmsg_t *resp = zmsg_recv(self->printer_store_req);
        zmsg_pushstr(resp, correlation_id);
        if (self->verbose) {
            zsys_debug("client proxy: sending response from printer store");
            zmsg_print(resp);
        }
        zmsg_send(&resp, self->rep);
    }
    zstr_free(&correlation_id);
    zstr_free(&command);
    zmsg_destroy(&request);
    return 0;
}
Example #11
0
enum BOCA_FEATURE_REPORT_STATUS boca_hid_status_feature(boca_hid_printer_t *self) {
    enum BOCA_FEATURE_REPORT_STATUS ret = BOCA_REPORT_UNKNOWN;
    if (!self->device)
        return ret;
    unsigned char *buf = (unsigned char *) calloc(PACKET_SIZE, sizeof(byte));
    buf[0] = 0x0;
    int byte_read = hid_get_feature_report(self->device, buf, sizeof(byte) * PACKET_SIZE);
    if(self->verbose)
        zsys_debug("hid printer: read %d bytes from feature port", byte_read);
    if (byte_read > 0) {
        if (byte_read == 2)
            ret = (enum BOCA_FEATURE_REPORT_STATUS) (int) buf[1];
        else
            ret = (enum BOCA_FEATURE_REPORT_STATUS) (int) buf[0];
        if (self->verbose) {
            zsys_debug("hid printer: BOCA status %s", boca_feature_report_display(ret));
        }

    } else {
        zsys_warning("hid printer: could not read from BOCA %ls", hid_error(self->device));
    }
    return ret;
}
Example #12
0
static int reader_internal_sub_event(zloop_t *loop, zsock_t *sub_sock, void *arg) {
    int ret = 0;
    client_proxy_t *self = (client_proxy_t *) arg;
    zmsg_t *request = zmsg_recv(sub_sock);
    if (!request) {
        return ret;
    }
    if (self->pub) {
        if (self->verbose) {
            zsys_debug("client proxy: republishing message.");
            zmsg_print(request);
        }
        zmsg_send(&request, self->pub);
    }
    return ret;
}
Example #13
0
void
zmsg_print (zmsg_t *self)
{
    assert (self);
    assert (zmsg_is (self));

    if (!self) {
        zsys_debug ("(NULL)");
        return;
    }
    zframe_t *frame = zmsg_first (self);
    while (frame) {
        zframe_print (frame, NULL);
        frame = zmsg_next (self);
    }
}
Example #14
0
void
zloop_reader_end (zloop_t *self, zsock_t *sock)
{
    assert (self);
    assert (sock);

    s_reader_t *reader = (s_reader_t *) zlistx_first (self->readers);
    while (reader) {
        if (reader->sock == sock) {
            zlistx_delete (self->readers, reader->list_handle);
            self->need_rebuild = true;
        }
        reader = (s_reader_t *) zlistx_next (self->readers);
    }
    if (self->verbose)
        zsys_debug ("zloop: cancel %s reader", zsock_type_str (sock));
}
Example #15
0
File: zloop.c Project: skaes/czmq
int
zloop_reader (zloop_t *self, zsock_t *sock, zloop_reader_fn handler, void *arg)
{
    assert (self);
    assert (sock);

    s_reader_t *reader = s_reader_new (sock, handler, arg);
    if (reader) {
        reader->list_handle = zlistx_add_end (self->readers, reader);
        assert (reader->list_handle);
        self->need_rebuild = true;
        if (self->verbose)
            zsys_debug ("zloop: register %s reader", zsock_type_str (sock));
        return 0;
    }
    else
        return -1;
}
Example #16
0
File: zloop.c Project: skaes/czmq
int
zloop_timer_end (zloop_t *self, int timer_id)
{
    assert (self);

    if (self->terminated)
        s_timer_remove (self, timer_id);
    else
        //  We cannot touch self->timers because we may be executing that
        //  from inside the poll loop. So, we hold the arg on the zombie
        //  list, and process that list when we're done executing timers.
        //  This hack lets us store an integer timer ID as a pointer
        zlistx_add_end (self->zombies, (byte *) NULL + timer_id);

    if (self->verbose)
        zsys_debug ("zloop: cancel timer id=%d", timer_id);

    return 0;
}
Example #17
0
static int reader_pipe_event(zloop_t *loop, zsock_t *reader, void *arg) {
    int ret = 0;
    ticket_printer_t *self = (ticket_printer_t *) arg;
    zmsg_t *request = zmsg_recv(self->pipe);
    if (!request) {
        return -1;
    }
    char *command = zmsg_popstr(request);
    if (self->verbose) {
        zsys_debug("ticket printer: API command %s", command);
    }
    if (streq(command, "$TERM")) {
        self->event = EVENT_TERM;
        s_state_machine(self);
        ret = -1;
    } else if (streq(command, "VERBOSE")) {
        self->verbose = true;
        if (self->hid_printer != NULL)
            boca_hid_set_verbose(self->hid_printer, self->verbose);
#ifdef __WIN32__
        if (self->serial_printer != NULL)
            boca_serial_set_verbose(self->serial_printer, self->verbose);
#endif
    } else if (streq(command, "START")) {
        char *ticket_store_endpoint = zmsg_popstr(request);
        self->ticket_store_req = zsock_new_req(ticket_store_endpoint);
        zsys_info("ticket printer: ticket printer is using ticket store %s", ticket_store_endpoint);
        zstr_free(&ticket_store_endpoint);
        self->event = EVENT_TICKET_STORE_READY;
        s_state_machine(self);
        zsock_signal(self->pipe, 0);
    } else if (streq(command, "SETPRINTERSTORE")) {
        char *printer_store_ep = zmsg_popstr(request);
        self->printer_store_req = zsock_new_req(printer_store_ep);
        zstr_free(&printer_store_ep);
        zsock_signal(self->pipe, 0);
    } else if (streq(command, "SETDIAGNOSTIC")) {
        self->diagnostic = true;
    }
    zstr_free(&command);
    zmsg_destroy(&request);
    return ret;
}
Example #18
0
static long
s_tickless_timer (zloop_t *self)
{
    //  Calculate tickless timer, up to 1 hour
    int64_t tickless = zclock_time () + 1000 * 3600;
    s_timer_t *timer = (s_timer_t *) zlist_first (self->timers);
    while (timer) {
        //  Find earliest timer
        if (timer->when == -1)
            timer->when = timer->delay + zclock_time ();
        if (tickless > timer->when)
            tickless = timer->when;
        timer = (s_timer_t *) zlist_next (self->timers);
    }
    long timeout = (long) (tickless - zclock_time ());
    if (timeout < 0)
        timeout = 0;
    if (self->verbose)
        zsys_debug ("zloop polling for %d msec", (int) timeout);
    return timeout;
}
Example #19
0
void
zloop_poller_end (zloop_t *self, zmq_pollitem_t *item)
{
    assert (self);
    assert (item->socket || item->fd);

    s_poller_t *poller = (s_poller_t *) zlist_first (self->pollers);
    while (poller) {
        if ((item->socket && item->socket == poller->item.socket)
        ||  (item->fd     && item->fd     == poller->item.fd)) {
            zlist_remove (self->pollers, poller);
            free (poller);
            //  Force rebuild to avoid reading from freed poller
            self->need_rebuild = true;
        }
        poller = (s_poller_t *) zlist_next (self->pollers);
    }
    if (self->verbose)
        zsys_debug ("zloop: cancel %s poller (%p, %d)",
            item->socket? zsocket_type_str (item->socket): "FD",
            item->socket, item->fd);
}
Example #20
0
void
zpubsub_filter_print (zpubsub_filter_t *self)
{
    assert (self);
    switch (self->id) {
        case ZPUBSUB_FILTER_FILTER:
            zsys_debug ("ZPUBSUB_FILTER_FILTER:");
            zsys_debug ("    magic=zpubsub_filter_magic_number");
            zsys_debug ("    version=zpubsub_filter_version");
            if (self->partition)
                zsys_debug ("    partition='%s'", self->partition);
            else
                zsys_debug ("    partition=");
            if (self->topic)
                zsys_debug ("    topic='%s'", self->topic);
            else
                zsys_debug ("    topic=");
            break;
            
    }
}
Example #21
0
static int
s_stream_engine_handle_command (stream_engine_t *self)
{
    char *method = zstr_recv (self->cmdpipe);
    if (!method)
        return -1;              //  Interrupted; exit zloop
    if (self->verbose)
        zsys_debug ("mlm_stream_simple: API command=%s", method);

    if (streq (method, "VERBOSE"))
        self->verbose = true;       //  Start verbose logging
    else
    if (streq (method, "$TERM"))
        self->terminated = true;    //  Shutdown the engine
    else
    if (streq (method, "COMPILE")) {
        void *client;
        char *pattern;
        zsock_recv (self->cmdpipe, "ps", &client, &pattern);
        s_stream_engine_compile (self, client, pattern);
        zstr_free (&pattern);
    }
    else
    if (streq (method, "CANCEL")) {
        void *client;
        zsock_recv (self->cmdpipe, "p", &client);
        s_stream_engine_cancel (self, client);
    }
    //  Cleanup pipe if any argument frames are still waiting to be eaten
    if (zsock_rcvmore (self->cmdpipe)) {
        zsys_error ("mlm_stream_simple: trailing API command frames (%s)", method);
        zmsg_t *more = zmsg_recv (self->cmdpipe);
        zmsg_print (more);
        zmsg_destroy (&more);
    }
    zstr_free (&method);
    return self->terminated? -1: 0;
}
Example #22
0
static int timer_main_serial_loop(zloop_t *loop, int timer_id, void *arg) {
    ticket_printer_t *self = (ticket_printer_t *) arg;
    if (self->serial_printer == NULL) {
        s_restart_boca_serial(self, self->boca_serial_port);
        return 0;
    }
    if (!s_heartbeat_boca_serial(self)) {
        enum BOCA_STATUS status = boca_serial_status(self->serial_printer);
        if (self->verbose && status != BOCA_UNKNOWN) {
            zsys_debug("ticket printer: got serial printer status %s.", boca_status_display(status));
        }
        s_handle_boca_status(self, &status, false);
    }
    switch (self->state) {
        case STATE_PRINTING:
            s_print_tix(self, true);
            break;
        case STATE_IDLE:
            s_print_tix(self, true);
            break;
        case STATE_ERROR:
            zstr_send(self->ticket_store_req, "TICKETUNPRINTED");
            char *code = zstr_recv(self->ticket_store_req);
            if (code != NULL && streq(code, "OK")) {
                self->tix_counter = 0;
            }
            if (code != NULL) zstr_free(&code);
            free(self->last_token);
            self->last_token = strdup("");
            s_restart_boca_serial(self, self->boca_serial_port);
            break;
        case STATE_PAUSE:
            break;
    }
    return 0;
}
Example #23
0
int
zloop_poller (zloop_t *self, zmq_pollitem_t *item, zloop_fn handler, void *arg)
{
    assert (self);

    if (item->socket
    &&  streq (zsocket_type_str (item->socket), "UNKNOWN"))
        return -1;

    s_poller_t *poller = s_poller_new (item, handler, arg);
    if (poller) {
        if (zlist_append (self->pollers, poller))
            return -1;

        self->need_rebuild = true;
        if (self->verbose)
            zsys_debug ("zloop: register %s poller (%p, %d)",
                item->socket? zsocket_type_str (item->socket): "FD",
                item->socket, item->fd);
        return 0;
    }
    else
        return -1;
}
Example #24
0
///
//  Log debug-level message - lowest priority
void QmlZsysAttached::debug (const QString &format) {
    zsys_debug (format.toUtf8().data());
};
Example #25
0
int
zyre_peer_connect (zyre_peer_t *self, zuuid_t *from, const char *endpoint, uint64_t expired_timeout)
{
    assert (self);
    assert (!self->connected);

    //  Create new outgoing socket (drop any messages in transit)
    self->mailbox = zsock_new (ZMQ_DEALER);
    if (!self->mailbox)
        return -1;             //  Null when we're shutting down

    //  Set our own identity on the socket so that receiving node
    //  knows who each message came from. Note that we cannot use
    //  the UUID directly as the identity since it may contain a
    //  zero byte at the start, which libzmq does not like for
    //  historical and arguably bogus reasons that it nonetheless
    //  enforces.
    byte routing_id [ZUUID_LEN + 1] = { 1 };
    memcpy (routing_id + 1, zuuid_data (from), ZUUID_LEN);
    int rc = zmq_setsockopt (zsock_resolve (self->mailbox),
                             ZMQ_IDENTITY, routing_id, ZUUID_LEN + 1);
    assert (rc == 0);

    //  Set a high-water mark that allows for reasonable activity
    zsock_set_sndhwm (self->mailbox, expired_timeout * 100);

    //  Send messages immediately or return EAGAIN
    zsock_set_sndtimeo (self->mailbox, 0);

    //  If the peer is a link-local IPv6 address but the interface is not set,
    //  use ZSYS_INTERFACE_ADDRESS if provided
    zrex_t *rex = zrex_new (NULL);
    char endpoint_iface [NI_MAXHOST] = {0};
    if (zsys_ipv6 () && zsys_interface () && strlen(zsys_interface ()) &&
            !streq (zsys_interface (), "*") &&
            zrex_eq (rex, endpoint, "^tcp://(fe80[^%]+)(:\\d+)$")) {
        const char *hostname, *port;
        zrex_fetch (rex, &hostname, &port, NULL);
        strcat (endpoint_iface, "tcp://");
        strcat (endpoint_iface, hostname);
        strcat (endpoint_iface, "%");
        strcat (endpoint_iface, zsys_interface ());
        strcat (endpoint_iface, port);
    } else
        strcat (endpoint_iface, endpoint);
    zrex_destroy (&rex);

    //  Connect through to peer node
    rc = zsock_connect (self->mailbox, "%s", endpoint_iface);
    if (rc != 0) {
        zsys_debug ("(%s) cannot connect to endpoint=%s",
                    self->origin, endpoint_iface);
        zsock_destroy (&self->mailbox);
        return -1;
    }
    if (self->verbose)
        zsys_info ("(%s) connect to peer: endpoint=%s",
                   self->origin, endpoint_iface);

    self->endpoint = strdup (endpoint_iface);
    self->connected = true;
    self->ready = false;

    return 0;
}
Example #26
0
int main() {

    if (!getenv("ENDPOINT")) {
        fprintf (stderr, "variable ENDPOINT must be declared\n");
        exit (EXIT_FAILURE);
    }

    char *endpoint = strdup(getenv("ENDPOINT"));

    //1. start malamute
    zactor_t *broker = s_broker(endpoint);

    //2. shout about it through zyre
    zyre_t *node = zyre_new (getenv("USER"));
    assert (node);
    char * UUID = strdup (zyre_uuid (node));
    zsys_info ("UUID: %s", UUID);
    bool to_shout = true;

    //zyre_set_verbose (node);
    zyre_start (node);
    zyre_join (node, "MALAMUTE");

    zpoller_t *poller = zpoller_new(zyre_socket (node), NULL);

    time_t last_leader_shout;

    // 3. get the comments
    while (!zsys_interrupted) {

        zsock_t *which = zpoller_wait(poller, 1000);

        if (time(NULL) - last_leader_shout > 5) {
            to_shout = true;
            zstr_free (&UUID);
            UUID = strdup (zyre_uuid (node));
            zstr_free (&endpoint);
            endpoint = strdup (getenv("ENDPOINT"));
            zactor_destroy (&broker);
            broker = s_broker (endpoint);
        }

        if (!which && to_shout) {
            zyre_shouts (node, "MALAMUTE", "%s", endpoint);
            continue;
        }

        zyre_event_t *event = zyre_event_new (node);
        if (!event)
            continue;

        switch (zyre_event_type (event)) {
            case ZYRE_EVENT_SHOUT:
                {
                int r = strcmp (UUID, zyre_event_sender (event));
                if (!r)
                    last_leader_shout = time(NULL);
                if (r >= 0)
                    goto event_destroy;

                zsys_debug ("UUID: %s, sender: %s, strcmp: %d",
                        UUID,
                        zyre_event_sender (event),
                        r
                        );

                to_shout = false;
                zstr_free (&UUID);
                UUID = strdup (zyre_event_sender (event));
                zstr_free (&endpoint);
                zmsg_t *msg = zyre_event_msg (event);
                endpoint = strdup (zmsg_popstr(msg));

                zactor_destroy (&broker);
                broker = s_broker (endpoint);
                zclock_sleep(1000);

                break;
                }

        }

event_destroy:
        zyre_event_destroy (&event);
    }

    zpoller_destroy (&poller);
    zstr_free (&UUID);
    zstr_free (&endpoint);
    zyre_destroy (&node);
    zactor_destroy (&broker);
}
Example #27
0
static int
mrb_actor_pipe_reader(zloop_t* reactor, zsock_t* pipe, void* args)
{
    errno = 0;
    self_t* self = (self_t*)args;
    int rc = 0;
    zmsg_t* msg = zmsg_recv(pipe);
    if (!msg)
        return -1;

    char* command = zmsg_popstr(msg);
    zsys_debug("command: %s", command);
    if (streq(command, "$TERM")) {
        rc = -1;
    }
    else if (streq(command, "BIND ROUTER")) {
        char* endpoint = zmsg_popstr(msg);
        if (zsock_bind(self->router, "%s", endpoint) >= 0) {
            const char* boundendpoint = zsock_endpoint(self->router);
            zyre_set_header(self->discovery, "mrb-actor-v1-router", "%s", boundendpoint);
            zsock_signal(pipe, 0);
            zsock_send(pipe, "s", boundendpoint);
        } else {
            zsock_signal(pipe, 1);
            zsock_send(pipe, "i", errno);
        }
        zstr_free(&endpoint);
    }
    else if (streq(command, "BIND PULL")) {
        char* endpoint = zmsg_popstr(msg);
        if (zsock_bind(self->pull, "%s", endpoint) >= 0) {
            const char* boundendpoint = zsock_endpoint(self->pull);
            zyre_set_header(self->discovery, "mrb-actor-v1-pull", "%s", boundendpoint);
            zsock_signal(pipe, 0);
            zsock_send(pipe, "s", boundendpoint);
        } else {
            zsock_signal(pipe, 1);
            zsock_send(pipe, "i", errno);
        }
        zstr_free(&endpoint);
    }
    else if (streq(command, "ZYRE SET ENDPOINT")) {
        char* endpoint = zmsg_popstr(msg);
        if (zyre_set_endpoint(self->discovery, "%s", endpoint) == -1) {
            zsock_signal(pipe, 1);
        } else {
            zsock_signal(pipe, 0);
        }
        zstr_free(&endpoint);
    }
    else if (streq(command, "ZYRE GOSSIP BIND")) {
        char* endpoint = zmsg_popstr(msg);
        zyre_gossip_bind(self->discovery, "%s", endpoint);
        zstr_free(&endpoint);
    }
    else if (streq(command, "ZYRE GOSSIP CONNECT")) {
        char* endpoint = zmsg_popstr(msg);
        zyre_gossip_connect(self->discovery, "%s", endpoint);
        zstr_free(&endpoint);
    }
    else if (streq(command, "ZYRE START")) {
        if (zyre_start(self->discovery) == -1) {
            zsock_signal(pipe, 1);
        } else {
            zyre_join(self->discovery, "mrb-actor-v1");
            zsock_signal(pipe, 0);
        }
    }
    else if (streq(command, "LOAD IREP FILE")) {
        char* mrb_file = zmsg_popstr(msg);
        mrb_state* mrb = self->mrb;
        int ai = mrb_gc_arena_save(mrb);
        struct mrb_jmpbuf* prev_jmp = mrb->jmp;
        struct mrb_jmpbuf c_jmp;

        MRB_TRY(&c_jmp)
        {
            mrb->jmp = &c_jmp;
            FILE* fp = fopen(mrb_file, "rb");
            if (!fp) {
                mrb_sys_fail(mrb, "fopen");
            }
            mrb_load_irep_file(mrb, fp);
            fclose(fp);
            if (mrb->exc) {
                mrb_exc_raise(mrb, mrb_obj_value(mrb->exc));
            }
            zsock_signal(pipe, 0);
            mrb->jmp = prev_jmp;
        }
        MRB_CATCH(&c_jmp)
        {
            mrb->jmp = prev_jmp;
            mrb_print_error(mrb);
            zsock_signal(pipe, 1);
            mrb->exc = NULL;
        }
        MRB_END_EXC(&c_jmp);
        mrb_gc_arena_restore(mrb, ai);

        zstr_free(&mrb_file);
    }
Example #28
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;
}
Example #29
0
void
zgossip_msg_print (zgossip_msg_t *self)
{
    assert (self);
    switch (self->id) {
        case ZGOSSIP_MSG_HELLO:
            zsys_debug ("ZGOSSIP_MSG_HELLO:");
            zsys_debug ("    version=1");
            break;
            
        case ZGOSSIP_MSG_PUBLISH:
            zsys_debug ("ZGOSSIP_MSG_PUBLISH:");
            zsys_debug ("    version=1");
            if (self->key)
                zsys_debug ("    key='%s'", self->key);
            else
                zsys_debug ("    key=");
            if (self->value)
                zsys_debug ("    value='%s'", self->value);
            else
                zsys_debug ("    value=");
            break;
            
        case ZGOSSIP_MSG_PING:
            zsys_debug ("ZGOSSIP_MSG_PING:");
            zsys_debug ("    version=1");
            break;
            
        case ZGOSSIP_MSG_PONG:
            zsys_debug ("ZGOSSIP_MSG_PONG:");
            zsys_debug ("    version=1");
            break;
            
        case ZGOSSIP_MSG_INVALID:
            zsys_debug ("ZGOSSIP_MSG_INVALID:");
            zsys_debug ("    version=1");
            break;
            
    }
}
Example #30
0
int main(int argc, char** argv) {
    if(argc != 2) {
        fprintf(stderr, "Usage: %s ups_name\n", argv[0]);
        exit(1);
    }

    char *addr = NULL;
    zyre_t *n = zyre_new(argv[1]);
    zyre_start(n);
    zyre_join(n, "MONITORS");

    char *hap_server = NULL;
    while (!zsys_interrupted) { 
        zmsg_t *zyre_msg = zyre_recv (n);
        zmsg_print (zyre_msg);
        char *command = zmsg_popstr (zyre_msg);
        if (!streq (command, "SHOUT"))
            continue;
        char *uuid = zmsg_popstr (zyre_msg);
        char *name = zmsg_popstr (zyre_msg);
        char *channel = zmsg_popstr (zyre_msg);
        hap_server = zmsg_popstr (zyre_msg);
        free (uuid); free (name); free (channel); free (command);
        break;
    }
    zsys_debug ("initial HAP server: %s", hap_server);
    
    addr = hap_server;
    if(addr == NULL)
        exit(1);

    zsock_t * sc = zsock_new(ZMQ_PUB);
    zsock_connect(sc, "%s", addr);
    zsys_debug ("socket created, connected.");
    bool state = random()%2;
    int timeout = 0;
    int cummulative = 0;
    while(!zsys_interrupted) {
        if(timeout == 0) {
            state = !state;
            timeout = 5 + random()%20;
        }
        timeout--;
        if(state) {
            zstr_sendx(sc, argv[1], "ON", NULL);
            zsys_debug("UPS %s ON", argv[1]);
        } else {
            zstr_sendx(sc, argv[1], "OFF", NULL);
            zsys_debug("UPS %s OFF", argv[1]);
        }
        sleep(1);
        cummulative++;
        if (cummulative == 5) {
            char *new_hap;
            while (!zsys_interrupted) { 
                zmsg_t *zyre_msg = zyre_recv (n);
                zmsg_print (zyre_msg);
                char *command = zmsg_popstr (zyre_msg);
                if (!streq (command, "SHOUT"))
                    continue;
                char *uuid = zmsg_popstr (zyre_msg);
                char *name = zmsg_popstr (zyre_msg);
                char *channel = zmsg_popstr (zyre_msg);
                new_hap = zmsg_popstr (zyre_msg);
                free (uuid); free (name); free (channel); free (command);
                break;
            }
            if (!streq (new_hap, hap_server)) {
                free (hap_server); hap_server = new_hap;
                zsock_connect(sc, "%s", addr);
            }
            cummulative = 0;
        }
    }
    if (hap_server)
        free (hap_server);
    zsock_destroy(&sc);
}