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:
if (System.currentTimeMillis() >= heartbeat_at) {
for (Worker worker: workers) {
worker.address.send(backend,
ZFrame.REUSE + ZFrame.MORE);
ZFrame frame = new ZFrame (PPP_HEARTBEAT);
frame.send(backend, 0);
}
heartbeat_at = System.currentTimeMillis() + HEARTBEAT_INTERVAL;
}
Worker.purge (workers);
}