2013-07-25 21:13:20 +02:00
|
|
|
package se.vidstige.jadb;
|
|
|
|
|
2017-03-17 17:48:13 +01:00
|
|
|
import se.vidstige.jadb.entities.TcpAddressEntity;
|
|
|
|
|
2013-07-25 21:13:20 +02:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.net.Socket;
|
2013-07-25 21:52:46 +02:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
2013-07-25 21:13:20 +02:00
|
|
|
|
2016-03-20 09:13:30 +01:00
|
|
|
public class JadbConnection implements ITransportFactory {
|
|
|
|
|
|
|
|
private final String host;
|
|
|
|
private final int port;
|
|
|
|
|
|
|
|
private static final int DEFAULTPORT = 5037;
|
|
|
|
|
2016-12-08 18:46:03 +01:00
|
|
|
public JadbConnection() {
|
2016-03-20 09:13:30 +01:00
|
|
|
this("localhost", DEFAULTPORT);
|
|
|
|
}
|
|
|
|
|
2016-12-08 18:46:03 +01:00
|
|
|
public JadbConnection(String host, int port) {
|
2016-03-20 09:13:30 +01:00
|
|
|
this.host = host;
|
|
|
|
this.port = port;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Transport createTransport() throws IOException {
|
|
|
|
return new Transport(new Socket(host, port));
|
|
|
|
}
|
|
|
|
|
2016-12-08 18:46:03 +01:00
|
|
|
public String getHostVersion() throws IOException, JadbException {
|
2016-03-20 09:13:30 +01:00
|
|
|
Transport main = createTransport();
|
|
|
|
main.send("host:version");
|
|
|
|
main.verifyResponse();
|
2016-12-08 18:46:03 +01:00
|
|
|
String version = main.readString();
|
2016-03-20 09:13:30 +01:00
|
|
|
main.close();
|
2016-12-08 18:46:03 +01:00
|
|
|
return version;
|
2016-03-20 09:13:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-17 17:48:13 +01:00
|
|
|
public TcpAddressEntity connectToTcpDevice(TcpAddressEntity tcpAddressEntity)
|
|
|
|
throws IOException, JadbException, ConnectionToRemoteDeviceException {
|
|
|
|
Transport transport = createTransport();
|
|
|
|
try {
|
|
|
|
return new HostConnectToRemoteTcpDevice(transport).connect(tcpAddressEntity);
|
|
|
|
} finally {
|
|
|
|
transport.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-20 11:18:56 +01:00
|
|
|
public TcpAddressEntity connectFromTcpDevice(TcpAddressEntity tcpAddressEntity)
|
|
|
|
throws IOException, JadbException, ConnectionToRemoteDeviceException {
|
|
|
|
Transport transport = createTransport();
|
|
|
|
try {
|
|
|
|
return new HostConnectToRemoteTcpDevice(transport).connect(tcpAddressEntity);
|
|
|
|
} finally {
|
|
|
|
transport.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-20 09:13:30 +01:00
|
|
|
public List<JadbDevice> getDevices() throws IOException, JadbException {
|
|
|
|
Transport devices = createTransport();
|
|
|
|
devices.send("host:devices");
|
|
|
|
devices.verifyResponse();
|
|
|
|
String body = devices.readString();
|
2016-10-23 16:55:31 +02:00
|
|
|
devices.close();
|
2016-03-20 09:13:30 +01:00
|
|
|
return parseDevices(body);
|
|
|
|
}
|
|
|
|
|
2016-10-26 16:49:14 +02:00
|
|
|
public DeviceWatcher createDeviceWatcher(DeviceDetectionListener listener) throws IOException, JadbException {
|
|
|
|
Transport transport = createTransport();
|
|
|
|
transport.send("host:track-devices");
|
|
|
|
transport.verifyResponse();
|
|
|
|
return new DeviceWatcher(transport, listener, this);
|
2016-10-23 16:55:31 +02:00
|
|
|
}
|
|
|
|
|
2016-10-26 16:49:14 +02:00
|
|
|
public List<JadbDevice> parseDevices(String body) {
|
2016-03-20 09:13:30 +01:00
|
|
|
String[] lines = body.split("\n");
|
|
|
|
ArrayList<JadbDevice> devices = new ArrayList<JadbDevice>(lines.length);
|
|
|
|
for (String line : lines) {
|
|
|
|
String[] parts = line.split("\t");
|
2014-03-20 17:30:21 +01:00
|
|
|
if (parts.length > 1) {
|
2016-03-20 09:13:30 +01:00
|
|
|
devices.add(new JadbDevice(parts[0], parts[1], this));
|
2014-03-20 17:30:21 +01:00
|
|
|
}
|
2016-03-20 09:13:30 +01:00
|
|
|
}
|
|
|
|
return devices;
|
|
|
|
}
|
2014-03-19 09:41:14 +01:00
|
|
|
|
2014-03-20 09:50:17 +01:00
|
|
|
public JadbDevice getAnyDevice() {
|
2016-02-28 03:04:59 +01:00
|
|
|
return JadbDevice.createAny(this);
|
2014-03-19 09:41:14 +01:00
|
|
|
}
|
2013-07-25 21:13:20 +02:00
|
|
|
}
|