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    *   http://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 org.jboss.netty.example.local;
17  
18  import org.jboss.netty.bootstrap.ClientBootstrap;
19  import org.jboss.netty.bootstrap.ServerBootstrap;
20  import org.jboss.netty.channel.ChannelFuture;
21  import org.jboss.netty.channel.ChannelPipeline;
22  import org.jboss.netty.channel.ChannelPipelineFactory;
23  import org.jboss.netty.channel.Channels;
24  import org.jboss.netty.channel.local.DefaultLocalClientChannelFactory;
25  import org.jboss.netty.channel.local.DefaultLocalServerChannelFactory;
26  import org.jboss.netty.channel.local.LocalAddress;
27  import org.jboss.netty.example.echo.EchoServerHandler;
28  import org.jboss.netty.handler.codec.string.StringDecoder;
29  import org.jboss.netty.handler.codec.string.StringEncoder;
30  import org.jboss.netty.handler.logging.LoggingHandler;
31  import org.jboss.netty.logging.InternalLogLevel;
32  
33  import java.io.BufferedReader;
34  import java.io.InputStreamReader;
35  
36  public final class LocalExample {
37  
38      static final String PORT = System.getProperty("port", "test_port");
39  
40      public static void main(String[] args) throws Exception {
41          // Address to bind on / connect to.
42          LocalAddress socketAddress = new LocalAddress(PORT);
43  
44          // Create the bootstraps for both client and server side.
45          ServerBootstrap sb = new ServerBootstrap(new DefaultLocalServerChannelFactory());
46          ClientBootstrap cb = new ClientBootstrap(new DefaultLocalClientChannelFactory());
47  
48          try {
49              // Set up the default server-side event pipeline.
50              EchoServerHandler handler = new EchoServerHandler();
51              sb.getPipeline().addLast("handler", handler);
52  
53              // Start up the server.
54              sb.bind(socketAddress);
55  
56              // Set up the client-side pipeline factory.
57              cb.setPipelineFactory(new ChannelPipelineFactory() {
58                  public ChannelPipeline getPipeline() {
59                      return Channels.pipeline(
60                              new StringDecoder(),
61                              new StringEncoder(),
62                              new LoggingHandler(InternalLogLevel.INFO));
63                  }
64              });
65  
66              // Make the connection attempt to the server.
67              ChannelFuture channelFuture = cb.connect(socketAddress);
68              channelFuture.sync();
69  
70              // Read commands from the stdin.
71              System.err.println("Enter text (quit to end)");
72              ChannelFuture lastWriteFuture = null;
73              BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
74              for (;;) {
75                  String line = in.readLine();
76                  if (line == null || "quit".equalsIgnoreCase(line)) {
77                      break;
78                  }
79  
80                  // Sends the received line to the server.
81                  lastWriteFuture = channelFuture.getChannel().write(line);
82              }
83  
84              // Wait until all messages are flushed before closing the channel.
85              if (lastWriteFuture != null) {
86                  lastWriteFuture.sync();
87              }
88              channelFuture.getChannel().close();
89  
90              // Wait until the connection is closed or the connection attempt fails.
91              channelFuture.getChannel().getCloseFuture().sync();
92          } finally {
93              // Release all resources used by the local transport.
94              cb.releaseExternalResources();
95              sb.releaseExternalResources();
96          }
97      }
98  }