Search in sources :

Example 26 with ZMsg

use of org.zeromq.ZMsg in project jeromq by zeromq.

the class flserver3 method main.

public static void main(String[] args) {
    boolean verbose = (args.length > 0 && args[0].equals("-v"));
    ZContext ctx = new ZContext();
    //  Prepare server socket with predictable identity
    String bindEndpoint = "tcp://*:5555";
    String connectEndpoint = "tcp://localhost:5555";
    Socket server = ctx.createSocket(ZMQ.ROUTER);
    server.setIdentity(connectEndpoint.getBytes(ZMQ.CHARSET));
    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)
            //  Interrupted
            break;
        //  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(new ZFrame("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();
}
Also used : ZFrame(org.zeromq.ZFrame) ZContext(org.zeromq.ZContext) ZMsg(org.zeromq.ZMsg) Socket(org.zeromq.ZMQ.Socket)

Example 27 with ZMsg

use of org.zeromq.ZMsg in project jeromq by zeromq.

the class mdclient method main.

public static void main(String[] args) {
    boolean verbose = (args.length > 0 && "-v".equals(args[0]));
    mdcliapi clientSession = new mdcliapi("tcp://localhost:5555", verbose);
    int count;
    for (count = 0; count < 100000; count++) {
        ZMsg request = new ZMsg();
        request.addString("Hello world");
        ZMsg reply = clientSession.send("echo", request);
        if (reply != null)
            reply.destroy();
        else
            // Interrupt or failure
            break;
    }
    System.out.printf("%d requests/replies processed\n", count);
    clientSession.destroy();
}
Also used : ZMsg(org.zeromq.ZMsg)

Example 28 with ZMsg

use of org.zeromq.ZMsg in project jeromq by zeromq.

the class mdclient2 method main.

public static void main(String[] args) {
    boolean verbose = (args.length > 0 && "-v".equals(args[0]));
    mdcliapi2 clientSession = new mdcliapi2("tcp://localhost:5555", verbose);
    int count;
    for (count = 0; count < 100000; count++) {
        ZMsg request = new ZMsg();
        request.addString("Hello world");
        clientSession.send("echo", request);
    }
    for (count = 0; count < 100000; count++) {
        ZMsg reply = clientSession.recv();
        if (reply != null)
            reply.destroy();
        else
            // Interrupt or failure
            break;
    }
    System.out.printf("%d requests/replies processed\n", count);
    clientSession.destroy();
}
Also used : ZMsg(org.zeromq.ZMsg)

Example 29 with ZMsg

use of org.zeromq.ZMsg in project jeromq by zeromq.

the class mdwrkapi method receive.

/**
     * Send reply, if any, to broker and wait for next request.
     */
public ZMsg receive(ZMsg reply) {
    // Format and send the reply if we were provided one
    assert (reply != null || !expectReply);
    if (reply != null) {
        assert (replyTo != null);
        reply.wrap(replyTo);
        sendToBroker(MDP.W_REPLY, null, reply);
        reply.destroy();
    }
    expectReply = true;
    while (!Thread.currentThread().isInterrupted()) {
        // Poll socket for a reply, with timeout
        ZMQ.Poller items = ctx.createPoller(1);
        items.register(worker, ZMQ.Poller.POLLIN);
        if (items.poll(timeout) == -1)
            // Interrupted
            break;
        if (items.pollin(0)) {
            ZMsg msg = ZMsg.recvMsg(worker);
            if (msg == null)
                // Interrupted
                break;
            if (verbose) {
                log.format("I: received message from broker: \n");
                msg.dump(log.out());
            }
            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();
                // We have a request to process
                return msg;
            } 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 {
                Thread.sleep(reconnect);
            } catch (InterruptedException e) {
                // Restore the
                Thread.currentThread().interrupt();
                // interrupted status
                break;
            }
            reconnectToBroker();
        }
        // Send HEARTBEAT if it's time
        if (System.currentTimeMillis() > heartbeatAt) {
            sendToBroker(MDP.W_HEARTBEAT, null, null);
            heartbeatAt = System.currentTimeMillis() + heartbeat;
        }
    }
    if (Thread.currentThread().isInterrupted())
        log.format("W: interrupt received, killing worker\n");
    return null;
}
Also used : ZFrame(org.zeromq.ZFrame) ZMsg(org.zeromq.ZMsg) ZMQ(org.zeromq.ZMQ)

Example 30 with ZMsg

use of org.zeromq.ZMsg in project jeromq by zeromq.

the class flcliapi method request.

//  .split request method
//  To implement the request method, the frontend object sends a message
//  to the backend, specifying a command "REQUEST" and the request message:
public ZMsg request(ZMsg request) {
    request.push("REQUEST");
    request.send(pipe);
    ZMsg reply = ZMsg.recvMsg(pipe);
    if (reply != null) {
        String status = reply.popString();
        if (status.equals("FAILED"))
            reply.destroy();
    }
    return reply;
}
Also used : ZMsg(org.zeromq.ZMsg)

Aggregations

ZMsg (org.zeromq.ZMsg)38 ZFrame (org.zeromq.ZFrame)20 ZContext (org.zeromq.ZContext)14 Socket (org.zeromq.ZMQ.Socket)14 Poller (org.zeromq.ZMQ.Poller)10 ArrayList (java.util.ArrayList)4 Random (java.util.Random)4 ZMQ (org.zeromq.ZMQ)4 IOException (java.io.IOException)3 File (java.io.File)2 RandomAccessFile (java.io.RandomAccessFile)2 BufferedWriter (java.io.BufferedWriter)1 DataInputStream (java.io.DataInputStream)1 DataOutputStream (java.io.DataOutputStream)1 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 FileOutputStream (java.io.FileOutputStream)1 FileWriter (java.io.FileWriter)1 LinkedList (java.util.LinkedList)1 PollItem (org.zeromq.ZMQ.PollItem)1