Merge "Bug 1073: Added Transaction Chain support to InMemoryDataTreeModification."
[controller.git] / opendaylight / netconf / netconf-ssh / src / test / java / org / opendaylight / controller / netconf / netty / EchoServer.java
1 /*
2  * Copyright (c) 2014 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
9 package org.opendaylight.controller.netconf.netty;
10
11 import io.netty.bootstrap.ServerBootstrap;
12 import io.netty.channel.ChannelFuture;
13 import io.netty.channel.ChannelInitializer;
14 import io.netty.channel.ChannelOption;
15 import io.netty.channel.EventLoopGroup;
16 import io.netty.channel.local.LocalAddress;
17 import io.netty.channel.local.LocalChannel;
18 import io.netty.channel.local.LocalServerChannel;
19 import io.netty.channel.nio.NioEventLoopGroup;
20 import io.netty.handler.logging.LogLevel;
21 import io.netty.handler.logging.LoggingHandler;
22 import java.io.BufferedReader;
23 import java.io.InputStreamReader;
24 import org.opendaylight.controller.netconf.util.osgi.NetconfConfigUtil;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 /**
29  * Echoes back any received data from a client.
30  */
31 public class EchoServer implements Runnable {
32     private static final Logger logger = LoggerFactory.getLogger(EchoServer.class);
33
34     public void run() {
35         // Configure the server.
36         EventLoopGroup bossGroup = new NioEventLoopGroup(1);
37         EventLoopGroup workerGroup = new NioEventLoopGroup();
38         try {
39             ServerBootstrap b = new ServerBootstrap();
40             b.group(bossGroup, workerGroup)
41                     .channel(LocalServerChannel.class)
42                     .option(ChannelOption.SO_BACKLOG, 100)
43                     .handler(new LoggingHandler(LogLevel.INFO))
44                     .childHandler(new ChannelInitializer<LocalChannel>() {
45                         @Override
46                         public void initChannel(LocalChannel ch) throws Exception {
47                             ch.pipeline().addLast(new EchoServerHandler());
48                         }
49                     });
50
51             // Start the server.
52             LocalAddress localAddress = NetconfConfigUtil.getNetconfLocalAddress();
53             ChannelFuture f = b.bind(localAddress).sync();
54
55             // Wait until the server socket is closed.
56             f.channel().closeFuture().sync();
57         } catch (Exception e) {
58             throw new RuntimeException(e);
59         } finally {
60             // Shut down all event loops to terminate all threads.
61             bossGroup.shutdownGracefully();
62             workerGroup.shutdownGracefully();
63         }
64     }
65
66     public static void main(String[] args) throws Exception {
67         new Thread(new EchoServer()).start();
68         Thread.sleep(1000);
69         EchoClientHandler clientHandler = new EchoClientHandler();
70         EchoClient echoClient = new EchoClient(clientHandler);
71         new Thread(echoClient).start();
72
73         BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
74         do {
75             String message = reader.readLine();
76             if (message == null ||  "exit".equalsIgnoreCase(message)) {
77                 break;
78             }
79             logger.debug("Got '{}'", message);
80             clientHandler.write(message);
81         } while (true);
82         System.exit(0);
83     }
84 }