103928aee4356845252ac6b662d5c72c29903813eJake Slack//
203928aee4356845252ac6b662d5c72c29903813eJake Slack//  ========================================================================
303928aee4356845252ac6b662d5c72c29903813eJake Slack//  Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd.
403928aee4356845252ac6b662d5c72c29903813eJake Slack//  ------------------------------------------------------------------------
503928aee4356845252ac6b662d5c72c29903813eJake Slack//  All rights reserved. This program and the accompanying materials
603928aee4356845252ac6b662d5c72c29903813eJake Slack//  are made available under the terms of the Eclipse Public License v1.0
703928aee4356845252ac6b662d5c72c29903813eJake Slack//  and Apache License v2.0 which accompanies this distribution.
803928aee4356845252ac6b662d5c72c29903813eJake Slack//
903928aee4356845252ac6b662d5c72c29903813eJake Slack//      The Eclipse Public License is available at
1003928aee4356845252ac6b662d5c72c29903813eJake Slack//      http://www.eclipse.org/legal/epl-v10.html
1103928aee4356845252ac6b662d5c72c29903813eJake Slack//
1203928aee4356845252ac6b662d5c72c29903813eJake Slack//      The Apache License v2.0 is available at
1303928aee4356845252ac6b662d5c72c29903813eJake Slack//      http://www.opensource.org/licenses/apache2.0.php
1403928aee4356845252ac6b662d5c72c29903813eJake Slack//
1503928aee4356845252ac6b662d5c72c29903813eJake Slack//  You may elect to redistribute this code under either of these licenses.
1603928aee4356845252ac6b662d5c72c29903813eJake Slack//  ========================================================================
1703928aee4356845252ac6b662d5c72c29903813eJake Slack//
1803928aee4356845252ac6b662d5c72c29903813eJake Slack
1903928aee4356845252ac6b662d5c72c29903813eJake Slackpackage org.eclipse.jetty.io;
2003928aee4356845252ac6b662d5c72c29903813eJake Slack
2103928aee4356845252ac6b662d5c72c29903813eJake Slackimport java.io.IOException;
2203928aee4356845252ac6b662d5c72c29903813eJake Slack
2303928aee4356845252ac6b662d5c72c29903813eJake Slack
2403928aee4356845252ac6b662d5c72c29903813eJake Slack/**
2503928aee4356845252ac6b662d5c72c29903813eJake Slack *
2603928aee4356845252ac6b662d5c72c29903813eJake Slack * A transport EndPoint
2703928aee4356845252ac6b662d5c72c29903813eJake Slack */
2803928aee4356845252ac6b662d5c72c29903813eJake Slackpublic interface EndPoint
2903928aee4356845252ac6b662d5c72c29903813eJake Slack{
3003928aee4356845252ac6b662d5c72c29903813eJake Slack    /**
3103928aee4356845252ac6b662d5c72c29903813eJake Slack     * Shutdown any backing output stream associated with the endpoint
3203928aee4356845252ac6b662d5c72c29903813eJake Slack     */
3303928aee4356845252ac6b662d5c72c29903813eJake Slack    void shutdownOutput() throws IOException;
3403928aee4356845252ac6b662d5c72c29903813eJake Slack
3503928aee4356845252ac6b662d5c72c29903813eJake Slack    boolean isOutputShutdown();
3603928aee4356845252ac6b662d5c72c29903813eJake Slack
3703928aee4356845252ac6b662d5c72c29903813eJake Slack    /**
3803928aee4356845252ac6b662d5c72c29903813eJake Slack     * Shutdown any backing input stream associated with the endpoint
3903928aee4356845252ac6b662d5c72c29903813eJake Slack     */
4003928aee4356845252ac6b662d5c72c29903813eJake Slack    void shutdownInput() throws IOException;
4103928aee4356845252ac6b662d5c72c29903813eJake Slack
4203928aee4356845252ac6b662d5c72c29903813eJake Slack    boolean isInputShutdown();
4303928aee4356845252ac6b662d5c72c29903813eJake Slack
4403928aee4356845252ac6b662d5c72c29903813eJake Slack    /**
4503928aee4356845252ac6b662d5c72c29903813eJake Slack     * Close any backing stream associated with the endpoint
4603928aee4356845252ac6b662d5c72c29903813eJake Slack     */
4703928aee4356845252ac6b662d5c72c29903813eJake Slack    void close() throws IOException;
4803928aee4356845252ac6b662d5c72c29903813eJake Slack
4903928aee4356845252ac6b662d5c72c29903813eJake Slack    /**
5003928aee4356845252ac6b662d5c72c29903813eJake Slack     * Fill the buffer from the current putIndex to it's capacity from whatever
5103928aee4356845252ac6b662d5c72c29903813eJake Slack     * byte source is backing the buffer. The putIndex is increased if bytes filled.
5203928aee4356845252ac6b662d5c72c29903813eJake Slack     * The buffer may chose to do a compact before filling.
5303928aee4356845252ac6b662d5c72c29903813eJake Slack     * @return an <code>int</code> value indicating the number of bytes
5403928aee4356845252ac6b662d5c72c29903813eJake Slack     * filled or -1 if EOF is reached.
5503928aee4356845252ac6b662d5c72c29903813eJake Slack     * @throws EofException If input is shutdown or the endpoint is closed.
5603928aee4356845252ac6b662d5c72c29903813eJake Slack     */
5703928aee4356845252ac6b662d5c72c29903813eJake Slack    int fill(Buffer buffer) throws IOException;
5803928aee4356845252ac6b662d5c72c29903813eJake Slack
5903928aee4356845252ac6b662d5c72c29903813eJake Slack
6003928aee4356845252ac6b662d5c72c29903813eJake Slack    /**
6103928aee4356845252ac6b662d5c72c29903813eJake Slack     * Flush the buffer from the current getIndex to it's putIndex using whatever byte
6203928aee4356845252ac6b662d5c72c29903813eJake Slack     * sink is backing the buffer. The getIndex is updated with the number of bytes flushed.
6303928aee4356845252ac6b662d5c72c29903813eJake Slack     * Any mark set is cleared.
6403928aee4356845252ac6b662d5c72c29903813eJake Slack     * If the entire contents of the buffer are flushed, then an implicit empty() is done.
6503928aee4356845252ac6b662d5c72c29903813eJake Slack     *
6603928aee4356845252ac6b662d5c72c29903813eJake Slack     * @param buffer The buffer to flush. This buffers getIndex is updated.
6703928aee4356845252ac6b662d5c72c29903813eJake Slack     * @return  the number of bytes written
6803928aee4356845252ac6b662d5c72c29903813eJake Slack     * @throws EofException If the endpoint is closed or output is shutdown.
6903928aee4356845252ac6b662d5c72c29903813eJake Slack     */
7003928aee4356845252ac6b662d5c72c29903813eJake Slack    int flush(Buffer buffer) throws IOException;
7103928aee4356845252ac6b662d5c72c29903813eJake Slack
7203928aee4356845252ac6b662d5c72c29903813eJake Slack    /**
7303928aee4356845252ac6b662d5c72c29903813eJake Slack     * Flush the buffer from the current getIndex to it's putIndex using whatever byte
7403928aee4356845252ac6b662d5c72c29903813eJake Slack     * sink is backing the buffer. The getIndex is updated with the number of bytes flushed.
7503928aee4356845252ac6b662d5c72c29903813eJake Slack     * Any mark set is cleared.
7603928aee4356845252ac6b662d5c72c29903813eJake Slack     * If the entire contents of the buffer are flushed, then an implicit empty() is done.
7703928aee4356845252ac6b662d5c72c29903813eJake Slack     * The passed header/trailer buffers are written before/after the contents of this buffer. This may be done
7803928aee4356845252ac6b662d5c72c29903813eJake Slack     * either as gather writes, as a poke into this buffer or as several writes. The implementation is free to
7903928aee4356845252ac6b662d5c72c29903813eJake Slack     * select the optimal mechanism.
8003928aee4356845252ac6b662d5c72c29903813eJake Slack     * @param header A buffer to write before flushing this buffer. This buffers getIndex is updated.
8103928aee4356845252ac6b662d5c72c29903813eJake Slack     * @param buffer The buffer to flush. This buffers getIndex is updated.
8203928aee4356845252ac6b662d5c72c29903813eJake Slack     * @param trailer A buffer to write after flushing this buffer. This buffers getIndex is updated.
8303928aee4356845252ac6b662d5c72c29903813eJake Slack     * @return the total number of bytes written.
8403928aee4356845252ac6b662d5c72c29903813eJake Slack     */
8503928aee4356845252ac6b662d5c72c29903813eJake Slack    int flush(Buffer header, Buffer buffer, Buffer trailer) throws IOException;
8603928aee4356845252ac6b662d5c72c29903813eJake Slack
8703928aee4356845252ac6b662d5c72c29903813eJake Slack
8803928aee4356845252ac6b662d5c72c29903813eJake Slack    /* ------------------------------------------------------------ */
8903928aee4356845252ac6b662d5c72c29903813eJake Slack    /**
9003928aee4356845252ac6b662d5c72c29903813eJake Slack     * @return The local IP address to which this <code>EndPoint</code> is bound, or <code>null</code>
9103928aee4356845252ac6b662d5c72c29903813eJake Slack     * if this <code>EndPoint</code> does not represent a network connection.
9203928aee4356845252ac6b662d5c72c29903813eJake Slack     */
9303928aee4356845252ac6b662d5c72c29903813eJake Slack    public String getLocalAddr();
9403928aee4356845252ac6b662d5c72c29903813eJake Slack
9503928aee4356845252ac6b662d5c72c29903813eJake Slack    /* ------------------------------------------------------------ */
9603928aee4356845252ac6b662d5c72c29903813eJake Slack    /**
9703928aee4356845252ac6b662d5c72c29903813eJake Slack     * @return The local host name to which this <code>EndPoint</code> is bound, or <code>null</code>
9803928aee4356845252ac6b662d5c72c29903813eJake Slack     * if this <code>EndPoint</code> does not represent a network connection.
9903928aee4356845252ac6b662d5c72c29903813eJake Slack     */
10003928aee4356845252ac6b662d5c72c29903813eJake Slack    public String getLocalHost();
10103928aee4356845252ac6b662d5c72c29903813eJake Slack
10203928aee4356845252ac6b662d5c72c29903813eJake Slack    /* ------------------------------------------------------------ */
10303928aee4356845252ac6b662d5c72c29903813eJake Slack    /**
10403928aee4356845252ac6b662d5c72c29903813eJake Slack     * @return The local port number on which this <code>EndPoint</code> is listening, or <code>0</code>
10503928aee4356845252ac6b662d5c72c29903813eJake Slack     * if this <code>EndPoint</code> does not represent a network connection.
10603928aee4356845252ac6b662d5c72c29903813eJake Slack     */
10703928aee4356845252ac6b662d5c72c29903813eJake Slack    public int getLocalPort();
10803928aee4356845252ac6b662d5c72c29903813eJake Slack
10903928aee4356845252ac6b662d5c72c29903813eJake Slack    /* ------------------------------------------------------------ */
11003928aee4356845252ac6b662d5c72c29903813eJake Slack    /**
11103928aee4356845252ac6b662d5c72c29903813eJake Slack     * @return The remote IP address to which this <code>EndPoint</code> is connected, or <code>null</code>
11203928aee4356845252ac6b662d5c72c29903813eJake Slack     * if this <code>EndPoint</code> does not represent a network connection.
11303928aee4356845252ac6b662d5c72c29903813eJake Slack     */
11403928aee4356845252ac6b662d5c72c29903813eJake Slack    public String getRemoteAddr();
11503928aee4356845252ac6b662d5c72c29903813eJake Slack
11603928aee4356845252ac6b662d5c72c29903813eJake Slack    /* ------------------------------------------------------------ */
11703928aee4356845252ac6b662d5c72c29903813eJake Slack    /**
11803928aee4356845252ac6b662d5c72c29903813eJake Slack     * @return The host name of the remote machine to which this <code>EndPoint</code> is connected, or <code>null</code>
11903928aee4356845252ac6b662d5c72c29903813eJake Slack     * if this <code>EndPoint</code> does not represent a network connection.
12003928aee4356845252ac6b662d5c72c29903813eJake Slack     */
12103928aee4356845252ac6b662d5c72c29903813eJake Slack    public String getRemoteHost();
12203928aee4356845252ac6b662d5c72c29903813eJake Slack
12303928aee4356845252ac6b662d5c72c29903813eJake Slack    /* ------------------------------------------------------------ */
12403928aee4356845252ac6b662d5c72c29903813eJake Slack    /**
12503928aee4356845252ac6b662d5c72c29903813eJake Slack     * @return The remote port number to which this <code>EndPoint</code> is connected, or <code>0</code>
12603928aee4356845252ac6b662d5c72c29903813eJake Slack     * if this <code>EndPoint</code> does not represent a network connection.
12703928aee4356845252ac6b662d5c72c29903813eJake Slack     */
12803928aee4356845252ac6b662d5c72c29903813eJake Slack    public int getRemotePort();
12903928aee4356845252ac6b662d5c72c29903813eJake Slack
13003928aee4356845252ac6b662d5c72c29903813eJake Slack    /* ------------------------------------------------------------ */
13103928aee4356845252ac6b662d5c72c29903813eJake Slack    public boolean isBlocking();
13203928aee4356845252ac6b662d5c72c29903813eJake Slack
13303928aee4356845252ac6b662d5c72c29903813eJake Slack    /* ------------------------------------------------------------ */
13403928aee4356845252ac6b662d5c72c29903813eJake Slack    public boolean blockReadable(long millisecs) throws IOException;
13503928aee4356845252ac6b662d5c72c29903813eJake Slack
13603928aee4356845252ac6b662d5c72c29903813eJake Slack    /* ------------------------------------------------------------ */
13703928aee4356845252ac6b662d5c72c29903813eJake Slack    public boolean blockWritable(long millisecs) throws IOException;
13803928aee4356845252ac6b662d5c72c29903813eJake Slack
13903928aee4356845252ac6b662d5c72c29903813eJake Slack    /* ------------------------------------------------------------ */
14003928aee4356845252ac6b662d5c72c29903813eJake Slack    public boolean isOpen();
14103928aee4356845252ac6b662d5c72c29903813eJake Slack
14203928aee4356845252ac6b662d5c72c29903813eJake Slack    /* ------------------------------------------------------------ */
14303928aee4356845252ac6b662d5c72c29903813eJake Slack    /**
14403928aee4356845252ac6b662d5c72c29903813eJake Slack     * @return The underlying transport object (socket, channel, etc.)
14503928aee4356845252ac6b662d5c72c29903813eJake Slack     */
14603928aee4356845252ac6b662d5c72c29903813eJake Slack    public Object getTransport();
14703928aee4356845252ac6b662d5c72c29903813eJake Slack
14803928aee4356845252ac6b662d5c72c29903813eJake Slack    /* ------------------------------------------------------------ */
14903928aee4356845252ac6b662d5c72c29903813eJake Slack    /** Flush any buffered output.
15003928aee4356845252ac6b662d5c72c29903813eJake Slack     * May fail to write all data if endpoint is non-blocking
15103928aee4356845252ac6b662d5c72c29903813eJake Slack     * @throws EofException If the endpoint is closed or output is shutdown.
15203928aee4356845252ac6b662d5c72c29903813eJake Slack     */
15303928aee4356845252ac6b662d5c72c29903813eJake Slack    public void flush() throws IOException;
15403928aee4356845252ac6b662d5c72c29903813eJake Slack
15503928aee4356845252ac6b662d5c72c29903813eJake Slack    /* ------------------------------------------------------------ */
15603928aee4356845252ac6b662d5c72c29903813eJake Slack    /** Get the max idle time in ms.
15703928aee4356845252ac6b662d5c72c29903813eJake Slack     * <p>The max idle time is the time the endpoint can be idle before
15803928aee4356845252ac6b662d5c72c29903813eJake Slack     * extraordinary handling takes place.  This loosely corresponds to
15903928aee4356845252ac6b662d5c72c29903813eJake Slack     * the {@link java.net.Socket#getSoTimeout()} for blocking connections,
16003928aee4356845252ac6b662d5c72c29903813eJake Slack     * but {@link AsyncEndPoint} implementations must use other mechanisms
16103928aee4356845252ac6b662d5c72c29903813eJake Slack     * to implement the max idle time.
16203928aee4356845252ac6b662d5c72c29903813eJake Slack     * @return the max idle time in ms or if ms <= 0 implies an infinite timeout
16303928aee4356845252ac6b662d5c72c29903813eJake Slack     */
16403928aee4356845252ac6b662d5c72c29903813eJake Slack    public int getMaxIdleTime();
16503928aee4356845252ac6b662d5c72c29903813eJake Slack
16603928aee4356845252ac6b662d5c72c29903813eJake Slack    /* ------------------------------------------------------------ */
16703928aee4356845252ac6b662d5c72c29903813eJake Slack    /** Set the max idle time.
16803928aee4356845252ac6b662d5c72c29903813eJake Slack     * @param timeMs the max idle time in MS. Timeout <= 0 implies an infinite timeout
16903928aee4356845252ac6b662d5c72c29903813eJake Slack     * @throws IOException if the timeout cannot be set.
17003928aee4356845252ac6b662d5c72c29903813eJake Slack     */
17103928aee4356845252ac6b662d5c72c29903813eJake Slack    public void setMaxIdleTime(int timeMs) throws IOException;
17203928aee4356845252ac6b662d5c72c29903813eJake Slack
17303928aee4356845252ac6b662d5c72c29903813eJake Slack
17403928aee4356845252ac6b662d5c72c29903813eJake Slack
17503928aee4356845252ac6b662d5c72c29903813eJake Slack}
176