return;
}
}
if (s_clusteredAgentMgr == null) {
throw new AgentUnavailableException("ClusteredAgentAttache not properly initialized", _id);
}
int i = 0;
SocketChannel ch = null;
boolean error = true;
try {
while (i++ < 5) {
String peerName = s_clusteredAgentMgr.findPeer(_id);
if (peerName == null) {
throw new AgentUnavailableException("Unable to find peer", _id);
}
ch = s_clusteredAgentMgr.connectToPeer(peerName, ch);
if (ch == null) {
if (s_logger.isDebugEnabled()) {
s_logger.debug(log(seq, "Unable to forward " + req.toString()));
}
continue;
}
SSLEngine sslEngine = s_clusteredAgentMgr.getSSLEngine(peerName);
if (sslEngine == null) {
throw new AgentUnavailableException("Unable to get SSLEngine of peer " + peerName, _id);
}
try {
if (s_logger.isDebugEnabled()) {
s_logger.debug(log(seq, "Forwarding " + req.toString() + " to " + peerName));
}
if (req.executeInSequence() && listener != null && listener instanceof SynchronousListener) {
SynchronousListener synchronous = (SynchronousListener)listener;
synchronous.setPeer(peerName);
}
Link.write(ch, req.toBytes(), sslEngine);
error = false;
return;
} catch (IOException e) {
if (s_logger.isDebugEnabled()) {
s_logger.debug(log(seq, "Error on connecting to management node: " + req.toString() + " try = " + i));
}
if(s_logger.isInfoEnabled()) {
s_logger.info("IOException " + e.getMessage() + " when sending data to peer " + peerName + ", close peer connection and let it re-open");
}
}
}
} finally {
if (error) {
unregisterListener(seq);
}
}
throw new AgentUnavailableException("Unable to reach the peer that the agent is connected", _id);
}