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.compression.CompressionFilter;
35  import org.apache.mina.filter.logging.LoggingFilter;
36  import org.apache.mina.filter.logging.MdcInjectionFilter;
37  import org.apache.mina.transport.socket.nio.NioSocketConnector;
38  
39  /**
40   * A simple chat client for a given user.
41   *
42   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
43   */
44  public class ChatClientSupport {
45      private final IoHandler handler;
46  
47      private final String name;
48  
49      private IoSession session;
50  
51      public ChatClientSupport(String name, IoHandler handler) {
52          if (name == null) {
53              throw new IllegalArgumentException("Name can not be null");
54          }
55          this.name = name;
56          this.handler = handler;
57      }
58  
59      public boolean connect(NioSocketConnector connector, SocketAddress address,
60              boolean useSsl) {
61          if (session != null && session.isConnected()) {
62              throw new IllegalStateException(
63                      "Already connected. Disconnect first.");
64          }
65  
66          try {
67              IoFilter LOGGING_FILTER = new LoggingFilter();
68  
69              IoFilter CODEC_FILTER = new ProtocolCodecFilter(
70                      new TextLineCodecFactory());
71              
72              connector.getFilterChain().addLast("mdc", new MdcInjectionFilter());
73              
74              // Add the compression filter
75              connector.getFilterChain().addLast( "Compression", new CompressionFilter() );
76              connector.getFilterChain().addLast("codec", CODEC_FILTER);
77              connector.getFilterChain().addLast("logger", LOGGING_FILTER);
78  
79              if (useSsl) {
80                  SSLContext sslContext = BogusSslContextFactory
81                          .getInstance(false);
82                  SslFilterslFilter.html#SslFilter">SslFilter sslFilter = new SslFilter(sslContext);
83                  sslFilter.setUseClientMode(true);
84                  connector.getFilterChain().addFirst("sslFilter", sslFilter);
85              }
86  
87              connector.setHandler(handler);
88              ConnectFuture future1 = connector.connect(address);
89              future1.awaitUninterruptibly();
90              if (!future1.isConnected()) {
91                  return false;
92              }
93              session = future1.getSession();
94              login();
95  
96              return true;
97          } catch (Exception e) {
98              return false;
99          }
100     }
101 
102     public void login() {
103         session.write("LOGIN " + name);
104     }
105 
106     public void broadcast(String message) {
107         try {
108             session.write("BROADCAST " + message);
109         } catch ( Exception e ) {
110             e.printStackTrace();
111         }
112     }
113 
114     public void quit() {
115         if (session != null) {
116             if (session.isConnected()) {
117                 session.write("QUIT");
118                 // Wait until the chat ends.
119                 session.getCloseFuture().awaitUninterruptibly();
120             }
121             session.closeNow();
122         }
123     }
124 
125 }