mirror of
https://github.com/revanced/jadb.git
synced 2025-02-05 23:16:47 +01:00
Merge pull request #92 from janosvitok/remove-duplicate-code
Extract common code from Host{ConnectTo/DisconnectFrom}RemoteTcpDevice
This commit is contained in:
commit
bad9f11441
@ -3,73 +3,27 @@ package se.vidstige.jadb;
|
||||
import java.io.IOException;
|
||||
import java.net.InetSocketAddress;
|
||||
|
||||
class HostConnectToRemoteTcpDevice {
|
||||
private final Transport transport;
|
||||
private final ResponseValidator responseValidator;
|
||||
|
||||
class HostConnectToRemoteTcpDevice extends HostConnectionCommand {
|
||||
HostConnectToRemoteTcpDevice(Transport transport) {
|
||||
this.transport = transport;
|
||||
this.responseValidator = new ResponseValidatorImp();
|
||||
super(transport, new ResponseValidatorImp());
|
||||
}
|
||||
|
||||
//Visible for testing
|
||||
HostConnectToRemoteTcpDevice(Transport transport, ResponseValidator responseValidator) {
|
||||
this.transport = transport;
|
||||
this.responseValidator = responseValidator;
|
||||
super(transport, responseValidator);
|
||||
}
|
||||
|
||||
InetSocketAddress connect(InetSocketAddress inetSocketAddress)
|
||||
throws IOException, JadbException, ConnectionToRemoteDeviceException {
|
||||
transport.send(String.format("host:connect:%s:%d", inetSocketAddress.getHostString(), inetSocketAddress.getPort()));
|
||||
verifyTransportLevel();
|
||||
verifyProtocolLevel();
|
||||
|
||||
return inetSocketAddress;
|
||||
return executeHostCommand("connect", 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 final class ResponseValidatorImp implements ResponseValidator {
|
||||
static final class ResponseValidatorImp extends ResponseValidatorBase {
|
||||
private static final String SUCCESSFULLY_CONNECTED = "connected to";
|
||||
private static final String ALREADY_CONNECTED = "already connected to";
|
||||
|
||||
|
||||
ResponseValidatorImp() {
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
super(SUCCESSFULLY_CONNECTED, ALREADY_CONNECTED);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
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.net.InetSocketAddress;
|
||||
|
||||
public class HostDisconnectFromRemoteTcpDevice {
|
||||
private final Transport transport;
|
||||
private final ResponseValidator responseValidator;
|
||||
|
||||
public class HostDisconnectFromRemoteTcpDevice extends HostConnectionCommand {
|
||||
HostDisconnectFromRemoteTcpDevice(Transport transport) {
|
||||
this.transport = transport;
|
||||
this.responseValidator = new ResponseValidatorImp();
|
||||
super(transport, new ResponseValidatorImp());
|
||||
|
||||
}
|
||||
|
||||
//Visible for testing
|
||||
HostDisconnectFromRemoteTcpDevice(Transport transport, ResponseValidator responseValidator) {
|
||||
this.transport = transport;
|
||||
this.responseValidator = responseValidator;
|
||||
super(transport, responseValidator);
|
||||
}
|
||||
|
||||
InetSocketAddress disconnect(InetSocketAddress inetSocketAddress)
|
||||
throws IOException, JadbException, ConnectionToRemoteDeviceException {
|
||||
transport.send(String.format("host:disconnect:%s:%d", inetSocketAddress.getHostString(), inetSocketAddress.getPort()));
|
||||
verifyTransportLevel();
|
||||
verifyProtocolLevel();
|
||||
|
||||
return inetSocketAddress;
|
||||
return executeHostCommand("disconnect", 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 final class ResponseValidatorImp implements ResponseValidator {
|
||||
static final class ResponseValidatorImp extends ResponseValidatorBase {
|
||||
private static final String SUCCESSFULLY_DISCONNECTED = "disconnected";
|
||||
private static final String ALREADY_DISCONNECTED = "error: no such device";
|
||||
|
||||
ResponseValidatorImp() {
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
super(SUCCESSFULLY_DISCONNECTED, ALREADY_DISCONNECTED);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -59,7 +59,7 @@ public class HostConnectToRemoteTcpDeviceTest {
|
||||
//Prepare
|
||||
Transport transport = mock(Transport.class);
|
||||
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());
|
||||
|
||||
InetSocketAddress tcpAddressEntity = new InetSocketAddress("somehost", 1);
|
||||
|
@ -47,7 +47,7 @@ public class HostDisconnectFromRemoteTcpDeviceTest {
|
||||
//Prepare
|
||||
Transport transport = mock(Transport.class);
|
||||
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());
|
||||
|
||||
InetSocketAddress inetSocketAddress = new InetSocketAddress("host", 1);
|
||||
|
Loading…
x
Reference in New Issue
Block a user