Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 1 | /* |
Ray Milkey | 34c9590 | 2015-04-15 09:47:53 -0700 | [diff] [blame] | 2 | * Copyright 2014-2015 Open Networking Laboratory |
Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 15 | */ |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 16 | package org.onlab.netty; |
| 17 | |
Madan Jampani | d36def0 | 2016-01-13 11:21:56 -0800 | [diff] [blame] | 18 | import com.google.common.cache.Cache; |
| 19 | import com.google.common.cache.CacheBuilder; |
| 20 | import com.google.common.cache.RemovalListener; |
| 21 | import com.google.common.cache.RemovalNotification; |
| 22 | import com.google.common.util.concurrent.MoreExecutors; |
| 23 | |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 24 | import io.netty.bootstrap.Bootstrap; |
| 25 | import io.netty.bootstrap.ServerBootstrap; |
| 26 | import io.netty.buffer.PooledByteBufAllocator; |
| 27 | import io.netty.channel.Channel; |
| 28 | import io.netty.channel.ChannelFuture; |
Madan Jampani | ddf7622 | 2014-10-04 23:48:44 -0700 | [diff] [blame] | 29 | import io.netty.channel.ChannelHandler; |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 30 | import io.netty.channel.ChannelHandlerContext; |
| 31 | import io.netty.channel.ChannelInitializer; |
| 32 | import io.netty.channel.ChannelOption; |
| 33 | import io.netty.channel.EventLoopGroup; |
Madan Jampani | 824a7c1 | 2014-10-21 09:46:15 -0700 | [diff] [blame] | 34 | import io.netty.channel.ServerChannel; |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 35 | import io.netty.channel.SimpleChannelInboundHandler; |
Madan Jampani | 824a7c1 | 2014-10-21 09:46:15 -0700 | [diff] [blame] | 36 | import io.netty.channel.epoll.EpollEventLoopGroup; |
| 37 | import io.netty.channel.epoll.EpollServerSocketChannel; |
| 38 | import io.netty.channel.epoll.EpollSocketChannel; |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 39 | import io.netty.channel.nio.NioEventLoopGroup; |
| 40 | import io.netty.channel.socket.SocketChannel; |
| 41 | import io.netty.channel.socket.nio.NioServerSocketChannel; |
| 42 | import io.netty.channel.socket.nio.NioSocketChannel; |
| 43 | |
Madan Jampani | d36def0 | 2016-01-13 11:21:56 -0800 | [diff] [blame] | 44 | import org.apache.commons.pool.KeyedPoolableObjectFactory; |
| 45 | import org.apache.commons.pool.impl.GenericKeyedObjectPool; |
| 46 | import org.onlab.util.Tools; |
| 47 | import org.onosproject.store.cluster.messaging.Endpoint; |
| 48 | import org.onosproject.store.cluster.messaging.MessagingService; |
| 49 | import org.slf4j.Logger; |
| 50 | import org.slf4j.LoggerFactory; |
| 51 | |
| 52 | import javax.net.ssl.KeyManagerFactory; |
| 53 | import javax.net.ssl.SSLContext; |
| 54 | import javax.net.ssl.SSLEngine; |
| 55 | import javax.net.ssl.TrustManagerFactory; |
| 56 | |
JunHuy Lam | 39eb429 | 2015-06-26 17:24:23 +0900 | [diff] [blame] | 57 | import java.io.FileInputStream; |
Madan Jampani | 348a9fe | 2014-11-09 01:37:51 -0800 | [diff] [blame] | 58 | import java.io.IOException; |
JunHuy Lam | 39eb429 | 2015-06-26 17:24:23 +0900 | [diff] [blame] | 59 | import java.security.KeyStore; |
Madan Jampani | afeebbd | 2015-05-19 15:26:01 -0700 | [diff] [blame] | 60 | import java.util.Map; |
Madan Jampani | 2bfa94c | 2015-04-11 05:03:49 -0700 | [diff] [blame] | 61 | import java.util.concurrent.CompletableFuture; |
Madan Jampani | 348a9fe | 2014-11-09 01:37:51 -0800 | [diff] [blame] | 62 | import java.util.concurrent.ConcurrentHashMap; |
Madan Jampani | ec5ae34 | 2015-04-13 15:43:10 -0700 | [diff] [blame] | 63 | import java.util.concurrent.Executor; |
Thomas Vachuska | fc52fec | 2015-05-18 19:13:56 -0700 | [diff] [blame] | 64 | import java.util.concurrent.RejectedExecutionException; |
Madan Jampani | 348a9fe | 2014-11-09 01:37:51 -0800 | [diff] [blame] | 65 | import java.util.concurrent.TimeUnit; |
| 66 | import java.util.concurrent.TimeoutException; |
Madan Jampani | afeebbd | 2015-05-19 15:26:01 -0700 | [diff] [blame] | 67 | import java.util.concurrent.atomic.AtomicBoolean; |
Madan Jampani | 348a9fe | 2014-11-09 01:37:51 -0800 | [diff] [blame] | 68 | import java.util.concurrent.atomic.AtomicLong; |
Madan Jampani | d36def0 | 2016-01-13 11:21:56 -0800 | [diff] [blame] | 69 | import java.util.function.BiConsumer; |
| 70 | import java.util.function.BiFunction; |
Madan Jampani | c26eede | 2015-04-16 11:42:16 -0700 | [diff] [blame] | 71 | import java.util.function.Consumer; |
Ray Milkey | 9f87e51 | 2016-01-05 10:00:22 -0800 | [diff] [blame] | 72 | |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 73 | /** |
Madan Jampani | c26eede | 2015-04-16 11:42:16 -0700 | [diff] [blame] | 74 | * Implementation of MessagingService based on <a href="http://netty.io/">Netty</a> framework. |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 75 | */ |
Madan Jampani | afeebbd | 2015-05-19 15:26:01 -0700 | [diff] [blame] | 76 | public class NettyMessaging implements MessagingService { |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 77 | |
| 78 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 79 | |
Madan Jampani | c26eede | 2015-04-16 11:42:16 -0700 | [diff] [blame] | 80 | private static final String REPLY_MESSAGE_TYPE = "NETTY_MESSAGING_REQUEST_REPLY"; |
| 81 | |
Madan Jampani | afeebbd | 2015-05-19 15:26:01 -0700 | [diff] [blame] | 82 | private Endpoint localEp; |
Aaron Kruglikov | eb0ae4e | 2015-11-10 19:16:16 -0800 | [diff] [blame] | 83 | private int preamble; |
Madan Jampani | afeebbd | 2015-05-19 15:26:01 -0700 | [diff] [blame] | 84 | private final AtomicBoolean started = new AtomicBoolean(false); |
| 85 | private final Map<String, Consumer<InternalMessage>> handlers = new ConcurrentHashMap<>(); |
Madan Jampani | 24f9efb | 2014-10-24 18:56:23 -0700 | [diff] [blame] | 86 | private final AtomicLong messageIdGenerator = new AtomicLong(0); |
Madan Jampani | d36def0 | 2016-01-13 11:21:56 -0800 | [diff] [blame] | 87 | private final Cache<Long, Callback> callbacks = CacheBuilder.newBuilder() |
Madan Jampani | 5f9ec9a | 2014-10-29 13:45:52 -0700 | [diff] [blame] | 88 | .expireAfterWrite(10, TimeUnit.SECONDS) |
Madan Jampani | d36def0 | 2016-01-13 11:21:56 -0800 | [diff] [blame] | 89 | .removalListener(new RemovalListener<Long, Callback>() { |
Madan Jampani | 5f9ec9a | 2014-10-29 13:45:52 -0700 | [diff] [blame] | 90 | @Override |
Madan Jampani | d36def0 | 2016-01-13 11:21:56 -0800 | [diff] [blame] | 91 | public void onRemoval(RemovalNotification<Long, Callback> entry) { |
Yuta HIGUCHI | c611d92 | 2015-02-10 16:07:38 -0800 | [diff] [blame] | 92 | if (entry.wasEvicted()) { |
Madan Jampani | 2bfa94c | 2015-04-11 05:03:49 -0700 | [diff] [blame] | 93 | entry.getValue().completeExceptionally(new TimeoutException("Timedout waiting for reply")); |
Yuta HIGUCHI | c611d92 | 2015-02-10 16:07:38 -0800 | [diff] [blame] | 94 | } |
Madan Jampani | 5f9ec9a | 2014-10-29 13:45:52 -0700 | [diff] [blame] | 95 | } |
| 96 | }) |
Madan Jampani | ddf7622 | 2014-10-04 23:48:44 -0700 | [diff] [blame] | 97 | .build(); |
Madan Jampani | 2e5f87b | 2015-02-22 10:37:15 -0800 | [diff] [blame] | 98 | |
Aaron Kruglikov | 8a35219 | 2016-01-22 15:13:08 -0800 | [diff] [blame^] | 99 | private final GenericKeyedObjectPool<Endpoint, Connection> channels |
| 100 | = new GenericKeyedObjectPool<Endpoint, Connection>(new OnosCommunicationChannelFactory()); |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 101 | |
Madan Jampani | 824a7c1 | 2014-10-21 09:46:15 -0700 | [diff] [blame] | 102 | private EventLoopGroup serverGroup; |
| 103 | private EventLoopGroup clientGroup; |
| 104 | private Class<? extends ServerChannel> serverChannelClass; |
| 105 | private Class<? extends Channel> clientChannelClass; |
| 106 | |
JunHuy Lam | 39eb429 | 2015-06-26 17:24:23 +0900 | [diff] [blame] | 107 | protected static final boolean TLS_DISABLED = false; |
Jonathan Hart | d9df7bd | 2015-11-10 17:10:25 -0800 | [diff] [blame] | 108 | protected boolean enableNettyTls = TLS_DISABLED; |
JunHuy Lam | 39eb429 | 2015-06-26 17:24:23 +0900 | [diff] [blame] | 109 | |
| 110 | protected String ksLocation; |
| 111 | protected String tsLocation; |
| 112 | protected char[] ksPwd; |
| 113 | protected char[] tsPwd; |
| 114 | |
Ray Milkey | aef4585 | 2016-01-11 17:13:19 -0800 | [diff] [blame] | 115 | @SuppressWarnings("squid:S1181") |
Ray Milkey | 86f20cc | 2015-12-09 16:54:09 -0800 | [diff] [blame] | 116 | // We really need to catch Throwable due to netty native epoll() handling |
Madan Jampani | 5e83f33 | 2014-10-20 15:35:09 -0700 | [diff] [blame] | 117 | private void initEventLoopGroup() { |
Madan Jampani | 824a7c1 | 2014-10-21 09:46:15 -0700 | [diff] [blame] | 118 | // try Epoll first and if that does work, use nio. |
Madan Jampani | 824a7c1 | 2014-10-21 09:46:15 -0700 | [diff] [blame] | 119 | try { |
Madan Jampani | 99e9fe2 | 2014-10-21 13:47:12 -0700 | [diff] [blame] | 120 | clientGroup = new EpollEventLoopGroup(); |
| 121 | serverGroup = new EpollEventLoopGroup(); |
| 122 | serverChannelClass = EpollServerSocketChannel.class; |
| 123 | clientChannelClass = EpollSocketChannel.class; |
| 124 | return; |
Thomas Vachuska | 2c2b768 | 2015-05-05 11:59:21 -0700 | [diff] [blame] | 125 | } catch (Throwable e) { |
Madan Jampani | afeebbd | 2015-05-19 15:26:01 -0700 | [diff] [blame] | 126 | log.debug("Failed to initialize native (epoll) transport. " |
| 127 | + "Reason: {}. Proceeding with nio.", e.getMessage()); |
Madan Jampani | 824a7c1 | 2014-10-21 09:46:15 -0700 | [diff] [blame] | 128 | } |
| 129 | clientGroup = new NioEventLoopGroup(); |
| 130 | serverGroup = new NioEventLoopGroup(); |
| 131 | serverChannelClass = NioServerSocketChannel.class; |
| 132 | clientChannelClass = NioSocketChannel.class; |
Madan Jampani | 5e83f33 | 2014-10-20 15:35:09 -0700 | [diff] [blame] | 133 | } |
| 134 | |
Aaron Kruglikov | eb0ae4e | 2015-11-10 19:16:16 -0800 | [diff] [blame] | 135 | public void start(int preamble, Endpoint localEp) throws Exception { |
Madan Jampani | afeebbd | 2015-05-19 15:26:01 -0700 | [diff] [blame] | 136 | if (started.get()) { |
| 137 | log.warn("Already running at local endpoint: {}", localEp); |
| 138 | return; |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 139 | } |
Aaron Kruglikov | eb0ae4e | 2015-11-10 19:16:16 -0800 | [diff] [blame] | 140 | this.preamble = preamble; |
Madan Jampani | afeebbd | 2015-05-19 15:26:01 -0700 | [diff] [blame] | 141 | this.localEp = localEp; |
Madan Jampani | cca4bdb | 2015-05-22 13:09:33 -0700 | [diff] [blame] | 142 | channels.setLifo(true); |
Madan Jampani | 86ed055 | 2014-10-03 16:45:42 -0700 | [diff] [blame] | 143 | channels.setTestOnBorrow(true); |
| 144 | channels.setTestOnReturn(true); |
Madan Jampani | cca4bdb | 2015-05-22 13:09:33 -0700 | [diff] [blame] | 145 | channels.setMinEvictableIdleTimeMillis(60_000L); |
| 146 | channels.setTimeBetweenEvictionRunsMillis(30_000L); |
Madan Jampani | 5e83f33 | 2014-10-20 15:35:09 -0700 | [diff] [blame] | 147 | initEventLoopGroup(); |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 148 | startAcceptingConnections(); |
Madan Jampani | afeebbd | 2015-05-19 15:26:01 -0700 | [diff] [blame] | 149 | started.set(true); |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 150 | } |
| 151 | |
Madan Jampani | afeebbd | 2015-05-19 15:26:01 -0700 | [diff] [blame] | 152 | public void stop() throws Exception { |
| 153 | if (started.get()) { |
| 154 | channels.close(); |
| 155 | serverGroup.shutdownGracefully(); |
| 156 | clientGroup.shutdownGracefully(); |
| 157 | started.set(false); |
| 158 | } |
Madan Jampani | 8710093 | 2014-10-21 16:46:12 -0700 | [diff] [blame] | 159 | } |
| 160 | |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 161 | @Override |
Madan Jampani | 175e8fd | 2015-05-20 14:10:45 -0700 | [diff] [blame] | 162 | public CompletableFuture<Void> sendAsync(Endpoint ep, String type, byte[] payload) { |
Madan Jampani | c26eede | 2015-04-16 11:42:16 -0700 | [diff] [blame] | 163 | InternalMessage message = new InternalMessage(messageIdGenerator.incrementAndGet(), |
| 164 | localEp, |
| 165 | type, |
| 166 | payload); |
Madan Jampani | 175e8fd | 2015-05-20 14:10:45 -0700 | [diff] [blame] | 167 | return sendAsync(ep, message); |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 168 | } |
| 169 | |
Madan Jampani | 175e8fd | 2015-05-20 14:10:45 -0700 | [diff] [blame] | 170 | protected CompletableFuture<Void> sendAsync(Endpoint ep, InternalMessage message) { |
Madan Jampani | d36def0 | 2016-01-13 11:21:56 -0800 | [diff] [blame] | 171 | if (ep.equals(localEp)) { |
| 172 | try { |
| 173 | dispatchLocally(message); |
| 174 | } catch (IOException e) { |
| 175 | return Tools.exceptionalFuture(e); |
| 176 | } |
| 177 | return CompletableFuture.completedFuture(null); |
| 178 | } |
| 179 | |
Madan Jampani | 175e8fd | 2015-05-20 14:10:45 -0700 | [diff] [blame] | 180 | CompletableFuture<Void> future = new CompletableFuture<>(); |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 181 | try { |
Aaron Kruglikov | 8a35219 | 2016-01-22 15:13:08 -0800 | [diff] [blame^] | 182 | Connection connection = null; |
Madan Jampani | d36def0 | 2016-01-13 11:21:56 -0800 | [diff] [blame] | 183 | try { |
Aaron Kruglikov | 8a35219 | 2016-01-22 15:13:08 -0800 | [diff] [blame^] | 184 | connection = channels.borrowObject(ep); |
| 185 | connection.send(message, future); |
| 186 | |
Madan Jampani | d36def0 | 2016-01-13 11:21:56 -0800 | [diff] [blame] | 187 | } finally { |
Aaron Kruglikov | 8a35219 | 2016-01-22 15:13:08 -0800 | [diff] [blame^] | 188 | channels.returnObject(ep, connection); |
Madan Jampani | 86ed055 | 2014-10-03 16:45:42 -0700 | [diff] [blame] | 189 | } |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 190 | } catch (Exception e) { |
Madan Jampani | 175e8fd | 2015-05-20 14:10:45 -0700 | [diff] [blame] | 191 | future.completeExceptionally(e); |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 192 | } |
Madan Jampani | 175e8fd | 2015-05-20 14:10:45 -0700 | [diff] [blame] | 193 | return future; |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | @Override |
Madan Jampani | 2bfa94c | 2015-04-11 05:03:49 -0700 | [diff] [blame] | 197 | public CompletableFuture<byte[]> sendAndReceive(Endpoint ep, String type, byte[] payload) { |
Madan Jampani | d36def0 | 2016-01-13 11:21:56 -0800 | [diff] [blame] | 198 | return sendAndReceive(ep, type, payload, MoreExecutors.directExecutor()); |
| 199 | } |
| 200 | |
| 201 | @Override |
| 202 | public CompletableFuture<byte[]> sendAndReceive(Endpoint ep, String type, byte[] payload, Executor executor) { |
Madan Jampani | 2bfa94c | 2015-04-11 05:03:49 -0700 | [diff] [blame] | 203 | CompletableFuture<byte[]> response = new CompletableFuture<>(); |
Madan Jampani | d36def0 | 2016-01-13 11:21:56 -0800 | [diff] [blame] | 204 | Callback callback = new Callback(response, executor); |
Madan Jampani | 24f9efb | 2014-10-24 18:56:23 -0700 | [diff] [blame] | 205 | Long messageId = messageIdGenerator.incrementAndGet(); |
Madan Jampani | d36def0 | 2016-01-13 11:21:56 -0800 | [diff] [blame] | 206 | callbacks.put(messageId, callback); |
Madan Jampani | c26eede | 2015-04-16 11:42:16 -0700 | [diff] [blame] | 207 | InternalMessage message = new InternalMessage(messageId, localEp, type, payload); |
Madan Jampani | d36def0 | 2016-01-13 11:21:56 -0800 | [diff] [blame] | 208 | return sendAsync(ep, message).whenComplete((r, e) -> { |
| 209 | if (e != null) { |
| 210 | callbacks.invalidate(messageId); |
| 211 | } |
| 212 | }).thenCompose(v -> response); |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | @Override |
Madan Jampani | d36def0 | 2016-01-13 11:21:56 -0800 | [diff] [blame] | 216 | public void registerHandler(String type, BiConsumer<Endpoint, byte[]> handler, Executor executor) { |
| 217 | handlers.put(type, message -> executor.execute(() -> handler.accept(message.sender(), message.payload()))); |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 218 | } |
| 219 | |
Yuta HIGUCHI | e5ca93b | 2014-10-23 09:49:00 -0700 | [diff] [blame] | 220 | @Override |
Madan Jampani | d36def0 | 2016-01-13 11:21:56 -0800 | [diff] [blame] | 221 | public void registerHandler(String type, BiFunction<Endpoint, byte[], byte[]> handler, Executor executor) { |
Madan Jampani | c26eede | 2015-04-16 11:42:16 -0700 | [diff] [blame] | 222 | handlers.put(type, message -> executor.execute(() -> { |
Madan Jampani | d36def0 | 2016-01-13 11:21:56 -0800 | [diff] [blame] | 223 | byte[] responsePayload = handler.apply(message.sender(), message.payload()); |
Madan Jampani | c26eede | 2015-04-16 11:42:16 -0700 | [diff] [blame] | 224 | if (responsePayload != null) { |
| 225 | InternalMessage response = new InternalMessage(message.id(), |
| 226 | localEp, |
| 227 | REPLY_MESSAGE_TYPE, |
| 228 | responsePayload); |
Madan Jampani | 175e8fd | 2015-05-20 14:10:45 -0700 | [diff] [blame] | 229 | sendAsync(message.sender(), response).whenComplete((result, error) -> { |
| 230 | if (error != null) { |
| 231 | log.debug("Failed to respond", error); |
| 232 | } |
| 233 | }); |
Madan Jampani | 2af244a | 2015-02-22 13:12:01 -0800 | [diff] [blame] | 234 | } |
Madan Jampani | c26eede | 2015-04-16 11:42:16 -0700 | [diff] [blame] | 235 | })); |
Madan Jampani | 2af244a | 2015-02-22 13:12:01 -0800 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | @Override |
Madan Jampani | d36def0 | 2016-01-13 11:21:56 -0800 | [diff] [blame] | 239 | public void registerHandler(String type, BiFunction<Endpoint, byte[], CompletableFuture<byte[]>> handler) { |
Madan Jampani | 27b69c6 | 2015-05-15 15:49:02 -0700 | [diff] [blame] | 240 | handlers.put(type, message -> { |
Madan Jampani | d36def0 | 2016-01-13 11:21:56 -0800 | [diff] [blame] | 241 | handler.apply(message.sender(), message.payload()).whenComplete((result, error) -> { |
Madan Jampani | 175e8fd | 2015-05-20 14:10:45 -0700 | [diff] [blame] | 242 | if (error == null) { |
| 243 | InternalMessage response = new InternalMessage(message.id(), |
JunHuy Lam | 39eb429 | 2015-06-26 17:24:23 +0900 | [diff] [blame] | 244 | localEp, |
| 245 | REPLY_MESSAGE_TYPE, |
| 246 | result); |
Madan Jampani | 175e8fd | 2015-05-20 14:10:45 -0700 | [diff] [blame] | 247 | sendAsync(message.sender(), response).whenComplete((r, e) -> { |
| 248 | if (e != null) { |
| 249 | log.debug("Failed to respond", e); |
| 250 | } |
| 251 | }); |
Madan Jampani | 27b69c6 | 2015-05-15 15:49:02 -0700 | [diff] [blame] | 252 | } |
Madan Jampani | 27b69c6 | 2015-05-15 15:49:02 -0700 | [diff] [blame] | 253 | }); |
| 254 | }); |
| 255 | } |
| 256 | |
| 257 | @Override |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 258 | public void unregisterHandler(String type) { |
Madan Jampani | 49115e9 | 2015-03-14 10:43:33 -0700 | [diff] [blame] | 259 | handlers.remove(type); |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 260 | } |
| 261 | |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 262 | private void startAcceptingConnections() throws InterruptedException { |
| 263 | ServerBootstrap b = new ServerBootstrap(); |
Madan Jampani | 86ed055 | 2014-10-03 16:45:42 -0700 | [diff] [blame] | 264 | b.option(ChannelOption.WRITE_BUFFER_HIGH_WATER_MARK, 32 * 1024); |
Madan Jampani | ddf7622 | 2014-10-04 23:48:44 -0700 | [diff] [blame] | 265 | b.option(ChannelOption.WRITE_BUFFER_LOW_WATER_MARK, 8 * 1024); |
Madan Jampani | c26eede | 2015-04-16 11:42:16 -0700 | [diff] [blame] | 266 | b.option(ChannelOption.SO_RCVBUF, 1048576); |
| 267 | b.option(ChannelOption.TCP_NODELAY, true); |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 268 | b.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT); |
JunHuy Lam | 39eb429 | 2015-06-26 17:24:23 +0900 | [diff] [blame] | 269 | b.group(serverGroup, clientGroup); |
| 270 | b.channel(serverChannelClass); |
Jonathan Hart | d9df7bd | 2015-11-10 17:10:25 -0800 | [diff] [blame] | 271 | if (enableNettyTls) { |
| 272 | b.childHandler(new SslServerCommunicationChannelInitializer()); |
JunHuy Lam | 39eb429 | 2015-06-26 17:24:23 +0900 | [diff] [blame] | 273 | } else { |
| 274 | b.childHandler(new OnosCommunicationChannelInitializer()); |
| 275 | } |
| 276 | b.option(ChannelOption.SO_BACKLOG, 128); |
| 277 | b.childOption(ChannelOption.SO_KEEPALIVE, true); |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 278 | |
| 279 | // Bind and start to accept incoming connections. |
Madan Jampani | afeebbd | 2015-05-19 15:26:01 -0700 | [diff] [blame] | 280 | b.bind(localEp.port()).sync().addListener(future -> { |
| 281 | if (future.isSuccess()) { |
| 282 | log.info("{} accepting incoming connections on port {}", localEp.host(), localEp.port()); |
| 283 | } else { |
| 284 | log.warn("{} failed to bind to port {}", localEp.host(), localEp.port(), future.cause()); |
| 285 | } |
| 286 | }); |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | private class OnosCommunicationChannelFactory |
Aaron Kruglikov | 8a35219 | 2016-01-22 15:13:08 -0800 | [diff] [blame^] | 290 | implements KeyedPoolableObjectFactory<Endpoint, Connection> { |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 291 | |
| 292 | @Override |
Aaron Kruglikov | 8a35219 | 2016-01-22 15:13:08 -0800 | [diff] [blame^] | 293 | public void activateObject(Endpoint endpoint, Connection connection) |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 294 | throws Exception { |
| 295 | } |
| 296 | |
| 297 | @Override |
Aaron Kruglikov | 8a35219 | 2016-01-22 15:13:08 -0800 | [diff] [blame^] | 298 | public void destroyObject(Endpoint ep, Connection connection) throws Exception { |
Madan Jampani | 8a0569e | 2015-05-26 12:06:00 -0700 | [diff] [blame] | 299 | log.debug("Closing connection to {}", ep); |
Aaron Kruglikov | 8a35219 | 2016-01-22 15:13:08 -0800 | [diff] [blame^] | 300 | //Is this the right way to destroy? |
| 301 | connection.destroy(); |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | @Override |
Aaron Kruglikov | 8a35219 | 2016-01-22 15:13:08 -0800 | [diff] [blame^] | 305 | public Connection makeObject(Endpoint ep) throws Exception { |
Madan Jampani | ddf7622 | 2014-10-04 23:48:44 -0700 | [diff] [blame] | 306 | Bootstrap bootstrap = new Bootstrap(); |
| 307 | bootstrap.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT); |
Madan Jampani | c26eede | 2015-04-16 11:42:16 -0700 | [diff] [blame] | 308 | bootstrap.option(ChannelOption.WRITE_BUFFER_HIGH_WATER_MARK, 10 * 64 * 1024); |
| 309 | bootstrap.option(ChannelOption.WRITE_BUFFER_LOW_WATER_MARK, 10 * 32 * 1024); |
| 310 | bootstrap.option(ChannelOption.SO_SNDBUF, 1048576); |
Aaron Kruglikov | 53b88a5 | 2016-01-19 11:13:23 -0800 | [diff] [blame] | 311 | bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 1000); |
Madan Jampani | 824a7c1 | 2014-10-21 09:46:15 -0700 | [diff] [blame] | 312 | bootstrap.group(clientGroup); |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 313 | // TODO: Make this faster: |
| 314 | // http://normanmaurer.me/presentations/2014-facebook-eng-netty/slides.html#37.0 |
Madan Jampani | 824a7c1 | 2014-10-21 09:46:15 -0700 | [diff] [blame] | 315 | bootstrap.channel(clientChannelClass); |
Madan Jampani | ddf7622 | 2014-10-04 23:48:44 -0700 | [diff] [blame] | 316 | bootstrap.option(ChannelOption.SO_KEEPALIVE, true); |
Jonathan Hart | d9df7bd | 2015-11-10 17:10:25 -0800 | [diff] [blame] | 317 | if (enableNettyTls) { |
| 318 | bootstrap.handler(new SslClientCommunicationChannelInitializer()); |
JunHuy Lam | 39eb429 | 2015-06-26 17:24:23 +0900 | [diff] [blame] | 319 | } else { |
| 320 | bootstrap.handler(new OnosCommunicationChannelInitializer()); |
| 321 | } |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 322 | // Start the client. |
Aaron Kruglikov | 8a35219 | 2016-01-22 15:13:08 -0800 | [diff] [blame^] | 323 | CompletableFuture<Channel> retFuture = new CompletableFuture<>(); |
| 324 | ChannelFuture f = bootstrap.connect(ep.host().toString(), ep.port()); |
| 325 | |
| 326 | f.addListener(future -> { |
| 327 | if (future.isSuccess()) { |
| 328 | retFuture.complete(f.channel()); |
| 329 | } else { |
| 330 | retFuture.completeExceptionally(future.cause()); |
| 331 | } |
| 332 | }); |
Madan Jampani | 8a0569e | 2015-05-26 12:06:00 -0700 | [diff] [blame] | 333 | log.debug("Established a new connection to {}", ep); |
Aaron Kruglikov | 8a35219 | 2016-01-22 15:13:08 -0800 | [diff] [blame^] | 334 | return new Connection(retFuture); |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | @Override |
Aaron Kruglikov | 8a35219 | 2016-01-22 15:13:08 -0800 | [diff] [blame^] | 338 | public void passivateObject(Endpoint ep, Connection connection) |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 339 | throws Exception { |
| 340 | } |
| 341 | |
| 342 | @Override |
Aaron Kruglikov | 8a35219 | 2016-01-22 15:13:08 -0800 | [diff] [blame^] | 343 | public boolean validateObject(Endpoint ep, Connection connection) { |
| 344 | return connection.validate(); |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 345 | } |
| 346 | } |
| 347 | |
Jonathan Hart | d9df7bd | 2015-11-10 17:10:25 -0800 | [diff] [blame] | 348 | private class SslServerCommunicationChannelInitializer extends ChannelInitializer<SocketChannel> { |
JunHuy Lam | 39eb429 | 2015-06-26 17:24:23 +0900 | [diff] [blame] | 349 | |
| 350 | private final ChannelHandler dispatcher = new InboundMessageDispatcher(); |
Aaron Kruglikov | eb0ae4e | 2015-11-10 19:16:16 -0800 | [diff] [blame] | 351 | private final ChannelHandler encoder = new MessageEncoder(preamble); |
JunHuy Lam | 39eb429 | 2015-06-26 17:24:23 +0900 | [diff] [blame] | 352 | |
| 353 | @Override |
| 354 | protected void initChannel(SocketChannel channel) throws Exception { |
| 355 | TrustManagerFactory tmFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); |
| 356 | KeyStore ts = KeyStore.getInstance("JKS"); |
| 357 | ts.load(new FileInputStream(tsLocation), tsPwd); |
| 358 | tmFactory.init(ts); |
| 359 | |
| 360 | KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()); |
| 361 | KeyStore ks = KeyStore.getInstance("JKS"); |
| 362 | ks.load(new FileInputStream(ksLocation), ksPwd); |
| 363 | kmf.init(ks, ksPwd); |
| 364 | |
| 365 | SSLContext serverContext = SSLContext.getInstance("TLS"); |
| 366 | serverContext.init(kmf.getKeyManagers(), tmFactory.getTrustManagers(), null); |
| 367 | |
Jonathan Hart | d9df7bd | 2015-11-10 17:10:25 -0800 | [diff] [blame] | 368 | SSLEngine serverSslEngine = serverContext.createSSLEngine(); |
JunHuy Lam | 39eb429 | 2015-06-26 17:24:23 +0900 | [diff] [blame] | 369 | |
Jonathan Hart | d9df7bd | 2015-11-10 17:10:25 -0800 | [diff] [blame] | 370 | serverSslEngine.setNeedClientAuth(true); |
| 371 | serverSslEngine.setUseClientMode(false); |
| 372 | serverSslEngine.setEnabledProtocols(serverSslEngine.getSupportedProtocols()); |
| 373 | serverSslEngine.setEnabledCipherSuites(serverSslEngine.getSupportedCipherSuites()); |
| 374 | serverSslEngine.setEnableSessionCreation(true); |
JunHuy Lam | 39eb429 | 2015-06-26 17:24:23 +0900 | [diff] [blame] | 375 | |
Jonathan Hart | d9df7bd | 2015-11-10 17:10:25 -0800 | [diff] [blame] | 376 | channel.pipeline().addLast("ssl", new io.netty.handler.ssl.SslHandler(serverSslEngine)) |
JunHuy Lam | 39eb429 | 2015-06-26 17:24:23 +0900 | [diff] [blame] | 377 | .addLast("encoder", encoder) |
Aaron Kruglikov | eb0ae4e | 2015-11-10 19:16:16 -0800 | [diff] [blame] | 378 | .addLast("decoder", new MessageDecoder(preamble)) |
JunHuy Lam | 39eb429 | 2015-06-26 17:24:23 +0900 | [diff] [blame] | 379 | .addLast("handler", dispatcher); |
| 380 | } |
| 381 | |
| 382 | } |
| 383 | |
Jonathan Hart | d9df7bd | 2015-11-10 17:10:25 -0800 | [diff] [blame] | 384 | private class SslClientCommunicationChannelInitializer extends ChannelInitializer<SocketChannel> { |
JunHuy Lam | 39eb429 | 2015-06-26 17:24:23 +0900 | [diff] [blame] | 385 | |
| 386 | private final ChannelHandler dispatcher = new InboundMessageDispatcher(); |
Aaron Kruglikov | eb0ae4e | 2015-11-10 19:16:16 -0800 | [diff] [blame] | 387 | private final ChannelHandler encoder = new MessageEncoder(preamble); |
JunHuy Lam | 39eb429 | 2015-06-26 17:24:23 +0900 | [diff] [blame] | 388 | |
| 389 | @Override |
| 390 | protected void initChannel(SocketChannel channel) throws Exception { |
| 391 | TrustManagerFactory tmFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); |
| 392 | KeyStore ts = KeyStore.getInstance("JKS"); |
| 393 | ts.load(new FileInputStream(tsLocation), tsPwd); |
| 394 | tmFactory.init(ts); |
| 395 | |
| 396 | KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()); |
| 397 | KeyStore ks = KeyStore.getInstance("JKS"); |
| 398 | ks.load(new FileInputStream(ksLocation), ksPwd); |
| 399 | kmf.init(ks, ksPwd); |
| 400 | |
| 401 | SSLContext clientContext = SSLContext.getInstance("TLS"); |
| 402 | clientContext.init(kmf.getKeyManagers(), tmFactory.getTrustManagers(), null); |
| 403 | |
Jonathan Hart | d9df7bd | 2015-11-10 17:10:25 -0800 | [diff] [blame] | 404 | SSLEngine clientSslEngine = clientContext.createSSLEngine(); |
JunHuy Lam | 39eb429 | 2015-06-26 17:24:23 +0900 | [diff] [blame] | 405 | |
Jonathan Hart | d9df7bd | 2015-11-10 17:10:25 -0800 | [diff] [blame] | 406 | clientSslEngine.setUseClientMode(true); |
| 407 | clientSslEngine.setEnabledProtocols(clientSslEngine.getSupportedProtocols()); |
| 408 | clientSslEngine.setEnabledCipherSuites(clientSslEngine.getSupportedCipherSuites()); |
| 409 | clientSslEngine.setEnableSessionCreation(true); |
JunHuy Lam | 39eb429 | 2015-06-26 17:24:23 +0900 | [diff] [blame] | 410 | |
Jonathan Hart | d9df7bd | 2015-11-10 17:10:25 -0800 | [diff] [blame] | 411 | channel.pipeline().addLast("ssl", new io.netty.handler.ssl.SslHandler(clientSslEngine)) |
JunHuy Lam | 39eb429 | 2015-06-26 17:24:23 +0900 | [diff] [blame] | 412 | .addLast("encoder", encoder) |
Aaron Kruglikov | eb0ae4e | 2015-11-10 19:16:16 -0800 | [diff] [blame] | 413 | .addLast("decoder", new MessageDecoder(preamble)) |
JunHuy Lam | 39eb429 | 2015-06-26 17:24:23 +0900 | [diff] [blame] | 414 | .addLast("handler", dispatcher); |
| 415 | } |
| 416 | |
| 417 | } |
| 418 | |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 419 | private class OnosCommunicationChannelInitializer extends ChannelInitializer<SocketChannel> { |
| 420 | |
Madan Jampani | ddf7622 | 2014-10-04 23:48:44 -0700 | [diff] [blame] | 421 | private final ChannelHandler dispatcher = new InboundMessageDispatcher(); |
Aaron Kruglikov | eb0ae4e | 2015-11-10 19:16:16 -0800 | [diff] [blame] | 422 | private final ChannelHandler encoder = new MessageEncoder(preamble); |
Madan Jampani | ddf7622 | 2014-10-04 23:48:44 -0700 | [diff] [blame] | 423 | |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 424 | @Override |
| 425 | protected void initChannel(SocketChannel channel) throws Exception { |
JunHuy Lam | 39eb429 | 2015-06-26 17:24:23 +0900 | [diff] [blame] | 426 | channel.pipeline() |
| 427 | .addLast("encoder", encoder) |
Aaron Kruglikov | eb0ae4e | 2015-11-10 19:16:16 -0800 | [diff] [blame] | 428 | .addLast("decoder", new MessageDecoder(preamble)) |
JunHuy Lam | 39eb429 | 2015-06-26 17:24:23 +0900 | [diff] [blame] | 429 | .addLast("handler", dispatcher); |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 430 | } |
| 431 | } |
| 432 | |
Madan Jampani | ddf7622 | 2014-10-04 23:48:44 -0700 | [diff] [blame] | 433 | @ChannelHandler.Sharable |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 434 | private class InboundMessageDispatcher extends SimpleChannelInboundHandler<InternalMessage> { |
| 435 | |
| 436 | @Override |
| 437 | protected void channelRead0(ChannelHandlerContext ctx, InternalMessage message) throws Exception { |
Thomas Vachuska | fc52fec | 2015-05-18 19:13:56 -0700 | [diff] [blame] | 438 | try { |
| 439 | dispatchLocally(message); |
| 440 | } catch (RejectedExecutionException e) { |
| 441 | log.warn("Unable to dispatch message due to {}", e.getMessage()); |
| 442 | } |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 443 | } |
Madan Jampani | 86ed055 | 2014-10-03 16:45:42 -0700 | [diff] [blame] | 444 | |
Madan Jampani | 86ed055 | 2014-10-03 16:45:42 -0700 | [diff] [blame] | 445 | @Override |
| 446 | public void exceptionCaught(ChannelHandlerContext context, Throwable cause) { |
Madan Jampani | 29e5dfd | 2014-10-07 17:26:25 -0700 | [diff] [blame] | 447 | log.error("Exception inside channel handling pipeline.", cause); |
Madan Jampani | 86ed055 | 2014-10-03 16:45:42 -0700 | [diff] [blame] | 448 | context.close(); |
| 449 | } |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 450 | } |
Madan Jampani | ba47223 | 2015-03-04 13:00:50 -0800 | [diff] [blame] | 451 | private void dispatchLocally(InternalMessage message) throws IOException { |
Madan Jampani | 49115e9 | 2015-03-14 10:43:33 -0700 | [diff] [blame] | 452 | String type = message.type(); |
Madan Jampani | c26eede | 2015-04-16 11:42:16 -0700 | [diff] [blame] | 453 | if (REPLY_MESSAGE_TYPE.equals(type)) { |
Madan Jampani | ba47223 | 2015-03-04 13:00:50 -0800 | [diff] [blame] | 454 | try { |
Madan Jampani | d36def0 | 2016-01-13 11:21:56 -0800 | [diff] [blame] | 455 | Callback callback = |
| 456 | callbacks.getIfPresent(message.id()); |
| 457 | if (callback != null) { |
| 458 | callback.complete(message.payload()); |
Madan Jampani | ba47223 | 2015-03-04 13:00:50 -0800 | [diff] [blame] | 459 | } else { |
| 460 | log.warn("Received a reply for message id:[{}]. " |
| 461 | + " from {}. But was unable to locate the" |
| 462 | + " request handle", message.id(), message.sender()); |
| 463 | } |
| 464 | } finally { |
Madan Jampani | d36def0 | 2016-01-13 11:21:56 -0800 | [diff] [blame] | 465 | callbacks.invalidate(message.id()); |
Madan Jampani | ba47223 | 2015-03-04 13:00:50 -0800 | [diff] [blame] | 466 | } |
| 467 | return; |
| 468 | } |
Madan Jampani | c26eede | 2015-04-16 11:42:16 -0700 | [diff] [blame] | 469 | Consumer<InternalMessage> handler = handlers.get(type); |
Madan Jampani | ba47223 | 2015-03-04 13:00:50 -0800 | [diff] [blame] | 470 | if (handler != null) { |
Madan Jampani | c26eede | 2015-04-16 11:42:16 -0700 | [diff] [blame] | 471 | handler.accept(message); |
Madan Jampani | ba47223 | 2015-03-04 13:00:50 -0800 | [diff] [blame] | 472 | } else { |
| 473 | log.debug("No handler registered for {}", type); |
| 474 | } |
| 475 | } |
Madan Jampani | d36def0 | 2016-01-13 11:21:56 -0800 | [diff] [blame] | 476 | |
| 477 | private final class Callback { |
| 478 | private final CompletableFuture<byte[]> future; |
| 479 | private final Executor executor; |
| 480 | |
| 481 | public Callback(CompletableFuture<byte[]> future, Executor executor) { |
| 482 | this.future = future; |
| 483 | this.executor = executor; |
| 484 | } |
| 485 | |
| 486 | public void complete(byte[] value) { |
| 487 | executor.execute(() -> future.complete(value)); |
| 488 | } |
| 489 | |
| 490 | public void completeExceptionally(Throwable error) { |
| 491 | executor.execute(() -> future.completeExceptionally(error)); |
| 492 | } |
| 493 | } |
Aaron Kruglikov | 8a35219 | 2016-01-22 15:13:08 -0800 | [diff] [blame^] | 494 | private final class Connection { |
| 495 | private final CompletableFuture<Channel> internalFuture; |
| 496 | |
| 497 | public Connection(CompletableFuture<Channel> internalFuture) { |
| 498 | this.internalFuture = internalFuture; |
| 499 | } |
| 500 | |
| 501 | /** |
| 502 | * Sends a message out on its channel and associated the message with a |
| 503 | * completable future used for signaling. |
| 504 | * @param message the message to be sent |
| 505 | * @param future a future that is completed normally or exceptionally if |
| 506 | * message sending succeeds or fails respectively |
| 507 | */ |
| 508 | public void send(Object message, CompletableFuture<Void> future) { |
| 509 | internalFuture.whenComplete((channel, throwable) -> { |
| 510 | if (throwable == null) { |
| 511 | channel.writeAndFlush(message).addListener(channelFuture -> { |
| 512 | if (!channelFuture.isSuccess()) { |
| 513 | future.completeExceptionally(channelFuture.cause()); |
| 514 | } else { |
| 515 | future.complete(null); |
| 516 | } |
| 517 | }); |
| 518 | } else { |
| 519 | future.completeExceptionally(throwable); |
| 520 | } |
| 521 | |
| 522 | }); |
| 523 | } |
| 524 | |
| 525 | /** |
| 526 | * Destroys a channel by closing its channel (if it exists) and |
| 527 | * cancelling its future. |
| 528 | */ |
| 529 | public void destroy() { |
| 530 | Channel channel = internalFuture.getNow(null); |
| 531 | if (channel != null) { |
| 532 | channel.close(); |
| 533 | } |
| 534 | internalFuture.cancel(false); |
| 535 | } |
| 536 | |
| 537 | /** |
| 538 | * Determines whether the connection is valid meaning it is either |
| 539 | * complete with and active channel |
| 540 | * or it has not yet completed. |
| 541 | * @return true if the channel has an active connection or has not |
| 542 | * yet completed |
| 543 | */ |
| 544 | public boolean validate() { |
| 545 | if (internalFuture.isCompletedExceptionally()) { |
| 546 | return false; |
| 547 | } |
| 548 | Channel channel = internalFuture.getNow(null); |
| 549 | return channel == null || channel.isActive(); |
| 550 | } |
| 551 | } |
Ray Milkey | 34c9590 | 2015-04-15 09:47:53 -0700 | [diff] [blame] | 552 | } |