Examples of ZFrame


Examples of org.zeromq.ZFrame

                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");
            }
View Full Code Here

Examples of org.zeromq.ZFrame

    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

Examples of org.zeromq.ZFrame

        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
            }
        }
        uuid.destroy();
        session.destroy();
    }
View Full Code Here

Examples of org.zeromq.ZFrame

        worker.setIdentity(identity.getBytes());
        worker.connect("tcp://localhost:5556");

        //  Tell broker we're ready for work
        System.out.printf("I: (%s) worker ready\n", identity);
        ZFrame frame = new ZFrame(WORKER_READY);
        frame.send(worker, 0);

        int cycles = 0;
        while (true) {
            ZMsg msg = ZMsg.recvMsg(worker);
            if (msg == null)
View Full Code Here

Examples of org.zeromq.ZFrame

            worker.connect("inproc://backend");

            while (!Thread.currentThread().isInterrupted()) {
                //  The DEALER socket gives us the address envelope and message
                ZMsg msg = ZMsg.recvMsg(worker);
                ZFrame address = msg.pop();
                ZFrame content = msg.pop();
                assert (content != null);
                msg.destroy();

                //  Send 0..4 replies back
                int replies = rand.nextInt(5);
                for (int reply = 0; reply < replies; reply++) {
                    //  Sleep for some fraction of a second
                    try {
                        Thread.sleep(rand.nextInt(1000) + 1);
                    } catch (InterruptedException e) {
                    }
                    address.send(worker, ZFrame.REUSE + ZFrame.MORE);
                    content.send(worker, ZFrame.REUSE);
                }
                address.destroy();
                content.destroy();
            }
            ctx.destroy();
        }
View Full Code Here

Examples of org.zeromq.ZFrame

            if (items [0].isReadable()) {
                //  Use worker address for LRU routing
                ZMsg msg = ZMsg.recvMsg(backend);
                if (msg == null)
                    break;          //  Interrupted
                ZFrame address = msg.unwrap();
                workers.add( address);

                //  Forward message to client if it's not a READY
                ZFrame frame = msg.getFirst();
                if (new String(frame.getData()).equals(WORKER_READY))
                    msg.destroy();
                else
                    msg.send(frontend);
            }
            if (items [1].isReadable()) {
                //  Get client request, route to first available worker
                ZMsg msg = ZMsg.recvMsg (frontend);
                if (msg != null) {
                    msg.wrap (workers.remove(0));
                    msg.send(backend);
                }
            }
        }
        //  When we're done, clean up properly
        while (workers.size()>0) {
            ZFrame frame = workers.remove(0);
            frame.destroy();
        }
        workers.clear();
        ctx.destroy();
    }
View Full Code Here

Examples of org.zeromq.ZFrame

     * @param request
     * @return
     */
    public ZMsg send(String service, ZMsg request) {

        request.push(new ZFrame(service));
        request.push(MDP.C_CLIENT.newFrame());
        if (verbose) {
            log.format("I: send request to '%s' service: \n", service);
            request.dump(log.out());
        }
        ZMsg reply = null;

        int retriesLeft = retries;
        while (retriesLeft > 0 && !Thread.currentThread().isInterrupted()) {

            request.duplicate().send(client);

            // Poll socket for a reply, with timeout
            ZMQ.Poller items = new ZMQ.Poller(1);
            items.register(client, ZMQ.Poller.POLLIN);
            if (items.poll(timeout) == -1)
                break; // 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() >= 3);

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

                ZFrame replyService = msg.pop();
                assert (service.equals(replyService.toString()));
                replyService.destroy();

                reply = msg;
                break;
            } else {
                items.unregister(client);
View Full Code Here

Examples of org.zeromq.ZFrame

    void sendToBroker(MDP command, String option, ZMsg msg) {
        msg = msg != null ? msg.duplicate() : new ZMsg();

        // 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());
        msg.addFirst(new ZFrame(new byte[0]));

        if (verbose) {
            log.format("I: sending %s to broker\n", command);
            msg.dump(log.out());
        }
View Full Code Here

Examples of org.zeromq.ZFrame

                }
                liveness = HEARTBEAT_LIVENESS;
                // Don't try to handle errors, just assert noisily
                assert (msg != null && msg.size() >= 3);

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

                ZFrame header = msg.pop();
                assert (MDP.W_WORKER.frameEquals(header));
                header.destroy();

                ZFrame command = msg.pop();
                if (MDP.W_REQUEST.frameEquals(command)) {
                    // We should pop and save as many addresses as there are
                    // up to a null part, but for now, just save one
                    replyTo = msg.unwrap();
                    command.destroy();
                    return msg; // We have a request to process
                } else if (MDP.W_HEARTBEAT.frameEquals(command)) {
                    // Do nothing for heartbeats
                } else if (MDP.W_DISCONNECT.frameEquals(command)) {
                    reconnectToBroker();
                } else {
                    log.format("E: invalid input message: \n");
                    msg.dump(log.out());
                }
                command.destroy();
                msg.destroy();
            } else if (--liveness == 0) {
                if (verbose)
                    log.format("W: disconnected from broker - retrying\n");
                try {
View Full Code Here

Examples of org.zeromq.ZFrame

                if (file != null)
                    file.close();
            } catch (IOException e) {
            }
        }
        ZFrame service = request.pop();
        String serviceName = service.toString();

        //  Create MDP client session with short timeout
        mdcliapi client = new mdcliapi("tcp://localhost:5555", false);
        client.setTimeout(1000)//  1 sec
        client.setRetries(1);     //  only 1 retry
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. 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.