mirror of
https://github.com/revanced/jadb.git
synced 2025-05-13 20:57:07 +02:00
Refactor: Splitting out a general SocketServer from the AdbServer.
This commit is contained in:
parent
06c62dca59
commit
6c01fcc86c
@ -7,12 +7,13 @@ import org.junit.Test;
|
|||||||
import se.vidstige.jadb.JadbConnection;
|
import se.vidstige.jadb.JadbConnection;
|
||||||
import se.vidstige.jadb.JadbDevice;
|
import se.vidstige.jadb.JadbDevice;
|
||||||
import se.vidstige.jadb.test.fakes.AdbServer;
|
import se.vidstige.jadb.test.fakes.AdbServer;
|
||||||
|
import se.vidstige.jadb.test.fakes.SocketServer;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class MockedTestCases {
|
public class MockedTestCases {
|
||||||
|
|
||||||
private AdbServer server;
|
private SocketServer server;
|
||||||
private JadbConnection connection;
|
private JadbConnection connection;
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
|
@ -1,71 +1,31 @@
|
|||||||
package se.vidstige.jadb.test.fakes;
|
package se.vidstige.jadb.test.fakes;
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.net.ServerSocket;
|
|
||||||
import java.net.Socket;
|
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;
|
public 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 AdbServer()
|
public AdbServer()
|
||||||
{
|
{
|
||||||
this(DEFAULT_PORT);
|
this(DEFAULT_PORT);
|
||||||
}
|
}
|
||||||
public AdbServer(int port)
|
|
||||||
{
|
|
||||||
this.port = port;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void start() throws InterruptedException
|
public AdbServer(int port) {
|
||||||
{
|
super(port);
|
||||||
thread = new Thread(this, "Fake Adb Server");
|
|
||||||
thread.setDaemon(true);
|
|
||||||
thread.start();
|
|
||||||
synchronized (lockObject) {
|
|
||||||
lockObject.wait();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getPort() {
|
|
||||||
return port;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
protected Runnable createResponder(Socket socket) {
|
||||||
try {
|
return new AdbResponder(socket);
|
||||||
System.out.println("Starting on port " + port);
|
|
||||||
socket = new ServerSocket(port);
|
|
||||||
socket.setReuseAddress(true);
|
|
||||||
|
|
||||||
synchronized (lockObject) {
|
|
||||||
lockObject.notify();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
while (true)
|
public static void main(String[] args)
|
||||||
{
|
{
|
||||||
Socket c = socket.accept();
|
SocketServer server = new AdbServer();
|
||||||
Thread clientThread = new Thread(new AdbResponder(c), "AdbClientWorker");
|
server.run();
|
||||||
clientThread.setDaemon(true);
|
|
||||||
clientThread.start();
|
|
||||||
}
|
|
||||||
} catch (IOException e) {
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void stop() throws IOException, InterruptedException {
|
|
||||||
socket.close();
|
|
||||||
thread.join();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
62
test/se/vidstige/jadb/test/fakes/SocketServer.java
Normal file
62
test/se/vidstige/jadb/test/fakes/SocketServer.java
Normal 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();
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user