} else if (m_agentControllerServerListener.received(AgentControllerServerListener.AGENT_UPDATE)) {
// Do update agent by downloading new version.
startMessage = null;
m_connectionPort = 0;
m_state = AgentControllerState.UPDATING;
final AgentUpdateGrinderMessage message = m_agentControllerServerListener.getLastAgentUpdateGrinderMessage();
m_agentControllerServerListener.discardMessages(AgentControllerServerListener.AGENT_UPDATE);
AgentDownloadGrinderMessage agentDownloadGrinderMessage = new AgentDownloadGrinderMessage(message.getVersion());
try {
// If it's initial message
if (agentUpdateHandler == null && message.getNext() == 0) {
IOUtils.closeQuietly(agentUpdateHandler);
agentUpdateHandler = new AgentUpdateHandler(agentConfig, message);
} else if (agentUpdateHandler != null) {
if (message.isValid()) {
retryCount = 0;
agentUpdateHandler.update(message);
agentDownloadGrinderMessage.setNext(message.getNext());
} else if (retryCount <= AgentDownloadGrinderMessage.MAX_RETRY_COUNT) {
retryCount++;
agentDownloadGrinderMessage.setNext(message.getOffset());
} else {
throw new CommunicationException("Error while getting the agent package from " +
"controller");
}
} else {
throw new CommunicationException("Error while getting the agent package from controller");
}
if (consoleCommunication != null) {
consoleCommunication.sendMessage(agentDownloadGrinderMessage);
} else {
break;
}
} catch (IllegalArgumentException ex) {
IOUtils.closeQuietly(agentUpdateHandler);
agentUpdateHandler = null;
retryCount = 0;
LOGGER.info("same or old agent version {} is sent for update. skip this.",
message.getVersion());
m_state = AgentControllerState.READY;
} catch (Exception e) {
retryCount = 0;
IOUtils.closeQuietly(agentUpdateHandler);
agentUpdateHandler = null;