Rework many parts and move some components into the core module

This commit is contained in:
Christopher Schnick
2022-01-08 04:30:16 +01:00
parent e5a84a83db
commit b192fd5b09
29 changed files with 644 additions and 67 deletions

View File

@@ -12,6 +12,8 @@ import io.xpipe.beacon.message.RequestMessage;
import io.xpipe.beacon.message.ResponseMessage;
import io.xpipe.beacon.message.ServerErrorMessage;
import io.xpipe.core.util.JacksonHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.io.InputStream;
@@ -25,12 +27,20 @@ import static io.xpipe.beacon.BeaconConfig.BODY_SEPARATOR;
public class BeaconClient {
private static final Logger log = LoggerFactory.getLogger("beacon");
@FunctionalInterface
public interface FailableBiConsumer<T, U, E extends Throwable> {
void accept(T var1, U var2) throws E;
}
@FunctionalInterface
public interface FailableBiPredicate<T, U, E extends Throwable> {
boolean test(T var1, U var2) throws E;
}
@FunctionalInterface
public interface FailableConsumer<T, E extends Throwable> {
@@ -70,7 +80,7 @@ public class BeaconClient {
public <REQ extends RequestMessage, RES extends ResponseMessage> void exchange(
REQ req,
FailableConsumer<OutputStream, IOException> reqWriter,
FailableBiConsumer<RES, InputStream, IOException> resReader)
FailableBiPredicate<RES, InputStream, IOException> resReader)
throws ConnectorException, ClientException, ServerException {
try {
sendRequest(req);
@@ -85,11 +95,12 @@ public class BeaconClient {
throw new ConnectorException("Invalid body separator");
}
resReader.accept(res, in);
if (resReader.test(res, in)) {
close();
}
} catch (IOException ex) {
throw new ConnectorException("Couldn't communicate with socket", ex);
} finally {
close();
throw new ConnectorException("Couldn't communicate with socket", ex);
}
}
@@ -116,6 +127,7 @@ public class BeaconClient {
var msg = JsonNodeFactory.instance.objectNode();
msg.set("xPipeMessage", json);
log.atTrace().addKeyValue("class", req.getClass().getSimpleName()).log("Sending request to server");
try {
var mapper = JacksonHelper.newMapper().disable(JsonGenerator.Feature.AUTO_CLOSE_TARGET);

View File

@@ -16,7 +16,7 @@ public abstract class BeaconConnector {
protected <REQ extends RequestMessage, RES extends ResponseMessage> void performInputExchange(
BeaconClient socket,
REQ req,
BeaconClient.FailableBiConsumer<RES, InputStream, IOException> responseConsumer) throws ServerException, ConnectorException, ClientException {
BeaconClient.FailableBiPredicate<RES, InputStream, IOException> responseConsumer) throws ServerException, ConnectorException, ClientException {
performInputOutputExchange(socket, req, null, responseConsumer);
}
@@ -24,7 +24,7 @@ public abstract class BeaconConnector {
BeaconClient socket,
REQ req,
BeaconClient.FailableConsumer<OutputStream, IOException> reqWriter,
BeaconClient.FailableBiConsumer<RES, InputStream, IOException> responseConsumer)
BeaconClient.FailableBiPredicate<RES, InputStream, IOException> responseConsumer)
throws ServerException, ConnectorException, ClientException {
socket.exchange(req, reqWriter, responseConsumer);
}
@@ -37,6 +37,7 @@ public abstract class BeaconConnector {
AtomicReference<RES> response = new AtomicReference<>();
socket.exchange(req, reqWriter, (RES res, InputStream in) -> {
response.set(res);
return true;
});
return response.get();
}

View File

@@ -18,7 +18,5 @@ public interface BeaconHandler {
public void sendServerErrorResponse(Throwable ex) throws Exception;
InputStream getInputStream() throws Exception;
OutputStream getOutputStream() throws Exception;
}

View File

@@ -2,9 +2,9 @@ package io.xpipe.beacon.exchange;
import io.xpipe.beacon.message.RequestMessage;
import io.xpipe.beacon.message.ResponseMessage;
import io.xpipe.core.source.DataSourceId;
import io.xpipe.core.source.DataSourceType;
import io.xpipe.core.source.DataSourceConfig;
import io.xpipe.core.source.DataSourceId;
import io.xpipe.core.source.DataSourceInfo;
import lombok.Builder;
import lombok.Value;
import lombok.extern.jackson.Jacksonized;
@@ -41,12 +41,7 @@ public class StoreResourceExchange implements MessageExchange<StoreResourceExcha
@Value
public static class Response implements ResponseMessage {
DataSourceId sourceId;
DataSourceType sourceType;
DataSourceConfig config;
Object data;
public ReadInfoExchange.TableData getTableData() {
return (ReadInfoExchange.TableData) data;
}
DataSourceInfo info;
}
}