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.channel.ChannelFuture;
11 import io.netty.channel.ChannelOutboundHandler;
12 import io.netty.channel.EventLoopGroup;
13 import io.netty.channel.socket.SocketChannel;
14 import io.netty.util.concurrent.Future;
15 import io.netty.util.concurrent.Promise;
17 import java.net.InetSocketAddress;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
22 import com.google.common.base.Preconditions;
24 public class SimpleDispatcher extends AbstractDispatcher<SimpleSession, SimpleSessionListener> {
25 private static final Logger logger = LoggerFactory.getLogger(SimpleDispatcher.class);
27 private final SessionNegotiatorFactory<SimpleMessage, SimpleSession, SimpleSessionListener> negotiatorFactory;
28 private final ChannelOutboundHandler encoder = new SimpleMessageToByteEncoder();
30 private final class SimplePipelineInitializer implements PipelineInitializer<SimpleSession> {
31 final SessionListenerFactory<SimpleSessionListener> listenerFactory;
33 SimplePipelineInitializer(final SessionListenerFactory<SimpleSessionListener> listenerFactory) {
34 this.listenerFactory = Preconditions.checkNotNull(listenerFactory);
38 public void initializeChannel(final SocketChannel channel, final Promise<SimpleSession> promise) {
39 channel.pipeline().addLast(new SimpleByteToMessageDecoder());
40 channel.pipeline().addLast("negotiator", negotiatorFactory.getSessionNegotiator(listenerFactory, channel, promise));
41 channel.pipeline().addLast(encoder);
42 logger.debug("initialization completed for channel {}", channel);
47 public SimpleDispatcher(final SessionNegotiatorFactory<SimpleMessage, SimpleSession, SimpleSessionListener> negotiatorFactory,
48 final Promise<SimpleSession> promise, final EventLoopGroup eventLoopGroup) {
49 super(eventLoopGroup, eventLoopGroup);
50 this.negotiatorFactory = Preconditions.checkNotNull(negotiatorFactory);
53 public Future<SimpleSession> createClient(final InetSocketAddress address, final ReconnectStrategy strategy, final SessionListenerFactory<SimpleSessionListener> listenerFactory) {
54 return super.createClient(address, strategy, new SimplePipelineInitializer(listenerFactory));
57 public ChannelFuture createServer(final InetSocketAddress address, final SessionListenerFactory<SimpleSessionListener> listenerFactory) {
58 return super.createServer(address, new SimplePipelineInitializer(listenerFactory));