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 The Apache MINA Project (dev@mina.apache.org)
42   * @version $Rev$, $Date$
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              connector.getFilterChain().addLast("codec", CODEC_FILTER);
74              connector.getFilterChain().addLast("logger", LOGGING_FILTER);
75  
76              if (useSsl) {
77                  SSLContext sslContext = BogusSslContextFactory
78                          .getInstance(false);
79                  SslFilter sslFilter = new SslFilter(sslContext);
80                  sslFilter.setUseClientMode(true);
81                  connector.getFilterChain().addLast("sslFilter", sslFilter);
82              }
83  
84              connector.setHandler(handler);
85              ConnectFuture future1 = connector.connect(address);
86              future1.awaitUninterruptibly();
87              if (!future1.isConnected()) {
88                  return false;
89              }
90              session = future1.getSession();
91              login();
92  
93              return true;
94          } catch (Exception e) {
95              return false;
96          }
97      }
98  
99      public void login() {
100         session.write("LOGIN " + name);
101     }
102 
103     public void broadcast(String message) {
104         session.write("BROADCAST " + message);
105     }
106 
107     public void quit() {
108         if (session != null) {
109             if (session.isConnected()) {
110                 session.write("QUIT");
111                 // Wait until the chat ends.
112                 session.getCloseFuture().awaitUninterruptibly();
113             }
114             session.close(true);
115         }
116     }
117 
118 }