View Javadoc
1   /*
2    * Copyright 2012 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package io.netty5.example.localecho;
17  
18  import io.netty5.bootstrap.Bootstrap;
19  import io.netty5.bootstrap.ServerBootstrap;
20  import io.netty5.channel.Channel;
21  import io.netty5.channel.ChannelInitializer;
22  import io.netty5.channel.EventLoopGroup;
23  import io.netty5.channel.MultithreadEventLoopGroup;
24  import io.netty5.channel.local.LocalAddress;
25  import io.netty5.channel.local.LocalChannel;
26  import io.netty5.channel.local.LocalHandler;
27  import io.netty5.channel.local.LocalServerChannel;
28  import io.netty5.handler.logging.LogLevel;
29  import io.netty5.handler.logging.LoggingHandler;
30  import io.netty5.util.concurrent.Future;
31  
32  import java.io.BufferedReader;
33  import java.io.InputStreamReader;
34  
35  public final class LocalEcho {
36  
37      static final String PORT = System.getProperty("port", "test_port");
38  
39      public static void main(String[] args) throws Exception {
40          // Address to bind on / connect to.
41          final LocalAddress addr = new LocalAddress(PORT);
42  
43          EventLoopGroup serverGroup = new MultithreadEventLoopGroup(LocalHandler.newFactory());
44          EventLoopGroup clientGroup = new MultithreadEventLoopGroup(LocalHandler.newFactory());
45          try {
46              // Note that we can use any event loop to ensure certain local channels
47              // are handled by the same event loop thread which drives a certain socket channel
48              // to reduce the communication latency between socket channels and local channels.
49              ServerBootstrap sb = new ServerBootstrap();
50              sb.group(serverGroup)
51                .channel(LocalServerChannel.class)
52                .handler(new ChannelInitializer<LocalServerChannel>() {
53                    @Override
54                    public void initChannel(LocalServerChannel ch) throws Exception {
55                        ch.pipeline().addLast(new LoggingHandler(LogLevel.INFO));
56                    }
57                })
58                .childHandler(new ChannelInitializer<LocalChannel>() {
59                    @Override
60                    public void initChannel(LocalChannel ch) throws Exception {
61                        ch.pipeline().addLast(
62                                new LoggingHandler(LogLevel.INFO),
63                                new LocalEchoServerHandler());
64                    }
65                });
66  
67              Bootstrap cb = new Bootstrap();
68              cb.group(clientGroup)
69                .channel(LocalChannel.class)
70                .handler(new ChannelInitializer<LocalChannel>() {
71                    @Override
72                    public void initChannel(LocalChannel ch) throws Exception {
73                        ch.pipeline().addLast(
74                                new LoggingHandler(LogLevel.INFO),
75                                new LocalEchoClientHandler());
76                    }
77                });
78  
79              // Start the server.
80              sb.bind(addr).asStage().sync();
81  
82              // Start the client.
83              Channel ch = cb.connect(addr).asStage().get();
84  
85              // Read commands from the stdin.
86              System.out.println("Enter text (quit to end)");
87              Future<Void> lastWriteFuture = null;
88              BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
89              for (;;) {
90                  String line = in.readLine();
91                  if (line == null || "quit".equalsIgnoreCase(line)) {
92                      break;
93                  }
94  
95                  // Sends the received line to the server.
96                  lastWriteFuture = ch.writeAndFlush(line);
97              }
98  
99              // Wait until all messages are flushed before closing the channel.
100             if (lastWriteFuture != null) {
101                 lastWriteFuture.asStage().await();
102             }
103         } finally {
104             serverGroup.shutdownGracefully();
105             clientGroup.shutdownGracefully();
106         }
107     }
108 }