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   * 
31   * // useReadOperation must be enabled to use read operation.
32   * session.getConfig().setUseReadOperation(true);
33   * 
34   * ReadFuture future = session.read();
35   * 
36   * // Wait until a message is received.
37   * future.awaitUninterruptibly();
38   * 
39   * try {
40   *     Object message = future.getMessage();
41   * } catch (Exception e) {
42   *     ...
43   * }
44   * </pre>
45   *
46   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
47   */
48  public interface ReadFuture extends IoFuture {
49  
50      /**
51       * Get the read message.
52       * 
53       * @return the received message.  It returns <tt>null</tt> if this
54       * future is not ready or the associated {@link IoSession} has been closed. 
55       */
56      Object getMessage();
57  
58      /**
59       * @return <tt>true</tt> if a message was received successfully.
60       */
61      boolean isRead();
62  
63      /**
64       * @return <tt>true</tt> if the {@link IoSession} associated with this
65       * future has been closed.
66       */
67      boolean isClosed();
68  
69      /**
70       * @return the cause of the read failure if and only if the read
71       * operation has failed due to an {@link Exception}.  Otherwise,
72       * <tt>null</tt> is returned.
73       */
74      Throwable getException();
75  
76      /**
77       * Sets the message is written, and notifies all threads waiting for
78       * this future.  This method is invoked by MINA internally.  Please do
79       * not call this method directly.
80       * 
81       * @param message The received message to store in this future
82       */
83      void setRead(Object message);
84  
85      /**
86       * Sets the associated {@link IoSession} is closed.  This method is invoked
87       * by MINA internally.  Please do not call this method directly.
88       */
89      void setClosed();
90  
91      /**
92       * Sets the cause of the read failure, and notifies all threads waiting
93       * for this future.  This method is invoked by MINA internally.  Please
94       * do not call this method directly.
95       * 
96       * @param cause The exception to store in the Future instance
97       */
98      void setException(Throwable cause);
99  
100     /**
101      * {@inheritDoc}
102      */
103     ReadFuture await() throws InterruptedException;
104 
105     /**
106      * {@inheritDoc}
107      */
108     ReadFuture awaitUninterruptibly();
109 
110     /**
111      * {@inheritDoc}
112      */
113     ReadFuture addListener(IoFutureListener<?> listener);
114 
115     /**
116      * {@inheritDoc}
117      */
118     ReadFuture removeListener(IoFutureListener<?> listener);
119 }