Esempio n. 1
0
int main (int argc, char *argv [])
{
    if (argc != 2) {
        fprintf (stderr, "usage: zmq_queue <config-file>\n");
        return 1;
    }

    XMLNode root = XMLNode::parseFile (argv [1]);
    if (root.isEmpty ()) {
        fprintf (stderr, "configuration file not found or not an XML file\n");
        return 1;
    }

    if (strcmp (root.getName (), "queue") != 0) {
        fprintf (stderr, "root element in the configuration file should be "
            "named 'queue'\n");
        return 1;
    }

    XMLNode in_node = root.getChildNode ("in");
    if (in_node.isEmpty ()) {
        fprintf (stderr, "'in' node is missing in the configuration file\n");
        return 1;
    }

    XMLNode out_node = root.getChildNode ("out");
    if (out_node.isEmpty ()) {
        fprintf (stderr, "'out' node is missing in the configuration file\n");
        return 1;
    }

    //  TODO: make the number of I/O threads configurable.
    zmq::context_t ctx (1);
    zmq::socket_t in_socket (ctx, ZMQ_XREP);
    zmq::socket_t out_socket (ctx, ZMQ_XREQ);

    int n = 0;
    while (true) {
        XMLNode bind = in_node.getChildNode ("bind", n);
        if (bind.isEmpty ())
            break;
        const char *addr = bind.getAttribute ("addr");
        if (!addr) {
            fprintf (stderr, "'bind' node is missing 'addr' attribute\n");
            return 1;
        }
        in_socket.bind (addr);
        n++;
    }

    n = 0;
    while (true) {
        XMLNode connect = in_node.getChildNode ("connect", n);
        if (connect.isEmpty ())
            break;
        const char *addr = connect.getAttribute ("addr");
        if (!addr) {
            fprintf (stderr, "'connect' node is missing 'addr' attribute\n");
            return 1;
        }
        in_socket.connect (addr);
        n++;
    }

    n = 0;
    while (true) {
        XMLNode bind = out_node.getChildNode ("bind", n);
        if (bind.isEmpty ())
            break;
        const char *addr = bind.getAttribute ("addr");
        if (!addr) {
            fprintf (stderr, "'bind' node is missing 'addr' attribute\n");
            return 1;
        }
        out_socket.bind (addr);
        n++;
    }

    n = 0;
    while (true) {
        XMLNode connect = out_node.getChildNode ("connect", n);
        if (connect.isEmpty ())
            break;
        const char *addr = connect.getAttribute ("addr");
        if (!addr) {
            fprintf (stderr, "'connect' node is missing 'addr' attribute\n");
            return 1;
        }
        out_socket.connect (addr);
        n++;
    }

    try {
        zmq::device (ZMQ_QUEUE, in_socket, out_socket);
    } catch (zmq::error_t& e) {
        fprintf(stderr, "device exit: %s\n", e.what());
    }

    return 0;
}
Esempio n. 2
0
int main (int argc, char *argv [])
{
    if (argc != 2) {
        fprintf (stderr, "usage: zmq_forwarder <config-file>\n");
        return 1;
    }

    XMLNode root = XMLNode::parseFile (argv [1]);
    if (root.isEmpty ()) {
        fprintf (stderr, "configuration file not found or not an XML file\n");
        return 1;
    }

    if (strcmp (root.getName (), "forwarder") != 0) {
        fprintf (stderr, "root element in the configuration file should be "
            "named 'forwarder'\n");
        return 1;
    }

    XMLNode in_node = root.getChildNode ("in");
    if (in_node.isEmpty ()) {
        fprintf (stderr, "'in' node is missing in the configuration file\n");
        return 1;
    }

    XMLNode out_node = root.getChildNode ("out");
    if (out_node.isEmpty ()) {
        fprintf (stderr, "'out' node is missing in the configuration file\n");
        return 1;
    }

    //  TODO: make the number of I/O threads configurable.
    zmq::context_t ctx (1);
    zmq::socket_t in_socket (ctx, ZMQ_SUB);
    in_socket.setsockopt (ZMQ_SUBSCRIBE, "", 0);
    zmq::socket_t out_socket (ctx, ZMQ_PUB);

    int n = 0;
    while (true) {
        XMLNode bind = in_node.getChildNode ("bind", n);
        if (bind.isEmpty ())
            break;
        const char *addr = bind.getAttribute ("addr");
        if (!addr) {
            fprintf (stderr, "'bind' node is missing 'addr' attribute\n");
            return 1;
        }
        in_socket.bind (addr);
        n++;
    }

    n = 0;
    while (true) {
        XMLNode connect = in_node.getChildNode ("connect", n);
        if (connect.isEmpty ())
            break;
        const char *addr = connect.getAttribute ("addr");
        if (!addr) {
            fprintf (stderr, "'connect' node is missing 'addr' attribute\n");
            return 1;
        }
        in_socket.connect (addr);
        n++;
    }

    n = 0;
    while (true) {
        XMLNode bind = out_node.getChildNode ("bind", n);
        if (bind.isEmpty ())
            break;
        const char *addr = bind.getAttribute ("addr");
        if (!addr) {
            fprintf (stderr, "'bind' node is missing 'addr' attribute\n");
            return 1;
        }
        out_socket.bind (addr);
        n++;
    }

    n = 0;
    while (true) {
        XMLNode connect = out_node.getChildNode ("connect", n);
        if (connect.isEmpty ())
            break;
        const char *addr = connect.getAttribute ("addr");
        if (!addr) {
            fprintf (stderr, "'connect' node is missing 'addr' attribute\n");
            return 1;
        }
        out_socket.connect (addr);
        n++;
    }

    zmq::device (ZMQ_FORWARDER, in_socket, out_socket);

    return 0;
}
Esempio n. 3
0
int main (int argc, char *argv []) 
{
    po::options_description desc ("Command-line options");
    po::variables_map vm;
    std::string filename;
    std::string user;
    int64_t inflight_size;
    uint64_t ack_timeout, reaper_frequency;
    std::string receiver_dsn, sender_dsn, monitor_dsn, peer_uuid;

    desc.add_options ()
        ("help", "produce help message");

    desc.add_options()
        ("database",
          po::value<std::string> (&filename)->default_value ("/tmp/sink.kch"),
         "Database sink file location")
    ;

    desc.add_options()
        ("ack-timeout",
          po::value<uint64_t> (&ack_timeout)->default_value (5000000),
         "How long to wait for ACK before resending message (microseconds)")
    ;

    desc.add_options()
        ("reaper-frequency",
          po::value<uint64_t> (&reaper_frequency)->default_value (2500000),
         "How often to clean up expired messages (microseconds)")
    ;

    desc.add_options()
        ("hard-sync",
         "If enabled the data is flushed to disk on every sync")
    ;

    desc.add_options()
        ("background",
         "Run in daemon mode")
    ;

    desc.add_options()
        ("inflight-size",
          po::value<int64_t> (&inflight_size)->default_value (31457280),
         "Maximum size in bytes for the in-flight messages database. Full database causes LRU collection")
    ;
    
    desc.add_options()
        ("user",
        po::value<std::string> (&user)->default_value (""),
        "User the process should run under")
    ;

    desc.add_options()
        ("receive-dsn",
          po::value<std::string> (&receiver_dsn)->default_value ("tcp://*:11131"),
         "The DSN for the receive socket")
    ;

    desc.add_options()
        ("send-dsn",
          po::value<std::string> (&sender_dsn)->default_value ("tcp://*:11132"),
         "The DSN for the backend client communication socket")
    ;

    desc.add_options()
        ("monitor-dsn",
          po::value<std::string> (&monitor_dsn)->default_value ("ipc:///tmp/pzq-monitor"),
         "The DSN for the monitoring socket")
    ;

    try {
        po::store (po::parse_command_line (argc, argv, desc), vm);
        po::notify (vm);
    } catch (po::error &e) {
        std::cerr << "Error parsing command-line options: " << e.what () << std::endl;
        std::cerr << desc << std::endl;
        return 1;
    }
    if (vm.count ("help")) {
        std::cerr << desc << std::endl;
        return 1;
    }
    
    if (vm.count ("user") && user.length() != 0) {
        struct passwd *res_user;
        
        res_user = getpwnam( user.c_str() );
        if ( !res_user ) {
            std::cerr << "Could not find user " << user << std::endl;
            exit(1);
        }
        
        if ( !drop_privileges ( res_user->pw_uid, res_user->pw_gid ) ) {
            std::cerr << "Failed to become user" << user << std::endl;
            exit(1);
        }
    }


    // Background
    if (vm.count ("background")) {
        if (daemonize () == -1) {
            exit (1);
        }
    } else {
        signal (SIGINT, time_to_go);
        signal (SIGHUP, time_to_go);
        signal (SIGTERM, time_to_go);
    }

    // Init new zeromq context
    zmq::context_t context (1);

    {
        int linger = 1000;
        uint64_t in_hwm = 10, out_hwm = 1;

        boost::shared_ptr<pzq::datastore_t> store (new pzq::datastore_t ());
        store.get ()->open (filename, inflight_size);
        store.get ()->set_ack_timeout (ack_timeout);

        boost::shared_ptr<pzq::socket_t> in_socket (new pzq::socket_t (context, ZMQ_ROUTER));
        in_socket.get ()->setsockopt (ZMQ_LINGER, &linger, sizeof (int));
        in_socket.get ()->setsockopt (ZMQ_HWM, &in_hwm, sizeof (uint64_t));
        in_socket.get ()->bind (receiver_dsn.c_str ());

        boost::shared_ptr<pzq::socket_t> out_socket (new pzq::socket_t (context, ZMQ_DEALER));
        out_socket.get ()->setsockopt (ZMQ_LINGER, &linger, sizeof (int));
        out_socket.get ()->setsockopt (ZMQ_HWM, &out_hwm, sizeof (uint64_t));
        out_socket.get ()->bind (sender_dsn.c_str ());

        boost::shared_ptr<pzq::socket_t> monitor (new pzq::socket_t (context, ZMQ_ROUTER));
        monitor.get ()->setsockopt (ZMQ_LINGER, &linger, sizeof (int));
        monitor.get ()->setsockopt (ZMQ_HWM, &out_hwm, sizeof (uint64_t));
        monitor.get ()->bind (monitor_dsn.c_str ());

        try {
            // Start the store manager
            pzq::manager_t manager;

            // Reaper for expired messages
            pzq::expiry_reaper_t reaper (store);
            reaper.set_frequency (reaper_frequency);
            reaper.set_ack_timeout (ack_timeout);
            reaper.start ();

            manager.set_datastore (store);
            manager.set_ack_timeout (ack_timeout);
            manager.set_sockets (in_socket, out_socket, monitor);
            manager.start ();

            while (keep_running)
            {
                boost::this_thread::sleep (
                    boost::posix_time::seconds (1)
                );
            }
            manager.stop ();
            reaper.stop ();

        } catch (std::exception &e) {
            pzq::log ("Error running store manager: %s", e.what ());
            return 1;
        }
    }
    pzq::log ("Terminating");
    return 0;
}
Esempio n. 4
0
int main (int argc, char *argv [])
{
    if (argc != 2) {
        fprintf (stderr, "usage: zmq_streamer <config-file>\n");
        return 1;
    }

    XMLNode root = XMLNode::parseFile (argv [1]);
    if (root.isEmpty ()) {
        fprintf (stderr, "configuration file not found or not an XML file\n");
        return 1;
    }

    if (strcmp (root.getName (), "streamer") != 0) {
        fprintf (stderr, "root element in the configuration file should be "
            "named 'streamer'\n");
        return 1;
    }

    XMLNode in_node = root.getChildNode ("in");
    if (in_node.isEmpty ()) {
        fprintf (stderr, "'in' node is missing in the configuration file\n");
        return 1;
    }

    XMLNode out_node = root.getChildNode ("out");
    if (out_node.isEmpty ()) {
        fprintf (stderr, "'out' node is missing in the configuration file\n");
        return 1;
    }

    //  TODO: make the number of I/O threads configurable.
    zmq::context_t ctx (1, 1);
    zmq::socket_t in_socket (ctx, ZMQ_UPSTREAM);
    zmq::socket_t out_socket (ctx, ZMQ_DOWNSTREAM);

    int n = 0;
    while (true) {
        XMLNode bind = in_node.getChildNode ("bind", n);
        if (bind.isEmpty ())
            break;
        const char *addr = bind.getAttribute ("addr");
        if (!addr) {
            fprintf (stderr, "'bind' node is missing 'addr' attribute\n");
            return 1;
        }
        in_socket.bind (addr);
        n++;
    }

    n = 0;
    while (true) {
        XMLNode connect = in_node.getChildNode ("connect", n);
        if (connect.isEmpty ())
            break;
        const char *addr = connect.getAttribute ("addr");
        if (!addr) {
            fprintf (stderr, "'connect' node is missing 'addr' attribute\n");
            return 1;
        }
        in_socket.connect (addr);
        n++;
    }

    n = 0;
    while (true) {
        XMLNode bind = out_node.getChildNode ("bind", n);
        if (bind.isEmpty ())
            break;
        const char *addr = bind.getAttribute ("addr");
        if (!addr) {
            fprintf (stderr, "'bind' node is missing 'addr' attribute\n");
            return 1;
        }
        out_socket.bind (addr);
        n++;
    }

    n = 0;
    while (true) {
        XMLNode connect = out_node.getChildNode ("connect", n);
        if (connect.isEmpty ())
            break;
        const char *addr = connect.getAttribute ("addr");
        if (!addr) {
            fprintf (stderr, "'connect' node is missing 'addr' attribute\n");
            return 1;
        }
        out_socket.connect (addr);
        n++;
    }

    zmq::message_t msg;
    while (true) {
        in_socket.recv (&msg);
        out_socket.send (msg);
    }

    return 0;
}