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