Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(434)

Unified Diff: net/test/android/javatests/src/org/chromium/net/test/BaseTcpTestServer.java

Issue 867073002: [Android] Add a java version of the test server spawner. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: findbugs Created 5 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: net/test/android/javatests/src/org/chromium/net/test/BaseTcpTestServer.java
diff --git a/net/test/android/javatests/src/org/chromium/net/test/BaseTcpTestServer.java b/net/test/android/javatests/src/org/chromium/net/test/BaseTcpTestServer.java
new file mode 100644
index 0000000000000000000000000000000000000000..3f823595e599a21702bce91ea935966f771b1edf
--- /dev/null
+++ b/net/test/android/javatests/src/org/chromium/net/test/BaseTcpTestServer.java
@@ -0,0 +1,71 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+package org.chromium.net.test;
+
+import android.util.Log;
+
+import java.io.IOException;
+import java.net.ServerSocket;
+import java.net.Socket;
+import java.net.SocketTimeoutException;
+
+/** A base class for simple TCP test servers. */
+public abstract class BaseTcpTestServer extends BaseTestServer {
+ private static final String TAG = "BaseTcpTestServer";
+
+ private ServerSocket mServerSocket;
+
+ /**
+ * Creates a TCP test server on the given port.
+ *
+ * @param serverPort The port to listen on for incoming TCP connections.
+ * @param acceptTimeoutMs The timeout for calls to ServerSocket.accept(), in milliseconds.
+ * @throws IOException If the server port can't be bound.
+ */
+ public BaseTcpTestServer(int serverPort, int acceptTimeoutMs) throws IOException {
+ mServerSocket = new ServerSocket(serverPort);
+ mServerSocket.setSoTimeout(acceptTimeoutMs);
+ }
+
+ /** Returns the port on which this server is listening for connections. */
+ @Override
+ public int getServerPort() {
+ return mServerSocket.getLocalPort();
+ }
+
+ /** Waits for and handles an incoming request. */
+ protected final void accept() {
+ try {
+ Socket sock = mServerSocket.accept();
+
+ if (!validateSocket(sock)) {
+ Log.e(TAG, "Socket failed validation.");
+ sock.close();
+ return;
+ }
+
+ handle(sock);
+ } catch (SocketTimeoutException e) {
+ Log.i(TAG, "Timed out waiting for incoming connection.");
+ } catch (IOException e) {
+ Log.e(TAG, "Error while handling incoming connection", e);
+ }
+ }
+
+ /**
+ * Returns whether the connection open on the given socket should be handled.
+ *
+ * @param sock The socket to validate.
+ */
+ protected abstract boolean validateSocket(Socket sock);
+
+ /**
+ * Handles the connection open on the given socket.
+ *
+ * @param sock The socket to handle.
+ * @throws IOException If an error occurs while reading from or writing to the socket.
+ */
+ protected abstract void handle(Socket sock) throws IOException;
+}

Powered by Google App Engine
This is Rietveld 408576698