Skip to content
This repository has been archived by the owner on Jan 12, 2024. It is now read-only.

Commit

Permalink
Merge pull request #10 from snieguu/master
Browse files Browse the repository at this point in the history
Devel
  • Loading branch information
szprutamich authored Oct 31, 2019
2 parents def2921 + 785a197 commit 21c0b2d
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 4 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<java.version>1.8</java.version>
<testdroid-api.version>2.84</testdroid-api.version>
<cli.version>1.3.1</cli.version>
<jsch.version>0.1.54</jsch.version>
<jsch.version>0.1.55</jsch.version>
<springboot.version>2.2.0.RELEASE</springboot.version>
</properties>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ public class ADBPortForwarding {

private static final Logger LOGGER = LoggerFactory.getLogger(ADBPortForwarding.class);

private static final String CONNECTION_RESET_MESSAGE = "SocketException: Connection reset";

private static final long CONNECTION_RETRY_DELAY = 1000;

private static final int MAX_CONNECTION_TRIES = 10;

private Session daemonSession;

ADBPortForwarding(JSch jsch, String daemonHost, Integer daemonPort) throws JSchException {
Expand All @@ -21,8 +27,21 @@ public class ADBPortForwarding {
daemonSession.setPortForwardingL(LOCAL_PORT, REMOTE_ADB_HOST, REMOTE_FORWARDED_PORT);
}

public void connect() throws JSchException {
daemonSession.connect();
public void connect() throws JSchException, InterruptedException {
int tries = 1;
while (!daemonSession.isConnected()) {
try {
daemonSession.connect();
} catch (JSchException e) {
if (tries < MAX_CONNECTION_TRIES && shouldBeRetried(e)) {
Thread.sleep(CONNECTION_RETRY_DELAY);
tries++;
} else {
throw e;
}
}
}
LOGGER.debug("SSH tunnel connected after {} tries", tries);
daemonSession.openChannel(CONNECTION_CHANNEL);
if (daemonSession.isConnected()) {
LOGGER.info("ADB forwarding has started");
Expand All @@ -41,4 +60,7 @@ public void disconnect() {
}
}

private boolean shouldBeRetried(JSchException exception) {
return exception.getMessage().contains(CONNECTION_RESET_MESSAGE);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public void handleAPIConnection(APIConnection connection) {
adbPortForwarding = new ADBPortForwarding(jsch, connection.getHost(), connection.getPort());
adbPortForwarding.connect();
connected = true;
} catch (JSchException e) {
} catch (JSchException | InterruptedException e) {
LOGGER.error("Problem occurred while trying to forward ADB port", e);
stop();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ public APIDevice getDevice(Long id) throws APIException {
List<FilterEntry> filters = ctx.getFilters();
filters.add(new NumberFilterEntry(MappingKey.ID, Operand.EQ, id));
APIList<APIDevice> devices = apiClient.getDevices(ctx).getEntity();
if (devices.isEmpty()) {
throw new APIException(String.format("Could not find device with id %d", id));
}
return devices.get(0);
}

Expand Down

0 comments on commit 21c0b2d

Please sign in to comment.