Converts a Future to a SendHandler.
| 34 | * Converts a Future to a SendHandler. |
| 35 | */ |
| 36 | class FutureToSendHandler implements Future<Void>, SendHandler { |
| 37 | |
| 38 | private static final StringManager sm = StringManager.getManager(FutureToSendHandler.class); |
| 39 | |
| 40 | private final CountDownLatch latch = new CountDownLatch(1); |
| 41 | private final WsSession wsSession; |
| 42 | private final AtomicReference<SendResult> result = new AtomicReference<>(null); |
| 43 | |
| 44 | FutureToSendHandler(WsSession wsSession) { |
| 45 | this.wsSession = wsSession; |
| 46 | } |
| 47 | |
| 48 | |
| 49 | // --------------------------------------------------------- SendHandler |
| 50 | |
| 51 | @Override |
| 52 | public void onResult(SendResult result) { |
| 53 | this.result.compareAndSet(null, result); |
| 54 | latch.countDown(); |
| 55 | } |
| 56 | |
| 57 | |
| 58 | // -------------------------------------------------------------- Future |
| 59 | |
| 60 | @Override |
| 61 | public boolean cancel(boolean mayInterruptIfRunning) { |
| 62 | // Cancelling the task is not supported |
| 63 | return false; |
| 64 | } |
| 65 | |
| 66 | @Override |
| 67 | public boolean isCancelled() { |
| 68 | // Cancelling the task is not supported |
| 69 | return false; |
| 70 | } |
| 71 | |
| 72 | @Override |
| 73 | public boolean isDone() { |
| 74 | return latch.getCount() == 0; |
| 75 | } |
| 76 | |
| 77 | @Override |
| 78 | public Void get() throws InterruptedException, ExecutionException { |
| 79 | try { |
| 80 | wsSession.registerFuture(this); |
| 81 | latch.await(); |
| 82 | } finally { |
| 83 | wsSession.unregisterFuture(this); |
| 84 | } |
| 85 | if (result.get().getException() != null) { |
| 86 | throw new ExecutionException(result.get().getException()); |
| 87 | } |
| 88 | return null; |
| 89 | } |
| 90 | |
| 91 | @Override |
| 92 | public Void get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException { |
| 93 | boolean retval; |
nothing calls this directly
no test coverage detected