import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-public class BGPProtocolSessionPromise<S extends BGPSession> extends DefaultPromise<S> {
+public final class BGPProtocolSessionPromise<S extends BGPSession> extends DefaultPromise<S> {
private static final Logger LOG = LoggerFactory.getLogger(BGPProtocolSessionPromise.class);
private static final int CONNECT_TIMEOUT = 5000;
private final int retryTimer;
private final Bootstrap bootstrap;
private final BGPPeerRegistry peerRegistry;
+ @GuardedBy("this")
private final AutoCloseable listenerRegistration;
@GuardedBy("this")
private ChannelFuture pending;
private boolean connectSkipped;
- public BGPProtocolSessionPromise(InetSocketAddress remoteAddress, int retryTimer, Bootstrap bootstrap, BGPPeerRegistry peerRegistry) {
+ public BGPProtocolSessionPromise(@Nonnull final InetSocketAddress remoteAddress, final int retryTimer,
+ @Nonnull final Bootstrap bootstrap, @Nonnull final BGPPeerRegistry peerRegistry) {
super(GlobalEventExecutor.INSTANCE);
this.address = Preconditions.checkNotNull(remoteAddress);
this.retryTimer = retryTimer;
this.bootstrap = Preconditions.checkNotNull(bootstrap);
this.peerRegistry = Preconditions.checkNotNull(peerRegistry);
- this.listenerRegistration = this.peerRegistry
- .registerPeerSessionListener(new BGPProtocolSessionPromise.PeerRegistrySessionListenerImpl(this,
- StrictBGPPeerRegistry.getIpAddress(this.address)));
+ this.listenerRegistration = this.peerRegistry.registerPeerSessionListener(
+ new BGPProtocolSessionPromise.PeerRegistrySessionListenerImpl(this,
+ StrictBGPPeerRegistry.getIpAddress(this.address)));
}
public synchronized void connect() {
final ChannelFuture connectFuture = this.bootstrap.connect();
connectFuture.addListener(new BGPProtocolSessionPromise.BootstrapConnectListener(lock));
this.pending = connectFuture;
- } catch (Exception e) {
+ } catch (final Exception e) {
LOG.info("Failed to connect to {}", this.address, e);
this.setFailure(e);
}
final BGPProtocolSessionPromise lock = this;
final EventLoop loop = this.pending.channel().eventLoop();
- loop.schedule(new Runnable() {
- @Override
- public void run() {
+ loop.schedule(() -> {
+ synchronized (BGPProtocolSessionPromise.this) {
if (BGPProtocolSessionPromise.this.peerSessionPresent) {
LOG.debug("Connection to {} already exists", BGPProtocolSessionPromise.this.address);
BGPProtocolSessionPromise.this.connectSkipped = true;
}
}
- private void closePeerSessionListener() {
+ private synchronized void closePeerSessionListener() {
try {
this.listenerRegistration.close();
} catch (final Exception e) {
}
private class BootstrapConnectListener implements ChannelFutureListener {
+ @GuardedBy("this")
private final Object lock;
- public BootstrapConnectListener(final Object lock) {
+ BootstrapConnectListener(final Object lock) {
this.lock = lock;
}
}
private class PeerRegistrySessionListenerImpl implements PeerRegistrySessionListener {
+ @GuardedBy("this")
private final Object lock;
private final IpAddress peerAddress;