BUG-58: refactor to take advantage of netty
[bgpcep.git] / framework / src / main / java / org / opendaylight / protocol / framework / ReconnectImmediatelyStrategy.java
1 package org.opendaylight.protocol.framework;
2
3 import io.netty.util.concurrent.EventExecutor;
4 import io.netty.util.concurrent.Future;
5
6 import javax.annotation.concurrent.ThreadSafe;
7
8 import org.slf4j.Logger;
9 import org.slf4j.LoggerFactory;
10
11 import com.google.common.base.Preconditions;
12
13 /**
14  * Utility ReconnectStrategy singleton, which will cause the reconnect process
15  * to immediately schedule a reconnection attempt.
16  */
17 @ThreadSafe
18 public final class ReconnectImmediatelyStrategy implements ReconnectStrategy {
19         private static final Logger logger = LoggerFactory.getLogger(ReconnectImmediatelyStrategy.class);
20         private final EventExecutor executor;
21         private final int timeout;
22
23         public ReconnectImmediatelyStrategy(final EventExecutor executor, final int timeout) {
24                 Preconditions.checkArgument(timeout >= 0);
25                 this.executor = Preconditions.checkNotNull(executor);
26                 this.timeout = timeout;
27         }
28
29         @Override
30         public Future<Void> scheduleReconnect(final Throwable cause) {
31                 logger.debug("Connection attempt failed", cause);
32                 return executor.newSucceededFuture(null);
33         }
34
35         @Override
36         public void reconnectSuccessful() {
37                 // Nothing to do
38         }
39
40         @Override
41         public int getConnectTimeout() {
42                 return timeout;
43         }
44 }