Beispiel #1
0
JNIEXPORT void JNICALL
Java_org_zeromq_zyre_Zyre__1_1setInterface (JNIEnv *env, jclass c, jlong self, jstring value)
{
    char *value_ = (char *) (*env)->GetStringUTFChars (env, value, NULL);
    zyre_set_interface ((zyre_t *) (intptr_t) self, value_);
    (*env)->ReleaseStringUTFChars (env, value, value_);
}
Beispiel #2
0
  GroupNodeImpl::GroupNodeImpl(const std::string& desc, bool promiscuous)
    : event_thread_(NULL), prom_thread_(NULL), promiscuous_(promiscuous), stopped_(false)
  {
    // create the zyre node
    node_ = zyre_new((GroupNode::name() + "/" + desc).c_str());
    // verbose output
    zyre_set_verbose(node_);
    // set the interface to use, otherwise zyre just guesses (default "")
    zyre_set_interface(node_, GroupNode::iface().c_str());
    // set the headers
    zyre_set_header(node_, "desc", "%s", desc.c_str());
    // access our uuid
    std::string uuid = zyre_uuid(node_);
    // access our name
    node_name_ = zyre_name(node_);
    // start the node
    if (zyre_start(node_)) {
      throw std::runtime_error("Could not start the zyre node");
    }

    // join the control group - too heavy
    if (zyre_join(node_, GroupNode::control_.c_str())) {
      throw std::runtime_error("Error joining CONTROL group");
    }

    // create our self peer
    self_.reset(new Peer(uuid, desc));

    /* we want to minimize the groups joined and messages sent -
       especially by other non-promiscuous components; in order to do
       so, we need to periodically join all the known groups.
     */
    if (promiscuous_) {
      prom_thread_ = new std::thread(&GroupNodeImpl::update_groups, this);
      BOOST_LOG_TRIVIAL(debug) << "Started promiscuous group thread..." << std::endl;
    }
  }
Beispiel #3
0
///
//  Set network interface for UDP beacons. If you do not set this, CZMQ will
//  choose an interface for you. On boxes with several interfaces you should
//  specify which one you want to use, or strange things can happen.        
void QmlZyre::setInterface (const QString &value) {
    zyre_set_interface (self, value.toUtf8().data());
};
Beispiel #4
0
static int
server_join_cluster (server_t *self)
{
    self->zyre = zyre_new (zconfig_resolve (self->config, "zyre/name", NULL));
    
    //  Get Zyre configuration properties
    char *value = zconfig_resolve (self->config, "zyre/discovery", "beacon");
    if (streq (value, "beacon")) {
        //  Set-up UDP beacon discovery
        zsys_info ("using UDP beacon discovery service");
        value = zconfig_resolve (self->config, "zyre/beacon/interface", "auto");
        if (strneq (value, "auto")) {
            zsys_info ("forcing cluster interface to %s", value);
            zyre_set_interface (self->zyre, value);
        }
        value = zconfig_resolve (self->config, "zyre/beacon/interval", NULL);
        if (value)
            zyre_set_interval (self->zyre, atoi (value));

        value = zconfig_resolve (self->config, "zyre/beacon/port", NULL);
        if (value) {
            zsys_info ("UDP beaconing on port %s", value);
            zyre_set_port (self->zyre, atoi (value));
        }
    }
    else
    if (streq (value, "gossip")) {
        //  Set-up TCP gossip discovery
        zsys_info ("using TCP gossip discovery service");
        zconfig_t *section = zconfig_locate (self->config, "zyre/gossip");
        if (!section) {
            zsys_warning ("please configure zyre/gossip section");
            return -1;
        }
        zconfig_t *entry = zconfig_child (section);
        while (entry) {
            char *name = zconfig_name (entry);
            char *value = zconfig_value (entry);
            if (streq (name, "endpoint")) {
                zsys_info ("Zyre node endpoint=%s", value);
                zyre_set_endpoint (self->zyre, "%s", value);
            }
            else
            if (streq (name, "bind")) {
                zsys_info ("gossip service bind to %s", value);
                zyre_gossip_bind (self->zyre, "%s", value);
            }
            else
            if (streq (name, "connect")) {
                zsys_info ("gossip service connect to %s", value);
                zyre_gossip_connect (self->zyre, "%s", value);
            }
            else
                zsys_warning ("unknown zyre/gossip entry '%s' ignored", name);
            entry = zconfig_next (entry);
        }
    }
    else {
        zsys_error ("bad zyre/discovery=%s (use beacon|gossip)", value);
        assert (0);
    }
    if (zyre_start (self->zyre)) {
        zsys_warning ("clustering not working");
        return -1;              //  Can't join cluster
    }
    zyre_join (self->zyre, "ZPIPES");

    //  Get Zyre public name for logging
    self->name = strdup (zyre_name (self->zyre));
    zsys_info ("joining cluster as %s", self->name);

    //  Set-up reader for Zyre events
    engine_handle_socket (self, zyre_socket (self->zyre), zyre_handler);
    return 0;
}
Beispiel #5
0
 void set_interface(const std::string& value) const
 {
     zyre_set_interface(m_self, value.c_str());
 }
