Thread.sleep(100);
} catch (InterruptedException e) {
}
} else {
try {
BinkpFrame frame = frames.removeFirst();
try {
process.getOutputStream().write(
frame.getBytes());
process.getOutputStream().flush();
logger.l5("Frame sent: " + frame);
} catch (IOException e) {
logger.l2("IOException: "
+ e.getLocalizedMessage());
break;
}
} catch (NoSuchElementException ignore) {
logger.l3("NoSuchElement exception");
}
}
}
logger.l3("PIPE processOutputObserver exits");
closed = true;
return;
}
};
ThreadPool.execute(processOutputObserver);
try {
greet();
while (!closed) {
if (!isConnected()) {
try {
Thread.sleep(100); // let's proccess to write messages;
} catch (InterruptedException ignore) {
}
continue;
}
int[] head = new int[2];
for (int i = 0; i < 2; i++) {
head[i] = readOrDie(process.getInputStream());
}
int len = ((head[0] & 0xff) << 8 | (head[1] & 0xff)) & 0x7FFF;
int remaining = len;
ByteBuffer data = ByteBuffer.allocate(len);
boolean command = (head[0] & 0x80) > 0;
while (remaining > 0) {
byte[] buf = readOrDie(process.getInputStream(), remaining);
remaining -= buf.length;
data.put(buf);
}
data.flip();
BinkpFrame frame;
if (command) {
BinkpCommand cmd = BinkpProtocolTools
.getCommand(data.get());
if (data.get(len - 1) == 0) {
len--;
}
byte[] ndata = new byte[len - 1];
data.get(ndata);
frame = new BinkpFrame(cmd, new String(ndata));
} else {
frame = new BinkpFrame(data.array());
}
logger.l5("Frame received: " + frame);
proccessFrame(frame);
}
finish("Connection closed");