001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: *
019: */
020: package org.apache.mina.example.chat.client;
021:
022: import java.net.SocketAddress;
023:
024: import javax.net.ssl.SSLContext;
025:
026: import org.apache.mina.common.ConnectFuture;
027: import org.apache.mina.common.IoHandler;
028: import org.apache.mina.common.IoSession;
029: import org.apache.mina.example.echoserver.ssl.BogusSslContextFactory;
030: import org.apache.mina.filter.ssl.SslFilter;
031: import org.apache.mina.filter.logging.MdcInjectionFilter;
032: import org.apache.mina.transport.socket.nio.NioSocketConnector;
033:
034: /**
035: * A simple chat client for a given user.
036: *
037: * @author The Apache MINA Project (dev@mina.apache.org)
038: * @version $Rev$, $Date$
039: */
040: public class ChatClientSupport {
041: private final IoHandler handler;
042:
043: private final String name;
044:
045: private IoSession session;
046:
047: public ChatClientSupport(String name, IoHandler handler) {
048: if (name == null) {
049: throw new IllegalArgumentException("Name can not be null");
050: }
051: this .name = name;
052: this .handler = handler;
053: }
054:
055: public boolean connect(NioSocketConnector connector,
056: SocketAddress address, boolean useSsl) {
057: if (session != null && session.isConnected()) {
058: throw new IllegalStateException(
059: "Already connected. Disconnect first.");
060: }
061:
062: try {
063: connector.getFilterChain().addLast("mdc",
064: new MdcInjectionFilter());
065:
066: if (useSsl) {
067: SSLContext sslContext = BogusSslContextFactory
068: .getInstance(false);
069: SslFilter sslFilter = new SslFilter(sslContext);
070: sslFilter.setUseClientMode(true);
071: connector.getFilterChain().addLast("sslFilter",
072: sslFilter);
073: }
074:
075: connector.setHandler(handler);
076: ConnectFuture future1 = connector.connect(address);
077: future1.awaitUninterruptibly();
078: if (!future1.isConnected()) {
079: return false;
080: }
081: session = future1.getSession();
082: login();
083:
084: return true;
085: } catch (Exception e) {
086: return false;
087: }
088: }
089:
090: public void login() {
091: session.write("LOGIN " + name);
092: }
093:
094: public void broadcast(String message) {
095: session.write("BROADCAST " + message);
096: }
097:
098: public void quit() {
099: if (session != null) {
100: if (session.isConnected()) {
101: session.write("QUIT");
102: // Wait until the chat ends.
103: session.getCloseFuture().awaitUninterruptibly();
104: }
105: session.close();
106: }
107: }
108:
109: }
|