Skip to content

Commit

Permalink
fix: more port release
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhouYixun committed Dec 14, 2022
1 parent 345d0b8 commit 53319d0
Showing 1 changed file with 24 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import java.io.File;
import java.io.IOException;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand All @@ -69,6 +70,8 @@ public class AndroidDeviceBridgeTool implements ApplicationListener<ContextRefre
private static String uiaApkVersion;
private static String apkVersion;
private static RestTemplate restTemplate;

private static Map<String, Integer> forwardPortMap = new ConcurrentHashMap<>();
@Value("${sonic.saa}")
private String ver;
@Value("${sonic.saus}")
Expand Down Expand Up @@ -288,18 +291,30 @@ public static boolean checkUiaApkVersion(IDevice iDevice) {
* @date 2021/8/16 19:52
*/
public static void forward(IDevice iDevice, int port, String service) {
String name = String.format("process-%s-forward-%s", iDevice.getSerialNumber(), service);
Integer oldP = forwardPortMap.get(name);
if (oldP != null) {
removeForward(iDevice, oldP, service);
}
try {
logger.info("{} device {} port forward to {}", iDevice.getSerialNumber(), service, port);
iDevice.createForward(port, service, IDevice.DeviceUnixSocketNamespace.ABSTRACT);
forwardPortMap.put(name, port);
} catch (Exception e) {
logger.error(e.getMessage());
}
}

public static void forward(IDevice iDevice, int port, int target) {
String name = String.format("process-%s-forward-%d", iDevice.getSerialNumber(), target);
Integer oldP = forwardPortMap.get(name);
if (oldP != null) {
removeForward(iDevice, oldP, target);
}
try {
logger.info("{} device {} forward to {}", iDevice.getSerialNumber(), target, port);
iDevice.createForward(port, target);
forwardPortMap.put(name, port);
} catch (Exception e) {
logger.error(e.getMessage());
}
Expand All @@ -318,6 +333,10 @@ public static void removeForward(IDevice iDevice, int port, String serviceName)
try {
logger.info("cancel {} device {} port forward to {}", iDevice.getSerialNumber(), serviceName, port);
iDevice.removeForward(port, serviceName, IDevice.DeviceUnixSocketNamespace.ABSTRACT);
String name = String.format("process-%s-forward-%s", iDevice.getSerialNumber(), serviceName);
if (forwardPortMap.get(name) != null) {
forwardPortMap.remove(name);
}
} catch (Exception e) {
logger.error(e.getMessage());
}
Expand All @@ -327,6 +346,10 @@ public static void removeForward(IDevice iDevice, int port, int target) {
try {
logger.info("cancel {} device {} forward to {}", iDevice.getSerialNumber(), target, port);
iDevice.removeForward(port, target);
String name = String.format("process-%s-forward-%d", iDevice.getSerialNumber(), target);
if (forwardPortMap.get(name) != null) {
forwardPortMap.remove(name);
}
} catch (Exception e) {
logger.error(e.getMessage());
}
Expand Down Expand Up @@ -734,7 +757,7 @@ public boolean isCancelled() {
}, 0, TimeUnit.MILLISECONDS);
} catch (Exception e) {
} finally {
AndroidDeviceBridgeTool.removeForward(iDevice, port, 6790);
removeForward(iDevice, port, 6790);
}
}

Expand Down

0 comments on commit 53319d0

Please sign in to comment.