Search in sources :

Example 36 with ZMsg

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

the class mdbroker method mediate.

// ---------------------------------------------------------------------
/**
     * Main broker work happens here
     */
public void mediate() {
    while (!Thread.currentThread().isInterrupted()) {
        ZMQ.Poller items = ctx.createPoller(1);
        items.register(socket, ZMQ.Poller.POLLIN);
        if (items.poll(HEARTBEAT_INTERVAL) == -1)
            // Interrupted
            break;
        if (items.pollin(0)) {
            ZMsg msg = ZMsg.recvMsg(socket);
            if (msg == null)
                // Interrupted
                break;
            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();
        }
        purgeWorkers();
        sendHeartbeats();
    }
    // interrupted
    destroy();
}
Also used : ZFrame(org.zeromq.ZFrame) ZMsg(org.zeromq.ZMsg) ZMQ(org.zeromq.ZMQ)

Example 37 with ZMsg

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

the class mdbroker method sendToWorker.

/**
     * Send message to worker. If message is provided, sends that message. Does
     * 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);
}
Also used : ZFrame(org.zeromq.ZFrame) ZMsg(org.zeromq.ZMsg)

Example 38 with ZMsg

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

the class lruqueue3 method run.

public void run() {
    ZContext context = new ZContext();
    //  Prepare our context and sockets
    Socket worker = context.createSocket(ZMQ.REQ);
    worker.connect("ipc://backend.ipc");
    ZFrame frame = new ZFrame(lruqueue3.LRU_READY);
    //  Tell backend we're ready for work
    frame.send(worker, 0);
    while (true) {
        ZMsg msg = ZMsg.recvMsg(worker);
        if (msg == null)
            break;
        msg.getLast().reset("OK".getBytes(ZMQ.CHARSET));
        msg.send(worker);
        System.out.println(Thread.currentThread().getName() + " Worker Sent OK");
    }
    context.destroy();
}
Also used : ZFrame(org.zeromq.ZFrame) ZContext(org.zeromq.ZContext) ZMsg(org.zeromq.ZMsg) Socket(org.zeromq.ZMQ.Socket)

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