Пример #1
0
void *
zfl_device_socket (
    zfl_device_t *self,
    char *device_name,
    char *socket_name,
    int   socket_type)
{
    assert (self);
    assert (device_name);
    assert (strneq (device_name, "context"));

    zfl_config_t *config = zfl_config_locate (self->config, device_name);
    if (!config)
        return NULL;            //  No such device

    void *socket = zmq_socket (self->context, socket_type);
    if (!socket)
        return NULL;            //  Can't create socket

    if (zfl_device_verbose (self))
        printf ("I: Configuring '%s' socket in '%s' device...\n",
            socket_name, device_name);

    //  Find socket in device
    int rc = 0;
    config = zfl_config_locate (config, socket_name);
    if (config) {
        config = zfl_config_child (config);
        while (config && rc == 0) {
            char *name = zfl_config_name (config);
            if (streq (name, "bind"))
                rc = zmq_bind (socket, zfl_config_string (config));
            else
            if (streq (name, "connect"))
                rc = zmq_connect (socket, zfl_config_string (config));
            else
            if (streq (name, "option"))
                rc = s_setsockopt (self, socket, config);
            //
            //  else ignore it, user space setting

            config = zfl_config_next (config);
        }
    }
    else
    if (self->verbose)
        printf ("W: No property found for '%s'\n", socket_name);

    if (rc) {
        printf ("E: property failed - %s\n", zmq_strerror (errno));
        zmq_close (socket);
        socket = NULL;
    }
    return socket;
}
Пример #2
0
//  --------------------------------------------------------------------------
//  Creates a named 0MQ socket within a named device, and configures the
//  socket as specified in the configuration data.  Returns NULL if the
//  device or socket do not exist, or if there was an error configuring the
//  socket.
//
void *
zfl_config_socket (zfl_config_t *self, char *device, char *name, int type)
{
    assert (self);
    assert (device);
    assert (strneq (device, "context"));

    zfl_tree_t *tree = zfl_tree_locate (self->tree, device);
    if (!tree)
        return NULL;            //  No such device

    void *socket = zmq_socket (self->context, type);
    if (!socket)
        return NULL;            //  Can't create socket

    if (zfl_config_verbose (self))
        printf ("I: Configuring '%s' socket in '%s' device...\n", name, device);

    //  Find socket in device
    int rc = 0;
    tree = zfl_tree_locate (tree, name);
    if (tree) {
        tree = zfl_tree_child (tree);
        while (tree && rc == 0) {
            char *name = zfl_tree_name (tree);
            if (streq (name, "bind"))
                rc = zmq_bind (socket, zfl_tree_string (tree));
            else
            if (streq (name, "connect"))
                rc = zmq_connect (socket, zfl_tree_string (tree));
            else
            if (streq (name, "option"))
                rc = s_setsockopt (self, socket, tree);
            else
            if (self->verbose)
                printf ("W: ignoring socket setting '%s'\n", name);

            tree = zfl_tree_next (tree);
        }
    }
    else
    if (self->verbose)
        printf ("W: No configuration found for '%s'\n", name);

    if (rc) {
        printf ("E: configuration failed - %s\n", zmq_strerror (errno));
        zmq_close (socket);
        socket = NULL;
    }
    return socket;
}