View Javadoc

    1   /*
    2    * Copyright 2009 Red Hat, Inc.
    3    *
    4    * Red Hat licenses this file to you under the Apache License, version 2.0
    5    * (the "License"); you may not use this file except in compliance with the
    6    * 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.InputStreamReader;
    20
    21  import org.jboss.netty.bootstrap.ClientBootstrap;
    22  import org.jboss.netty.bootstrap.ServerBootstrap;
    23  import org.jboss.netty.channel.ChannelFuture;
    24  import org.jboss.netty.channel.ChannelPipeline;
    25  import org.jboss.netty.channel.ChannelPipelineFactory;
    26  import org.jboss.netty.channel.Channels;
    27  import org.jboss.netty.channel.local.DefaultLocalClientChannelFactory;
    28  import org.jboss.netty.channel.local.DefaultLocalServerChannelFactory;
    29  import org.jboss.netty.channel.local.LocalAddress;
    30  import org.jboss.netty.example.echo.EchoServerHandler;
    31  import org.jboss.netty.handler.codec.string.StringDecoder;
    32  import org.jboss.netty.handler.codec.string.StringEncoder;
    33  import org.jboss.netty.handler.logging.LoggingHandler;
    34  import org.jboss.netty.logging.InternalLogLevel;
    35
    36  /**
    37   * @author <a href="http://www.jboss.org/netty/">The Netty Project</a>
    38   * @author Andy Taylor (andy.taylor@jboss.org)
    39   * @version $Rev: 2080 $, $Date: 2010-01-26 18:04:19 +0900 (Tue, 26 Jan 2010) $
    40   */
    41  public class LocalExample {
    42      public static void main(String[] args) throws Exception {
    43          // Address to bind on / connect to.
    44          LocalAddress socketAddress = new LocalAddress("1");
    45
    46          // Configure the server.
    47          ServerBootstrap sb = new ServerBootstrap(
    48                  new DefaultLocalServerChannelFactory());
    49
    50          // Set up the default server-side event pipeline.
    51          EchoServerHandler handler = new EchoServerHandler();
    52          sb.getPipeline().addLast("handler", handler);
    53
    54          // Start up the server.
    55          sb.bind(socketAddress);
    56
    57          // Configure the client.
    58          ClientBootstrap cb = new ClientBootstrap(
    59                  new DefaultLocalClientChannelFactory());
    60
    61          // Set up the client-side pipeline factory.
    62          cb.setPipelineFactory(new ChannelPipelineFactory() {
    63              public ChannelPipeline getPipeline() throws Exception {
    64                  return Channels.pipeline(
    65                          new StringDecoder(),
    66                          new StringEncoder(),
    67                          new LoggingHandler(InternalLogLevel.INFO));
    68              }
    69          });
    70
    71          // Make the connection attempt to the server.
    72          ChannelFuture channelFuture = cb.connect(socketAddress);
    73          channelFuture.awaitUninterruptibly();
    74
    75          // Read commands from the stdin.
    76          System.out.println("Enter text (quit to end)");
    77          ChannelFuture lastWriteFuture = null;
    78          BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    79          for (; ;) {
    80              String line = in.readLine();
    81              if (line == null || "quit".equalsIgnoreCase(line)) {
    82                  break;
    83              }
    84
    85              // Sends the received line to the server.
    86              lastWriteFuture = channelFuture.getChannel().write(line);
    87          }
    88
    89          // Wait until all messages are flushed before closing the channel.
    90          if (lastWriteFuture != null) {
    91              lastWriteFuture.awaitUninterruptibly();
    92          }
    93          channelFuture.getChannel().close();
    94
    95          // Wait until the connection is closed or the connection attempt fails.
    96          channelFuture.getChannel().getCloseFuture().awaitUninterruptibly();
    97
    98          // Release all resources used by the local transport.
    99          cb.releaseExternalResources();
    100         sb.releaseExternalResources();
    101     }
    102 }