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();
}
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);
}
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();
}
Aggregations