diff --git a/.idea/compiler.xml b/.idea/compiler.xml
deleted file mode 100644
index f329bae..0000000
--- a/.idea/compiler.xml
+++ /dev/null
@@ -1,32 +0,0 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/.idea/misc.xml b/.idea/misc.xml
deleted file mode 100644
index b6da4f3..0000000
--- a/.idea/misc.xml
+++ /dev/null
@@ -1,17 +0,0 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/jadb.iml b/jadb.iml
deleted file mode 100644
index c419de5..0000000
--- a/jadb.iml
+++ /dev/null
@@ -1,17 +0,0 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/pom.xml b/pom.xml
index 1490413..31fb6ce 100644
--- a/pom.xml
+++ b/pom.xml
@@ -39,6 +39,13 @@
4.10
test
+
+
+ junit
+ junit
+ 4.10
+ test
+
diff --git a/src/se/vidstige/jadb/ConnectionToRemoteDeviceException.java b/src/se/vidstige/jadb/ConnectionToRemoteDeviceException.java
new file mode 100644
index 0000000..f4719bb
--- /dev/null
+++ b/src/se/vidstige/jadb/ConnectionToRemoteDeviceException.java
@@ -0,0 +1,7 @@
+package se.vidstige.jadb;
+
+public class ConnectionToRemoteDeviceException extends Exception {
+ public ConnectionToRemoteDeviceException(String message) {
+ super(message);
+ }
+}
diff --git a/src/se/vidstige/jadb/HostConnectToRemoteTcpDevice.java b/src/se/vidstige/jadb/HostConnectToRemoteTcpDevice.java
new file mode 100644
index 0000000..6b6b1a5
--- /dev/null
+++ b/src/se/vidstige/jadb/HostConnectToRemoteTcpDevice.java
@@ -0,0 +1,65 @@
+package se.vidstige.jadb;
+
+import se.vidstige.jadb.entities.TcpAddressEntity;
+
+import java.io.IOException;
+
+public class HostConnectToRemoteTcpDevice {
+ private final Transport transport;
+
+ public HostConnectToRemoteTcpDevice(Transport transport) {
+ this.transport = transport;
+ }
+
+ public TcpAddressEntity connect(TcpAddressEntity tcpAddressEntity)
+ throws IOException, JadbException, ConnectionToRemoteDeviceException {
+ transport.send(String.format("host:connect:%s:%d", tcpAddressEntity.getHost(), tcpAddressEntity.getPort()));
+ verifyProtocolLevel();
+ verifyCommandLevel();
+
+ return tcpAddressEntity;
+ }
+
+ private void verifyProtocolLevel() throws IOException, JadbException {
+ transport.verifyResponse();
+ }
+
+ private void verifyCommandLevel() throws IOException, ConnectionToRemoteDeviceException {
+ String status = transport.readString();
+ new ResponseValidator(status).validate();
+ }
+
+ final static class ResponseValidator {
+ private final static String SUCCESSFULLY_CONNECTED = "connected to";
+ private final static String ALREADY_CONNECTED = "already connected to";
+
+ private final String response;
+
+ public ResponseValidator(String response) {
+ this.response = response;
+ }
+
+ public void validate() throws ConnectionToRemoteDeviceException {
+ if(!checkIfConnectedSuccessfully() && !checkIfAlreadyConnected()) {
+ throw new ConnectionToRemoteDeviceException(extractError());
+ }
+ }
+
+ private boolean checkIfConnectedSuccessfully() {
+ return response.contains(SUCCESSFULLY_CONNECTED);
+ }
+
+ private boolean checkIfAlreadyConnected() {
+ return response.equals(ALREADY_CONNECTED);
+ }
+
+ private String extractError() {
+ int lastColon = response.lastIndexOf(":");
+ if(lastColon != -1) {
+ return response.substring(lastColon, response.length());
+ } else {
+ return response;
+ }
+ }
+ }
+}
diff --git a/src/se/vidstige/jadb/JadbConnection.java b/src/se/vidstige/jadb/JadbConnection.java
index 7a76ec1..2956b72 100644
--- a/src/se/vidstige/jadb/JadbConnection.java
+++ b/src/se/vidstige/jadb/JadbConnection.java
@@ -1,5 +1,7 @@
package se.vidstige.jadb;
+import se.vidstige.jadb.entities.TcpAddressEntity;
+
import java.io.IOException;
import java.net.Socket;
import java.util.ArrayList;
@@ -34,6 +36,16 @@ public class JadbConnection implements ITransportFactory {
return version;
}
+ public TcpAddressEntity connectToTcpDevice(TcpAddressEntity tcpAddressEntity)
+ throws IOException, JadbException, ConnectionToRemoteDeviceException {
+ Transport transport = createTransport();
+ try {
+ return new HostConnectToRemoteTcpDevice(transport).connect(tcpAddressEntity);
+ } finally {
+ transport.close();
+ }
+ }
+
public List getDevices() throws IOException, JadbException {
Transport devices = createTransport();
devices.send("host:devices");
diff --git a/src/se/vidstige/jadb/entities/TcpAddressEntity.java b/src/se/vidstige/jadb/entities/TcpAddressEntity.java
new file mode 100644
index 0000000..3979ce2
--- /dev/null
+++ b/src/se/vidstige/jadb/entities/TcpAddressEntity.java
@@ -0,0 +1,42 @@
+package se.vidstige.jadb.entities;
+
+
+/**
+ * Tcp address
+ */
+public final class TcpAddressEntity {
+ private final String host;
+ private final Integer port;
+
+ public TcpAddressEntity(String host, Integer port) {
+ this.host = host;
+ this.port = port;
+ }
+
+ public String getHost() {
+ return host;
+ }
+
+ public Integer getPort() {
+ return port;
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ if (this == o) return true;
+ if (!(o instanceof TcpAddressEntity)) return false;
+
+ TcpAddressEntity that = (TcpAddressEntity) o;
+
+ if (!host.equals(that.host)) return false;
+ return port.equals(that.port);
+ }
+
+ @Override
+ public int hashCode() {
+ int result = host.hashCode();
+ result = 31 * result + port.hashCode();
+ return result;
+ }
+}
+
diff --git a/test/se/vidstige/jadb/HostConnectToRemoteTcpDeviceTest.java b/test/se/vidstige/jadb/HostConnectToRemoteTcpDeviceTest.java
new file mode 100644
index 0000000..62e93e8
--- /dev/null
+++ b/test/se/vidstige/jadb/HostConnectToRemoteTcpDeviceTest.java
@@ -0,0 +1,20 @@
+package se.vidstige.jadb;
+
+import org.junit.Test;
+
+import static org.junit.Assert.*;
+
+public class HostConnectToRemoteTcpDeviceTest {
+ @Test
+ public void testNormalConnection() {
+ //mock()
+
+ //HostConnectToRemoteTcpDevice hostConnectToRemoteTcpDevice = new HostConnectToRemoteTcpDevice();
+ }
+
+
+ @Test
+ public void parserTest() {
+
+ }
+}
\ No newline at end of file
diff --git a/test/se/vidstige/jadb/test/integration/RealDeviceTestCases.java b/test/se/vidstige/jadb/test/integration/RealDeviceTestCases.java
index be6120c..f3bdcff 100644
--- a/test/se/vidstige/jadb/test/integration/RealDeviceTestCases.java
+++ b/test/se/vidstige/jadb/test/integration/RealDeviceTestCases.java
@@ -7,6 +7,7 @@ import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import se.vidstige.jadb.*;
+import se.vidstige.jadb.entities.TcpAddressEntity;
import java.io.ByteArrayOutputStream;
import java.io.File;
@@ -117,4 +118,10 @@ public class RealDeviceTestCases {
if (outputStream != null) outputStream.close();
}
}
+
+
+ @Test
+ public void testConnectionToTcpDevice() throws IOException, JadbException, ConnectionToRemoteDeviceException {
+ TcpAddressEntity tcpAddressEntity = jadb.connectToTcpDevice(new TcpAddressEntity("127.0.0.1", 10001));
+ }
}