}
public class lruqueue {
public static void main(String[] args) {
Context context = ZMQ.context(1);
// Prepare our context and sockets
Socket frontend = context.socket(ZMQ.ROUTER);
Socket backend = context.socket(ZMQ.ROUTER);
frontend.bind("ipc://frontend.ipc");
backend.bind("ipc://backend.ipc");
int client_nbr;
for (client_nbr = 0; client_nbr < 10; client_nbr++)
new ClientThread().start();
int worker_nbr;
for (worker_nbr = 0; worker_nbr < 3; worker_nbr++)
new WorkerThread().start();
// Logic of LRU loop
// - Poll backend always, frontend only if 1+ worker ready
// - If worker replies, queue worker as ready and forward reply
// to client if necessary
// - If client requests, pop next worker and send request to it
//
// A very simple queue structure with known max size
Queue<String> worker_queue = new LinkedList<String>();
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));
assert empty.length()==0 | true;
String request = new String(frontend.recv(0));
String worker_addr = worker_queue.poll();//worker_queue [0];
backend.send(worker_addr.getBytes(), ZMQ.SNDMORE);
backend.send("".getBytes(), ZMQ.SNDMORE);
backend.send(client_addr.getBytes(), ZMQ.SNDMORE);
backend.send("".getBytes(), ZMQ.SNDMORE);
backend.send(request.getBytes(), 0);
}
}
frontend.close();
backend.close();
context.term();
System.exit(0);
}