*/
public synchronized byte[] dispatch(byte[] payload, boolean flush) throws ICPException {
if (connectionDropped) {
myLogger.log(Logger.INFO, "Dispatching with connection dropped. Reconnecting...");
undrop();
throw new ICPException("Connection dropped");
}
else {
if (myConnection != null) {
if (waitingForFlush && !flush) {
throw new ICPException("Upsetting dispatching order");
}
waitingForFlush = false;
if (myLogger.isLoggable(Logger.FINE)) {
myLogger.log(Logger.FINE, "Issuing outgoing command "+outCnt);
}
JICPPacket pkt = new JICPPacket(JICPProtocol.COMMAND_TYPE, JICPProtocol.DEFAULT_INFO, payload);
pkt.setSessionID((byte) outCnt);
try {
lastOutgoingResponse = null;
//System.out.println("Sending command to BE "+pkt.getSessionID());
writePacket(pkt, myConnection);
//System.out.println("Waiting for response from BE "+pkt.getSessionID());
JICPPacket response = waitForResponse(outCnt, RESPONSE_TIMEOUT);
if (response != null) {
//System.out.println("Response received from BE "+response.getSessionID());
if (myLogger.isLoggable(Logger.FINER)) {
myLogger.log(Logger.FINER, "Response received "+response.getSessionID());
}
if (response.getType() == JICPProtocol.ERROR_TYPE) {
// Communication OK, but there was a JICP error on the peer
throw new ICPException(new String(response.getData()));
}
outCnt = (outCnt+1) & 0x0f;
return response.getData();
}
else {
myLogger.log(Logger.WARNING, "Response timeout expired "+pkt.getSessionID());
handleDisconnection();
throw new ICPException("Response timeout expired");
}
}
catch (IOException ioe) {
// Can't reach the BackEnd.
myLogger.log(Logger.WARNING, ioe.toString());
handleDisconnection();
throw new ICPException("Dispatching error.", ioe);
}
}
else {
throw new ICPException("Unreachable");
}
}
}