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.securechat;
17  
18  import java.io.BufferedReader;
19  import java.io.InputStreamReader;
20  import java.net.InetSocketAddress;
21  import java.util.concurrent.Executors;
22  
23  import org.jboss.netty.bootstrap.ClientBootstrap;
24  import org.jboss.netty.channel.Channel;
25  import org.jboss.netty.channel.ChannelFuture;
26  import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory;
27  import org.jboss.netty.example.telnet.TelnetClient;
28  
29  /**
30   * Simple SSL chat client modified from {@link TelnetClient}.
31   *
32   * @author <a href="http://www.jboss.org/netty/">The Netty Project</a>
33   * @author <a href="http://gleamynode.net/">Trustin Lee</a>
34   *
35   * @version $Rev: 2080 $, $Date: 2010-01-26 18:04:19 +0900 (Tue, 26 Jan 2010) $
36   *
37   */
38  public class SecureChatClient {
39  
40      public static void main(String[] args) throws Exception {
41          // Print usage if no argument is specified.
42          if (args.length != 2) {
43              System.err.println(
44                      "Usage: " + SecureChatClient.class.getSimpleName() +
45                      " <host> <port>");
46              return;
47          }
48  
49          // Parse options.
50          String host = args[0];
51          int port = Integer.parseInt(args[1]);
52  
53          // Configure the client.
54          ClientBootstrap bootstrap = new ClientBootstrap(
55                  new NioClientSocketChannelFactory(
56                          Executors.newCachedThreadPool(),
57                          Executors.newCachedThreadPool()));
58  
59          // Configure the pipeline factory.
60          bootstrap.setPipelineFactory(new SecureChatClientPipelineFactory());
61  
62          // Start the connection attempt.
63          ChannelFuture future = bootstrap.connect(new InetSocketAddress(host, port));
64  
65          // Wait until the connection attempt succeeds or fails.
66          Channel channel = future.awaitUninterruptibly().getChannel();
67          if (!future.isSuccess()) {
68              future.getCause().printStackTrace();
69              bootstrap.releaseExternalResources();
70              return;
71          }
72  
73          // Read commands from the stdin.
74          ChannelFuture lastWriteFuture = null;
75          BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
76          for (;;) {
77              String line = in.readLine();
78              if (line == null) {
79                  break;
80              }
81  
82              // Sends the received line to the server.
83              lastWriteFuture = channel.write(line + "\r\n");
84  
85              // If user typed the 'bye' command, wait until the server closes
86              // the connection.
87              if (line.toLowerCase().equals("bye")) {
88                  channel.getCloseFuture().awaitUninterruptibly();
89                  break;
90              }
91          }
92  
93          // Wait until all messages are flushed before closing the channel.
94          if (lastWriteFuture != null) {
95              lastWriteFuture.awaitUninterruptibly();
96          }
97  
98          // Close the connection.  Make sure the close operation ends because
99          // all I/O operations are asynchronous in Netty.
100         channel.close().awaitUninterruptibly();
101 
102         // Shut down all thread pools to exit.
103         bootstrap.releaseExternalResources();
104     }
105 }