use of org.apache.mina.core.session.IoSessionInitializer in project opennms by OpenNMS.
the class AsyncBasicDetectorMinaImpl method isServiceDetected.
/** {@inheritDoc} */
@Override
public final DetectFuture isServiceDetected(final InetAddress address) {
final DetectFutureMinaImpl detectFuture = new DetectFutureMinaImpl(this);
try {
// Set this up here because it can throw an Exception, which we want
// to throw now, not in initializeSession
final SSLContext c = createClientSSLContext();
// Create an IoSessionInitializer that will configure this individual
// session. Previously, all this was done on a new Connector each time
// but that was leaking file handles all over the place. This way gives
// us per-connection settings without the overhead of creating new
// Connectors each time
IoSessionInitializer<ConnectFuture> init = new IoSessionInitializer<ConnectFuture>() {
@Override
public void initializeSession(IoSession session, ConnectFuture future) {
// Add filters to the session
if (isUseSSLFilter()) {
final SslFilter filter = new SslFilter(c);
filter.setUseClientMode(true);
session.getFilterChain().addFirst("SSL", filter);
}
session.getFilterChain().addLast("logger", getLoggingFilter() != null ? getLoggingFilter() : new SlightlyMoreVerboseLoggingFilter());
session.getFilterChain().addLast("codec", getProtocolCodecFilter());
// Make the minimum idle timeout 1 second
int idleTimeInSeconds = Math.max(1, Math.round(getIdleTime() / 1000.0f));
// Set all of the idle time limits. Make sure to specify values in
// seconds!!!
session.getConfig().setReaderIdleTime(idleTimeInSeconds);
session.getConfig().setWriterIdleTime(idleTimeInSeconds);
session.getConfig().setBothIdleTime(idleTimeInSeconds);
}
};
// Start communication
final InetSocketAddress socketAddress = new InetSocketAddress(address, getPort());
final ConnectFuture cf = m_connectionFactory.connect(socketAddress, init, createDetectorHandler(detectFuture));
cf.addListener(retryAttemptListener(detectFuture, socketAddress, init, getRetries()));
} catch (KeyManagementException e) {
detectFuture.setException(e);
} catch (NoSuchAlgorithmException e) {
detectFuture.setException(e);
} catch (Throwable e) {
detectFuture.setException(e);
}
return detectFuture;
}
Aggregations