mirror of
https://github.com/revanced/jadb.git
synced 2025-05-30 21:30:16 +02:00
Refactor: Using exceptions to send errors back to client in mocked server.
This commit is contained in:
parent
2e9dd17bdd
commit
25745857b7
@ -1,6 +1,7 @@
|
|||||||
package se.vidstige.jadb.server;
|
package se.vidstige.jadb.server;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
|
import java.net.ProtocolException;
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
import java.nio.charset.Charset;
|
import java.nio.charset.Charset;
|
||||||
|
|
||||||
@ -13,17 +14,30 @@ public class AdbProtocolHandler implements Runnable {
|
|||||||
this.responder = responder;
|
this.responder = responder;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private AdbDeviceResponder findDevice(String serial) throws ProtocolException {
|
||||||
|
for (AdbDeviceResponder d : responder.getDevices())
|
||||||
|
{
|
||||||
|
if (d.getSerial().equals(serial)) return d;
|
||||||
|
}
|
||||||
|
throw new ProtocolException("'" + serial + "' not connected");
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run()
|
public void run()
|
||||||
{
|
{
|
||||||
System.out.println("Serving client");
|
try{
|
||||||
|
runServer();
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.out.println("IO Error: " + e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
private void runServer() throws IOException {
|
||||||
|
DataInput input = new DataInputStream(socket.getInputStream());
|
||||||
|
OutputStreamWriter output = new OutputStreamWriter(socket.getOutputStream());
|
||||||
|
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
DataInput input = new DataInputStream(socket.getInputStream());
|
|
||||||
OutputStreamWriter output = new OutputStreamWriter(socket.getOutputStream());
|
|
||||||
byte[] buffer = new byte[4];
|
byte[] buffer = new byte[4];
|
||||||
input.readFully(buffer);
|
input.readFully(buffer);
|
||||||
String encodedLength = new String(buffer, Charset.forName("utf-8"));
|
String encodedLength = new String(buffer, Charset.forName("utf-8"));
|
||||||
@ -35,12 +49,15 @@ public class AdbProtocolHandler implements Runnable {
|
|||||||
|
|
||||||
responder.onCommand(command);
|
responder.onCommand(command);
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
if ("host:version".equals(command)) {
|
if ("host:version".equals(command)) {
|
||||||
output.write("OKAY");
|
output.write("OKAY");
|
||||||
send(output, String.format("%04x", responder.getVersion()));
|
send(output, String.format("%04x", responder.getVersion()));
|
||||||
}
|
}
|
||||||
else if ("host:transport-any".equals(command))
|
else if ("host:transport-any".equals(command))
|
||||||
{
|
{
|
||||||
|
// TODO: Check so that exactly one device is selected.
|
||||||
output.write("OKAY");
|
output.write("OKAY");
|
||||||
}
|
}
|
||||||
else if ("host:devices".equals(command)) {
|
else if ("host:devices".equals(command)) {
|
||||||
@ -53,16 +70,21 @@ public class AdbProtocolHandler implements Runnable {
|
|||||||
output.write("OKAY");
|
output.write("OKAY");
|
||||||
send(output, new String(tmp.toByteArray(), Charset.forName("utf-8")));
|
send(output, new String(tmp.toByteArray(), Charset.forName("utf-8")));
|
||||||
}
|
}
|
||||||
|
else if (command.startsWith("host:transport:"))
|
||||||
|
{
|
||||||
|
String serial = command.substring("host:transport:".length());
|
||||||
|
findDevice(serial);
|
||||||
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
throw new ProtocolException("Unknown command: " + command);
|
||||||
|
}
|
||||||
|
} catch (ProtocolException e) {
|
||||||
output.write("FAIL");
|
output.write("FAIL");
|
||||||
send(output, "Unknown command: " + command);
|
send(output, e.getMessage());
|
||||||
}
|
}
|
||||||
output.flush();
|
output.flush();
|
||||||
}
|
}
|
||||||
} catch (IOException e) {
|
|
||||||
System.out.println("IO Error: " + e.getMessage());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private String getCommandLength(String command) {
|
private String getCommandLength(String command) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user