/** * Sends the peer a ping message and returns a future that will be invoked when the pong is received back. * The future provides a number which is the number of milliseconds elapsed between the ping and the pong. * Once the pong is received the value returned by {@link org.bitcoinj.core.Peer#getLastPingTime()} is * updated. * @throws ProtocolException if the peer version is too low to support measurable pings. */ public ListenableFuture<Long> ping() throws ProtocolException { return ping((long) (Math.random() * Long.MAX_VALUE)); }
/** * Sends the peer a ping message and returns a future that will be invoked when the pong is received back. * The future provides a number which is the number of milliseconds elapsed between the ping and the pong. * Once the pong is received the value returned by {@link org.bitcoinj.core.Peer#getLastPingTime()} is * updated. * @throws ProtocolException if the peer version is too low to support measurable pings. */ public ListenableFuture<Long> ping() throws ProtocolException { return ping((long) (Math.random() * Long.MAX_VALUE)); }
/** * Sends the peer a ping message and returns a future that will be invoked when the pong is received back. * The future provides a number which is the number of milliseconds elapsed between the ping and the pong. * Once the pong is received the value returned by {@link org.bitcoinj.core.Peer#getLastPingTime()} is * updated. * @throws ProtocolException if the peer version is too low to support measurable pings. */ public ListenableFuture<Long> ping() throws ProtocolException { return ping((long) (Math.random() * Long.MAX_VALUE)); }
/** * Sends the peer a ping message and returns a future that will be invoked when the pong is received back. * The future provides a number which is the number of milliseconds elapsed between the ping and the pong. * Once the pong is received the value returned by {@link org.bitcoinj.core.Peer#getLastPingTime()} is * updated. * @throws ProtocolException if the peer version is too low to support measurable pings. */ public ListenableFuture<Long> ping() throws ProtocolException { return ping((long) (Math.random() * Long.MAX_VALUE)); }
ping().addListener(new Runnable() { @Override public void run() {
ping().addListener(new Runnable() { @Override public void run() {
ping().addListener(new Runnable() { @Override public void run() {
ping().addListener(new Runnable() { @Override public void run() {
/** * Ping all connected peers to see if there is an active network connection * * @return true is two or more peers respond to the ping */ public boolean pingPeers() { List<Peer> connectedPeers = peerGroup.getConnectedPeers(); int numberOfSuccessfulPings = 0; if (connectedPeers != null) { for (Peer peer : connectedPeers) { log.debug("Ping: {}", peer.getAddress().toString()); try { ListenableFuture<Long> result = peer.ping(); result.get(4, TimeUnit.SECONDS); numberOfSuccessfulPings++; if (numberOfSuccessfulPings >= NUMBER_OF_PEERS_TO_PING) { break; } } catch (ProtocolException | InterruptedException | ExecutionException | TimeoutException e) { log.warn("Peer '" + peer.getAddress().toString() + "' failed ping test. Message was " + e.getMessage()); } } } return numberOfSuccessfulPings >= 2; }
@Override public void run() { try { if (getPingIntervalMsec() <= 0) { ListenableScheduledFuture<?> task = vPingTask; if (task != null) { task.cancel(false); vPingTask = null; } return; // Disabled. } for (Peer peer : getConnectedPeers()) { if (peer.getPeerVersionMessage().clientVersion < params.getProtocolVersionNum(NetworkParameters.ProtocolVersion.PONG)) continue; peer.ping(); } } catch (Throwable e) { log.error("Exception in ping loop", e); // The executor swallows exceptions :( } } }, getPingIntervalMsec(), getPingIntervalMsec(), TimeUnit.MILLISECONDS);
@Override public void run() { try { if (getPingIntervalMsec() <= 0) { ListenableScheduledFuture<?> task = vPingTask; if (task != null) { task.cancel(false); vPingTask = null; } return; // Disabled. } for (Peer peer : getConnectedPeers()) { if (peer.getPeerVersionMessage().clientVersion < params.getProtocolVersionNum(NetworkParameters.ProtocolVersion.PONG)) continue; peer.ping(); } } catch (Throwable e) { log.error("Exception in ping loop", e); // The executor swallows exceptions :( } } }, getPingIntervalMsec(), getPingIntervalMsec(), TimeUnit.MILLISECONDS);
@Override public void run() { try { if (getPingIntervalMsec() <= 0) { ListenableScheduledFuture<?> task = vPingTask; if (task != null) { task.cancel(false); vPingTask = null; } return; // Disabled. } for (Peer peer : getConnectedPeers()) { if (peer.getPeerVersionMessage().clientVersion < params.getProtocolVersionNum(NetworkParameters.ProtocolVersion.PONG)) continue; peer.ping(); } } catch (Throwable e) { log.error("Exception in ping loop", e); // The executor swallows exceptions :( } } }, getPingIntervalMsec(), getPingIntervalMsec(), TimeUnit.MILLISECONDS);
@Override public void run() { try { if (getPingIntervalMsec() <= 0) { ListenableScheduledFuture<?> task = vPingTask; if (task != null) { task.cancel(false); vPingTask = null; } return; // Disabled. } for (Peer peer : getConnectedPeers()) { if (peer.getPeerVersionMessage().clientVersion < params.getProtocolVersionNum(NetworkParameters.ProtocolVersion.PONG)) continue; peer.ping(); } } catch (Throwable e) { log.error("Exception in ping loop", e); // The executor swallows exceptions :( } } }, getPingIntervalMsec(), getPingIntervalMsec(), TimeUnit.MILLISECONDS);
peerList.get(0).ping().get(); } catch (BlockStoreException e) { throw new RuntimeException(e);
peerList.get(0).ping().get(); } catch (BlockStoreException e) { throw new RuntimeException(e);
peerList.get(0).ping().get(); } catch (BlockStoreException e) { throw new RuntimeException(e);
peerList.get(0).ping().get(); } catch (BlockStoreException e) { throw new RuntimeException(e);
locator.add(bitcoindChainHead); bitcoind.sendMessage(new GetHeadersMessage(params, locator, hashTo)); bitcoind.ping().get(); if (!chain.getChainHead().getHeader().getHash().equals(bitcoindChainHead)) { rulesSinceFirstFail++; MemoryPoolMessage message = new MemoryPoolMessage(); bitcoind.sendMessage(message); bitcoind.ping().get(); if (mostRecentInv == null && !((MemoryPoolState) rule).mempool.isEmpty()) { log.error("ERROR: bitcoind had an empty mempool, but we expected some transactions on rule " + rule.ruleName);
@Test public void pingPong() throws Exception { connect(); Utils.setMockClock(); // No ping pong happened yet. assertEquals(Long.MAX_VALUE, peer.getLastPingTime()); assertEquals(Long.MAX_VALUE, peer.getPingTime()); ListenableFuture<Long> future = peer.ping(); assertEquals(Long.MAX_VALUE, peer.getLastPingTime()); assertEquals(Long.MAX_VALUE, peer.getPingTime()); assertFalse(future.isDone()); Ping pingMsg = (Ping) outbound(writeTarget); Utils.rollMockClock(5); // The pong is returned. inbound(writeTarget, new Pong(pingMsg.getNonce())); pingAndWait(writeTarget); assertTrue(future.isDone()); long elapsed = future.get(); assertTrue("" + elapsed, elapsed > 1000); assertEquals(elapsed, peer.getLastPingTime()); assertEquals(elapsed, peer.getPingTime()); // Do it again and make sure it affects the average. future = peer.ping(); pingMsg = (Ping) outbound(writeTarget); Utils.rollMockClock(50); inbound(writeTarget, new Pong(pingMsg.getNonce())); elapsed = future.get(); assertEquals(elapsed, peer.getLastPingTime()); assertEquals(7250, peer.getPingTime()); }