Merge "Add XSQL into default compilation"
[controller.git] / opendaylight / commons / protocol-framework / src / test / java / org / opendaylight / protocol / framework / SimpleDispatcher.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.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;
16
17 import java.net.InetSocketAddress;
18
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21
22 import com.google.common.base.Preconditions;
23
24 public class SimpleDispatcher extends AbstractDispatcher<SimpleSession, SimpleSessionListener> {
25     private static final Logger logger = LoggerFactory.getLogger(SimpleDispatcher.class);
26
27     private final SessionNegotiatorFactory<SimpleMessage, SimpleSession, SimpleSessionListener> negotiatorFactory;
28     private final ChannelOutboundHandler encoder = new SimpleMessageToByteEncoder();
29
30     private final class SimplePipelineInitializer implements PipelineInitializer<SimpleSession> {
31         final SessionListenerFactory<SimpleSessionListener> listenerFactory;
32
33         SimplePipelineInitializer(final SessionListenerFactory<SimpleSessionListener> listenerFactory) {
34             this.listenerFactory = Preconditions.checkNotNull(listenerFactory);
35         }
36
37         @Override
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);
43         }
44
45     }
46
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);
51     }
52
53     public Future<SimpleSession> createClient(final InetSocketAddress address, final ReconnectStrategy strategy, final SessionListenerFactory<SimpleSessionListener> listenerFactory) {
54         return super.createClient(address, strategy, new SimplePipelineInitializer(listenerFactory));
55     }
56
57     public ChannelFuture createServer(final InetSocketAddress address, final SessionListenerFactory<SimpleSessionListener> listenerFactory) {
58         return super.createServer(address, new SimplePipelineInitializer(listenerFactory));
59     }
60
61     @Override
62     public void close() {
63     }
64 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.