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   */
41  public class ImageClient extends IoHandlerAdapter {
42      public static final int CONNECT_TIMEOUT = 3000;
43  
44      private String host;
45      private int port;
46      private SocketConnector connector;
47      private IoSession session;
48      private ImageListener imageListener;
49  
50      public ImageClient(String host, int port, ImageListener imageListener) {
51          this.host = host;
52          this.port = port;
53          this.imageListener = imageListener;
54          connector = new NioSocketConnector();
55          connector.getFilterChain().addLast("codec", new ProtocolCodecFilter(new ImageCodecFactory(true)));
56          connector.setHandler(this);
57      }
58  
59      public boolean isConnected() {
60          return (session != null && session.isConnected());
61      }
62  
63      public void connect() {
64          ConnectFuture connectFuture = connector.connect(new InetSocketAddress(host, port));
65          connectFuture.awaitUninterruptibly(CONNECT_TIMEOUT);
66          try {
67              session = connectFuture.getSession();
68          }
69          catch (RuntimeIoException e) {
70              imageListener.onException(e);
71          }
72      }
73  
74      public void disconnect() {
75          if (session != null) {
76              session.close(true).awaitUninterruptibly(CONNECT_TIMEOUT);
77              session = null;
78          }
79      }
80  
81      public void sessionOpened(IoSession session) throws Exception {
82          imageListener.sessionOpened();
83      }
84  
85      public void sessionClosed(IoSession session) throws Exception {
86          imageListener.sessionClosed();
87      }
88  
89      public void sendRequest(ImageRequest imageRequest) {
90          if (session == null) {
91              //noinspection ThrowableInstanceNeverThrown
92              imageListener.onException(new Throwable("not connected"));
93          } else {
94              session.write(imageRequest);
95          }
96      }
97  
98      public void messageReceived(IoSession session, Object message) throws Exception {
99          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 }