org.opendaylight.usc.test.plugin.EchoServerTcp.java Source code

Java tutorial

Introduction

Here is the source code for org.opendaylight.usc.test.plugin.EchoServerTcp.java

Source

/*
 * Copyright (c) 2015 Huawei, Inc and others.  All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/epl-v10.html
 */
package org.opendaylight.usc.test.plugin;

import org.opendaylight.usc.manager.UscConfigurationServiceImpl;
import org.opendaylight.usc.manager.api.UscSecureService;
import org.opendaylight.usc.util.UscServiceUtils;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;

/**
 * TCP echo server.
 */
public class EchoServerTcp implements Runnable, AutoCloseable {
    static int PORT = Integer.parseInt(System.getProperty("port", "2007"));
    EventLoopGroup bossGroup = new NioEventLoopGroup(1);
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    ServerBootstrap b = new ServerBootstrap();
    private UscSecureService secureService = null;

    public EchoServerTcp(final boolean enableEncryption) {
        this(enableEncryption, PORT);
    }

    public EchoServerTcp(final boolean enableEncryption, int port) {
        PORT = port;
        UscConfigurationServiceImpl.setDefaultPropertyFilePath("src/test/resources/etc/usc/usc.properties");
        secureService = UscServiceUtils.getService(UscSecureService.class);
        b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).option(ChannelOption.SO_BACKLOG, 100)
                .handler(new LoggingHandler("EchoServerTcp server handler", LogLevel.TRACE))
                .childHandler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    public void initChannel(SocketChannel ch) throws Exception {
                        ChannelPipeline p = ch.pipeline();
                        if (enableEncryption) {
                            p.addLast(new LoggingHandler("EchoServerTcp Handler 3", LogLevel.TRACE));
                            p.addLast(secureService.getTcpServerHandler(ch));
                        }
                        p.addLast(new LoggingHandler("EchoServerTcp Handler 2", LogLevel.TRACE));
                        p.addLast(new EchoServerTcpHandler());
                        p.addLast(new LoggingHandler("EchoServerTcp Handler 1", LogLevel.TRACE));
                    }
                });

    }

    @Override
    public void run() {
        // Start the server.
        try {
            ChannelFuture f = b.bind(PORT).sync();
            // Wait until the server socket is closed.
            f.channel().closeFuture().sync();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    @Override
    public void close() throws Exception {
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
    }

    public static void main(String[] args) throws Exception {
        if (args.length > 0) {
            try {
                int port = Integer.parseInt(args[0]);
                PORT = port;
            } catch (NumberFormatException e) {
                System.err.println("Argument " + args[0] + " must be an integer (port #).");
                System.exit(1);
            }
        }

        try (EchoServerTcp server = new EchoServerTcp(false)) {
            server.run();
        }
    }

}