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