/* * Copyright (c) 2012, 2018, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions.
*/
/** * An abstract server identifies a server which listens on a port on a * given machine.
*/ staticabstractclass AbstractServer {
private AbstractServer() {
}
publicabstractint getPort();
publicabstract InetAddress getAddress();
}
/** * A downgraded type of AbstractServer which will refuse connections. Note: * use it once and throw it away - this implementation opens an anonymous * socket and closes it, returning the address of the closed socket. If * other servers are started afterwards, the address/port might get reused * and become connectable again - so it's not a good idea to assume that * connections using this address/port will always be refused. Connections * will be refused as long as the address/port of the refusing server has * not been reused.
*/ staticclass RefusingServer extends AbstractServer {
/** * An abstract class for implementing small TCP servers for the nio tests * purposes. Disclaimer: This is a naive implementation that uses the old * networking APIs (not those from {@code java.nio.*}) and shamelessly * extends/creates Threads instead of using an executor service.
*/ staticabstractclass AbstractTcpServer extends AbstractServer implements Runnable, Closeable {
protectedfinallong linger; // #of ms to wait before responding privateThread acceptThread; // thread waiting for accept // list of opened connections that should be closed on close. private List<TcpConnectionThread> connections = new ArrayList<>(); private ServerSocket serverSocket; // the server socket privateboolean started = false; // whether the server is started
Throwable error = null;
/** * Creates a new abstract TCP server. * * @param linger the amount of time the server should wait before * responding to requests.
*/ protected AbstractTcpServer(long linger) { this.linger = linger;
}
/** * The local port to which the server is bound. * * @return The local port to which the server is bound. * @exception IllegalStateException is thrown if the server is not * started.
*/
@Override publicfinalsynchronizedint getPort() { if (!started) { thrownew IllegalStateException("Not started");
} return serverSocket.getLocalPort();
}
/** * The local address to which the server is bound. * * @return The local address to which the server is bound. * @exception IllegalStateException is thrown if the server is not * started.
*/
@Override publicfinalsynchronized InetAddress getAddress() { if (!started) { thrownew IllegalStateException("Not started");
} return serverSocket.getInetAddress();
}
/** * Tells whether the server is started. * * @return true if the server is started.
*/ publicfinalsynchronizedboolean isStarted() { return started;
}
/** * Creates a new server socket. * * @param port local port to bind to. * @param backlog requested maximum length of the queue of incoming * connections. * @param address local address to bind to. * @return a new bound server socket ready to accept connections. * @throws IOException if the socket cannot be created or bound.
*/ protected ServerSocket newServerSocket(int port, int backlog,
InetAddress address) throws IOException { returnnew ServerSocket(port, backlog, address);
}
/** * Starts listening for connections. * * @throws IOException if the server socket cannot be created or bound.
*/ publicfinalsynchronizedvoid start() throws IOException { if (started) { return;
} final ServerSocket socket =
newServerSocket(0, 100, InetAddress.getLocalHost());
serverSocket = socket;
acceptThread = newThread(this);
acceptThread.setDaemon(true);
acceptThread.start();
started = true;
}
/** * Creates a new TcpConnnectionThread to handle the connection through * an accepted socket. * * @param s the socket returned by {@code serverSocket.accept()}. * @return a new TcpConnnectionThread to handle the connection through * an accepted socket.
*/ protectedabstract TcpConnectionThread createConnection(Socket s);
/** * Creates and starts a new TcpConnectionThread to handle the accepted * socket. * * @param s the socket returned by {@code serverSocket.accept()}.
*/ privatesynchronizedvoid listen(Socket s) {
TcpConnectionThread c = createConnection(s);
c.start();
addConnection(c);
}
/** * Add the connection to the list of accepted connections. * * @param connection an accepted connection.
*/ protectedsynchronizedvoid addConnection(
TcpConnectionThread connection) {
connections.add(connection);
}
/** * Remove the connection from the list of accepted connections. * * @param connection an accepted connection.
*/ protectedsynchronizedvoid removeConnection(
TcpConnectionThread connection) {
connections.remove(connection);
}
/** * Close the server socket and all the connections present in the list * of accepted connections. * * @throws IOException
*/
@Override publicsynchronizedvoid close() throws IOException { if (serverSocket != null && !serverSocket.isClosed()) {
serverSocket.close();
} if (acceptThread != null) {
acceptThread.interrupt();
} int failed = 0; for (TcpConnectionThread c : connections) { try {
c.close();
} catch (IOException x) { // no matter - we're closing.
failed++;
}
}
connections.clear(); if (failed > 0) { thrownew IOException("Failed to close some connections");
}
}
}
/** * A small TCP Server that emulates the echo service for tests purposes. See * http://en.wikipedia.org/wiki/Echo_Protocol This server uses an anonymous * port - NOT the standard port 7. We don't guarantee that its behavior * exactly matches the RFC - the only purpose of this server is to have * something that responds to nio tests...
*/ staticclass EchoServer extends AbstractTcpServer {
publicstatic EchoServer startNewServer(long linger) throws IOException { final EchoServer echoServer = new EchoServer(linger);
echoServer.start(); return echoServer;
}
}
/** * A small TCP Server that accept connections but does not response to any input.
*/ staticfinalclass NoResponseServer extends EchoServer { public NoResponseServer() { super(Long.MAX_VALUE);
}
publicstatic NoResponseServer startNewServer() throws IOException { final NoResponseServer noResponseServer = new NoResponseServer();
noResponseServer.start(); return noResponseServer;
}
}
/** * A small TCP server that emulates the Day & Time service for tests * purposes. See http://en.wikipedia.org/wiki/Daytime_Protocol This server * uses an anonymous port - NOT the standard port 13. We don't guarantee * that its behavior exactly matches the RFC - the only purpose of this * server is to have something that responds to nio tests...
*/ staticfinalclass DayTimeServer extends AbstractTcpServer {
public DayTimeServer() { this(0L);
}
public DayTimeServer(long linger) { super(linger);
}
publicstatic DayTimeServer startNewServer(long linger) throws IOException { final DayTimeServer daytimeServer = new DayTimeServer(linger);
daytimeServer.start(); return daytimeServer;
}
}
/** * An abstract class for implementing small UDP Servers for the nio tests * purposes. Disclaimer: This is a naive implementation that uses the old * networking APIs (not those from {@code java.nio.*}) and shamelessly * extends/creates Threads instead of using an executor service.
*/ staticabstractclass AbstractUdpServer extends AbstractServer implements Runnable, Closeable {
protectedfinallong linger; // #of ms to wait before responding privateThread acceptThread; // thread waiting for packets private DatagramSocket serverSocket; // the server socket privateboolean started = false; // whether the server is started
Throwable error = null;
/** * Creates a new abstract UDP server. * * @param linger the amount of time the server should wait before * responding to requests.
*/ protected AbstractUdpServer(long linger) { this.linger = linger;
}
/** * The local port to which the server is bound. * * @return The local port to which the server is bound. * @exception IllegalStateException is thrown if the server is not * started.
*/
@Override publicfinalsynchronizedint getPort() { if (!started) { thrownew IllegalStateException("Not started");
} return serverSocket.getLocalPort();
}
/** * The local address to which the server is bound. * * @return The local address to which the server is bound. * @exception IllegalStateException is thrown if the server is not * started.
*/
@Override publicfinalsynchronized InetAddress getAddress() { if (!started) { thrownew IllegalStateException("Not started");
} return serverSocket.getLocalAddress();
}
/** * Tells whether the server is started. * * @return true if the server is started.
*/ publicfinalsynchronizedboolean isStarted() { return started;
}
/** * Creates a new datagram socket. * * @param port local port to bind to. * @param address local address to bind to. * @return a new bound server socket ready to listen for packets. * @throws IOException if the socket cannot be created or bound.
*/ protected DatagramSocket newDatagramSocket(int port,
InetAddress address) throws IOException { returnnew DatagramSocket(port, address);
}
/** * Starts listening for connections. * * @throws IOException if the server socket cannot be created or bound.
*/ publicfinalsynchronizedvoid start() throws IOException { if (started) { return;
} final DatagramSocket socket =
newDatagramSocket(0, InetAddress.getLocalHost());
serverSocket = socket;
acceptThread = newThread(this);
acceptThread.setDaemon(true);
acceptThread.start();
started = true;
}
/** * Creates a new UdpRequestThread to handle a DatagramPacket received * through a DatagramSocket. * * @param socket the socket through which the request was received. * @param request the datagram packet received through the socket. * @return a new UdpRequestThread to handle the request received through * a DatagramSocket.
*/ protectedabstract UdpRequestThread createConnection(DatagramSocket socket,
DatagramPacket request);
/** * Creates and starts a new UdpRequestThread to handle the received * datagram packet. * * @param socket the socket through which the request was received. * @param request the datagram packet received through the socket.
*/ privatesynchronizedvoid handle(DatagramSocket socket,
DatagramPacket request) {
UdpRequestThread c = createConnection(socket, request); // c can be null if the request requires no response. if (c != null) {
c.start();
}
}
/** * Close the server socket. * * @throws IOException
*/
@Override publicsynchronizedvoid close() throws IOException { if (serverSocket != null && !serverSocket.isClosed()) {
serverSocket.close();
} if (acceptThread != null) {
acceptThread.interrupt();
}
}
}
/** * A small UDP Server that emulates the discard service for tests purposes. * See http://en.wikipedia.org/wiki/Discard_Protocol This server uses an * anonymous port - NOT the standard port 9. We don't guarantee that its * behavior exactly matches the RFC - the only purpose of this server is to * have something that responds to nio tests...
*/ staticfinalclass UdpDiscardServer extends AbstractUdpServer {
public UdpDiscardServer() { this(0L);
}
public UdpDiscardServer(long linger) { super(linger);
}
publicstatic UdpDiscardServer startNewServer(long linger) throws IOException { final UdpDiscardServer discardServer = new UdpDiscardServer(linger);
discardServer.start(); return discardServer;
}
}
/** * A small UDP Server that emulates the echo service for tests purposes. See * http://en.wikipedia.org/wiki/Echo_Protocol This server uses an anonymous * port - NOT the standard port 7. We don't guarantee that its behavior * exactly matches the RFC - the only purpose of this server is to have * something that responds to nio tests...
*/ staticfinalclass UdpEchoServer extends AbstractUdpServer {
public UdpEchoServer() { this(0L);
}
public UdpEchoServer(long linger) { super(linger);
}
publicstatic UdpEchoServer startNewServer(long linger) throws IOException { final UdpEchoServer echoServer = new UdpEchoServer(linger);
echoServer.start(); return echoServer;
}
}
/** * A small UDP server that emulates the Day & Time service for tests * purposes. See http://en.wikipedia.org/wiki/Daytime_Protocol This server * uses an anonymous port - NOT the standard port 13. We don't guarantee * that its behavior exactly matches the RFC - the only purpose of this * server is to have something that responds to nio tests...
*/ staticfinalclass UdpDayTimeServer extends AbstractUdpServer {
public UdpDayTimeServer() { this(0L);
}
public UdpDayTimeServer(long linger) { super(linger);
}
publicstatic UdpDayTimeServer startNewServer(long linger) throws IOException { final UdpDayTimeServer echoServer = new UdpDayTimeServer(linger);
echoServer.start(); return echoServer;
}
}
}
¤ Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.0.44Bemerkung:
(vorverarbeitet)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.