Using extra socket for listing devices as server closes connection after that command.

This commit is contained in:
Samuel Carlsson 2013-07-25 23:36:18 +02:00
parent a4a2e1a6af
commit 8e9b82ee74
2 changed files with 27 additions and 17 deletions

View File

@ -8,34 +8,39 @@ import java.util.List;
public class JadbConnection {
private final Socket _socket;
private final String host;
private final int port;
private static final int DEFAULTPORT = 5037;
private final Transport transport;
private final Transport main;
public JadbConnection() throws UnknownHostException, IOException
{
_socket = new Socket("localhost", DEFAULTPORT);
transport = new Transport(_socket.getOutputStream(), _socket.getInputStream());
host = "localhost";
port = DEFAULTPORT;
main = createTransport();
}
private Transport createTransport() throws IOException {
return new Transport(new Socket(host, port));
}
public void getHostVersion() throws IOException, JadbException {
transport.send("host:version");
transport.verifyResponse();
main.send("host:version");
main.verifyResponse();
}
public List<AndroidDevice> getDevices() throws IOException, JadbException
{
transport.send("host:devices");
transport.verifyResponse();
String body = transport.readString();
Transport devices = createTransport();
devices.send("host:devices");
devices.verifyResponse();
String body = devices.readString();
return parseDevices(body);
}
public void close() throws IOException
{
_socket.close();
}
}
private List<AndroidDevice> parseDevices(String body) {
String[] lines = body.split("\n");
@ -43,7 +48,7 @@ public class JadbConnection {
for (String line : lines)
{
String[] parts = line.split("\t");
devices.add(new AndroidDevice(parts[0], parts[1], transport));
devices.add(new AndroidDevice(parts[0], parts[1], main));
}
return devices;
}

View File

@ -6,6 +6,7 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.nio.charset.Charset;
class Transport {
@ -13,11 +14,15 @@ class Transport {
private final OutputStream outputStream;
private final InputStream inputStream;
public Transport(OutputStream outputStream, InputStream inputStream) {
private Transport(OutputStream outputStream, InputStream inputStream) {
this.outputStream = outputStream;
this.inputStream = inputStream;
}
public Transport(Socket socket) throws IOException {
this(socket.getOutputStream(), socket.getInputStream());
}
public String readString() throws IOException {
String encodedLength = readString(4);
int length = Integer.parseInt(encodedLength, 16);