Automatically close forked beacon connections, fix connection forwards

This commit is contained in:
Christopher Schnick 2022-08-19 16:14:49 +02:00
parent e0266fec6b
commit 6efad1fe42
6 changed files with 10 additions and 19 deletions

View file

@ -30,7 +30,7 @@ public class BeaconConfig {
public static final String BEACON_PORT_PROP = "io.xpipe.beacon.port"; public static final String BEACON_PORT_PROP = "io.xpipe.beacon.port";
public static final int DEFAULT_PORT = 21721; public static final int DEFAULT_PORT = System.getProperty("os.name").startsWith("Windows") ? 21721 : 21722;
public static int getUsedPort() { public static int getUsedPort() {
if (System.getProperty(BEACON_PORT_PROP) != null) { if (System.getProperty(BEACON_PORT_PROP) != null) {

View file

@ -6,8 +6,6 @@ import lombok.experimental.UtilityClass;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.net.DatagramSocket;
import java.net.ServerSocket;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.Optional; import java.util.Optional;
@ -18,19 +16,14 @@ import java.util.Optional;
@UtilityClass @UtilityClass
public class BeaconServer { public class BeaconServer {
private static boolean isPortAvailable(int port) { public static boolean isRunning() {
try (var ignored = new ServerSocket(port); var ignored1 = new DatagramSocket(port)) { try (var socket = new BeaconClient()) {
return true; return true;
} catch (IOException e) { } catch (Exception e) {
return false; return false;
} }
} }
public static boolean isRunning() {
var port = BeaconConfig.getUsedPort();
return !isPortAvailable(port);
}
private static void startFork(String custom) throws IOException { private static void startFork(String custom) throws IOException {
boolean print = BeaconConfig.execDebugEnabled(); boolean print = BeaconConfig.execDebugEnabled();
if (print) { if (print) {
@ -72,14 +65,17 @@ public class BeaconServer {
}, "daemon fork syserr").start(); }, "daemon fork syserr").start();
} }
public static boolean tryStart() throws Exception { public static boolean tryStartFork() throws Exception {
var custom = BeaconConfig.getCustomExecCommand(); var custom = BeaconConfig.getCustomExecCommand();
if (custom != null) { if (custom != null) {
System.out.println("Starting fork: " + custom); System.out.println("Starting fork: " + custom);
startFork(custom); startFork(custom);
return true; return true;
} }
return false;
}
public static boolean tryStart() throws Exception {
var daemonExecutable = getDaemonExecutable(); var daemonExecutable = getDaemonExecutable();
if (daemonExecutable.isPresent()) { if (daemonExecutable.isPresent()) {
if (BeaconConfig.debugEnabled()) { if (BeaconConfig.debugEnabled()) {
@ -99,7 +95,7 @@ public class BeaconServer {
public static boolean tryStop(BeaconClient client) throws Exception { public static boolean tryStop(BeaconClient client) throws Exception {
client.sendRequest(StopExchange.Request.builder().build()); client.sendRequest(StopExchange.Request.builder().build());
StopExchange.Response res =client.receiveResponse(); StopExchange.Response res = client.receiveResponse();
return res.isSuccess(); return res.isSuccess();
} }

View file

@ -18,7 +18,6 @@ public interface RawReadConnection extends DataSourceReadConnection {
default void forward(DataSourceConnection con) throws Exception { default void forward(DataSourceConnection con) throws Exception {
try (var tCon = (RawWriteConnection) con) { try (var tCon = (RawWriteConnection) con) {
tCon.init();
byte[] b; byte[] b;
while ((b = readBytes(BUFFER_SIZE)).length > 0) { while ((b = readBytes(BUFFER_SIZE)).length > 0) {
tCon.write(b); tCon.write(b);

View file

@ -11,7 +11,6 @@ public interface StructureReadConnection extends DataSourceReadConnection {
default void forward(DataSourceConnection con) throws Exception { default void forward(DataSourceConnection con) throws Exception {
try (var tCon = (StructureWriteConnection) con) { try (var tCon = (StructureWriteConnection) con) {
tCon.init();
tCon.write(read()); tCon.write(read());
} }
} }

View file

@ -28,7 +28,6 @@ public interface TextReadConnection extends DataSourceReadConnection {
default void forward(DataSourceConnection con) throws Exception { default void forward(DataSourceConnection con) throws Exception {
try (var tCon = (TextWriteConnection) con) { try (var tCon = (TextWriteConnection) con) {
tCon.init();
for (var it = lines().iterator(); it.hasNext(); ) { for (var it = lines().iterator(); it.hasNext(); ) {
tCon.writeLine(it.next()); tCon.writeLine(it.next());
} }

View file

@ -78,9 +78,7 @@ public interface DataStoreProvider {
return null; return null;
} }
default DataStore defaultStore() { DataStore defaultStore();
throw new ExtensionException("CLI Dialog not implemented by provider");
}
default String display(DataStore store) { default String display(DataStore store) {
return store.toSummaryString(); return store.toSummaryString();