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.awaitUninterruptibly(); // 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 The {link IoSession} instance that has been associated with the connection, 43 * if the connection was successful, {@code null} otherwise 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, otherwise returns 52 * teh cause of the exception 53 */ 54 Throwable getException(); 55 56 /** 57 * @return {@code true} if the connect operation is finished successfully. 58 */ 59 boolean isConnected(); 60 61 /** 62 * @return {@code true} if the connect operation has been canceled by 63 * {@link #cancel()} method. 64 */ 65 boolean isCanceled(); 66 67 /** 68 * Sets the newly connected session and notifies all threads waiting for 69 * this future. This method is invoked by MINA internally. Please do not 70 * call this method directly. 71 * 72 * @param session The created session to store in the ConnectFuture insteance 73 */ 74 void setSession(IoSession session); 75 76 /** 77 * Sets the exception caught due to connection failure and notifies all 78 * threads waiting for this future. This method is invoked by MINA 79 * internally. Please do not call this method directly. 80 * 81 * @param exception The exception to store in the ConnectFuture instance 82 */ 83 void setException(Throwable exception); 84 85 /** 86 * Cancels the connection attempt and notifies all threads waiting for 87 * this future. 88 * 89 * @return {@code true} if the future has been cancelled by this call, {@code false} 90 * if the future was already cancelled. 91 */ 92 boolean cancel(); 93 94 /** 95 * {@inheritDoc} 96 */ 97 ConnectFuture await() throws InterruptedException; 98 99 /** 100 * {@inheritDoc} 101 */ 102 ConnectFuture awaitUninterruptibly(); 103 104 /** 105 * {@inheritDoc} 106 */ 107 ConnectFuture addListener(IoFutureListener<?> listener); 108 109 /** 110 * {@inheritDoc} 111 */ 112 ConnectFuture removeListener(IoFutureListener<?> listener); 113 }