- final boolean ignoreAttachments = Boolean.TRUE.equals(ns.getBoolean("ignore-attachments"));
- m.setIgnoreAttachments(ignoreAttachments);
-
- final var objectMapper = Util.createJsonObjectMapper();
- final var jsonRpcSender = new JsonRpcSender((JsonWriter) outputWriter);
-
- final var receiveThread = receiveMessages(s -> jsonRpcSender.sendRequest(JsonRpcRequest.forNotification(
- "receive",
- objectMapper.valueToTree(s),
- null)), m);
-
- // Maybe this should be handled inside the Manager
- while (!m.hasCaughtUpWithOldMessages()) {
- try {
- synchronized (m) {
- m.wait();
- }
- } catch (InterruptedException ignored) {
- }
- }
-
- final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
-
- final var jsonRpcReader = new JsonRpcReader(jsonRpcSender, () -> {
- try {
- return reader.readLine();
- } catch (IOException e) {
- throw new AssertionError(e);
- }
- });
- jsonRpcReader.readRequests((method, params) -> handleRequest(m, objectMapper, method, params),
- response -> logger.debug("Received unexpected response for id {}", response.getId()));
-
- receiveThread.interrupt();
- try {
- receiveThread.join();
- } catch (InterruptedException ignored) {
- }
- }
-
- private JsonNode handleRequest(
- final Manager m, final ObjectMapper objectMapper, final String method, ContainerNode<?> params
- ) throws JsonRpcException {
- final Object[] result = {null};
- final JsonWriter commandOutputWriter = s -> {
- if (result[0] != null) {
- throw new AssertionError("Command may only write one json result");
- }
-
- result[0] = s;
- };
-
- var command = Commands.getCommand(method);
- if (!(command instanceof JsonRpcCommand)) {
- throw new JsonRpcException(new JsonRpcResponse.Error(JsonRpcResponse.Error.METHOD_NOT_FOUND,
- "Method not implemented",
- null));
- }
-
- try {
- parseParamsAndRunCommand(m, objectMapper, params, commandOutputWriter, (JsonRpcCommand<?>) command);
- } catch (JsonMappingException e) {
- throw new JsonRpcException(new JsonRpcResponse.Error(JsonRpcResponse.Error.INVALID_REQUEST,
- e.getMessage(),
- null));
- } catch (UserErrorException e) {
- throw new JsonRpcException(new JsonRpcResponse.Error(USER_ERROR, e.getMessage(), null));
- } catch (IOErrorException e) {
- throw new JsonRpcException(new JsonRpcResponse.Error(IO_ERROR, e.getMessage(), null));
- } catch (UntrustedKeyErrorException e) {
- throw new JsonRpcException(new JsonRpcResponse.Error(UNTRUSTED_KEY_ERROR, e.getMessage(), null));
- } catch (Throwable e) {
- logger.error("Command execution failed", e);
- throw new JsonRpcException(new JsonRpcResponse.Error(JsonRpcResponse.Error.INTERNAL_ERROR,
- e.getMessage(),
- null));
- }
-
- Object output = result[0] == null ? Map.of() : result[0];
- return objectMapper.valueToTree(output);
+ Shutdown.installHandler();
+ final var receiveMode = ns.<ReceiveMode>get("receive-mode");
+ final var receiveConfig = getReceiveConfig(ns);
+ m.setReceiveConfig(receiveConfig);
+
+ final var jsonOutputWriter = (JsonWriter) outputWriter;
+ final var lineSupplier = getLineSupplier();
+
+ final var handler = new SignalJsonRpcDispatcherHandler(jsonOutputWriter,
+ lineSupplier,
+ receiveMode == ReceiveMode.MANUAL);
+ final var thread = Thread.currentThread();
+ Shutdown.registerShutdownListener(thread::interrupt);
+ handler.handleConnection(m);