2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.netconf.nettyutil;
10 import com.google.common.base.Preconditions;
11 import io.netty.bootstrap.Bootstrap;
12 import io.netty.channel.ChannelFuture;
13 import io.netty.channel.ChannelFutureListener;
14 import io.netty.channel.ChannelOption;
15 import io.netty.util.concurrent.DefaultPromise;
16 import io.netty.util.concurrent.EventExecutor;
17 import io.netty.util.concurrent.Future;
18 import io.netty.util.concurrent.FutureListener;
19 import io.netty.util.concurrent.Promise;
20 import java.net.InetSocketAddress;
21 import org.checkerframework.checker.lock.qual.GuardedBy;
22 import org.opendaylight.netconf.api.NetconfSession;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
27 final class NetconfSessionPromise<S extends NetconfSession> extends DefaultPromise<S> {
28 private static final Logger LOG = LoggerFactory.getLogger(NetconfSessionPromise.class);
29 private final ReconnectStrategy strategy;
30 private InetSocketAddress address;
31 private final Bootstrap bootstrap;
34 private Future<?> pending;
36 NetconfSessionPromise(final EventExecutor executor, final InetSocketAddress address,
37 final ReconnectStrategy strategy, final Bootstrap bootstrap) {
39 this.strategy = Preconditions.checkNotNull(strategy);
40 this.address = Preconditions.checkNotNull(address);
41 this.bootstrap = Preconditions.checkNotNull(bootstrap);
44 @SuppressWarnings("checkstyle:illegalCatch")
45 synchronized void connect() {
47 final int timeout = this.strategy.getConnectTimeout();
49 LOG.debug("Promise {} attempting connect for {}ms", this, timeout);
51 if (this.address.isUnresolved()) {
52 this.address = new InetSocketAddress(this.address.getHostName(), this.address.getPort());
54 this.bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, timeout);
55 final ChannelFuture connectFuture = this.bootstrap.connect(this.address);
56 // Add listener that attempts reconnect by invoking this method again.
57 connectFuture.addListener(new BootstrapConnectListener());
58 this.pending = connectFuture;
59 } catch (final Exception e) {
60 LOG.info("Failed to connect to {}", address, e);
66 public synchronized boolean cancel(final boolean mayInterruptIfRunning) {
67 if (super.cancel(mayInterruptIfRunning)) {
68 this.pending.cancel(mayInterruptIfRunning);
76 public synchronized Promise<S> setSuccess(final S result) {
77 LOG.debug("Promise {} completed", this);
78 this.strategy.reconnectSuccessful();
79 return super.setSuccess(result);
82 private class BootstrapConnectListener implements ChannelFutureListener {
84 public void operationComplete(final ChannelFuture cf) {
85 synchronized (NetconfSessionPromise.this) {
87 LOG.debug("Promise {} connection resolved", NetconfSessionPromise.this);
89 // Triggered when a connection attempt is resolved.
90 Preconditions.checkState(NetconfSessionPromise.this.pending.equals(cf));
93 * The promise we gave out could have been cancelled,
94 * which cascades to the connect getting cancelled,
95 * but there is a slight race window, where the connect
96 * is already resolved, but the listener has not yet
97 * been notified -- cancellation at that point won't
98 * stop the notification arriving, so we have to close
102 if (cf.isSuccess()) {
103 LOG.debug("Closing channel for cancelled promise {}", NetconfSessionPromise.this);
104 cf.channel().close();
109 if (cf.isSuccess()) {
110 LOG.debug("Promise {} connection successful", NetconfSessionPromise.this);
114 LOG.debug("Attempt to connect to {} failed", NetconfSessionPromise.this.address, cf.cause());
116 final Future<Void> rf = NetconfSessionPromise.this.strategy.scheduleReconnect(cf.cause());
117 rf.addListener(new ReconnectingStrategyListener());
118 NetconfSessionPromise.this.pending = rf;
122 private class ReconnectingStrategyListener implements FutureListener<Void> {
124 public void operationComplete(final Future<Void> sf) {
125 synchronized (NetconfSessionPromise.this) {
126 // Triggered when a connection attempt is to be made.
127 Preconditions.checkState(NetconfSessionPromise.this.pending.equals(sf));
130 * The promise we gave out could have been cancelled,
131 * which cascades to the reconnect attempt getting
132 * cancelled, but there is a slight race window, where
133 * the reconnect attempt is already enqueued, but the
134 * listener has not yet been notified -- if cancellation
135 * happens at that point, we need to catch it here.
137 if (!isCancelled()) {
138 if (sf.isSuccess()) {
141 setFailure(sf.cause());