Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 1 | /* |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 2 | * Copyright 2014 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 | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 18 | import io.netty.bootstrap.Bootstrap; |
| 19 | import io.netty.bootstrap.ServerBootstrap; |
| 20 | import io.netty.buffer.PooledByteBufAllocator; |
| 21 | import io.netty.channel.Channel; |
| 22 | import io.netty.channel.ChannelFuture; |
Madan Jampani | 5f9ec9a | 2014-10-29 13:45:52 -0700 | [diff] [blame] | 23 | import io.netty.channel.ChannelFutureListener; |
Madan Jampani | ddf7622 | 2014-10-04 23:48:44 -0700 | [diff] [blame] | 24 | import io.netty.channel.ChannelHandler; |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 25 | import io.netty.channel.ChannelHandlerContext; |
| 26 | import io.netty.channel.ChannelInitializer; |
| 27 | import io.netty.channel.ChannelOption; |
| 28 | import io.netty.channel.EventLoopGroup; |
Madan Jampani | 824a7c1 | 2014-10-21 09:46:15 -0700 | [diff] [blame] | 29 | import io.netty.channel.ServerChannel; |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 30 | import io.netty.channel.SimpleChannelInboundHandler; |
Madan Jampani | 824a7c1 | 2014-10-21 09:46:15 -0700 | [diff] [blame] | 31 | import io.netty.channel.epoll.EpollEventLoopGroup; |
| 32 | import io.netty.channel.epoll.EpollServerSocketChannel; |
| 33 | import io.netty.channel.epoll.EpollSocketChannel; |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 34 | import io.netty.channel.nio.NioEventLoopGroup; |
| 35 | import io.netty.channel.socket.SocketChannel; |
| 36 | import io.netty.channel.socket.nio.NioServerSocketChannel; |
| 37 | import io.netty.channel.socket.nio.NioSocketChannel; |
| 38 | |
Madan Jampani | 348a9fe | 2014-11-09 01:37:51 -0800 | [diff] [blame] | 39 | import java.io.IOException; |
Madan Jampani | 2e5f87b | 2015-02-22 10:37:15 -0800 | [diff] [blame] | 40 | import java.net.InetAddress; |
Madan Jampani | 348a9fe | 2014-11-09 01:37:51 -0800 | [diff] [blame] | 41 | import java.net.UnknownHostException; |
| 42 | import java.util.concurrent.ConcurrentHashMap; |
| 43 | import java.util.concurrent.ConcurrentMap; |
Madan Jampani | 2af244a | 2015-02-22 13:12:01 -0800 | [diff] [blame] | 44 | import java.util.concurrent.ExecutorService; |
Madan Jampani | 348a9fe | 2014-11-09 01:37:51 -0800 | [diff] [blame] | 45 | import java.util.concurrent.TimeUnit; |
| 46 | import java.util.concurrent.TimeoutException; |
| 47 | import java.util.concurrent.atomic.AtomicLong; |
| 48 | |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 49 | import org.apache.commons.pool.KeyedPoolableObjectFactory; |
| 50 | import org.apache.commons.pool.impl.GenericKeyedObjectPool; |
Madan Jampani | 2e5f87b | 2015-02-22 10:37:15 -0800 | [diff] [blame] | 51 | import org.onlab.packet.IpAddress; |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 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 | 49115e9 | 2015-03-14 10:43:33 -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) { |
Yuta HIGUCHI | c611d92 | 2015-02-10 16:07:38 -0800 | [diff] [blame] | 78 | if (entry.wasEvicted()) { |
| 79 | entry.getValue().setException(new TimeoutException("Timedout waiting for reply")); |
| 80 | } |
Madan Jampani | 5f9ec9a | 2014-10-29 13:45:52 -0700 | [diff] [blame] | 81 | } |
| 82 | }) |
Madan Jampani | ddf7622 | 2014-10-04 23:48:44 -0700 | [diff] [blame] | 83 | .build(); |
Madan Jampani | 2e5f87b | 2015-02-22 10:37:15 -0800 | [diff] [blame] | 84 | |
Madan Jampani | ddf7622 | 2014-10-04 23:48:44 -0700 | [diff] [blame] | 85 | private final GenericKeyedObjectPool<Endpoint, Channel> channels |
| 86 | = new GenericKeyedObjectPool<Endpoint, Channel>(new OnosCommunicationChannelFactory()); |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 87 | |
Madan Jampani | 824a7c1 | 2014-10-21 09:46:15 -0700 | [diff] [blame] | 88 | private EventLoopGroup serverGroup; |
| 89 | private EventLoopGroup clientGroup; |
| 90 | private Class<? extends ServerChannel> serverChannelClass; |
| 91 | private Class<? extends Channel> clientChannelClass; |
| 92 | |
Madan Jampani | 5e83f33 | 2014-10-20 15:35:09 -0700 | [diff] [blame] | 93 | private void initEventLoopGroup() { |
Madan Jampani | 824a7c1 | 2014-10-21 09:46:15 -0700 | [diff] [blame] | 94 | // try Epoll first and if that does work, use nio. |
Madan Jampani | 824a7c1 | 2014-10-21 09:46:15 -0700 | [diff] [blame] | 95 | try { |
Madan Jampani | 99e9fe2 | 2014-10-21 13:47:12 -0700 | [diff] [blame] | 96 | clientGroup = new EpollEventLoopGroup(); |
| 97 | serverGroup = new EpollEventLoopGroup(); |
| 98 | serverChannelClass = EpollServerSocketChannel.class; |
| 99 | clientChannelClass = EpollSocketChannel.class; |
| 100 | return; |
Madan Jampani | 824a7c1 | 2014-10-21 09:46:15 -0700 | [diff] [blame] | 101 | } catch (Throwable t) { |
Madan Jampani | cfbc054 | 2014-10-24 20:38:07 -0700 | [diff] [blame] | 102 | 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] | 103 | } |
| 104 | clientGroup = new NioEventLoopGroup(); |
| 105 | serverGroup = new NioEventLoopGroup(); |
| 106 | serverChannelClass = NioServerSocketChannel.class; |
| 107 | clientChannelClass = NioSocketChannel.class; |
Madan Jampani | 5e83f33 | 2014-10-20 15:35:09 -0700 | [diff] [blame] | 108 | } |
| 109 | |
Madan Jampani | 2e5f87b | 2015-02-22 10:37:15 -0800 | [diff] [blame] | 110 | public NettyMessagingService(IpAddress ip, int port) { |
Madan Jampani | 8710093 | 2014-10-21 16:46:12 -0700 | [diff] [blame] | 111 | localEp = new Endpoint(ip, port); |
| 112 | } |
| 113 | |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 114 | public NettyMessagingService() { |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 115 | this(8080); |
| 116 | } |
| 117 | |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 118 | public NettyMessagingService(int port) { |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 119 | try { |
Madan Jampani | 2e5f87b | 2015-02-22 10:37:15 -0800 | [diff] [blame] | 120 | localEp = new Endpoint(IpAddress.valueOf(InetAddress.getLocalHost()), port); |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 121 | } catch (UnknownHostException e) { |
Ray Milkey | 3f0c97e | 2014-12-08 14:53:30 -0800 | [diff] [blame] | 122 | // Cannot resolve the local host, something is very wrong. Bailing out. |
| 123 | throw new IllegalStateException("Cannot resolve local host", e); |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 124 | } |
| 125 | } |
| 126 | |
Ray Milkey | 3f0c97e | 2014-12-08 14:53:30 -0800 | [diff] [blame] | 127 | public void activate() throws InterruptedException { |
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) |
Madan Jampani | 49115e9 | 2015-03-14 10:43:33 -0700 | [diff] [blame] | 153 | .withType(type) |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 154 | .withPayload(payload) |
| 155 | .build(); |
| 156 | sendAsync(ep, message); |
| 157 | } |
| 158 | |
| 159 | protected void sendAsync(Endpoint ep, InternalMessage message) throws IOException { |
Madan Jampani | ba47223 | 2015-03-04 13:00:50 -0800 | [diff] [blame] | 160 | if (ep.equals(localEp)) { |
| 161 | dispatchLocally(message); |
| 162 | return; |
| 163 | } |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 164 | Channel channel = null; |
| 165 | try { |
Madan Jampani | 86ed055 | 2014-10-03 16:45:42 -0700 | [diff] [blame] | 166 | try { |
| 167 | channel = channels.borrowObject(ep); |
| 168 | channel.eventLoop().execute(new WriteTask(channel, message)); |
| 169 | } finally { |
| 170 | channels.returnObject(ep, channel); |
| 171 | } |
Madan Jampani | 5f9ec9a | 2014-10-29 13:45:52 -0700 | [diff] [blame] | 172 | } catch (IOException e) { |
| 173 | throw e; |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 174 | } catch (Exception e) { |
Madan Jampani | 5f9ec9a | 2014-10-29 13:45:52 -0700 | [diff] [blame] | 175 | throw new IOException(e); |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 176 | } |
Madan Jampani | 5f9ec9a | 2014-10-29 13:45:52 -0700 | [diff] [blame] | 177 | |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | @Override |
Madan Jampani | 24f9efb | 2014-10-24 18:56:23 -0700 | [diff] [blame] | 181 | public ListenableFuture<byte[]> sendAndReceive(Endpoint ep, String type, byte[] payload) |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 182 | throws IOException { |
Madan Jampani | 24f9efb | 2014-10-24 18:56:23 -0700 | [diff] [blame] | 183 | SettableFuture<byte[]> futureResponse = SettableFuture.create(); |
| 184 | Long messageId = messageIdGenerator.incrementAndGet(); |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 185 | responseFutures.put(messageId, futureResponse); |
| 186 | InternalMessage message = new InternalMessage.Builder(this) |
| 187 | .withId(messageId) |
| 188 | .withSender(localEp) |
Madan Jampani | 49115e9 | 2015-03-14 10:43:33 -0700 | [diff] [blame] | 189 | .withType(type) |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 190 | .withPayload(payload) |
| 191 | .build(); |
Madan Jampani | 15cd0b8 | 2014-10-28 08:40:23 -0700 | [diff] [blame] | 192 | try { |
| 193 | sendAsync(ep, message); |
Madan Jampani | 5f9ec9a | 2014-10-29 13:45:52 -0700 | [diff] [blame] | 194 | } catch (Exception e) { |
Madan Jampani | 15cd0b8 | 2014-10-28 08:40:23 -0700 | [diff] [blame] | 195 | responseFutures.invalidate(messageId); |
| 196 | throw e; |
| 197 | } |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 198 | return futureResponse; |
| 199 | } |
| 200 | |
| 201 | @Override |
| 202 | public void registerHandler(String type, MessageHandler handler) { |
Madan Jampani | cd48bfd | 2015-03-25 18:06:22 -0700 | [diff] [blame] | 203 | handlers.put(type, handler); |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 204 | } |
| 205 | |
Yuta HIGUCHI | e5ca93b | 2014-10-23 09:49:00 -0700 | [diff] [blame] | 206 | @Override |
Madan Jampani | 2af244a | 2015-02-22 13:12:01 -0800 | [diff] [blame] | 207 | public void registerHandler(String type, MessageHandler handler, ExecutorService executor) { |
Madan Jampani | cd48bfd | 2015-03-25 18:06:22 -0700 | [diff] [blame] | 208 | handlers.put(type, new MessageHandler() { |
Madan Jampani | 2af244a | 2015-02-22 13:12:01 -0800 | [diff] [blame] | 209 | @Override |
| 210 | public void handle(Message message) throws IOException { |
| 211 | executor.submit(() -> { |
| 212 | try { |
| 213 | handler.handle(message); |
| 214 | } catch (Exception e) { |
Madan Jampani | 52860be | 2015-02-27 12:52:37 -0800 | [diff] [blame] | 215 | log.debug("Failed to process message of type {}", type, e); |
Madan Jampani | 2af244a | 2015-02-22 13:12:01 -0800 | [diff] [blame] | 216 | } |
| 217 | }); |
| 218 | } |
| 219 | }); |
| 220 | } |
| 221 | |
| 222 | @Override |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 223 | public void unregisterHandler(String type) { |
Madan Jampani | 49115e9 | 2015-03-14 10:43:33 -0700 | [diff] [blame] | 224 | handlers.remove(type); |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 225 | } |
| 226 | |
Madan Jampani | 49115e9 | 2015-03-14 10:43:33 -0700 | [diff] [blame] | 227 | private MessageHandler getMessageHandler(String type) { |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 228 | return handlers.get(type); |
| 229 | } |
| 230 | |
| 231 | private void startAcceptingConnections() throws InterruptedException { |
| 232 | ServerBootstrap b = new ServerBootstrap(); |
Madan Jampani | 86ed055 | 2014-10-03 16:45:42 -0700 | [diff] [blame] | 233 | b.option(ChannelOption.WRITE_BUFFER_HIGH_WATER_MARK, 32 * 1024); |
Madan Jampani | ddf7622 | 2014-10-04 23:48:44 -0700 | [diff] [blame] | 234 | b.option(ChannelOption.WRITE_BUFFER_LOW_WATER_MARK, 8 * 1024); |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 235 | b.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT); |
Madan Jampani | 824a7c1 | 2014-10-21 09:46:15 -0700 | [diff] [blame] | 236 | b.group(serverGroup, clientGroup) |
| 237 | .channel(serverChannelClass) |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 238 | .childHandler(new OnosCommunicationChannelInitializer()) |
| 239 | .option(ChannelOption.SO_BACKLOG, 128) |
| 240 | .childOption(ChannelOption.SO_KEEPALIVE, true); |
| 241 | |
| 242 | // Bind and start to accept incoming connections. |
Madan Jampani | 8710093 | 2014-10-21 16:46:12 -0700 | [diff] [blame] | 243 | b.bind(localEp.port()).sync(); |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | private class OnosCommunicationChannelFactory |
| 247 | implements KeyedPoolableObjectFactory<Endpoint, Channel> { |
| 248 | |
| 249 | @Override |
| 250 | public void activateObject(Endpoint endpoint, Channel channel) |
| 251 | throws Exception { |
| 252 | } |
| 253 | |
| 254 | @Override |
| 255 | public void destroyObject(Endpoint ep, Channel channel) throws Exception { |
| 256 | channel.close(); |
| 257 | } |
| 258 | |
| 259 | @Override |
| 260 | public Channel makeObject(Endpoint ep) throws Exception { |
Madan Jampani | ddf7622 | 2014-10-04 23:48:44 -0700 | [diff] [blame] | 261 | Bootstrap bootstrap = new Bootstrap(); |
| 262 | bootstrap.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT); |
| 263 | bootstrap.option(ChannelOption.WRITE_BUFFER_HIGH_WATER_MARK, 32 * 1024); |
| 264 | bootstrap.option(ChannelOption.WRITE_BUFFER_LOW_WATER_MARK, 8 * 1024); |
Madan Jampani | 824a7c1 | 2014-10-21 09:46:15 -0700 | [diff] [blame] | 265 | bootstrap.group(clientGroup); |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 266 | // TODO: Make this faster: |
| 267 | // http://normanmaurer.me/presentations/2014-facebook-eng-netty/slides.html#37.0 |
Madan Jampani | 824a7c1 | 2014-10-21 09:46:15 -0700 | [diff] [blame] | 268 | bootstrap.channel(clientChannelClass); |
Madan Jampani | ddf7622 | 2014-10-04 23:48:44 -0700 | [diff] [blame] | 269 | bootstrap.option(ChannelOption.SO_KEEPALIVE, true); |
| 270 | bootstrap.handler(new OnosCommunicationChannelInitializer()); |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 271 | // Start the client. |
Madan Jampani | 2e5f87b | 2015-02-22 10:37:15 -0800 | [diff] [blame] | 272 | ChannelFuture f = bootstrap.connect(ep.host().toString(), ep.port()).sync(); |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 273 | return f.channel(); |
| 274 | } |
| 275 | |
| 276 | @Override |
| 277 | public void passivateObject(Endpoint ep, Channel channel) |
| 278 | throws Exception { |
| 279 | } |
| 280 | |
| 281 | @Override |
| 282 | public boolean validateObject(Endpoint ep, Channel channel) { |
| 283 | return channel.isOpen(); |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | private class OnosCommunicationChannelInitializer extends ChannelInitializer<SocketChannel> { |
| 288 | |
Madan Jampani | ddf7622 | 2014-10-04 23:48:44 -0700 | [diff] [blame] | 289 | private final ChannelHandler dispatcher = new InboundMessageDispatcher(); |
Madan Jampani | 53e44e6 | 2014-10-07 12:39:51 -0700 | [diff] [blame] | 290 | private final ChannelHandler encoder = new MessageEncoder(); |
Madan Jampani | ddf7622 | 2014-10-04 23:48:44 -0700 | [diff] [blame] | 291 | |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 292 | @Override |
| 293 | protected void initChannel(SocketChannel channel) throws Exception { |
| 294 | channel.pipeline() |
Madan Jampani | ddf7622 | 2014-10-04 23:48:44 -0700 | [diff] [blame] | 295 | .addLast("encoder", encoder) |
Madan Jampani | 53e44e6 | 2014-10-07 12:39:51 -0700 | [diff] [blame] | 296 | .addLast("decoder", new MessageDecoder(NettyMessagingService.this)) |
Madan Jampani | ddf7622 | 2014-10-04 23:48:44 -0700 | [diff] [blame] | 297 | .addLast("handler", dispatcher); |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 298 | } |
| 299 | } |
| 300 | |
Yuta HIGUCHI | e5ca93b | 2014-10-23 09:49:00 -0700 | [diff] [blame] | 301 | private static class WriteTask implements Runnable { |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 302 | |
Madan Jampani | 86ed055 | 2014-10-03 16:45:42 -0700 | [diff] [blame] | 303 | private final InternalMessage message; |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 304 | private final Channel channel; |
| 305 | |
Madan Jampani | 86ed055 | 2014-10-03 16:45:42 -0700 | [diff] [blame] | 306 | public WriteTask(Channel channel, InternalMessage message) { |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 307 | this.channel = channel; |
Madan Jampani | 86ed055 | 2014-10-03 16:45:42 -0700 | [diff] [blame] | 308 | this.message = message; |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | @Override |
| 312 | public void run() { |
Madan Jampani | 5f9ec9a | 2014-10-29 13:45:52 -0700 | [diff] [blame] | 313 | channel.writeAndFlush(message).addListener(ChannelFutureListener.CLOSE_ON_FAILURE); |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 314 | } |
| 315 | } |
| 316 | |
Madan Jampani | ddf7622 | 2014-10-04 23:48:44 -0700 | [diff] [blame] | 317 | @ChannelHandler.Sharable |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 318 | private class InboundMessageDispatcher extends SimpleChannelInboundHandler<InternalMessage> { |
| 319 | |
| 320 | @Override |
| 321 | protected void channelRead0(ChannelHandlerContext ctx, InternalMessage message) throws Exception { |
Madan Jampani | ba47223 | 2015-03-04 13:00:50 -0800 | [diff] [blame] | 322 | dispatchLocally(message); |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 323 | } |
Madan Jampani | 86ed055 | 2014-10-03 16:45:42 -0700 | [diff] [blame] | 324 | |
Madan Jampani | 86ed055 | 2014-10-03 16:45:42 -0700 | [diff] [blame] | 325 | @Override |
| 326 | public void exceptionCaught(ChannelHandlerContext context, Throwable cause) { |
Madan Jampani | 29e5dfd | 2014-10-07 17:26:25 -0700 | [diff] [blame] | 327 | log.error("Exception inside channel handling pipeline.", cause); |
Madan Jampani | 86ed055 | 2014-10-03 16:45:42 -0700 | [diff] [blame] | 328 | context.close(); |
| 329 | } |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 330 | } |
Madan Jampani | 2e5f87b | 2015-02-22 10:37:15 -0800 | [diff] [blame] | 331 | |
Madan Jampani | ba47223 | 2015-03-04 13:00:50 -0800 | [diff] [blame] | 332 | private void dispatchLocally(InternalMessage message) throws IOException { |
Madan Jampani | 49115e9 | 2015-03-14 10:43:33 -0700 | [diff] [blame] | 333 | String type = message.type(); |
| 334 | if (InternalMessage.REPLY_MESSAGE_TYPE.equals(type)) { |
Madan Jampani | ba47223 | 2015-03-04 13:00:50 -0800 | [diff] [blame] | 335 | try { |
| 336 | SettableFuture<byte[]> futureResponse = |
| 337 | NettyMessagingService.this.responseFutures.getIfPresent(message.id()); |
| 338 | if (futureResponse != null) { |
| 339 | futureResponse.set(message.payload()); |
| 340 | } else { |
| 341 | log.warn("Received a reply for message id:[{}]. " |
| 342 | + " from {}. But was unable to locate the" |
| 343 | + " request handle", message.id(), message.sender()); |
| 344 | } |
| 345 | } finally { |
| 346 | NettyMessagingService.this.responseFutures.invalidate(message.id()); |
| 347 | } |
| 348 | return; |
| 349 | } |
| 350 | MessageHandler handler = NettyMessagingService.this.getMessageHandler(type); |
| 351 | if (handler != null) { |
| 352 | handler.handle(message); |
| 353 | } else { |
| 354 | log.debug("No handler registered for {}", type); |
| 355 | } |
| 356 | } |
Madan Jampani | 2e5f87b | 2015-02-22 10:37:15 -0800 | [diff] [blame] | 357 | } |