final JICPPacket dispatch(JICPPacket pkt, boolean flush) throws ICPException {
dispatching = true;
try {
if (active && myConnection != null) {
if (waitingForFlush && !flush) {
throw new ICPException("Upsetting dispatching order");
}
waitingForFlush = false;
if (myLogger.isLoggable(Logger.FINE)) {
myLogger.log(Logger.FINE, myID+": Sending command "+inpCnt+" to FE");
}
pkt.setSessionID((byte) inpCnt);
try {
lastIncomingResponse = null;
System.out.println("Sending command to FE "+pkt.getSessionID());
writePacket(myConnection, pkt);
System.out.println("Waiting for response from FE "+pkt.getSessionID());
pkt = waitForResponse(inpCnt, RESPONSE_TIMEOUT);
if (pkt != null) {
System.out.println("Response received from FE "+pkt.getSessionID());
if (myLogger.isLoggable(Logger.FINER)) {
myLogger.log(Logger.FINER, myID+": Response received from FE "+pkt.getSessionID());
}
if (pkt.getType() == JICPProtocol.ERROR_TYPE) {
// Communication OK, but there was a JICP error on the peer
throw new ICPException(new String(pkt.getData()));
}
checkTerminatedInfo(pkt);
if (!peerActive) {
// This is the response to an exit command --> Suicide, without
// killing the above container since it is already dying.
BackEndDispatcher.this.shutdown();
}
inpCnt = (inpCnt+1) & 0x0f;
return pkt;
}
else {
myLogger.log(Logger.WARNING, myID+": Response timeout expired");
handleConnectionError(myConnection, null);
throw new ICPException("Response timeout expired");
}
}
catch (IOException ioe) {
// There was an IO exception writing data to the connection
// --> reset the connection.
myLogger.log(Logger.WARNING, myID+": "+ioe);
handleConnectionError(myConnection, ioe);
throw new ICPException("Dispatching error.", ioe);
}
}
else {
throw new ICPException("Unreachable");
}
}
finally {
dispatching = false;
}