BUG-58: refactor to take advantage of netty
[bgpcep.git] / framework / src / main / java / org / opendaylight / protocol / framework / DispatcherImpl.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.protocol.framework;
9
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;
19
20 import java.io.Closeable;
21 import java.net.InetSocketAddress;
22
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 import com.google.common.base.Preconditions;
27
28 /**
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.
31  */
32 public final class DispatcherImpl implements Closeable, Dispatcher {
33
34         private static final Logger logger = LoggerFactory.getLogger(DispatcherImpl.class);
35
36         private final EventLoopGroup bossGroup;
37
38         private final EventLoopGroup workerGroup;
39
40         public DispatcherImpl() {
41                 // FIXME: we should get these as arguments
42                 this.bossGroup = new NioEventLoopGroup();
43                 this.workerGroup = new NioEventLoopGroup();
44         }
45
46         @Override
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);
57
58                 // Bind and start to accept incoming connections.
59                 final ChannelFuture f = b.bind(address);
60                 logger.debug("Initiated server {} at {}.", f, address);
61                 return f;
62
63         }
64
65         @Override
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;
72
73                         @Override
74                         public synchronized L getSessionListener() {
75                                 Preconditions.checkState(created == false);
76                                 created = true;
77                                 return listener;
78                         }
79
80                 }, new ProtocolHandlerFactory<M>(messageFactory), strategy);
81
82                 p.connect();
83                 logger.debug("Client created.");
84                 return p;
85         }
86
87         @Override
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) {
92
93                 final ReconnectPromise<M, S, L> p = new ReconnectPromise<M, S, L>(this, address, listener, negotiatorFactory,
94                                 messageFactory, connectStrategyFactory, reestablishStrategy);
95
96                 p.connect();
97
98                 return p;
99
100         }
101
102         @Override
103         public void close() {
104                 try {
105                         this.workerGroup.shutdownGracefully();
106                 } finally {
107                         this.bossGroup.shutdownGracefully();
108                 }
109         }
110 }