(Command<X> command)
| 111 | } |
| 112 | |
| 113 | public <X> CompletableFuture<@Nullable X> send(Command<X> command) { |
| 114 | long id = NEXT_ID.getAndIncrement(); |
| 115 | |
| 116 | CompletableFuture<@Nullable X> result = new CompletableFuture<>(); |
| 117 | if (command.getSendsResponse()) { |
| 118 | methodCallbacks.put( |
| 119 | id, |
| 120 | NamedConsumer.of( |
| 121 | command.getMethod(), |
| 122 | inputOrException -> { |
| 123 | if (inputOrException.isRight()) { |
| 124 | try { |
| 125 | X value = command.getMapper().apply(inputOrException.right()); |
| 126 | result.complete(value); |
| 127 | } catch (Exception e) { |
| 128 | LOG.log( |
| 129 | Level.WARNING, |
| 130 | String.format("Unable to map result for %s", command.getMethod()), |
| 131 | e); |
| 132 | result.completeExceptionally(e); |
| 133 | } |
| 134 | } else { |
| 135 | result.completeExceptionally(inputOrException.left()); |
| 136 | } |
| 137 | })); |
| 138 | } |
| 139 | |
| 140 | Map<String, Object> serialized = |
| 141 | Map.of( |
| 142 | "id", id, |
| 143 | "method", command.getMethod(), |
| 144 | "params", command.getParams()); |
| 145 | |
| 146 | StringBuilder json = new StringBuilder(); |
| 147 | try (JsonOutput out = JSON.newOutput(json).writeClassName(false)) { |
| 148 | out.write(serialized); |
| 149 | } |
| 150 | LOG.log(getDebugLogLevel(), "-> {0}", json); |
| 151 | socket.sendText(json); |
| 152 | |
| 153 | if (!command.getSendsResponse()) { |
| 154 | result.complete(null); |
| 155 | } |
| 156 | |
| 157 | return result; |
| 158 | } |
| 159 | |
| 160 | public <X> X sendAndWait(Command<X> command, Duration timeout) { |
| 161 | try { |
no test coverage detected