mirror of
https://github.com/AsamK/signal-cli
synced 2025-08-29 10:30:38 +00:00
Refactor JsonRpcReader to for handling a single message
This commit is contained in:
parent
ae678871ec
commit
bf76c04664
1 changed files with 83 additions and 47 deletions
|
@ -13,6 +13,7 @@ import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
import java.util.function.Supplier;
|
import java.util.function.Supplier;
|
||||||
|
@ -24,21 +25,56 @@ public class JsonRpcReader {
|
||||||
|
|
||||||
private final JsonRpcSender jsonRpcSender;
|
private final JsonRpcSender jsonRpcSender;
|
||||||
private final ObjectMapper objectMapper;
|
private final ObjectMapper objectMapper;
|
||||||
|
private final InputStream input;
|
||||||
private final Supplier<String> lineSupplier;
|
private final Supplier<String> lineSupplier;
|
||||||
|
|
||||||
public JsonRpcReader(
|
public JsonRpcReader(final JsonRpcSender jsonRpcSender, final Supplier<String> lineSupplier) {
|
||||||
final JsonRpcSender jsonRpcSender, final Supplier<String> lineSupplier
|
|
||||||
) {
|
|
||||||
this.jsonRpcSender = jsonRpcSender;
|
this.jsonRpcSender = jsonRpcSender;
|
||||||
|
this.input = null;
|
||||||
this.lineSupplier = lineSupplier;
|
this.lineSupplier = lineSupplier;
|
||||||
this.objectMapper = Util.createJsonObjectMapper();
|
this.objectMapper = Util.createJsonObjectMapper();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void readMessages(final RequestHandler requestHandler, final Consumer<JsonRpcResponse> responseHandler) {
|
public JsonRpcReader(final JsonRpcSender jsonRpcSender, final InputStream input) {
|
||||||
while (!Thread.interrupted()) {
|
this.jsonRpcSender = jsonRpcSender;
|
||||||
JsonRpcMessage message = readMessage();
|
this.input = input;
|
||||||
if (message == null) break;
|
this.lineSupplier = null;
|
||||||
|
this.objectMapper = Util.createJsonObjectMapper();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void readMessages(final RequestHandler requestHandler, final Consumer<JsonRpcResponse> responseHandler) {
|
||||||
|
if (input != null) {
|
||||||
|
JsonRpcMessage message = parseJsonRpcMessage(input);
|
||||||
|
if (message == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
handleMessage(message, requestHandler, responseHandler);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
while (!Thread.interrupted()) {
|
||||||
|
String input = lineSupplier.get();
|
||||||
|
if (input == null) {
|
||||||
|
logger.trace("Reached end of JSON-RPC input stream.");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.trace("Incoming JSON-RPC message: {}", input);
|
||||||
|
JsonRpcMessage message = parseJsonRpcMessage(input);
|
||||||
|
if (message == null) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
handleMessage(message, requestHandler, responseHandler);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void handleMessage(
|
||||||
|
final JsonRpcMessage message,
|
||||||
|
final RequestHandler requestHandler,
|
||||||
|
final Consumer<JsonRpcResponse> responseHandler
|
||||||
|
) {
|
||||||
if (message instanceof final JsonRpcRequest jsonRpcRequest) {
|
if (message instanceof final JsonRpcRequest jsonRpcRequest) {
|
||||||
logger.debug("Received json rpc request, method: " + jsonRpcRequest.getMethod());
|
logger.debug("Received json rpc request, method: " + jsonRpcRequest.getMethod());
|
||||||
final var response = handleRequest(requestHandler, jsonRpcRequest);
|
final var response = handleRequest(requestHandler, jsonRpcRequest);
|
||||||
|
@ -62,7 +98,6 @@ public class JsonRpcReader {
|
||||||
jsonRpcSender.sendBatchResponses(responseList);
|
jsonRpcSender.sendBatchResponses(responseList);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
private JsonRpcResponse handleRequest(final RequestHandler requestHandler, final JsonRpcRequest request) {
|
private JsonRpcResponse handleRequest(final RequestHandler requestHandler, final JsonRpcRequest request) {
|
||||||
try {
|
try {
|
||||||
|
@ -85,25 +120,6 @@ public class JsonRpcReader {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
private JsonRpcMessage readMessage() {
|
|
||||||
while (!Thread.interrupted()) {
|
|
||||||
String input = lineSupplier.get();
|
|
||||||
|
|
||||||
if (input == null) {
|
|
||||||
logger.trace("Reached end of JSON-RPC input stream.");
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
logger.trace("Incoming JSON-RPC message: {}", input);
|
|
||||||
JsonRpcMessage message = parseJsonRpcMessage(input);
|
|
||||||
if (message == null) continue;
|
|
||||||
|
|
||||||
return message;
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
private JsonRpcMessage parseJsonRpcMessage(final String input) {
|
private JsonRpcMessage parseJsonRpcMessage(final String input) {
|
||||||
final JsonNode jsonNode;
|
final JsonNode jsonNode;
|
||||||
try {
|
try {
|
||||||
|
@ -117,6 +133,26 @@ public class JsonRpcReader {
|
||||||
throw new AssertionError(e);
|
throw new AssertionError(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return parseJsonRpcMessage(jsonNode);
|
||||||
|
}
|
||||||
|
|
||||||
|
private JsonRpcMessage parseJsonRpcMessage(final InputStream input) {
|
||||||
|
final JsonNode jsonNode;
|
||||||
|
try {
|
||||||
|
jsonNode = objectMapper.readTree(input);
|
||||||
|
} catch (JsonParseException e) {
|
||||||
|
jsonRpcSender.sendResponse(JsonRpcResponse.forError(new JsonRpcResponse.Error(JsonRpcResponse.Error.PARSE_ERROR,
|
||||||
|
e.getMessage(),
|
||||||
|
null), null));
|
||||||
|
return null;
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new AssertionError(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
return parseJsonRpcMessage(jsonNode);
|
||||||
|
}
|
||||||
|
|
||||||
|
private JsonRpcMessage parseJsonRpcMessage(final JsonNode jsonNode) {
|
||||||
if (jsonNode == null) {
|
if (jsonNode == null) {
|
||||||
jsonRpcSender.sendResponse(JsonRpcResponse.forError(new JsonRpcResponse.Error(JsonRpcResponse.Error.INVALID_REQUEST,
|
jsonRpcSender.sendResponse(JsonRpcResponse.forError(new JsonRpcResponse.Error(JsonRpcResponse.Error.INVALID_REQUEST,
|
||||||
"invalid request",
|
"invalid request",
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue