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 asynchronous connect requests.
26   *
27   * <h3>Example</h3>
28   * <pre>
29   * IoConnector connector = ...;
30   * ConnectFuture future = connector.connect(...);
31   * future.join(); // Wait until the connection attempt is finished.
32   * IoSession session = future.getSession();
33   * session.write(...);
34   * </pre>
35   *
36   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
37   */
38  public interface ConnectFuture extends IoFuture {
39      /**
40       * Returns {@link IoSession} which is the result of connect operation.
41       *
42       * @return <tt>null</tt> if the connect operation is not finished yet
43       * @throws RuntimeException if connection attempt failed by an exception
44       */
45      IoSession getSession();
46  
47      /**
48       * Returns the cause of the connection failure.
49       *
50       * @return <tt>null</tt> if the connect operation is not finished yet,
51       *         or if the connection attempt is successful.
52       */
53      Throwable getException();
54  
55      /**
56       * Returns <tt>true</tt> if the connect operation is finished successfully.
57       */
58      boolean isConnected();
59  
60      /**
61       * Returns {@code true} if the connect operation has been canceled by
62       * {@link #cancel()} method.
63       */
64      boolean isCanceled();
65  
66      /**
67       * Sets the newly connected session and notifies all threads waiting for
68       * this future.  This method is invoked by MINA internally.  Please do not
69       * call this method directly.
70       */
71      void setSession(IoSession session);
72  
73      /**
74       * Sets the exception caught due to connection failure and notifies all
75       * threads waiting for this future.  This method is invoked by MINA
76       * internally.  Please do not call this method directly.
77       */
78      void setException(Throwable exception);
79  
80      /**
81       * Cancels the connection attempt and notifies all threads waiting for
82       * this future.
83       */
84      void cancel();
85  
86      ConnectFuture await() throws InterruptedException;
87  
88      ConnectFuture awaitUninterruptibly();
89  
90      ConnectFuture addListener(IoFutureListener<?> listener);
91  
92      ConnectFuture removeListener(IoFutureListener<?> listener);
93  }