Beispiel #6
0
void
ztask_run_manager_loop (ztask_run_manager_t *self)
{
    assert (self);

    if (!self->packetizer) {
        zclock_log ("E: No packetizer set !!!");
        return;
    }

    //  Setting network interface if neede
    if (self->node_interface)
        zyre_set_interface(self->node, self->node_interface);

    zyre_set_header (self->node, "X-ZTASK-RUN", "ZTASK RUN");

    zyre_start (self->node);
    zyre_dump (self->node);
    zclock_sleep (10);
//    ztask_packetizer_dump (self->packetizer);

    ztask_packet_t *packet;
    int request_sent = 0;
    zyre_event_t *event;
    while (!zsys_interrupted) {

        if (ztask_packetizer_is_finished (self->packetizer))
            break;

        event = zyre_event_new (self->node);
        if (!event)
            break;
        if (zyre_event_type (event) == ZYRE_EVENT_ENTER) {
            //  Ignoring nodes which don't have service X-ZTASK-NODE defined
            if (zyre_event_header (event, "X-ZTASK-NODE")) {
                zhash_insert (self->nodes, zyre_event_sender (event),"");
                ztask_log_debug (self->log, "Adding node -> workers=%ld", zhash_size (self->nodes));
            } else {
                //  TODO disconnect worker (zyre peer) somehow
            }
        }
        else
        if (zyre_event_type (event) == ZYRE_EVENT_EXIT) {
            if (zhash_lookup (self->nodes, zyre_event_sender (event))) {
                ztask_log_debug (self->log, "Removing node -> workers=%ld", zhash_size (self->nodes));
                zhash_delete (self->nodes, zyre_event_sender (event));
                //  Cleanup packetizer in case ztask_node was killed
                request_sent -= ztask_packetizer_running_node_cleanup (self->packetizer, zyre_event_sender (event));
            }
        }
        else
        if (zyre_event_type (event) == ZYRE_EVENT_WHISPER) {

            //  Ingoring whispers when they are not from our nodes
            if (!zhash_lookup(self->nodes, zyre_event_sender (event))) {
                ztask_log_warning (self->log, "W: Ingoring whisper from %s", zyre_event_sender (event));
                zyre_event_destroy (&event);
                continue;
            }
            zmsg_t *msg_report = zyre_event_msg(event);
            char *str_msg = zmsg_popstr (msg_report);
            if (streq(str_msg, "REQUEST")) {
                //  Let's handle request
                packet = ztask_packetizer_next_packet (self->packetizer);
                if (packet) {
                    char *cmd;
                    if ( asprintf (&cmd, "%s", ztask_packet_cmd (packet)) < 0)
                         cmd = NULL;
                    assert (cmd);
                    zmsg_t *msg = zmsg_new ();
                    zmsg_addstr (msg, "REQUEST");
                    zmsg_addstrf (msg, "%s_%ld", zyre_uuid (self->node), ztask_packet_id (packet));
                    zmsg_addstr (msg, cmd);
                    zyre_whisper (self->node, zyre_event_sender (event), &msg);
                    ztask_packet_set_node (packet, zyre_event_sender (event));
                    request_sent++;
                    ztask_packetizer_info (self->packetizer, request_sent);
                    ztask_log_debug (self->log, "ID=%s_%ld cmd='%s' running=%d", zyre_uuid (self->node), ztask_packet_id (packet), cmd, request_sent);
                    free (cmd);
                }
                else {
                    zmsg_t *msg = zmsg_new ();
                    zmsg_addstr (msg, "NO_PACKETS");
                    zyre_whisper (self->node, zyre_event_sender (event), &msg);
                }

            }
            else
            if (streq(str_msg, "REPORT")) {
                //  It's report
                char *str_id = zmsg_popstr (msg_report);
                char *str_pid_rc = zmsg_popstr (msg_report);

                ztask_log_debug (self->log, "REPORT ID=%s rc=%s", str_id, str_pid_rc);
                ztask_packetizer_report (self->packetizer, str_id, str_pid_rc);
                request_sent--;
                ztask_packetizer_info (self->packetizer, request_sent);
                free (str_id);
                free (str_pid_rc);
            }
            else {
                ztask_log_error (self->log, "E: ztask_run_manager_loop : Wrong message %s", str_msg);
                assert (false);
            }
            free (str_msg);
        }

        zyre_event_destroy (&event);
    }

    //  FIXME : simplify zhash_foreach()
    zlist_t *keys = zhash_keys (self->nodes);
    char *key = (char *) zlist_first (keys);
    while (key) {
        zmsg_t *msg = zmsg_new ();
        zmsg_addstr (msg, "END");
        zyre_whisper (self->node, key, &msg);
        key = (char *) zlist_next (keys);
    }
    zlist_destroy (&keys);

    // wait for shout to be delivered
    zclock_sleep (100);

    ztask_packetizer_summary(self->packetizer, 0);

    zyre_stop (self->node);
}
Beispiel #7
0
int main (int argc, char *argv [])
{
    int argn = 1;
    if (argn == argc
    ||  streq (argv [argn], "-h") || streq (argv [argn], "--help")) {
        puts ("midicast [-v] [-p port] [-i interface]");
        puts ("Reads MIDI events from port and sends to Zyre MIDI group");
        puts (" -h, --help: this help");
        puts (" -v, --verbose: trace events as they happen");
        puts (" -p, --port: specify port name, e.g. '-p hw:1,0,0'");
        puts (" -i, --interface: specify WiFi interface, e.g. '-i wlan0'");
        return 0;
    }
    char *midi_port_name = "hw:2,0";
    char *wifi_interface = NULL;
    bool verbose = false;
    while (argn < argc) {
        if (streq (argv [argn], "-p") || streq (argv [argn], "--port"))
            midi_port_name = argv [++argn];
        else
        if (streq (argv [argn], "-i") || streq (argv [argn], "--interface"))
            wifi_interface = argv [++argn];
        else
        if (streq (argv [argn], "-v") || streq (argv [argn], "--verbose"))
            verbose = true;
        argn++;
    }

    snd_rawmidi_t *output;
    int rc = snd_rawmidi_open (NULL, &output, midi_port_name, SND_RAWMIDI_SYNC);
    if (rc < 0) {
        zsys_error ("cannot open port \"%s\": %s", midi_port_name, snd_strerror (rc));
        return 0;
    }
    zsys_info ("forwarding MIDI cast to %s", midi_port_name);

    zyre_t *zyre = zyre_new (NULL);
    if (wifi_interface)
        zyre_set_interface (zyre, wifi_interface);
    zyre_start (zyre);
    zyre_join (zyre, "MIDI");
    zsys_info ("this player is %s", zyre_name (zyre));

    while (!zsys_interrupted) {
        zyre_event_t *event = zyre_event_new (zyre);
        if (!event) {
            printf (" interrupted\n");
            break;
        }
        if (zyre_event_type (event) == ZYRE_EVENT_JOIN)
            zsys_info ("[%s] player joined", zyre_event_peer_name (event));
        else
        if (zyre_event_type (event) == ZYRE_EVENT_LEAVE)
            zsys_info ("[%s] player left", zyre_event_peer_name (event));
        else
        if (zyre_event_type (event) == ZYRE_EVENT_SHOUT) {
            if (streq (zyre_event_group (event), "MIDI")) {
                zframe_t *frame = zmsg_first (zyre_event_msg (event));
                //  Forward the MIDI event
                snd_rawmidi_write (output, zframe_data (frame), zframe_size (frame));

                if (verbose) {
                    printf ("%zd:", zframe_size (frame));
                    int byte_nbr;
                    for (byte_nbr = 0; byte_nbr < zframe_size (frame); byte_nbr++)
                        printf (" %02X", zframe_data (frame) [byte_nbr]);
                    printf ("\n");
                }
            }
        }
        zyre_event_destroy (&event);
    }
    snd_rawmidi_close (output);
    zyre_destroy (&zyre);
	return 0;
}