Ejemplo n.º 1
0
//  Starts a device as configured by a full ZDCF config file
//
static void
s_start_configured_device (char *filename)
{
    FILE *file;                 //  JSON config file stream
    if (streq (filename, "-"))
        file = stdin;           //  "-" means read from stdin
    else {
        file = fopen (filename, "rb");
        if (!file) {
            printf ("E: '%s' doesn't exist or can't be read\n", filename);
            exit (EXIT_FAILURE);
        }
    }
    //  Load config data into blob
    zfl_blob_t *blob = zfl_blob_new ();
    assert (blob);
    zfl_blob_load (blob, file);
    fclose (file);

    //  Create a new config from the blob data
    zfl_config_t *config = zfl_config_new (zfl_blob_data (blob));
    assert (config);

    //  Find first device
    char *device = zfl_config_device (config, 0);
    if (!*device) {
        printf ("E: No device specified, please read http://rfc.zeromq.org/spec:3\n");
        exit (EXIT_FAILURE);
    }
    //  Process device type
    char *type = zfl_config_device_type (config, device);

    int device_type;            //  0MQ defined type
    int frontend_type;          //  Socket types depending
    int backend_type;           //    on the device type
    s_parse_device_type (type, &device_type, &frontend_type, &backend_type);

    //  Create and configure sockets
    void *frontend = zfl_config_socket (config, device, "frontend", frontend_type);
    assert (frontend);
    void *backend = zfl_config_socket (config, device, "backend", backend_type);
    assert (backend);

    //  Start the device now
    if (zfl_config_verbose (config))
        printf ("I: Starting device...\n");

    //  Will not actually ever return
    zmq_device (device_type, frontend, backend);

    zfl_config_destroy (&config);
}
Ejemplo n.º 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;
}