Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 19 | package org.onlab.netty; |
| 20 | |
| 21 | import java.io.IOException; |
| 22 | import java.net.UnknownHostException; |
| 23 | import java.util.concurrent.ConcurrentHashMap; |
| 24 | import java.util.concurrent.ConcurrentMap; |
| 25 | import java.util.concurrent.TimeUnit; |
Madan Jampani | 5f9ec9a | 2014-10-29 13:45:52 -0700 | [diff] [blame^] | 26 | import java.util.concurrent.TimeoutException; |
Madan Jampani | 24f9efb | 2014-10-24 18:56:23 -0700 | [diff] [blame] | 27 | import java.util.concurrent.atomic.AtomicLong; |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 28 | |
| 29 | import io.netty.bootstrap.Bootstrap; |
| 30 | import io.netty.bootstrap.ServerBootstrap; |
| 31 | import io.netty.buffer.PooledByteBufAllocator; |
| 32 | import io.netty.channel.Channel; |
| 33 | import io.netty.channel.ChannelFuture; |
Madan Jampani | 5f9ec9a | 2014-10-29 13:45:52 -0700 | [diff] [blame^] | 34 | import io.netty.channel.ChannelFutureListener; |
Madan Jampani | ddf7622 | 2014-10-04 23:48:44 -0700 | [diff] [blame] | 35 | import io.netty.channel.ChannelHandler; |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 36 | import io.netty.channel.ChannelHandlerContext; |
| 37 | import io.netty.channel.ChannelInitializer; |
| 38 | import io.netty.channel.ChannelOption; |
| 39 | import io.netty.channel.EventLoopGroup; |
Madan Jampani | 824a7c1 | 2014-10-21 09:46:15 -0700 | [diff] [blame] | 40 | import io.netty.channel.ServerChannel; |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 41 | import io.netty.channel.SimpleChannelInboundHandler; |
Madan Jampani | 824a7c1 | 2014-10-21 09:46:15 -0700 | [diff] [blame] | 42 | import io.netty.channel.epoll.EpollEventLoopGroup; |
| 43 | import io.netty.channel.epoll.EpollServerSocketChannel; |
| 44 | import io.netty.channel.epoll.EpollSocketChannel; |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 45 | import io.netty.channel.nio.NioEventLoopGroup; |
| 46 | import io.netty.channel.socket.SocketChannel; |
| 47 | import io.netty.channel.socket.nio.NioServerSocketChannel; |
| 48 | import io.netty.channel.socket.nio.NioSocketChannel; |
| 49 | |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 50 | import org.apache.commons.pool.KeyedPoolableObjectFactory; |
| 51 | import org.apache.commons.pool.impl.GenericKeyedObjectPool; |
| 52 | import org.slf4j.Logger; |
| 53 | import org.slf4j.LoggerFactory; |
| 54 | |
| 55 | import com.google.common.cache.Cache; |
| 56 | import com.google.common.cache.CacheBuilder; |
Madan Jampani | 5f9ec9a | 2014-10-29 13:45:52 -0700 | [diff] [blame^] | 57 | import com.google.common.cache.RemovalListener; |
| 58 | import com.google.common.cache.RemovalNotification; |
Madan Jampani | 24f9efb | 2014-10-24 18:56:23 -0700 | [diff] [blame] | 59 | import com.google.common.util.concurrent.ListenableFuture; |
| 60 | import com.google.common.util.concurrent.SettableFuture; |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 61 | |
| 62 | /** |
| 63 | * A Netty based implementation of MessagingService. |
| 64 | */ |
| 65 | public class NettyMessagingService implements MessagingService { |
| 66 | |
| 67 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 68 | |
Madan Jampani | ddf7622 | 2014-10-04 23:48:44 -0700 | [diff] [blame] | 69 | private final Endpoint localEp; |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 70 | private final ConcurrentMap<String, MessageHandler> handlers = new ConcurrentHashMap<>(); |
Madan Jampani | 24f9efb | 2014-10-24 18:56:23 -0700 | [diff] [blame] | 71 | private final AtomicLong messageIdGenerator = new AtomicLong(0); |
| 72 | private final Cache<Long, SettableFuture<byte[]>> responseFutures = CacheBuilder.newBuilder() |
Madan Jampani | ddf7622 | 2014-10-04 23:48:44 -0700 | [diff] [blame] | 73 | .maximumSize(100000) |
Madan Jampani | 5f9ec9a | 2014-10-29 13:45:52 -0700 | [diff] [blame^] | 74 | .expireAfterWrite(10, TimeUnit.SECONDS) |
| 75 | .removalListener(new RemovalListener<Long, SettableFuture<byte[]>>() { |
| 76 | @Override |
| 77 | public void onRemoval(RemovalNotification<Long, SettableFuture<byte[]>> entry) { |
| 78 | entry.getValue().setException(new TimeoutException("Timedout waiting for reply")); |
| 79 | } |
| 80 | }) |
Madan Jampani | ddf7622 | 2014-10-04 23:48:44 -0700 | [diff] [blame] | 81 | .build(); |
| 82 | private final GenericKeyedObjectPool<Endpoint, Channel> channels |
| 83 | = new GenericKeyedObjectPool<Endpoint, Channel>(new OnosCommunicationChannelFactory()); |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 84 | |
Madan Jampani | 824a7c1 | 2014-10-21 09:46:15 -0700 | [diff] [blame] | 85 | private EventLoopGroup serverGroup; |
| 86 | private EventLoopGroup clientGroup; |
| 87 | private Class<? extends ServerChannel> serverChannelClass; |
| 88 | private Class<? extends Channel> clientChannelClass; |
| 89 | |
Madan Jampani | 5e83f33 | 2014-10-20 15:35:09 -0700 | [diff] [blame] | 90 | private void initEventLoopGroup() { |
Madan Jampani | 824a7c1 | 2014-10-21 09:46:15 -0700 | [diff] [blame] | 91 | // try Epoll first and if that does work, use nio. |
| 92 | // TODO: make this configurable. |
| 93 | try { |
Madan Jampani | 99e9fe2 | 2014-10-21 13:47:12 -0700 | [diff] [blame] | 94 | clientGroup = new EpollEventLoopGroup(); |
| 95 | serverGroup = new EpollEventLoopGroup(); |
| 96 | serverChannelClass = EpollServerSocketChannel.class; |
| 97 | clientChannelClass = EpollSocketChannel.class; |
| 98 | return; |
Madan Jampani | 824a7c1 | 2014-10-21 09:46:15 -0700 | [diff] [blame] | 99 | } catch (Throwable t) { |
Madan Jampani | cfbc054 | 2014-10-24 20:38:07 -0700 | [diff] [blame] | 100 | log.warn("Failed to initialize native (epoll) transport. Reason: {}. Proceeding with nio.", t.getMessage()); |
Madan Jampani | 824a7c1 | 2014-10-21 09:46:15 -0700 | [diff] [blame] | 101 | } |
| 102 | clientGroup = new NioEventLoopGroup(); |
| 103 | serverGroup = new NioEventLoopGroup(); |
| 104 | serverChannelClass = NioServerSocketChannel.class; |
| 105 | clientChannelClass = NioSocketChannel.class; |
Madan Jampani | 5e83f33 | 2014-10-20 15:35:09 -0700 | [diff] [blame] | 106 | } |
| 107 | |
Madan Jampani | 8710093 | 2014-10-21 16:46:12 -0700 | [diff] [blame] | 108 | public NettyMessagingService(String ip, int port) { |
| 109 | localEp = new Endpoint(ip, port); |
| 110 | } |
| 111 | |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 112 | public NettyMessagingService() { |
| 113 | // TODO: Default port should be configurable. |
| 114 | this(8080); |
| 115 | } |
| 116 | |
| 117 | // FIXME: Constructor should not throw exceptions. |
| 118 | public NettyMessagingService(int port) { |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 119 | try { |
| 120 | localEp = new Endpoint(java.net.InetAddress.getLocalHost().getHostName(), port); |
| 121 | } catch (UnknownHostException e) { |
| 122 | // bailing out. |
| 123 | throw new RuntimeException(e); |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | public void activate() throws Exception { |
Madan Jampani | 86ed055 | 2014-10-03 16:45:42 -0700 | [diff] [blame] | 128 | channels.setTestOnBorrow(true); |
| 129 | channels.setTestOnReturn(true); |
Madan Jampani | 5e83f33 | 2014-10-20 15:35:09 -0700 | [diff] [blame] | 130 | initEventLoopGroup(); |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 131 | startAcceptingConnections(); |
| 132 | } |
| 133 | |
| 134 | public void deactivate() throws Exception { |
| 135 | channels.close(); |
Madan Jampani | 824a7c1 | 2014-10-21 09:46:15 -0700 | [diff] [blame] | 136 | serverGroup.shutdownGracefully(); |
| 137 | clientGroup.shutdownGracefully(); |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 138 | } |
| 139 | |
Madan Jampani | 8710093 | 2014-10-21 16:46:12 -0700 | [diff] [blame] | 140 | /** |
| 141 | * Returns the local endpoint for this instance. |
| 142 | * @return local end point. |
| 143 | */ |
| 144 | public Endpoint localEp() { |
| 145 | return localEp; |
| 146 | } |
| 147 | |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 148 | @Override |
Madan Jampani | 53e44e6 | 2014-10-07 12:39:51 -0700 | [diff] [blame] | 149 | public void sendAsync(Endpoint ep, String type, byte[] payload) throws IOException { |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 150 | InternalMessage message = new InternalMessage.Builder(this) |
Madan Jampani | 24f9efb | 2014-10-24 18:56:23 -0700 | [diff] [blame] | 151 | .withId(messageIdGenerator.incrementAndGet()) |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 152 | .withSender(localEp) |
| 153 | .withType(type) |
| 154 | .withPayload(payload) |
| 155 | .build(); |
| 156 | sendAsync(ep, message); |
| 157 | } |
| 158 | |
| 159 | protected void sendAsync(Endpoint ep, InternalMessage message) throws IOException { |
| 160 | Channel channel = null; |
| 161 | try { |
Madan Jampani | 86ed055 | 2014-10-03 16:45:42 -0700 | [diff] [blame] | 162 | try { |
| 163 | channel = channels.borrowObject(ep); |
| 164 | channel.eventLoop().execute(new WriteTask(channel, message)); |
| 165 | } finally { |
| 166 | channels.returnObject(ep, channel); |
| 167 | } |
Madan Jampani | 5f9ec9a | 2014-10-29 13:45:52 -0700 | [diff] [blame^] | 168 | } catch (IOException e) { |
| 169 | throw e; |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 170 | } catch (Exception e) { |
Madan Jampani | 5f9ec9a | 2014-10-29 13:45:52 -0700 | [diff] [blame^] | 171 | throw new IOException(e); |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 172 | } |
Madan Jampani | 5f9ec9a | 2014-10-29 13:45:52 -0700 | [diff] [blame^] | 173 | |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | @Override |
Madan Jampani | 24f9efb | 2014-10-24 18:56:23 -0700 | [diff] [blame] | 177 | public ListenableFuture<byte[]> sendAndReceive(Endpoint ep, String type, byte[] payload) |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 178 | throws IOException { |
Madan Jampani | 24f9efb | 2014-10-24 18:56:23 -0700 | [diff] [blame] | 179 | SettableFuture<byte[]> futureResponse = SettableFuture.create(); |
| 180 | Long messageId = messageIdGenerator.incrementAndGet(); |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 181 | responseFutures.put(messageId, futureResponse); |
| 182 | InternalMessage message = new InternalMessage.Builder(this) |
| 183 | .withId(messageId) |
| 184 | .withSender(localEp) |
| 185 | .withType(type) |
| 186 | .withPayload(payload) |
| 187 | .build(); |
Madan Jampani | 15cd0b8 | 2014-10-28 08:40:23 -0700 | [diff] [blame] | 188 | try { |
| 189 | sendAsync(ep, message); |
Madan Jampani | 5f9ec9a | 2014-10-29 13:45:52 -0700 | [diff] [blame^] | 190 | } catch (Exception e) { |
Madan Jampani | 15cd0b8 | 2014-10-28 08:40:23 -0700 | [diff] [blame] | 191 | responseFutures.invalidate(messageId); |
| 192 | throw e; |
| 193 | } |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 194 | return futureResponse; |
| 195 | } |
| 196 | |
| 197 | @Override |
| 198 | public void registerHandler(String type, MessageHandler handler) { |
| 199 | // TODO: Is this the right semantics for handler registration? |
| 200 | handlers.putIfAbsent(type, handler); |
| 201 | } |
| 202 | |
Yuta HIGUCHI | e5ca93b | 2014-10-23 09:49:00 -0700 | [diff] [blame] | 203 | @Override |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 204 | public void unregisterHandler(String type) { |
| 205 | handlers.remove(type); |
| 206 | } |
| 207 | |
| 208 | private MessageHandler getMessageHandler(String type) { |
| 209 | return handlers.get(type); |
| 210 | } |
| 211 | |
| 212 | private void startAcceptingConnections() throws InterruptedException { |
| 213 | ServerBootstrap b = new ServerBootstrap(); |
Madan Jampani | 86ed055 | 2014-10-03 16:45:42 -0700 | [diff] [blame] | 214 | b.option(ChannelOption.WRITE_BUFFER_HIGH_WATER_MARK, 32 * 1024); |
Madan Jampani | ddf7622 | 2014-10-04 23:48:44 -0700 | [diff] [blame] | 215 | b.option(ChannelOption.WRITE_BUFFER_LOW_WATER_MARK, 8 * 1024); |
| 216 | // TODO: Need JVM options to configure PooledByteBufAllocator. |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 217 | b.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT); |
Madan Jampani | 824a7c1 | 2014-10-21 09:46:15 -0700 | [diff] [blame] | 218 | b.group(serverGroup, clientGroup) |
| 219 | .channel(serverChannelClass) |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 220 | .childHandler(new OnosCommunicationChannelInitializer()) |
| 221 | .option(ChannelOption.SO_BACKLOG, 128) |
| 222 | .childOption(ChannelOption.SO_KEEPALIVE, true); |
| 223 | |
| 224 | // Bind and start to accept incoming connections. |
Madan Jampani | 8710093 | 2014-10-21 16:46:12 -0700 | [diff] [blame] | 225 | b.bind(localEp.port()).sync(); |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | private class OnosCommunicationChannelFactory |
| 229 | implements KeyedPoolableObjectFactory<Endpoint, Channel> { |
| 230 | |
| 231 | @Override |
| 232 | public void activateObject(Endpoint endpoint, Channel channel) |
| 233 | throws Exception { |
| 234 | } |
| 235 | |
| 236 | @Override |
| 237 | public void destroyObject(Endpoint ep, Channel channel) throws Exception { |
| 238 | channel.close(); |
| 239 | } |
| 240 | |
| 241 | @Override |
| 242 | public Channel makeObject(Endpoint ep) throws Exception { |
Madan Jampani | ddf7622 | 2014-10-04 23:48:44 -0700 | [diff] [blame] | 243 | Bootstrap bootstrap = new Bootstrap(); |
| 244 | bootstrap.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT); |
| 245 | bootstrap.option(ChannelOption.WRITE_BUFFER_HIGH_WATER_MARK, 32 * 1024); |
| 246 | bootstrap.option(ChannelOption.WRITE_BUFFER_LOW_WATER_MARK, 8 * 1024); |
Madan Jampani | 824a7c1 | 2014-10-21 09:46:15 -0700 | [diff] [blame] | 247 | bootstrap.group(clientGroup); |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 248 | // TODO: Make this faster: |
| 249 | // http://normanmaurer.me/presentations/2014-facebook-eng-netty/slides.html#37.0 |
Madan Jampani | 824a7c1 | 2014-10-21 09:46:15 -0700 | [diff] [blame] | 250 | bootstrap.channel(clientChannelClass); |
Madan Jampani | ddf7622 | 2014-10-04 23:48:44 -0700 | [diff] [blame] | 251 | bootstrap.option(ChannelOption.SO_KEEPALIVE, true); |
| 252 | bootstrap.handler(new OnosCommunicationChannelInitializer()); |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 253 | // Start the client. |
Madan Jampani | ddf7622 | 2014-10-04 23:48:44 -0700 | [diff] [blame] | 254 | ChannelFuture f = bootstrap.connect(ep.host(), ep.port()).sync(); |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 255 | return f.channel(); |
| 256 | } |
| 257 | |
| 258 | @Override |
| 259 | public void passivateObject(Endpoint ep, Channel channel) |
| 260 | throws Exception { |
| 261 | } |
| 262 | |
| 263 | @Override |
| 264 | public boolean validateObject(Endpoint ep, Channel channel) { |
| 265 | return channel.isOpen(); |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | private class OnosCommunicationChannelInitializer extends ChannelInitializer<SocketChannel> { |
| 270 | |
Madan Jampani | ddf7622 | 2014-10-04 23:48:44 -0700 | [diff] [blame] | 271 | private final ChannelHandler dispatcher = new InboundMessageDispatcher(); |
Madan Jampani | 53e44e6 | 2014-10-07 12:39:51 -0700 | [diff] [blame] | 272 | private final ChannelHandler encoder = new MessageEncoder(); |
Madan Jampani | ddf7622 | 2014-10-04 23:48:44 -0700 | [diff] [blame] | 273 | |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 274 | @Override |
| 275 | protected void initChannel(SocketChannel channel) throws Exception { |
| 276 | channel.pipeline() |
Madan Jampani | ddf7622 | 2014-10-04 23:48:44 -0700 | [diff] [blame] | 277 | .addLast("encoder", encoder) |
Madan Jampani | 53e44e6 | 2014-10-07 12:39:51 -0700 | [diff] [blame] | 278 | .addLast("decoder", new MessageDecoder(NettyMessagingService.this)) |
Madan Jampani | ddf7622 | 2014-10-04 23:48:44 -0700 | [diff] [blame] | 279 | .addLast("handler", dispatcher); |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 280 | } |
| 281 | } |
| 282 | |
Yuta HIGUCHI | e5ca93b | 2014-10-23 09:49:00 -0700 | [diff] [blame] | 283 | private static class WriteTask implements Runnable { |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 284 | |
Madan Jampani | 86ed055 | 2014-10-03 16:45:42 -0700 | [diff] [blame] | 285 | private final InternalMessage message; |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 286 | private final Channel channel; |
| 287 | |
Madan Jampani | 86ed055 | 2014-10-03 16:45:42 -0700 | [diff] [blame] | 288 | public WriteTask(Channel channel, InternalMessage message) { |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 289 | this.channel = channel; |
Madan Jampani | 86ed055 | 2014-10-03 16:45:42 -0700 | [diff] [blame] | 290 | this.message = message; |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | @Override |
| 294 | public void run() { |
Madan Jampani | 5f9ec9a | 2014-10-29 13:45:52 -0700 | [diff] [blame^] | 295 | channel.writeAndFlush(message).addListener(ChannelFutureListener.CLOSE_ON_FAILURE); |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 296 | } |
| 297 | } |
| 298 | |
Madan Jampani | ddf7622 | 2014-10-04 23:48:44 -0700 | [diff] [blame] | 299 | @ChannelHandler.Sharable |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 300 | private class InboundMessageDispatcher extends SimpleChannelInboundHandler<InternalMessage> { |
| 301 | |
| 302 | @Override |
| 303 | protected void channelRead0(ChannelHandlerContext ctx, InternalMessage message) throws Exception { |
| 304 | String type = message.type(); |
| 305 | if (type.equals(InternalMessage.REPLY_MESSAGE_TYPE)) { |
| 306 | try { |
Madan Jampani | 24f9efb | 2014-10-24 18:56:23 -0700 | [diff] [blame] | 307 | SettableFuture<byte[]> futureResponse = |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 308 | NettyMessagingService.this.responseFutures.getIfPresent(message.id()); |
| 309 | if (futureResponse != null) { |
Madan Jampani | 24f9efb | 2014-10-24 18:56:23 -0700 | [diff] [blame] | 310 | futureResponse.set(message.payload()); |
Madan Jampani | f1d425a | 2014-10-07 09:52:36 -0700 | [diff] [blame] | 311 | } else { |
Madan Jampani | 15cd0b8 | 2014-10-28 08:40:23 -0700 | [diff] [blame] | 312 | log.warn("Received a reply for message id:[{}]. " |
| 313 | + "But was unable to locate the request handle", message.id()); |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 314 | } |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 315 | } finally { |
| 316 | NettyMessagingService.this.responseFutures.invalidate(message.id()); |
| 317 | } |
| 318 | return; |
| 319 | } |
| 320 | MessageHandler handler = NettyMessagingService.this.getMessageHandler(type); |
| 321 | handler.handle(message); |
| 322 | } |
Madan Jampani | 86ed055 | 2014-10-03 16:45:42 -0700 | [diff] [blame] | 323 | |
Madan Jampani | 86ed055 | 2014-10-03 16:45:42 -0700 | [diff] [blame] | 324 | @Override |
| 325 | public void exceptionCaught(ChannelHandlerContext context, Throwable cause) { |
Madan Jampani | 29e5dfd | 2014-10-07 17:26:25 -0700 | [diff] [blame] | 326 | log.error("Exception inside channel handling pipeline.", cause); |
Madan Jampani | 86ed055 | 2014-10-03 16:45:42 -0700 | [diff] [blame] | 327 | context.close(); |
| 328 | } |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 329 | } |
| 330 | } |