mirror of
https://github.com/revanced/jadb.git
synced 2025-02-11 09:36:48 +01:00
Extract common code to HostConnectionCommand class
This commit is contained in:
parent
c380a1bcad
commit
3f8b86d8e5
@ -3,73 +3,27 @@ package se.vidstige.jadb;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.InetSocketAddress;
|
import java.net.InetSocketAddress;
|
||||||
|
|
||||||
class HostConnectToRemoteTcpDevice {
|
class HostConnectToRemoteTcpDevice extends HostConnectionCommand {
|
||||||
private final Transport transport;
|
|
||||||
private final ResponseValidator responseValidator;
|
|
||||||
|
|
||||||
HostConnectToRemoteTcpDevice(Transport transport) {
|
HostConnectToRemoteTcpDevice(Transport transport) {
|
||||||
this.transport = transport;
|
super(transport, new ResponseValidatorImp());
|
||||||
this.responseValidator = new ResponseValidatorImp();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//Visible for testing
|
//Visible for testing
|
||||||
HostConnectToRemoteTcpDevice(Transport transport, ResponseValidator responseValidator) {
|
HostConnectToRemoteTcpDevice(Transport transport, ResponseValidator responseValidator) {
|
||||||
this.transport = transport;
|
super(transport, responseValidator);
|
||||||
this.responseValidator = responseValidator;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
InetSocketAddress connect(InetSocketAddress inetSocketAddress)
|
InetSocketAddress connect(InetSocketAddress inetSocketAddress)
|
||||||
throws IOException, JadbException, ConnectionToRemoteDeviceException {
|
throws IOException, JadbException, ConnectionToRemoteDeviceException {
|
||||||
transport.send(String.format("host:connect:%s:%d", inetSocketAddress.getHostString(), inetSocketAddress.getPort()));
|
return executeHostCommand("connect", inetSocketAddress);
|
||||||
verifyTransportLevel();
|
|
||||||
verifyProtocolLevel();
|
|
||||||
|
|
||||||
return inetSocketAddress;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void verifyTransportLevel() throws IOException, JadbException {
|
static final class ResponseValidatorImp extends ResponseValidatorBase {
|
||||||
transport.verifyResponse();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void verifyProtocolLevel() throws IOException, ConnectionToRemoteDeviceException {
|
|
||||||
String status = transport.readString();
|
|
||||||
responseValidator.validate(status);
|
|
||||||
}
|
|
||||||
|
|
||||||
//@VisibleForTesting
|
|
||||||
interface ResponseValidator {
|
|
||||||
void validate(String response) throws ConnectionToRemoteDeviceException;
|
|
||||||
}
|
|
||||||
|
|
||||||
static final class ResponseValidatorImp implements ResponseValidator {
|
|
||||||
private static final String SUCCESSFULLY_CONNECTED = "connected to";
|
private static final String SUCCESSFULLY_CONNECTED = "connected to";
|
||||||
private static final String ALREADY_CONNECTED = "already connected to";
|
private static final String ALREADY_CONNECTED = "already connected to";
|
||||||
|
|
||||||
|
|
||||||
ResponseValidatorImp() {
|
ResponseValidatorImp() {
|
||||||
}
|
super(SUCCESSFULLY_CONNECTED, ALREADY_CONNECTED);
|
||||||
|
|
||||||
public void validate(String response) throws ConnectionToRemoteDeviceException {
|
|
||||||
if (!checkIfConnectedSuccessfully(response) && !checkIfAlreadyConnected(response)) {
|
|
||||||
throw new ConnectionToRemoteDeviceException(extractError(response));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private boolean checkIfConnectedSuccessfully(String response) {
|
|
||||||
return response.startsWith(SUCCESSFULLY_CONNECTED);
|
|
||||||
}
|
|
||||||
|
|
||||||
private boolean checkIfAlreadyConnected(String response) {
|
|
||||||
return response.startsWith(ALREADY_CONNECTED);
|
|
||||||
}
|
|
||||||
|
|
||||||
private String extractError(String response) {
|
|
||||||
int lastColon = response.lastIndexOf(':');
|
|
||||||
if (lastColon != -1) {
|
|
||||||
return response.substring(lastColon, response.length());
|
|
||||||
} else {
|
|
||||||
return response;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
70
src/se/vidstige/jadb/HostConnectionCommand.java
Normal file
70
src/se/vidstige/jadb/HostConnectionCommand.java
Normal file
@ -0,0 +1,70 @@
|
|||||||
|
package se.vidstige.jadb;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.net.InetSocketAddress;
|
||||||
|
|
||||||
|
public class HostConnectionCommand {
|
||||||
|
private final Transport transport;
|
||||||
|
private final ResponseValidator responseValidator;
|
||||||
|
|
||||||
|
HostConnectionCommand(Transport transport, ResponseValidator responseValidator) {
|
||||||
|
this.transport = transport;
|
||||||
|
this.responseValidator = responseValidator;
|
||||||
|
}
|
||||||
|
|
||||||
|
InetSocketAddress executeHostCommand(String command, InetSocketAddress inetSocketAddress)
|
||||||
|
throws IOException, JadbException, ConnectionToRemoteDeviceException {
|
||||||
|
transport.send(String.format("host:%s:%s:%d", command, inetSocketAddress.getHostString(), inetSocketAddress.getPort()));
|
||||||
|
verifyTransportLevel();
|
||||||
|
verifyProtocolLevel();
|
||||||
|
|
||||||
|
return inetSocketAddress;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void verifyTransportLevel() throws IOException, JadbException {
|
||||||
|
transport.verifyResponse();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void verifyProtocolLevel() throws IOException, ConnectionToRemoteDeviceException {
|
||||||
|
String status = transport.readString();
|
||||||
|
responseValidator.validate(status);
|
||||||
|
}
|
||||||
|
|
||||||
|
//@VisibleForTesting
|
||||||
|
interface ResponseValidator {
|
||||||
|
void validate(String response) throws ConnectionToRemoteDeviceException;
|
||||||
|
}
|
||||||
|
|
||||||
|
static class ResponseValidatorBase implements ResponseValidator {
|
||||||
|
private final String successMessage;
|
||||||
|
private final String errorMessage;
|
||||||
|
|
||||||
|
ResponseValidatorBase(String successMessage, String errorMessage) {
|
||||||
|
this.successMessage = successMessage;
|
||||||
|
this.errorMessage = errorMessage;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void validate(String response) throws ConnectionToRemoteDeviceException {
|
||||||
|
if (!checkIfConnectedSuccessfully(response) && !checkIfAlreadyConnected(response)) {
|
||||||
|
throw new ConnectionToRemoteDeviceException(extractError(response));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean checkIfConnectedSuccessfully(String response) {
|
||||||
|
return response.startsWith(successMessage);
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean checkIfAlreadyConnected(String response) {
|
||||||
|
return response.startsWith(errorMessage);
|
||||||
|
}
|
||||||
|
|
||||||
|
private String extractError(String response) {
|
||||||
|
int lastColon = response.lastIndexOf(':');
|
||||||
|
if (lastColon != -1) {
|
||||||
|
return response.substring(lastColon, response.length());
|
||||||
|
} else {
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -3,72 +3,28 @@ package se.vidstige.jadb;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.InetSocketAddress;
|
import java.net.InetSocketAddress;
|
||||||
|
|
||||||
public class HostDisconnectFromRemoteTcpDevice {
|
public class HostDisconnectFromRemoteTcpDevice extends HostConnectionCommand {
|
||||||
private final Transport transport;
|
|
||||||
private final ResponseValidator responseValidator;
|
|
||||||
|
|
||||||
HostDisconnectFromRemoteTcpDevice(Transport transport) {
|
HostDisconnectFromRemoteTcpDevice(Transport transport) {
|
||||||
this.transport = transport;
|
super(transport, new ResponseValidatorImp());
|
||||||
this.responseValidator = new ResponseValidatorImp();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//Visible for testing
|
//Visible for testing
|
||||||
HostDisconnectFromRemoteTcpDevice(Transport transport, ResponseValidator responseValidator) {
|
HostDisconnectFromRemoteTcpDevice(Transport transport, ResponseValidator responseValidator) {
|
||||||
this.transport = transport;
|
super(transport, responseValidator);
|
||||||
this.responseValidator = responseValidator;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
InetSocketAddress disconnect(InetSocketAddress inetSocketAddress)
|
InetSocketAddress disconnect(InetSocketAddress inetSocketAddress)
|
||||||
throws IOException, JadbException, ConnectionToRemoteDeviceException {
|
throws IOException, JadbException, ConnectionToRemoteDeviceException {
|
||||||
transport.send(String.format("host:disconnect:%s:%d", inetSocketAddress.getHostString(), inetSocketAddress.getPort()));
|
return executeHostCommand("disconnect", inetSocketAddress);
|
||||||
verifyTransportLevel();
|
|
||||||
verifyProtocolLevel();
|
|
||||||
|
|
||||||
return inetSocketAddress;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void verifyTransportLevel() throws IOException, JadbException {
|
static final class ResponseValidatorImp extends ResponseValidatorBase {
|
||||||
transport.verifyResponse();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void verifyProtocolLevel() throws IOException, ConnectionToRemoteDeviceException {
|
|
||||||
String status = transport.readString();
|
|
||||||
responseValidator.validate(status);
|
|
||||||
}
|
|
||||||
|
|
||||||
//@VisibleForTesting
|
|
||||||
interface ResponseValidator {
|
|
||||||
void validate(String response) throws ConnectionToRemoteDeviceException;
|
|
||||||
}
|
|
||||||
|
|
||||||
static final class ResponseValidatorImp implements ResponseValidator {
|
|
||||||
private static final String SUCCESSFULLY_DISCONNECTED = "disconnected";
|
private static final String SUCCESSFULLY_DISCONNECTED = "disconnected";
|
||||||
private static final String ALREADY_DISCONNECTED = "error: no such device";
|
private static final String ALREADY_DISCONNECTED = "error: no such device";
|
||||||
|
|
||||||
ResponseValidatorImp() {
|
ResponseValidatorImp() {
|
||||||
}
|
super(SUCCESSFULLY_DISCONNECTED, ALREADY_DISCONNECTED);
|
||||||
|
|
||||||
public void validate(String response) throws ConnectionToRemoteDeviceException {
|
|
||||||
if (!checkIfConnectedSuccessfully(response) && !checkIfAlreadyConnected(response)) {
|
|
||||||
throw new ConnectionToRemoteDeviceException(extractError(response));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private boolean checkIfConnectedSuccessfully(String response) {
|
|
||||||
return response.startsWith(SUCCESSFULLY_DISCONNECTED);
|
|
||||||
}
|
|
||||||
|
|
||||||
private boolean checkIfAlreadyConnected(String response) {
|
|
||||||
return response.startsWith(ALREADY_DISCONNECTED);
|
|
||||||
}
|
|
||||||
|
|
||||||
private String extractError(String response) {
|
|
||||||
int lastColon = response.lastIndexOf(':');
|
|
||||||
if (lastColon != -1) {
|
|
||||||
return response.substring(lastColon, response.length());
|
|
||||||
} else {
|
|
||||||
return response;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -59,7 +59,7 @@ public class HostConnectToRemoteTcpDeviceTest {
|
|||||||
//Prepare
|
//Prepare
|
||||||
Transport transport = mock(Transport.class);
|
Transport transport = mock(Transport.class);
|
||||||
when(transport.readString()).thenReturn("connected to somehost:1");
|
when(transport.readString()).thenReturn("connected to somehost:1");
|
||||||
HostConnectToRemoteTcpDevice.ResponseValidator responseValidator = mock(HostConnectToRemoteTcpDevice.ResponseValidator.class);
|
HostConnectToRemoteTcpDevice.ResponseValidator responseValidator = mock(HostConnectionCommand.ResponseValidator.class);
|
||||||
doThrow(new ConnectionToRemoteDeviceException("Fake exception")).when(responseValidator).validate(anyString());
|
doThrow(new ConnectionToRemoteDeviceException("Fake exception")).when(responseValidator).validate(anyString());
|
||||||
|
|
||||||
InetSocketAddress tcpAddressEntity = new InetSocketAddress("somehost", 1);
|
InetSocketAddress tcpAddressEntity = new InetSocketAddress("somehost", 1);
|
||||||
|
@ -47,7 +47,7 @@ public class HostDisconnectFromRemoteTcpDeviceTest {
|
|||||||
//Prepare
|
//Prepare
|
||||||
Transport transport = mock(Transport.class);
|
Transport transport = mock(Transport.class);
|
||||||
when(transport.readString()).thenReturn("any string");
|
when(transport.readString()).thenReturn("any string");
|
||||||
HostDisconnectFromRemoteTcpDevice.ResponseValidator responseValidator = mock(HostDisconnectFromRemoteTcpDevice.ResponseValidator.class);
|
HostDisconnectFromRemoteTcpDevice.ResponseValidator responseValidator = mock(HostConnectionCommand.ResponseValidator.class);
|
||||||
doThrow(new ConnectionToRemoteDeviceException("Fake exception")).when(responseValidator).validate(anyString());
|
doThrow(new ConnectionToRemoteDeviceException("Fake exception")).when(responseValidator).validate(anyString());
|
||||||
|
|
||||||
InetSocketAddress inetSocketAddress = new InetSocketAddress("host", 1);
|
InetSocketAddress inetSocketAddress = new InetSocketAddress("host", 1);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user