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.imagine.step1.client;
021
022import org.apache.mina.core.RuntimeIoException;
023import org.apache.mina.core.future.ConnectFuture;
024import org.apache.mina.core.service.IoHandlerAdapter;
025import org.apache.mina.core.session.IoSession;
026import org.apache.mina.example.imagine.step1.ImageRequest;
027import org.apache.mina.example.imagine.step1.ImageResponse;
028import org.apache.mina.example.imagine.step1.server.ImageServer;
029import org.apache.mina.example.imagine.step1.codec.ImageCodecFactory;
030import org.apache.mina.filter.codec.ProtocolCodecFilter;
031import org.apache.mina.transport.socket.SocketConnector;
032import org.apache.mina.transport.socket.nio.NioSocketConnector;
033
034import java.net.InetSocketAddress;
035
036/**
037 * client for the {@link ImageServer}
038 *
039 * @author <a href="http://mina.apache.org">Apache MINA Project</a>
040 */
041public class ImageClient extends IoHandlerAdapter {
042    public static final int CONNECT_TIMEOUT = 3000;
043
044    private String host;
045    private int port;
046    private SocketConnector connector;
047    private IoSession session;
048    private ImageListener imageListener;
049
050    public ImageClient(String host, int port, ImageListener imageListener) {
051        this.host = host;
052        this.port = port;
053        this.imageListener = imageListener;
054        connector = new NioSocketConnector();
055        connector.getFilterChain().addLast("codec", new ProtocolCodecFilter(new ImageCodecFactory(true)));
056        connector.setHandler(this);
057    }
058
059    public boolean isConnected() {
060        return (session != null && session.isConnected());
061    }
062
063    public void connect() {
064        ConnectFuture connectFuture = connector.connect(new InetSocketAddress(host, port));
065        connectFuture.awaitUninterruptibly(CONNECT_TIMEOUT);
066        try {
067            session = connectFuture.getSession();
068        }
069        catch (RuntimeIoException e) {
070            imageListener.onException(e);
071        }
072    }
073
074    public void disconnect() {
075        if (session != null) {
076            session.closeNow().awaitUninterruptibly(CONNECT_TIMEOUT);
077            session = null;
078        }
079    }
080
081    public void sessionOpened(IoSession session) throws Exception {
082        imageListener.sessionOpened();
083    }
084
085    public void sessionClosed(IoSession session) throws Exception {
086        imageListener.sessionClosed();
087    }
088
089    public void sendRequest(ImageRequest imageRequest) {
090        if (session == null) {
091            //noinspection ThrowableInstanceNeverThrown
092            imageListener.onException(new Throwable("not connected"));
093        } else {
094            session.write(imageRequest);
095        }
096    }
097
098    public void messageReceived(IoSession session, Object message) throws Exception {
099        ImageResponse response = (ImageResponse) message;
100        imageListener.onImages(response.getImage1(), response.getImage2());
101    }
102
103    public void exceptionCaught(IoSession session, Throwable cause) throws Exception {
104        imageListener.onException(cause);
105    }
106
107}