// first byte of identity has hint for routing
public static boolean addressDevice(Socket front,
Socket backend, List<byte[]> identities)
{
SocketBase front_ = front.base();
SocketBase backend_ = backend.base();
boolean success = true;
boolean sentAddress = false;
boolean recvAddress = false;
int rc;
boolean more;
Msg msg;
byte[] target;
int size = identities.size();
PollItem items[] = new PollItem[2];
if (front_.getSocketOpt(ZMQ.ZMQ_TYPE) != ZMQ.ZMQ_ROUTER ||
backend_.getSocketOpt(ZMQ.ZMQ_TYPE) != ZMQ.ZMQ_ROUTER) {
throw new IllegalArgumentException("Both router socket is required");
}
items[0] = new PollItem(front_, ZMQ.ZMQ_POLLIN);
items[1] = new PollItem(backend_, ZMQ.ZMQ_POLLIN);
Selector selector;
try {
selector = Selector.open();
} catch (IOException e) {
throw new ZError.IOException(e);
}
while (success) {
// Wait while there are either requests or replies to process.
rc = ZMQ.zmq_poll(selector, items, -1);
if (rc < 0)
break;
// Process a request.
if (items[0].isReadable()) {
while (true) {
msg = front_.recv(0);
if (msg == null) {
success = false;
break;
}
if (!sentAddress) {
int hint = (int) msg.data()[0];
if (hint < 0) {
hint = (0xFF) & hint;
}
target = identities.get(hint % size);
// routing address
success = backend_.send(new Msg(target), ZMQ.ZMQ_SNDMORE);
if (!success) {
System.out.println("send 0 failed");
break;
}
sentAddress = true;
}
more = msg.hasMore();
success = backend_.send(msg, more ? ZMQ.ZMQ_SNDMORE : 0);
if (!success) {
System.out.println("send failed");
break;
}
if (!more) {
sentAddress = false;
break;
}
}
}
// Process a reply.
if (success && items[1].isReadable()) {
while (true) {
msg = backend_.recv(0);
if (msg == null) {
System.out.println("recv failed");
success = false;
break;