import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
+import java.util.List;
import java.util.Map;
-import java.util.Set;
import java.util.concurrent.TimeUnit;
public class JsonRpcDispatcherCommand implements LocalCommand {
private static final int IO_ERROR = -3;
private static final int UNTRUSTED_KEY_ERROR = -4;
- private final OutputWriter outputWriter;
+ @Override
+ public String getName() {
+ return "jsonRpc";
+ }
- public static void attachToSubparser(final Subparser subparser) {
+ @Override
+ public void attachToSubparser(final Subparser subparser) {
subparser.help("Take commands from standard input as line-delimited JSON RPC while receiving messages.");
subparser.addArgument("--ignore-attachments")
.help("Don’t download attachments of received messages.")
.action(Arguments.storeTrue());
}
- public JsonRpcDispatcherCommand(final OutputWriter outputWriter) {
- this.outputWriter = outputWriter;
- }
-
@Override
- public Set<OutputType> getSupportedOutputTypes() {
- return Set.of(OutputType.JSON);
+ public List<OutputType> getSupportedOutputTypes() {
+ return List.of(OutputType.JSON);
}
@Override
- public void handleCommand(final Namespace ns, final Manager m) throws CommandException {
- final boolean ignoreAttachments = ns.getBoolean("ignore-attachments");
+ public void handleCommand(
+ final Namespace ns, final Manager m, final OutputWriter outputWriter
+ ) throws CommandException {
+ 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, ignoreAttachments);
+ 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));
result[0] = s;
};
- var command = Commands.getCommand(method, commandOutputWriter);
+ var command = Commands.getCommand(method);
if (!(command instanceof JsonRpcCommand)) {
throw new JsonRpcException(new JsonRpcResponse.Error(JsonRpcResponse.Error.METHOD_NOT_FOUND,
"Method not implemented",
}
try {
- parseParamsAndRunCommand(m, objectMapper, params, (JsonRpcCommand<?>) command);
+ parseParamsAndRunCommand(m, objectMapper, params, commandOutputWriter, (JsonRpcCommand<?>) command);
} catch (JsonMappingException e) {
throw new JsonRpcException(new JsonRpcResponse.Error(JsonRpcResponse.Error.INVALID_REQUEST,
e.getMessage(),
}
private <T> void parseParamsAndRunCommand(
- final Manager m, final ObjectMapper objectMapper, final TreeNode params, final JsonRpcCommand<T> command
+ final Manager m,
+ final ObjectMapper objectMapper,
+ final TreeNode params,
+ final OutputWriter outputWriter,
+ final JsonRpcCommand<T> command
) throws CommandException, JsonMappingException {
T requestParams = null;
final var requestType = command.getRequestType();
throw new AssertionError(e);
}
}
- command.handleCommand(requestParams, m);
+ command.handleCommand(requestParams, m, outputWriter);
}
- private Thread receiveMessages(
- JsonWriter jsonWriter, Manager m, boolean ignoreAttachments
- ) {
+ private Thread receiveMessages(JsonWriter jsonWriter, Manager m) {
final var thread = new Thread(() -> {
while (!Thread.interrupted()) {
try {
final var receiveMessageHandler = new JsonReceiveMessageHandler(m, jsonWriter);
- m.receiveMessages(1, TimeUnit.HOURS, false, ignoreAttachments, receiveMessageHandler);
+ m.receiveMessages(1, TimeUnit.HOURS, false, receiveMessageHandler);
break;
} catch (IOException e) {
logger.warn("Receiving messages failed, retrying", e);
- } catch (InterruptedException e) {
- break;
}
}
});