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