001/**
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
004 *
005 * This program and the accompanying materials are dual-licensed under
006 * either the terms of the Eclipse Public License v1.0 as published by
007 * the Eclipse Foundation
008 *
009 *   or (per the licensee's choosing)
010 *
011 * under the terms of the GNU Lesser General Public License version 2.1
012 * as published by the Free Software Foundation.
013 */
014package ch.qos.logback.core.net.server;
015
016import java.io.Closeable;
017import java.io.IOException;
018
019/**
020 * A listener that accepts {@link Client} connections on behalf of a
021 * {@link ServerRunner}.
022 * <p>
023 * This interface exists primarily to abstract away the details of the
024 * listener's underlying {@code ServerSocket} and the concurrency associated
025 * with handling multiple clients. Such realities make it difficult to create
026 * effective unit tests for the {@link ServerRunner} that are easy to 
027 * understand and maintain.
028 * <p>
029 * This interface captures the only those details about the listener that the
030 * {@code ServerRunner} cares about; namely, that it is something that has
031 * an underlying resource (or resources) that need to be closed before the
032 * listener is discarded.
033 * 
034 */
035public interface ServerListener<T extends Client> extends Closeable {
036
037    /**
038     * Accepts the next client that appears on this listener.
039     * <p>
040     * An implementation of this method is expected to block the calling thread
041     * and not return until either a client appears or an exception occurs.
042     * 
043     * @return client object
044     * @throws IOException
045     * @throws InterruptedException
046     */
047    T acceptClient() throws IOException, InterruptedException;
048
049    /**
050     * Closes any underlying {@link Closeable} resources associated with this
051     * listener.
052     * <p>
053     * Note that (as described in Doug Lea's discussion about interrupting I/O
054     * operations in "Concurrent Programming in Java" (Addison-Wesley 
055     * Professional, 2nd edition, 1999) this method is used to interrupt
056     * any blocked I/O operation in the client when the server is shutting
057     * down.  The client implementation must anticipate this potential,
058     * and gracefully exit when the blocked I/O operation throws the 
059     * relevant {@link IOException} subclass. 
060     * <p>
061     * Note also, that unlike {@link Closeable#close()} this method is not
062     * permitted to propagate any {@link IOException} that occurs when closing
063     * the underlying resource(s).
064     */
065    void close();
066
067}