Package org.zeromq

Examples of org.zeromq.ZMsg


    //  .split connect method
    //  Connect to a new server endpoint. We can connect to at most two
    //  servers. Sends [CONNECT][endpoint][service] to the agent:
    public void connect(String address, String service)
    {
        ZMsg msg = new ZMsg();
        msg.add("CONNECT");
        msg.add(address);
        msg.add(service);
        msg.send(pipe);
    }
View Full Code Here


     */
    public static void main(String[] args) {
        boolean verbose = (args.length > 0 && "-v".equals(args[0]));
        mdwrkapi workerSession = new mdwrkapi("tcp://localhost:5555", "echo", verbose);

        ZMsg reply = null;
        while (!Thread.currentThread().isInterrupted()) {
            ZMsg request = workerSession.receive(reply);
            if (request == null)
                break; //Interrupted
            reply = request; //  Echo is complex :-)
        }
        workerSession.destroy();
View Full Code Here

     * Returns the reply message or NULL if there was no reply. Does not attempt
     * to recover from a broker failure, this is not possible without storing
     * all unanswered requests and resending them all…
     */
    public ZMsg recv() {
        ZMsg reply = null;

        // Poll socket for a reply, with timeout
        ZMQ.Poller items = new ZMQ.Poller(1);
        items.register(client, ZMQ.Poller.POLLIN);
        if (items.poll(timeout * 1000) == -1)
            return null; // Interrupted

        if (items.pollin(0)) {
            ZMsg msg = ZMsg.recvMsg(client);
            if (verbose) {
                log.format("I: received reply: \n");
                msg.dump(log.out());
            }
            // Don't try to handle errors, just assert noisily
            assert (msg.size() >= 4);

            ZFrame empty = msg.pop();
            assert (empty.getData().length == 0);
            empty.destroy();

            ZFrame header = msg.pop();
            assert (MDP.C_CLIENT.equals(header.toString()));
            header.destroy();

            ZFrame replyService = msg.pop();
            replyService.destroy();

            reply = msg;
        }
        return reply;
View Full Code Here

                break;              //  Interrupted

            //  Handle worker activity on backend
            if (items [0].isReadable()) {
                //  Use worker address for LRU routing
                ZMsg msg = ZMsg.recvMsg (backend);
                if (msg == null)
                    break;          //  Interrupted

                //  Any sign of life from worker means it's ready
                ZFrame address = msg.unwrap();
                Worker worker = new Worker(address);
                worker.ready(workers);

                //  Validate control message, or return reply to client
                if (msg.size() == 1) {
                    ZFrame frame = msg.getFirst();
                    String data = new String(frame.getData());
                    if (!data.equals(PPP_READY)
                    &&  !data.equals( PPP_HEARTBEAT)) {
                        System.out.println ("E: invalid message from worker");
                        msg.dump(System.out);
                    }
                    msg.destroy();
                }
                else
                    msg.send(frontend);
            }
            if (items [1].isReadable()) {
                //  Now get next client request, route to next worker
                ZMsg msg = ZMsg.recvMsg (frontend);
                if (msg == null)
                    break;          //  Interrupted
                msg.push(Worker.next(workers));
                msg.send( backend);
            }

            //  We handle heartbeating after any socket activity. First we send
            //  heartbeats to any idle workers if it's time. Then we purge any
            //  dead workers:
View Full Code Here

            ZMQ.Poller items = new ZMQ.Poller(1);
            items.register(socket, ZMQ.Poller.POLLIN);
            if (items.poll(HEARTBEAT_INTERVAL) == -1)
                break; // Interrupted
            if (items.pollin(0)) {
                ZMsg msg = ZMsg.recvMsg(socket);
                if (msg == null)
                    break; // Interrupted

                if (verbose) {
                    log.format("I: received message:\n");
                    msg.dump(log.out());
                }

                ZFrame sender = msg.pop();
                ZFrame empty = msg.pop();
                ZFrame header = msg.pop();

                if (MDP.C_CLIENT.frameEquals(header)) {
                    processClient(sender, msg);
                } else if (MDP.W_WORKER.frameEquals(header))
                    processWorker(sender, msg);
                else {
                    log.format("E: invalid message:\n");
                    msg.dump(log.out());
                    msg.destroy();
                }

                sender.destroy();
                empty.destroy();
                header.destroy();
View Full Code Here

     * not destroy the message, this is the caller's job.
     */
    public void sendToWorker(Worker worker, MDP command, String option,
            ZMsg msgp) {

        ZMsg msg = msgp == null ? new ZMsg() : msgp.duplicate();

        // Stack protocol envelope to start of message
        if (option != null)
            msg.addFirst(new ZFrame(option));
        msg.addFirst(command.newFrame());
        msg.addFirst(MDP.W_WORKER.newFrame());

        // Stack routing envelope to start of message
        msg.wrap(worker.address.duplicate());
        if (verbose) {
            log.format("I: sending %s to worker\n", command);
            msg.dump(log.out());
        }
        msg.send(socket);
    }
View Full Code Here

        server.setIdentity(connectEndpoint.getBytes());
        server.bind(bindEndpoint);
        System.out.printf ("I: service is ready at %s\n", bindEndpoint);

        while (!Thread.currentThread().isInterrupted()) {
            ZMsg request = ZMsg.recvMsg(server);
            if (verbose && request != null)
                request.dump(System.out);

            if (request == null)
                break;          //  Interrupted

            //  Frame 0: identity of client
            //  Frame 1: PING, or client control frame
            //  Frame 2: request body
            ZFrame identity = request.pop();
            ZFrame control = request.pop();
            ZMsg reply = new ZMsg();
            if (control.equals("PING"))
                reply.add("PONG");
            else {
                reply.add(control);
                reply.add("OK");
            }
            request.destroy();
            reply.push(identity);
            if (verbose && reply != null)
                reply.dump(System.out);
            reply.send(server);
        }
        if (Thread.currentThread().isInterrupted())
            System.out.printf ("W: interrupted\n");

        ctx.destroy();
View Full Code Here

public class ticlient
{
    static ZMsg
    serviceCall (mdcliapi session, String service, ZMsg request)
    {
        ZMsg reply = session.send(service, request);
        if (reply != null) {
            ZFrame status = reply.pop();
            if (status.streq("200")) {
                status.destroy();
                return reply;
            }
            else if (status.streq("400")) {
                System.out.println("E: client fatal error, aborting");
            }
            else
            if (status.streq("500")) {
                System.out.println("E: server fatal error, aborting");
            }
            reply.destroy();
        }
        return null;        //  Didn't succeed; don't care why not
    }
View Full Code Here

    {
        boolean verbose = (args.length > 0 && args[0].equals("-v"));
        mdcliapi session = new mdcliapi("tcp://localhost:5555", verbose);

        //  1. Send 'echo' request to Titanic
        ZMsg request = new ZMsg();
        request.add("echo");
        request.add("Hello world");
        ZMsg reply = serviceCall(
                session, "titanic.request", request);

        ZFrame uuid = null;
        if (reply != null) {
            uuid = reply.pop();
            reply.destroy();
            uuid.print("I: request UUID ");
        }
        //  2. Wait until we get a reply
        while (!Thread.currentThread().isInterrupted()) {
            Thread.sleep(100);
            request = new ZMsg();
            request.add(uuid.duplicate());
            reply = serviceCall(
                    session, "titanic.reply", request);

            if (reply != null) {
                String replyString = reply.getLast().toString();
                System.out.printf ("Reply: %s\n", replyString);
                reply.destroy();

                //  3. Close request
                request = new ZMsg();
                request.add(uuid.duplicate());
                reply = serviceCall(session, "titanic.close", request);
                reply.destroy();
                break;
            }
            else {
                System.out.println("I: no reply yet, trying again...");
                Thread.sleep (5000);     //  Try again in 5 seconds
View Full Code Here

        ZFrame frame = new ZFrame(WORKER_READY);
        frame.send(worker, 0);

        int cycles = 0;
        while (true) {
            ZMsg msg = ZMsg.recvMsg(worker);
            if (msg == null)
                break;              //  Interrupted

            //  Simulate various problems, after a few cycles
            cycles++;
            if (cycles > 3 && rand.nextInt(5) == 0) {
                System.out.printf("I: (%s) simulating a crash\n", identity);
                msg.destroy();
                break;
            } else if (cycles > 3 && rand.nextInt(5) == 0) {
                System.out.printf("I: (%s) simulating CPU overload\n", identity);
                Thread.sleep(3000);
            }
            System.out.printf("I: (%s) normal reply\n", identity);
            Thread.sleep(1000); //  Do some heavy work
            msg.send(worker);
        }
        ctx.destroy();
    }
View Full Code Here

TOP

Related Classes of org.zeromq.ZMsg

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.