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.future.ConnectFuture;
27  import org.apache.mina.core.service.IoHandler;
28  import org.apache.mina.core.session.IoSession;
29  import org.apache.mina.example.echoserver.ssl.BogusSslContextFactory;
30  import org.apache.mina.filter.ssl.SslFilter;
31  import org.apache.mina.filter.logging.MdcInjectionFilter;
32  import org.apache.mina.transport.socket.nio.NioSocketConnector;
33  
34  /**
35   * A simple chat client for a given user.
36   *
37   * @author The Apache MINA Project (dev@mina.apache.org)
38   * @version $Rev$, $Date$
39   */
40  public class ChatClientSupport {
41      private final IoHandler handler;
42  
43      private final String name;
44  
45      private IoSession session;
46  
47      public ChatClientSupport(String name, IoHandler handler) {
48          if (name == null) {
49              throw new IllegalArgumentException("Name can not be null");
50          }
51          this.name = name;
52          this.handler = handler;
53      }
54  
55      public boolean connect(NioSocketConnector connector, SocketAddress address,
56              boolean useSsl) {
57          if (session != null && session.isConnected()) {
58              throw new IllegalStateException(
59                      "Already connected. Disconnect first.");
60          }
61  
62          try {
63              connector.getFilterChain().addLast("mdc", new MdcInjectionFilter());
64  
65              if (useSsl) {
66                  SSLContext sslContext = BogusSslContextFactory
67                          .getInstance(false);
68                  SslFilter sslFilter = new SslFilter(sslContext);
69                  sslFilter.setUseClientMode(true);
70                  connector.getFilterChain().addLast("sslFilter", sslFilter);
71              }
72  
73              connector.setHandler(handler);
74              ConnectFuture future1 = connector.connect(address);
75              future1.awaitUninterruptibly();
76              if (!future1.isConnected()) {
77                  return false;
78              }
79              session = future1.getSession();
80              login();
81  
82              return true;
83          } catch (Exception e) {
84              return false;
85          }
86      }
87  
88      public void login() {
89          session.write("LOGIN " + name);
90      }
91  
92      public void broadcast(String message) {
93          session.write("BROADCAST " + message);
94      }
95  
96      public void quit() {
97          if (session != null) {
98              if (session.isConnected()) {
99                  session.write("QUIT");
100                 // Wait until the chat ends.
101                 session.getCloseFuture().awaitUninterruptibly();
102             }
103             session.close();
104         }
105     }
106 
107 }