Remove netconf-impl's getConfig_candidate.xml
[netconf.git] / netconf / netconf-ssh / src / test / java / org / opendaylight / 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.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.netconf.util.NetconfConfiguration;
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 LOG = LoggerFactory.getLogger(EchoServer.class);
33
34     @SuppressWarnings("checkstyle:IllegalCatch")
35     public void run() {
36         // Configure the server.
37         EventLoopGroup bossGroup = new NioEventLoopGroup(1);
38         EventLoopGroup workerGroup = new NioEventLoopGroup();
39         try {
40             ServerBootstrap bootstrap = new ServerBootstrap();
41             bootstrap.group(bossGroup, workerGroup)
42                     .channel(LocalServerChannel.class)
43                     .option(ChannelOption.SO_BACKLOG, 100)
44                     .handler(new LoggingHandler(LogLevel.INFO))
45                     .childHandler(new ChannelInitializer<LocalChannel>() {
46                         @Override
47                         public void initChannel(LocalChannel ch) throws Exception {
48                             ch.pipeline().addLast(new EchoServerHandler());
49                         }
50                     });
51
52             // Start the server.
53             LocalAddress localAddress = NetconfConfiguration.NETCONF_LOCAL_ADDRESS;
54             ChannelFuture future = bootstrap.bind(localAddress).sync();
55
56             // Wait until the server socket is closed.
57             future.channel().closeFuture().sync();
58         } catch (Exception e) {
59             throw new RuntimeException(e);
60         } finally {
61             // Shut down all event loops to terminate all threads.
62             bossGroup.shutdownGracefully();
63             workerGroup.shutdownGracefully();
64         }
65     }
66
67     public static void main(String[] args) throws Exception {
68         new Thread(new EchoServer()).start();
69         Thread.sleep(1000);
70         EchoClientHandler clientHandler = new EchoClientHandler();
71         EchoClient echoClient = new EchoClient(clientHandler);
72         new Thread(echoClient).start();
73
74         BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
75         do {
76             String message = reader.readLine();
77             if (message == null ||  "exit".equalsIgnoreCase(message)) {
78                 break;
79             }
80             LOG.debug("Got '{}'", message);
81             clientHandler.write(message);
82         } while (true);
83         System.exit(0);
84     }
85 }