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.core.future;
21  
22  import org.apache.mina.core.session.IoSession;
23  
24  /**
25   * An {@link IoFuture} for {@link IoSession#read() asynchronous read requests}. 
26   *
27   * <h3>Example</h3>
28   * <pre>
29   * IoSession session = ...;
30   * // useReadOperation must be enabled to use read operation.
31   * session.getConfig().setUseReadOperation(true);
32   * 
33   * ReadFuture future = session.read();
34   * // Wait until a message is received.
35   * future.await();
36   * try {
37   *     Object message = future.getMessage();
38   * } catch (Exception e) {
39   *     ...
40   * }
41   * </pre>
42   *
43   * @author The Apache MINA Project (dev@mina.apache.org)
44   * @version $Rev: 671827 $, $Date: 2008-06-26 10:49:48 +0200 (jeu, 26 jun 2008) $
45   */
46  public interface ReadFuture extends IoFuture {
47      
48      /**
49       * Returns the received message.  It returns <tt>null</tt> if this
50       * future is not ready or the associated {@link IoSession} has been closed. 
51       * 
52       * @throws RuntimeException if read or any relevant operation has failed.
53       */
54      Object getMessage();
55      
56      /**
57       * Returns <tt>true</tt> if a message was received successfully.
58       */
59      boolean isRead();
60      
61      /**
62       * Returns <tt>true</tt> if the {@link IoSession} associated with this
63       * future has been closed.
64       */
65      boolean isClosed();
66      
67      /**
68       * Returns the cause of the read failure if and only if the read
69       * operation has failed due to an {@link Exception}.  Otherwise,
70       * <tt>null</tt> is returned.
71       */
72      Throwable getException();
73  
74      /**
75       * Sets the message is written, and notifies all threads waiting for
76       * this future.  This method is invoked by MINA internally.  Please do
77       * not call this method directly.
78       */
79      void setRead(Object message);
80      
81      /**
82       * Sets the associated {@link IoSession} is closed.  This method is invoked
83       * by MINA internally.  Please do not call this method directly.
84       */
85      void setClosed();
86      
87      /**
88       * Sets the cause of the read failure, and notifies all threads waiting
89       * for this future.  This method is invoked by MINA internally.  Please
90       * do not call this method directly.
91       */
92      void setException(Throwable cause);
93  
94      ReadFuture await() throws InterruptedException;
95  
96      ReadFuture awaitUninterruptibly();
97  
98      ReadFuture addListener(IoFutureListener<?> listener);
99  
100     ReadFuture removeListener(IoFutureListener<?> listener);
101 }