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