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 */
020package org.apache.mina.example.chat.client;
021
022import java.net.SocketAddress;
023
024import javax.net.ssl.SSLContext;
025
026import org.apache.mina.core.filterchain.IoFilter;
027import org.apache.mina.core.future.ConnectFuture;
028import org.apache.mina.core.service.IoHandler;
029import org.apache.mina.core.session.IoSession;
030import org.apache.mina.example.echoserver.ssl.BogusSslContextFactory;
031import org.apache.mina.filter.ssl.SslFilter;
032import org.apache.mina.filter.codec.ProtocolCodecFilter;
033import org.apache.mina.filter.codec.textline.TextLineCodecFactory;
034import org.apache.mina.filter.logging.LoggingFilter;
035import org.apache.mina.filter.logging.MdcInjectionFilter;
036import org.apache.mina.transport.socket.nio.NioSocketConnector;
037
038/**
039 * A simple chat client for a given user.
040 *
041 * @author <a href="http://mina.apache.org">Apache MINA Project</a>
042 */
043public class ChatClientSupport {
044    private final IoHandler handler;
045
046    private final String name;
047
048    private IoSession session;
049
050    public ChatClientSupport(String name, IoHandler handler) {
051        if (name == null) {
052            throw new IllegalArgumentException("Name can not be null");
053        }
054        this.name = name;
055        this.handler = handler;
056    }
057
058    public boolean connect(NioSocketConnector connector, SocketAddress address,
059            boolean useSsl) {
060        if (session != null && session.isConnected()) {
061            throw new IllegalStateException(
062                    "Already connected. Disconnect first.");
063        }
064
065        try {
066            IoFilter LOGGING_FILTER = new LoggingFilter();
067
068            IoFilter CODEC_FILTER = new ProtocolCodecFilter(
069                    new TextLineCodecFactory());
070            
071            connector.getFilterChain().addLast("mdc", new MdcInjectionFilter());
072            connector.getFilterChain().addLast("codec", CODEC_FILTER);
073            connector.getFilterChain().addLast("logger", LOGGING_FILTER);
074
075            if (useSsl) {
076                SSLContext sslContext = BogusSslContextFactory
077                        .getInstance(false);
078                SslFilter sslFilter = new SslFilter(sslContext);
079                sslFilter.setUseClientMode(true);
080                connector.getFilterChain().addFirst("sslFilter", sslFilter);
081            }
082
083            connector.setHandler(handler);
084            ConnectFuture future1 = connector.connect(address);
085            future1.awaitUninterruptibly();
086            if (!future1.isConnected()) {
087                return false;
088            }
089            session = future1.getSession();
090            login();
091
092            return true;
093        } catch (Exception e) {
094            return false;
095        }
096    }
097
098    public void login() {
099        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}