View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License.
18   *
19   */
20  package org.apache.mina.example.chat.client;
21  
22  import java.net.SocketAddress;
23  
24  import javax.net.ssl.SSLContext;
25  
26  import org.apache.mina.core.filterchain.IoFilter;
27  import org.apache.mina.core.future.ConnectFuture;
28  import org.apache.mina.core.service.IoHandler;
29  import org.apache.mina.core.session.IoSession;
30  import org.apache.mina.example.echoserver.ssl.BogusSslContextFactory;
31  import org.apache.mina.filter.ssl.SslFilter;
32  import org.apache.mina.filter.codec.ProtocolCodecFilter;
33  import org.apache.mina.filter.codec.textline.TextLineCodecFactory;
34  import org.apache.mina.filter.logging.LoggingFilter;
35  import org.apache.mina.filter.logging.MdcInjectionFilter;
36  import org.apache.mina.transport.socket.nio.NioSocketConnector;
37  
38  /**
39   * A simple chat client for a given user.
40   *
41   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
42   */
43  public class ChatClientSupport {
44      private final IoHandler handler;
45  
46      private final String name;
47  
48      private IoSession session;
49  
50      public ChatClientSupport(String name, IoHandler handler) {
51          if (name == null) {
52              throw new IllegalArgumentException("Name can not be null");
53          }
54          this.name = name;
55          this.handler = handler;
56      }
57  
58      public boolean connect(NioSocketConnector connector, SocketAddress address,
59              boolean useSsl) {
60          if (session != null && session.isConnected()) {
61              throw new IllegalStateException(
62                      "Already connected. Disconnect first.");
63          }
64  
65          try {
66              IoFilter LOGGING_FILTER = new LoggingFilter();
67  
68              IoFilter CODEC_FILTER = new ProtocolCodecFilter(
69                      new TextLineCodecFactory());
70              
71              connector.getFilterChain().addLast("mdc", new MdcInjectionFilter());
72              connector.getFilterChain().addLast("codec", CODEC_FILTER);
73              connector.getFilterChain().addLast("logger", LOGGING_FILTER);
74  
75              if (useSsl) {
76                  SSLContext sslContext = BogusSslContextFactory
77                          .getInstance(false);
78                  SslFilter sslFilter = new SslFilter(sslContext);
79                  sslFilter.setUseClientMode(true);
80                  connector.getFilterChain().addFirst("sslFilter", sslFilter);
81              }
82  
83              connector.setHandler(handler);
84              ConnectFuture future1 = connector.connect(address);
85              future1.awaitUninterruptibly();
86              if (!future1.isConnected()) {
87                  return false;
88              }
89              session = future1.getSession();
90              login();
91  
92              return true;
93          } catch (Exception e) {
94              return false;
95          }
96      }
97  
98      public void login() {
99          session.write("LOGIN " + name);
100     }
101 
102     public void broadcast(String message) {
103         session.write("BROADCAST " + message);
104     }
105 
106     public void quit() {
107         if (session != null) {
108             if (session.isConnected()) {
109                 session.write("QUIT");
110                 // Wait until the chat ends.
111                 session.getCloseFuture().awaitUninterruptibly();
112             }
113             session.close(true);
114         }
115     }
116 
117 }