public static CallMessage fromJson(String json) throws JSONException { return new CallMessage(json); }
public ListenableFuture<ResultMessage> call(CallMessage message) { SettableFuture<ResultMessage> future = SettableFuture.create(); message.setId(idCounter.getAndIncrement()); try { toServer.writeBytes(message.toString()); callers.put(message.getId(), future); } catch (Throwable e) { future.setException(e); log.error("Error making a call to the server: {}", e.getMessage()); triggerShutdown(); } return future; }
@Override public void onFailure(Throwable t) { if (t instanceof CancellationException) { log.debug("Canceling {} call", message.getMethod()); } else { log.error("Could not get reply for blockchain.block.get_header", t); } } }, Threading.USER_THREAD);
public JSONArray getParams() { createParamsIfNeeded(); try { return getJSONArray("params"); } catch (JSONException e) { // Should never happen because we created the params return new JSONArray(); } }
@Test public void testCallMessage() { CallMessage call = new CallMessage(1L, "blockchain.address.listunspent", Arrays.asList("npF3ApeWwMS8kwXJyybPZ76vNbv5txVjDf")); Assert.assertEquals(call.getId(), 1L); Assert.assertEquals(call.getMethod(), "blockchain.address.listunspent"); Assert.assertEquals("{\"id\":1,\"method\":\"blockchain.address.listunspent\"," + "\"params\":[\"npF3ApeWwMS8kwXJyybPZ76vNbv5txVjDf\"]}\n", call.toString()); }
@Test public void testCallMessageSubscribe() { CallMessage call = new CallMessage("blockchain.headers.subscribe", new ArrayList()); Assert.assertEquals(call.getMethod(), "blockchain.headers.subscribe"); Assert.assertEquals("{\"id\":null,\"method\":\"blockchain.headers.subscribe\",\"params\":[]}\n", call.toString()); }
CallMessage call = new CallMessage("blockchain.address.subscribe", Arrays.asList("mrx4EmF6zHXky3zDoeJ1K7KvYcuNn8Mmc4")); "\"29b083b2b25cfbc8cc4b46977b74512a8cc7bc152f4533c1b2e50f1489d6df67\"]," + "\"id\": null, \"method\": \"blockchain.address.subscribe\"}"; final CallMessage subscribeCall = CallMessage.fromJson(subscribeCallJson); Assert.assertEquals(call.toString(), new String(serverInput.toByteArray())); "\"e0dc94c40d4331306eb60ad55b537ee92446b8a8dc19f25dc3373d96c1904719\"}"; final ResultMessage serverResult = ResultMessage.fromJson(resultJson); serverResult.setId(call.getId()); serverOutput.write(subscribeCall.toString().getBytes());
@Test public void testCallMessage3() { CallMessage call = new CallMessage("blockchain.address.listunspent", (List) null); call.setParam("npF3ApeWwMS8kwXJyybPZ76vNbv5txVjDf"); Assert.assertEquals("{\"id\":null,\"method\":\"blockchain.address.listunspent\"," + "\"params\":[\"npF3ApeWwMS8kwXJyybPZ76vNbv5txVjDf\"]}\n", call.toString()); }
@Test public void testCallCommand() throws Exception { client.startAsync(); client.awaitRunning(5, TimeUnit.SECONDS); CallMessage call = new CallMessage("blockchain.address.get_history", Arrays.asList("mrx4EmF6zHXky3zDoeJ1K7KvYcuNn8Mmc4")); final ListenableFuture<ResultMessage> futureReply = client.call(call); // Check if server got the correct message Assert.assertEquals(call.toString(), new String(serverInput.toByteArray())); // Reply to the client String resultJson = "{\"id\": 0, \"result\": [{" + "\"tx_hash\": \"3aa2a5a9825ca767e092bcc19487aa13969eeb217fd0fba8492543bbb8c30954\", " + "\"height\": 260144}]}"; final ResultMessage serverResult = ResultMessage.fromJson(resultJson); serverResult.setId(call.getId()); serverOutput.write(serverResult.toString().getBytes()); ResultMessage result = futureReply.get(3, TimeUnit.SECONDS); Assert.assertEquals(serverResult.toString(), result.toString()); serverOutput.close(); }
checkNotNull(stratumClient); final CallMessage callMessage = new CallMessage("blockchain.address.subscribe", (List)null); callMessage.setParam(address.toString());
reply = ResultMessage.fromJson(serverMessage); } else if (reply.isCall()) { reply = CallMessage.fromJson(serverMessage);
@Override public void handle(CallMessage message) { try { BlockHeader header = parseBlockHeader(type, message.getParams().getJSONObject(0)); listener.onNewBlock(header); } catch (JSONException e) { log.error("Unexpected JSON format", e); } } };
@Test public void testCallMessage2() { CallMessage call = new CallMessage(1L, "blockchain.address.listunspent", "npF3ApeWwMS8kwXJyybPZ76vNbv5txVjDf"); Assert.assertEquals(call.getMethod(), "blockchain.address.listunspent"); Assert.assertEquals("{\"id\":1,\"method\":\"blockchain.address.listunspent\"," + "\"params\":[\"npF3ApeWwMS8kwXJyybPZ76vNbv5txVjDf\"]}\n", call.toString()); }
@Test public void testIoFail() throws TimeoutException, IOException, JSONException { final AtomicBoolean success = new AtomicBoolean(false); final Thread testThread = Thread.currentThread(); serverInput = mock(ByteArrayOutputStream.class); when(socket.getOutputStream()).thenReturn(serverInput); doThrow(IOException.class).when(serverInput).write(anyInt()); client.startAsync(); client.awaitRunning(5, TimeUnit.SECONDS); CallMessage call = CallMessage.fromJson("{}"); final ListenableFuture<ResultMessage> futureReply = client.call(call); Futures.addCallback(futureReply, new FutureCallback<ResultMessage>() { @Override public void onSuccess(@Nullable ResultMessage result) { Assert.fail(); } @Override public void onFailure(Throwable t) { Assert.assertTrue(t instanceof IOException); success.set(true); testThread.interrupt(); } }); try { Thread.sleep(5000); } catch (InterruptedException ignored) { } Assert.assertTrue(success.get()); }
@Override public void handle(CallMessage message) { try { AbstractAddress address = BitAddress.from(type, message.getParams().getString(0)); AddressStatus status; if (message.getParams().isNull(1)) { status = new AddressStatus(address, null); } else { status = new AddressStatus(address, message.getParams().getString(1)); } listener.onAddressStatusUpdate(status); } catch (AddressMalformedException e) { log.error("Address subscribe sent a malformed address", e); } catch (JSONException e) { log.error("Unexpected JSON format", e); } } };
@Override public void getBlock(final int height, final TransactionEventListener<BitTransaction> listener) { checkNotNull(stratumClient); final CallMessage message = new CallMessage("blockchain.block.get_header", height); final ListenableFuture<ResultMessage> result = stratumClient.call(message); Futures.addCallback(result, new FutureCallback<ResultMessage>() { @Override public void onSuccess(ResultMessage result) { try { BlockHeader header = parseBlockHeader(type, result.getResult().getJSONObject(0)); listener.onBlockUpdate(header); } catch (JSONException e) { log.error("Unexpected JSON format", e); } } @Override public void onFailure(Throwable t) { if (t instanceof CancellationException) { log.debug("Canceling {} call", message.getMethod()); } else { log.error("Could not get reply for blockchain.block.get_header", t); } } }, Threading.USER_THREAD); }