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.protocol.framework;
10 import io.netty.bootstrap.ServerBootstrap;
11 import io.netty.channel.ChannelFuture;
12 import io.netty.channel.ChannelOption;
13 import io.netty.channel.EventLoopGroup;
14 import io.netty.channel.nio.NioEventLoopGroup;
15 import io.netty.channel.socket.nio.NioServerSocketChannel;
16 import io.netty.util.concurrent.DefaultPromise;
17 import io.netty.util.concurrent.Future;
18 import io.netty.util.concurrent.GlobalEventExecutor;
20 import java.io.Closeable;
21 import java.net.InetSocketAddress;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
26 import com.google.common.base.Preconditions;
29 * Dispatcher class for creating servers and clients. The idea is to first create servers and clients and the run the
30 * start method that will handle sockets in different thread.
32 public final class DispatcherImpl implements Closeable, Dispatcher {
34 private static final Logger logger = LoggerFactory.getLogger(DispatcherImpl.class);
36 private final EventLoopGroup bossGroup;
38 private final EventLoopGroup workerGroup;
40 public DispatcherImpl() {
41 // FIXME: we should get these as arguments
42 this.bossGroup = new NioEventLoopGroup();
43 this.workerGroup = new NioEventLoopGroup();
47 public <M extends ProtocolMessage, S extends ProtocolSession<M>, L extends SessionListener<M, ?, ?>> ChannelFuture createServer(
48 final InetSocketAddress address, final SessionListenerFactory<L> listenerFactory,
49 final SessionNegotiatorFactory<M, S, L> negotiatorFactory, final ProtocolMessageFactory<M> messageFactory) {
50 final ServerBootstrap b = new ServerBootstrap();
51 b.group(this.bossGroup, this.workerGroup);
52 b.channel(NioServerSocketChannel.class);
53 b.option(ChannelOption.SO_BACKLOG, 128);
54 b.childHandler(new ChannelInitializerImpl<M, S, L>(negotiatorFactory,
55 listenerFactory, new ProtocolHandlerFactory<M>(messageFactory), new DefaultPromise<S>(GlobalEventExecutor.INSTANCE)));
56 b.childOption(ChannelOption.SO_KEEPALIVE, true);
58 // Bind and start to accept incoming connections.
59 final ChannelFuture f = b.bind(address);
60 logger.debug("Initiated server {} at {}.", f, address);
66 public <M extends ProtocolMessage, S extends ProtocolSession<M>, L extends SessionListener<M, ?, ?>> Future<S> createClient(
67 final InetSocketAddress address, final L listener, final SessionNegotiatorFactory<M, S, L> negotiatorFactory,
68 final ProtocolMessageFactory<M> messageFactory, final ReconnectStrategy strategy) {
69 final ProtocolSessionPromise<M, S, L> p = new ProtocolSessionPromise<M, S, L>(workerGroup, address, negotiatorFactory,
70 new SessionListenerFactory<L>() {
71 private boolean created = false;
74 public synchronized L getSessionListener() {
75 Preconditions.checkState(created == false);
80 }, new ProtocolHandlerFactory<M>(messageFactory), strategy);
83 logger.debug("Client created.");
88 public <M extends ProtocolMessage, S extends ProtocolSession<M>, L extends SessionListener<M, ?, ?>> Future<Void> createReconnectingClient(
89 final InetSocketAddress address, final L listener, final SessionNegotiatorFactory<M, S, L> negotiatorFactory,
90 final ProtocolMessageFactory<M> messageFactory, final ReconnectStrategyFactory connectStrategyFactory,
91 final ReconnectStrategy reestablishStrategy) {
93 final ReconnectPromise<M, S, L> p = new ReconnectPromise<M, S, L>(this, address, listener, negotiatorFactory,
94 messageFactory, connectStrategyFactory, reestablishStrategy);
103 public void close() {
105 this.workerGroup.shutdownGracefully();
107 this.bossGroup.shutdownGracefully();