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.imagine.step1.client;
21  
22  import org.apache.mina.core.RuntimeIoException;
23  import org.apache.mina.core.future.ConnectFuture;
24  import org.apache.mina.core.service.IoHandlerAdapter;
25  import org.apache.mina.core.session.IoSession;
26  import org.apache.mina.example.imagine.step1.ImageRequest;
27  import org.apache.mina.example.imagine.step1.ImageResponse;
28  import org.apache.mina.example.imagine.step1.server.ImageServer;
29  import org.apache.mina.example.imagine.step1.codec.ImageCodecFactory;
30  import org.apache.mina.filter.codec.ProtocolCodecFilter;
31  import org.apache.mina.transport.socket.SocketConnector;
32  import org.apache.mina.transport.socket.nio.NioSocketConnector;
33  
34  import java.net.InetSocketAddress;
35  
36  /**
37   * client for the {@link ImageServer}
38   *
39   * @author The Apache MINA Project (dev@mina.apache.org)
40   * @version $Rev: 597940 $, $Date: 2007-11-25 02:00:09 +0100 (Sun, 25 Nov 2007) $
41   */
42  public class ImageClient extends IoHandlerAdapter {
43      public static final int CONNECT_TIMEOUT = 3000;
44  
45      private String host;
46      private int port;
47      private SocketConnector connector;
48      private IoSession session;
49      private ImageListener imageListener;
50  
51      public ImageClient(String host, int port, ImageListener imageListener) {
52          this.host = host;
53          this.port = port;
54          this.imageListener = imageListener;
55          connector = new NioSocketConnector();
56          connector.getFilterChain().addLast("codec", new ProtocolCodecFilter(new ImageCodecFactory(true)));
57          connector.setHandler(this);
58      }
59  
60      public boolean isConnected() {
61          return (session != null && session.isConnected());
62      }
63  
64      public void connect() {
65          ConnectFuture connectFuture = connector.connect(new InetSocketAddress(host, port));
66          connectFuture.awaitUninterruptibly(CONNECT_TIMEOUT);
67          try {
68              session = connectFuture.getSession();
69          }
70          catch (RuntimeIoException e) {
71              imageListener.onException(e);
72          }
73      }
74  
75      public void disconnect() {
76          if (session != null) {
77              session.close().awaitUninterruptibly(CONNECT_TIMEOUT);
78              session = null;
79          }
80      }
81  
82      public void sessionOpened(IoSession session) throws Exception {
83          imageListener.sessionOpened();
84      }
85  
86      public void sessionClosed(IoSession session) throws Exception {
87          imageListener.sessionClosed();
88      }
89  
90      public void sendRequest(ImageRequest imageRequest) {
91          if (session == null) {
92              //noinspection ThrowableInstanceNeverThrown
93              imageListener.onException(new Throwable("not connected"));
94          } else {
95              session.write(imageRequest);
96          }
97      }
98  
99      public void messageReceived(IoSession session, Object message) throws Exception {
100         ImageResponse response = (ImageResponse) message;
101         imageListener.onImages(response.getImage1(), response.getImage2());
102     }
103 
104     public void exceptionCaught(IoSession session, Throwable cause) throws Exception {
105         imageListener.onException(cause);
106     }
107 
108 }