Refactor: Splitting out a general SocketServer from the AdbServer.

This commit is contained in:
Samuel Carlsson 2014-03-20 10:45:23 +01:00
parent 06c62dca59
commit 6c01fcc86c
3 changed files with 80 additions and 57 deletions

View File

@ -7,12 +7,13 @@ import org.junit.Test;
import se.vidstige.jadb.JadbConnection;
import se.vidstige.jadb.JadbDevice;
import se.vidstige.jadb.test.fakes.AdbServer;
import se.vidstige.jadb.test.fakes.SocketServer;
import java.util.List;
public class MockedTestCases {
private AdbServer server;
private SocketServer server;
private JadbConnection connection;
@Before

View File

@ -1,71 +1,31 @@
package se.vidstige.jadb.test.fakes;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
// >set ANDROID_ADB_SERVER_PORT=15037
public class AdbServer implements Runnable {
/**
* Created by vidstige on 2014-03-20
*/
public class AdbServer extends SocketServer {
private static final int DEFAULT_PORT = 15037;
private final int port;
private ServerSocket socket;
private Thread thread;
private final Object lockObject = new Object();
public static void main(String[] args)
{
AdbServer server = new AdbServer();
server.run();
}
public static final int DEFAULT_PORT = 15037;
public AdbServer()
{
this(DEFAULT_PORT);
}
public AdbServer(int port)
{
this.port = port;
public AdbServer(int port) {
super(port);
}
public void start() throws InterruptedException
{
thread = new Thread(this, "Fake Adb Server");
thread.setDaemon(true);
thread.start();
synchronized (lockObject) {
lockObject.wait();
}
}
public int getPort() {
return port;
}
@Override
public void run() {
try {
System.out.println("Starting on port " + port);
socket = new ServerSocket(port);
socket.setReuseAddress(true);
synchronized (lockObject) {
lockObject.notify();
}
while (true)
{
Socket c = socket.accept();
Thread clientThread = new Thread(new AdbResponder(c), "AdbClientWorker");
clientThread.setDaemon(true);
clientThread.start();
}
} catch (IOException e) {
}
}
@Override
protected Runnable createResponder(Socket socket) {
return new AdbResponder(socket);
}
public void stop() throws IOException, InterruptedException {
socket.close();
thread.join();
public static void main(String[] args)
{
SocketServer server = new AdbServer();
server.run();
}
}

View File

@ -0,0 +1,62 @@
package se.vidstige.jadb.test.fakes;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
// >set ANDROID_ADB_SERVER_PORT=15037
public abstract class SocketServer implements Runnable {
private final int port;
private ServerSocket socket;
private Thread thread;
private final Object lockObject = new Object();
public SocketServer(int port)
{
this.port = port;
}
public void start() throws InterruptedException
{
thread = new Thread(this, "Fake Adb Server");
thread.setDaemon(true);
thread.start();
synchronized (lockObject) {
lockObject.wait();
}
}
public int getPort() {
return port;
}
@Override
public void run() {
try {
System.out.println("Starting on port " + port);
socket = new ServerSocket(port);
socket.setReuseAddress(true);
synchronized (lockObject) {
lockObject.notify();
}
while (true)
{
Socket c = socket.accept();
Thread clientThread = new Thread(createResponder(c), "AdbClientWorker");
clientThread.setDaemon(true);
clientThread.start();
}
} catch (IOException e) {
}
}
protected abstract Runnable createResponder(Socket socket);
public void stop() throws IOException, InterruptedException {
socket.close();
thread.join();
}
}