_log.error("Too many communication failures with client '" + _client.getInetAddress() + "'. Closing connection.");
break;
}
// retrieve command from client
Command command = null;
try {
command = (Command) _xstream.fromXML(new InputStreamReader(_client.getInputStream()));
} catch (Exception e) {
if (e.getCause() != null) {
Throwable cause = e.getCause();
if (cause instanceof SocketTimeoutException) {
// client connection timed out
_log.warn("Connection for client '" + _client.getInetAddress() + "' timed out. Sending disconnect command.");
// send disconnect command to client
command = new Disconnect();
} else {
_log.error("Unable to retrieve command from client '" + _client.getInetAddress() + "'.", e);
_failures++;
}
} else {
_log.error("Unable to retrieve command from client '" + _client.getInetAddress() + "'.", e);
_failures++;
}
}
// execute command
if (command != null) {
try {
if (command instanceof Connect) {
_log.info("connect command from client '" + _client.getInetAddress() + "' recieved");
String clientVersion = ((Connect)command).getClientVersion();
if (clientVersion.equals(getVersion())) {
_log.info("client version accepted");
sendResponse(new Boolean(true));
} else {
throw new CommandException("Invalid client version '" + clientVersion + "'.");
}
} else if (command instanceof Disconnect) {
_log.info("disconnect command from client '" + _client.getInetAddress() + "' recieved.");
sendResponse(new Boolean(true));
break;
} else {
_log.info("executing command '" + command.getClass().getName() + "' for client '" + _client.getInetAddress() + "'.");
Object response = _commandHandler.execute(command);
_log.info("sending response to client '" + _client.getInetAddress() + "'.");
sendResponse(response);
}
} catch (CommandException e) {
_log.warn("executing command '" + command.getClass().getName() + "' for client '" + _client.getInetAddress() + "' failed.", e);
// send exception to client
sendResponse(e);
} catch (Exception e) {
_log.error("Unexpected error in ClientWorker.run().", e);
// send exception to client