public class SignalJsonRpcDispatcherHandler {
- private final static Logger logger = LoggerFactory.getLogger(SignalJsonRpcDispatcherHandler.class);
+ private static final Logger logger = LoggerFactory.getLogger(SignalJsonRpcDispatcherHandler.class);
private final ObjectMapper objectMapper;
private final JsonRpcSender jsonRpcSender;
private SignalJsonRpcCommandHandler commandHandler;
public SignalJsonRpcDispatcherHandler(
- final JsonWriter jsonWriter, final Supplier<String> lineSupplier, final boolean noReceiveOnStart
+ final JsonWriter jsonWriter,
+ final Supplier<String> lineSupplier,
+ final boolean noReceiveOnStart
) {
this.noReceiveOnStart = noReceiveOnStart;
this.objectMapper = Util.createJsonObjectMapper();
this.commandHandler = new SignalJsonRpcCommandHandler(c, this::getCommand);
if (!noReceiveOnStart) {
- this.subscribeReceive(c.getManagers());
- c.addOnManagerAddedHandler(this::subscribeReceive);
+ this.subscribeReceive(c.getManagers(), true);
+ c.addOnManagerAddedHandler(m -> subscribeReceive(m, true));
c.addOnManagerRemovedHandler(this::unsubscribeReceive);
}
this.commandHandler = new SignalJsonRpcCommandHandler(m, this::getCommand);
if (!noReceiveOnStart) {
- subscribeReceive(m);
+ subscribeReceive(m, true);
}
final var currentThread = Thread.currentThread();
private static final AtomicInteger nextSubscriptionId = new AtomicInteger(0);
- private int subscribeReceive(final Manager manager) {
- return subscribeReceive(List.of(manager));
+ private int subscribeReceive(final Manager manager, boolean internalSubscription) {
+ return subscribeReceive(List.of(manager), internalSubscription);
}
- private int subscribeReceive(final List<Manager> managers) {
+ private int subscribeReceive(final List<Manager> managers, boolean internalSubscription) {
final var subscriptionId = nextSubscriptionId.getAndIncrement();
final var handlers = managers.stream().map(m -> {
final var receiveMessageHandler = new JsonReceiveMessageHandler(m, s -> {
- final ContainerNode<?> params = objectMapper.valueToTree(s);
- ((ObjectNode) params).set("subscription", IntNode.valueOf(subscriptionId));
+ ContainerNode<?> params;
+ if (internalSubscription) {
+ params = objectMapper.valueToTree(s);
+ } else {
+ final var paramsNode = new ObjectNode(objectMapper.getNodeFactory());
+ paramsNode.set("subscription", IntNode.valueOf(subscriptionId));
+ paramsNode.set("result", objectMapper.valueToTree(s));
+ params = paramsNode;
+ }
final var jsonRpcRequest = JsonRpcRequest.forNotification("receive", params, null);
try {
jsonRpcSender.sendRequest(jsonRpcRequest);
private void unsubscribeReceive(final Manager m) {
final var subscriptionId = receiveHandlers.entrySet()
.stream()
- .filter(e -> e.getValue().size() == 1 && e.getValue().get(0).first().equals(m))
+ .filter(e -> e.getValue().size() == 1 && e.getValue().getFirst().first().equals(m))
.map(Map.Entry::getKey)
.findFirst();
subscriptionId.ifPresent(this::unsubscribeReceive);
@Override
public void handleCommand(
- final Void request, final Manager m, final JsonWriter jsonWriter
+ final Void request,
+ final Manager m,
+ final JsonWriter jsonWriter
) throws CommandException {
- final var subscriptionId = subscribeReceive(m);
+ final var subscriptionId = subscribeReceive(m, false);
jsonWriter.write(subscriptionId);
}
@Override
public void handleCommand(
- final Void request, final MultiAccountManager c, final JsonWriter jsonWriter
+ final Void request,
+ final MultiAccountManager c,
+ final JsonWriter jsonWriter
) throws CommandException {
- final var subscriptionId = subscribeReceive(c.getManagers());
+ final var subscriptionId = subscribeReceive(c.getManagers(), false);
jsonWriter.write(subscriptionId);
}
}
@Override
public void handleCommand(
- final JsonNode request, final Manager m, final JsonWriter jsonWriter
+ final JsonNode request,
+ final Manager m,
+ final JsonWriter jsonWriter
) throws CommandException {
final var subscriptionId = getSubscriptionId(request);
if (subscriptionId == null) {
@Override
public void handleCommand(
- final JsonNode request, final MultiAccountManager c, final JsonWriter jsonWriter
+ final JsonNode request,
+ final MultiAccountManager c,
+ final JsonWriter jsonWriter
) throws CommandException {
final var subscriptionId = getSubscriptionId(request);
if (subscriptionId == null) {
}
private Integer getSubscriptionId(final JsonNode request) {
- if (request instanceof ArrayNode req) {
- return req.get(0).asInt();
- } else if (request instanceof ObjectNode req) {
- return req.get("subscription").asInt();
- } else {
- return null;
- }
+ return switch (request) {
+ case ArrayNode req -> req.get(0).asInt();
+ case ObjectNode req -> req.get("subscription").asInt();
+ case null, default -> null;
+ };
}
}
}