Пример #1
0
//  Starts an automagical device
//
//  - zqueue acts as broker, binds XREP and XREQ
//  - zforwarder acts as proxy, connects SUB, binds PUB
//  - zstreamer acts as proxy, binds PULL, connects PUSH
//
static void
s_start_automagic_device (char *type, char *frontend, char *backend)
{
    void *context = zmq_init (1);

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

    //  Create and configure sockets
    void *frontend_socket = zmq_socket (context, frontend_type);
    assert (frontend_socket);
    void *backend_socket = zmq_socket (context, backend_type);
    assert (backend_socket);

    if (device == ZMQ_QUEUE) {
        printf ("I: Binding to %s for client connections\n", frontend);
        if (zmq_bind (frontend_socket, frontend)) {
            printf ("E: cannot bind to '%s': %s\n", frontend, zmq_strerror (errno));
            exit (EXIT_FAILURE);
        }
        printf ("I: Binding to %s for service connections\n", backend);
        if (zmq_bind (backend_socket, backend)) {
            printf ("E: cannot bind to '%s': %s\n", backend, zmq_strerror (errno));
            exit (EXIT_FAILURE);
        }
    }
    else
    if (device == ZMQ_FORWARDER) {
        printf ("I: Connecting to publisher at %s\n", frontend);
        if (zmq_connect (frontend_socket, frontend)) {
            printf ("E: cannot connect to '%s': %s\n", frontend, zmq_strerror (errno));
            exit (EXIT_FAILURE);
        }
        printf ("I: Binding to %s for subscriber connections\n", backend);
        if (zmq_bind (backend_socket, backend)) {
            printf ("E: cannot bind to '%s': %s\n", backend, zmq_strerror (errno));
            exit (EXIT_FAILURE);
        }
    }
    else
    if (device == ZMQ_STREAMER) {
        printf ("I: Binding to %s for upstream nodes\n", frontend);
        if (zmq_bind (frontend_socket, frontend)) {
            printf ("E: cannot bind to '%s': %s\n", frontend, zmq_strerror (errno));
            exit (EXIT_FAILURE);
        }
        printf ("I: Connecting downstream to %s\n", backend);
        if (zmq_connect (backend_socket, backend)) {
            printf ("E: cannot connect to '%s': %s\n", backend, zmq_strerror (errno));
            exit (EXIT_FAILURE);
        }
    }
    zmq_device (device, frontend_socket, backend_socket);
}
Пример #2
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);
}
Пример #3
0
//  Starts a device as configured by a JSON or text config file
//
static void
s_start_configured_device (char *filename)
{


    //  Create and configure a zfl_device object
    zfl_device_t *device = zfl_device_new (filename);
    if (!device) {
        printf ("E: '%s' can't be read, or has invalid syntax\n", filename);
        exit (EXIT_FAILURE);
    }
    //  Find first device
    char *main_device = zfl_device_locate (device, 0);
    if (!*main_device) {
        printf ("E: No device specified, please read http://rfc.zeromq.org/spec:5\n");
        exit (EXIT_FAILURE);
    }
    //  Process device type
    char *type = zfl_device_property (device, main_device, "type");

    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_device_socket (device, main_device, "frontend", frontend_type);
    assert (frontend);
    void *backend = zfl_device_socket (device, main_device, "backend", backend_type);
    assert (backend);

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

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

    zfl_device_destroy (&device);
}