diff options
Diffstat (limited to 'ui-client')
3 files changed, 65 insertions, 3 deletions
diff --git a/ui-client/src/main/java/market/guess/ui/client/App.java b/ui-client/src/main/java/market/guess/ui/client/App.java index 4ba9600..93ae414 100644 --- a/ui-client/src/main/java/market/guess/ui/client/App.java +++ b/ui-client/src/main/java/market/guess/ui/client/App.java | |||
| @@ -2,11 +2,13 @@ package market.guess.ui.client; | |||
| 2 | 2 | ||
| 3 | import java.io.IOException; | 3 | import java.io.IOException; |
| 4 | import java.io.UncheckedIOException; | 4 | import java.io.UncheckedIOException; |
| 5 | import java.util.concurrent.CompletableFuture; | ||
| 5 | import javafx.application.Application; | 6 | import javafx.application.Application; |
| 6 | import javafx.application.Platform; | 7 | import javafx.application.Platform; |
| 7 | import javafx.fxml.FXMLLoader; | 8 | import javafx.fxml.FXMLLoader; |
| 8 | import javafx.scene.Scene; | 9 | import javafx.scene.Scene; |
| 9 | import javafx.stage.Stage; | 10 | import javafx.stage.Stage; |
| 11 | import market.guess.model.chat.ChatMessageDTO; | ||
| 10 | import market.guess.ui.desktop.AppView; | 12 | import market.guess.ui.desktop.AppView; |
| 11 | 13 | ||
| 12 | public final class App extends Application { | 14 | public final class App extends Application { |
| @@ -47,7 +49,20 @@ public final class App extends Application { | |||
| 47 | new RemoteGuessMarketContext(client), | 49 | new RemoteGuessMarketContext(client), |
| 48 | new RemoteAccountContext(client), | 50 | new RemoteAccountContext(client), |
| 49 | user); | 51 | user); |
| 50 | client.startPolling(() -> Platform.runLater(view.getState()::refreshData)); | 52 | var state = view.getState(); |
| 53 | state.setChatSender( | ||
| 54 | text -> | ||
| 55 | CompletableFuture.runAsync( | ||
| 56 | () -> { | ||
| 57 | var sent = client.say(text); | ||
| 58 | if (!sent.isSuccess()) { | ||
| 59 | var note = new ChatMessageDTO(0, "", "Not sent", sent.getMessage()); | ||
| 60 | Platform.runLater(() -> state.getChat().add(note)); | ||
| 61 | } | ||
| 62 | })); | ||
| 63 | client.startPolling( | ||
| 64 | () -> Platform.runLater(state::refreshData), | ||
| 65 | said -> Platform.runLater(() -> state.getChat().addAll(said))); | ||
| 51 | stage.show(); | 66 | stage.show(); |
| 52 | } | 67 | } |
| 53 | 68 | ||
diff --git a/ui-client/src/main/java/market/guess/ui/client/ServerClient.java b/ui-client/src/main/java/market/guess/ui/client/ServerClient.java index 01962a2..00f3d68 100644 --- a/ui-client/src/main/java/market/guess/ui/client/ServerClient.java +++ b/ui-client/src/main/java/market/guess/ui/client/ServerClient.java | |||
| @@ -2,6 +2,7 @@ package market.guess.ui.client; | |||
| 2 | 2 | ||
| 3 | import com.google.gson.Gson; | 3 | import com.google.gson.Gson; |
| 4 | import com.google.gson.JsonObject; | 4 | import com.google.gson.JsonObject; |
| 5 | import com.google.gson.reflect.TypeToken; | ||
| 5 | import java.io.IOException; | 6 | import java.io.IOException; |
| 6 | import java.lang.reflect.Type; | 7 | import java.lang.reflect.Type; |
| 7 | import java.util.HashMap; | 8 | import java.util.HashMap; |
| @@ -11,7 +12,9 @@ import java.util.concurrent.ConcurrentHashMap; | |||
| 11 | import java.util.concurrent.Executors; | 12 | import java.util.concurrent.Executors; |
| 12 | import java.util.concurrent.ScheduledExecutorService; | 13 | import java.util.concurrent.ScheduledExecutorService; |
| 13 | import java.util.concurrent.TimeUnit; | 14 | import java.util.concurrent.TimeUnit; |
| 15 | import java.util.function.Consumer; | ||
| 14 | import market.guess.api.Result; | 16 | import market.guess.api.Result; |
| 17 | import market.guess.model.chat.ChatMessageDTO; | ||
| 15 | import market.guess.model.event.EventDetailDTO; | 18 | import market.guess.model.event.EventDetailDTO; |
| 16 | import market.guess.model.event.EventSummaryDTO; | 19 | import market.guess.model.event.EventSummaryDTO; |
| 17 | import market.guess.model.ledger.LedgerDTO; | 20 | import market.guess.model.ledger.LedgerDTO; |
| @@ -51,6 +54,7 @@ public final class ServerClient implements AutoCloseable { | |||
| 51 | return t; | 54 | return t; |
| 52 | }); | 55 | }); |
| 53 | private volatile Snapshot snapshot = new Snapshot(-1, List.of(), Map.of(), List.of(), null); | 56 | private volatile Snapshot snapshot = new Snapshot(-1, List.of(), Map.of(), List.of(), null); |
| 57 | private long lastChatId; // touched only under the pullChat lock | ||
| 54 | 58 | ||
| 55 | public ServerClient(String apiUrl) { | 59 | public ServerClient(String apiUrl) { |
| 56 | this.api = HttpUrl.get(apiUrl); | 60 | this.api = HttpUrl.get(apiUrl); |
| @@ -115,13 +119,34 @@ public final class ServerClient implements AutoCloseable { | |||
| 115 | return true; | 119 | return true; |
| 116 | } | 120 | } |
| 117 | 121 | ||
| 118 | public void startPolling(Runnable onChange) { | 122 | public synchronized List<ChatMessageDTO> pullChat() { |
| 123 | var url = url("chat").newBuilder().addQueryParameter("since", "" + lastChatId).build(); | ||
| 124 | Result<List<ChatMessageDTO>> res = | ||
| 125 | call( | ||
| 126 | new Request.Builder().url(url).build(), | ||
| 127 | new TypeToken<List<ChatMessageDTO>>() {}.getType()); | ||
| 128 | if (!res.isSuccess() || res.getData().isEmpty()) { | ||
| 129 | return List.of(); | ||
| 130 | } | ||
| 131 | lastChatId = res.getData().getLast().id(); | ||
| 132 | return res.getData(); | ||
| 133 | } | ||
| 134 | |||
| 135 | public Result<ChatMessageDTO> say(String text) { | ||
| 136 | return post(ChatMessageDTO.class, new FormBody.Builder().add("text", text).build(), "chat"); | ||
| 137 | } | ||
| 138 | |||
| 139 | public void startPolling(Runnable onChange, Consumer<List<ChatMessageDTO>> onChat) { | ||
| 119 | poller.scheduleWithFixedDelay( | 140 | poller.scheduleWithFixedDelay( |
| 120 | () -> { | 141 | () -> { |
| 121 | try { | 142 | try { |
| 122 | if (pull()) { | 143 | if (pull()) { |
| 123 | onChange.run(); | 144 | onChange.run(); |
| 124 | } | 145 | } |
| 146 | var said = pullChat(); | ||
| 147 | if (!said.isEmpty()) { | ||
| 148 | onChat.accept(said); | ||
| 149 | } | ||
| 125 | } catch (RuntimeException e) { | 150 | } catch (RuntimeException e) { |
| 126 | } | 151 | } |
| 127 | }, | 152 | }, |
diff --git a/ui-client/src/test/java/market/guess/ui/client/RemoteContextsTest.java b/ui-client/src/test/java/market/guess/ui/client/RemoteContextsTest.java index e3b11f0..ea9d5ea 100644 --- a/ui-client/src/test/java/market/guess/ui/client/RemoteContextsTest.java +++ b/ui-client/src/test/java/market/guess/ui/client/RemoteContextsTest.java | |||
| @@ -72,7 +72,7 @@ class RemoteContextsTest { | |||
| 72 | 72 | ||
| 73 | maya.pull(); | 73 | maya.pull(); |
| 74 | var heard = new CountDownLatch(1); | 74 | var heard = new CountDownLatch(1); |
| 75 | maya.startPolling(heard::countDown); | 75 | maya.startPolling(heard::countDown, said -> {}); |
| 76 | var bought = | 76 | var bought = |
| 77 | tomMarket.placeOrder("Tom", event.key(), event.key() + ":0", "BUY", BigDecimal.ONE, 10); | 77 | tomMarket.placeOrder("Tom", event.key(), event.key() + ":0", "BUY", BigDecimal.ONE, 10); |
| 78 | assertTrue(bought.isSuccess(), bought.getMessage()); | 78 | assertTrue(bought.isSuccess(), bought.getMessage()); |
| @@ -150,6 +150,28 @@ class RemoteContextsTest { | |||
| 150 | } | 150 | } |
| 151 | 151 | ||
| 152 | @Test | 152 | @Test |
| 153 | void chatReachesEveryoneOnceAndLeavesTheMarketAlone() { | ||
| 154 | try (var yael = new ServerClient(server.api); | ||
| 155 | var zohar = new ServerClient(server.api)) { | ||
| 156 | yael.login("Yael"); | ||
| 157 | zohar.login("Zohar"); | ||
| 158 | zohar.pullChat(); // whatever other tests said | ||
| 159 | zohar.pull(); | ||
| 160 | |||
| 161 | var hello = "שלום, anyone trading?"; | ||
| 162 | assertTrue(yael.say(hello).isSuccess()); | ||
| 163 | assertFalse(yael.say(" ").isSuccess()); | ||
| 164 | |||
| 165 | var heard = zohar.pullChat(); | ||
| 166 | assertEquals(1, heard.size()); | ||
| 167 | assertEquals("Yael", heard.get(0).sender()); | ||
| 168 | assertEquals(hello, heard.get(0).text()); | ||
| 169 | assertTrue(zohar.pullChat().isEmpty(), "a message is delivered once"); | ||
| 170 | assertFalse(zohar.pull(), "chat must not look like a market change"); | ||
| 171 | } | ||
| 172 | } | ||
| 173 | |||
| 174 | @Test | ||
| 153 | void anUnreachableServerIsAFailedResultNotAnException() { | 175 | void anUnreachableServerIsAFailedResultNotAnException() { |
| 154 | try (var nobody = new ServerClient("http://localhost:1/api")) { | 176 | try (var nobody = new ServerClient("http://localhost:1/api")) { |
| 155 | assertFalse(nobody.login("Zed").isSuccess()); | 177 | assertFalse(nobody.login("Zed").isSuccess()); |