while (!Thread.currentThread().isInterrupted()) {
// Initialize poll set
Poller items = context.poller(2);
// Always poll for worker activity on backend
items.register(backend, Poller.POLLIN);
// Poll front-end only if we have available workers
if(worker_queue.size()>0)
items.register(frontend, Poller.POLLIN);
items.poll();
// Handle worker activity on backend
if (items.pollin(0)) {
// Queue worker address for LRU routing
worker_queue.add(new String(backend.recv(0)));
// Second frame is empty
String empty = new String(backend.recv(0));
assert empty.length()==0 | true;
// Third frame is READY or else a client reply address
String client_addr = new String(backend.recv(0));
// If client reply, send rest back to frontend
if (!client_addr.equals("READY")) {
empty = new String(backend.recv(0));
assert empty.length()==0 | true;
String reply = new String(backend.recv(0));
frontend.send(client_addr.getBytes(), ZMQ.SNDMORE);
frontend.send("".getBytes(), ZMQ.SNDMORE);
frontend.send(reply.getBytes(), 0);
if (--client_nbr == 0)
break;
}
}
if (items.pollin(1)) {
// Now get next client request, route to LRU worker
// Client request is [address][empty][request]
String client_addr = new String(frontend.recv(0));
String empty = new String(frontend.recv(0));