001/*
002 * ====================================================================
003 * Licensed to the Apache Software Foundation (ASF) under one
004 * or more contributor license agreements.  See the NOTICE file
005 * distributed with this work for additional information
006 * regarding copyright ownership.  The ASF licenses this file
007 * to you under the Apache License, Version 2.0 (the
008 * "License"); you may not use this file except in compliance
009 * with the License.  You may obtain a copy of the License at
010 *
011 *   http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing,
014 * software distributed under the License is distributed on an
015 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
016 * KIND, either express or implied.  See the License for the
017 * specific language governing permissions and limitations
018 * under the License.
019 * ====================================================================
020 *
021 * This software consists of voluntary contributions made by many
022 * individuals on behalf of the Apache Software Foundation.  For more
023 * information on the Apache Software Foundation, please see
024 * <http://www.apache.org/>.
025 *
026 */
027
028package org.apache.http.nio.reactor;
029
030import java.io.IOException;
031import java.net.SocketAddress;
032
033/**
034 * SessionRequest interface represents a request to establish a new connection
035 * (or session) to a remote host. It can be used to monitor the status of the
036 * request, to block waiting for its completion, or to cancel the request.
037 * <p>
038 * Implementations of this interface are expected to be threading safe.
039 *
040 * @since 4.0
041 */
042public interface SessionRequest {
043
044    /**
045     * Returns socket address of the remote host.
046     *
047     * @return socket address of the remote host
048     */
049    SocketAddress getRemoteAddress();
050
051    /**
052     * Returns local socket address.
053     *
054     * @return local socket address.
055     */
056    SocketAddress getLocalAddress();
057
058    /**
059     * Returns attachment object will be added to the session's context upon
060     * initialization. This object can be used to pass an initial processing
061     * state to the protocol handler.
062     *
063     * @return attachment object.
064     */
065    Object getAttachment();
066
067    /**
068     * Determines whether the request has been completed (either successfully
069     * or unsuccessfully).
070     *
071     * @return {@code true} if the request has been completed,
072     *  {@code false} if still pending.
073     */
074    boolean isCompleted();
075
076    /**
077     * Returns {@link IOSession} instance created as a result of this request
078     * or {@code null} if the request is still pending.
079     *
080     * @return I/O session or {@code null} if the request is still pending.
081     */
082    IOSession getSession();
083
084    /**
085     * Returns {@link IOException} instance if the request could not be
086     * successfully executed due to an I/O error or {@code null} if no
087     * error occurred to this point.
088     *
089     * @return I/O exception or {@code null} if no error occurred to
090     * this point.
091     */
092    IOException getException();
093
094    /**
095     * Waits for completion of this session request.
096     *
097     * @throws InterruptedException in case the execution process was
098     *   interrupted.
099     */
100    void waitFor() throws InterruptedException;
101
102    /**
103     * Sets connect timeout value in milliseconds.
104     *
105     * @param timeout connect timeout value in milliseconds.
106     */
107    void setConnectTimeout(int timeout);
108
109    /**
110     * Returns connect timeout value in milliseconds.
111     *
112     * @return connect timeout value in milliseconds.
113     */
114    int getConnectTimeout();
115
116    /**
117     * Cancels the request. Invocation of this method will set the status of
118     * the request to completed and will unblock threads blocked in
119     * the {{@link #waitFor()}} method.
120     */
121    void cancel();
122
123}