/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* 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.
*/
/* Test utility classes
*
*/
public class TestServers {
private TestServers() { }
/**
* An abstract server identifies a server which listens on a port on on a
* given machine.
*/
static abstract class AbstractServer {
private AbstractServer() {
}
public abstract int getPort();
}
/**
* 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
* and become connectable again - so it's not a good idea to assume that
* not been reused.
*/
final int port;
}
public int getPort() {
return port;
}
return address;
}
socket.getLocalPort());
return server;
}
}
/**
* 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
*/
// list of opened connections that should be closed on close.
/**
* Creates a new abstract TCP server.
*
* @param linger the amount of time the server should wait before
* responding to requests.
*/
}
/**
* 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.
*/
public final synchronized int getPort() {
if (!started) {
throw new 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.
*/
if (!started) {
throw new IllegalStateException("Not started");
}
return serverSocket.getInetAddress();
}
/**
* Tells whether the server is started.
*
* @return true if the server is started.
*/
public final synchronized boolean 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.
*/
throws IOException {
}
/**
* Starts listening for connections.
*
* @throws IOException if the server socket cannot be created or bound.
*/
if (started) {
return;
}
final ServerSocket socket =
acceptThread = new Thread(this);
acceptThread.setDaemon(true);
started = true;
}
/**
* Calls {@code Thread.sleep(linger);}
*/
protected final void lingerIfRequired() {
if (linger > 0) {
try {
} catch (InterruptedException x) {
}
}
}
}
return this.serverSocket;
}
/**
* The main accept loop.
*/
public final void run() {
try {
Socket s;
listen(s);
}
} catch (Exception x) {
error = x;
} finally {
synchronized (this) {
try {
} catch (IOException x) {
}
}
started = false;
this.serverSocket = null;
this.acceptThread = null;
}
}
}
}
/**
* Represents a connection accepted by the server.
*/
this.setDaemon(true);
}
interrupt();
}
}
/**
* 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.
*/
/**
* Creates and starts a new TcpConnectionThread to handle the accepted
* socket.
*
* @param s the socket returned by {@code serverSocket.accept()}.
*/
TcpConnectionThread c = createConnection(s);
c.start();
addConnection(c);
}
/**
* Add the connection to the list of accepted connections.
*
* @param connection an accepted connection.
*/
protected synchronized void addConnection(
}
/**
* Remove the connection from the list of accepted connections.
*
* @param connection an accepted connection.
*/
protected synchronized void removeConnection(
}
/**
* Close the server socket and all the connections present in the list
* of accepted connections.
*
* @throws IOException
*/
}
if (acceptThread != null) {
}
int failed = 0;
for (TcpConnectionThread c : connections) {
try {
c.close();
} catch (IOException x) {
// no matter - we're closing.
failed++;
}
}
connections.clear();
if (failed > 0) {
throw new 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...
*/
public EchoServer() {
this(0L);
}
super(linger);
}
return new EchoConnection(s);
}
super(socket);
}
public void run() {
try {
byte[] b = new byte[255];
int n;
}
} catch (IOException io) {
// fall through to finally
} finally {
try {
} catch (IOException x) {
"Failed to close echo connection socket");
}
}
removeConnection(this);
}
}
}
return startNewServer(0);
}
echoServer.start();
return echoServer;
}
}
/**
* 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...
*/
public DayTimeServer() {
this(0L);
}
super(linger);
}
return new DayTimeServerConnection(s);
}
// do nothing - the connection just write the date and terminates.
}
// do nothing - we're not adding connections to the list...
}
super(socket);
}
public void run() {
try {
} catch (IOException io) {
// fall through to finally
} finally {
try {
} catch (IOException x) {
"Failed to close echo connection socket");
}
}
}
}
}
throws IOException {
return startNewServer(0);
}
throws IOException {
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
*/
/**
* Creates a new abstract UDP server.
*
* @param linger the amount of time the server should wait before
* responding to requests.
*/
}
/**
* 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.
*/
public final synchronized int getPort() {
if (!started) {
throw new 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.
*/
if (!started) {
throw new IllegalStateException("Not started");
}
return serverSocket.getLocalAddress();
}
/**
* Tells whether the server is started.
*
* @return true if the server is started.
*/
public final synchronized boolean 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.
*/
throws IOException {
}
/**
* Starts listening for connections.
*
* @throws IOException if the server socket cannot be created or bound.
*/
if (started) {
return;
}
final DatagramSocket socket =
acceptThread = new Thread(this);
acceptThread.setDaemon(true);
started = true;
}
/**
* Calls {@code Thread.sleep(linger);}
*/
protected final void lingerIfRequired() {
if (linger > 0) {
try {
} catch (InterruptedException x) {
}
}
}
}
return this.serverSocket;
}
return true;
} else {
return false;
}
}
/**
* The main receive loop.
*/
public final void run() {
try {
}
final DatagramPacket packet =
//System.out.println("Received packet from: "
// + packet.getAddress()+":"+packet.getPort());
}
} catch (Exception x) {
error = x;
} finally {
synchronized (this) {
}
started = false;
this.serverSocket = null;
this.acceptThread = null;
}
}
}
}
/**
* Represents an UDP request received by the server.
*/
this.setDaemon(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.
*/
/**
* 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.
*/
// c can be null if the request requires no response.
if (c != null) {
c.start();
}
}
/**
* Close the server socket.
*
* @throws IOException
*/
}
if (acceptThread != null) {
}
}
}
/**
* 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...
*/
public UdpDiscardServer() {
this(0L);
}
super(linger);
}
// no response required
return null;
}
return startNewServer(0);
}
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...
*/
public UdpEchoServer() {
this(0L);
}
super(linger);
}
}
}
public void run() {
try {
final DatagramPacket response =
} catch (IOException io) {
}
}
}
return startNewServer(0);
}
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...
*/
public UdpDayTimeServer() {
this(0L);
}
super(linger);
}
}
}
public void run() {
try {
final DatagramPacket response =
} catch (IOException io) {
}
}
}
return startNewServer(0);
}
throws IOException {
echoServer.start();
return echoServer;
}
}
}