Search in sources :

Example 1 with GetPeersRequest

use of io.bitsquare.p2p.peers.peerexchange.messages.GetPeersRequest in project bitsquare by bitsquare.

the class PeerExchangeHandler method sendGetPeersRequest.

private void sendGetPeersRequest(NodeAddress nodeAddress) {
    Log.traceCall("nodeAddress=" + nodeAddress + " / this=" + this);
    if (!stopped) {
        if (networkNode.getNodeAddress() != null) {
            GetPeersRequest getPeersRequest = new GetPeersRequest(networkNode.getNodeAddress(), nonce, peerManager.getConnectedNonSeedNodeReportedPeers(nodeAddress));
            if (timeoutTimer == null) {
                timeoutTimer = UserThread.runAfter(() -> {
                    // setup before sending to avoid race conditions
                    if (!stopped) {
                        String errorMessage = "A timeout occurred at sending getPeersRequest:" + getPeersRequest + " for nodeAddress:" + nodeAddress;
                        log.debug(errorMessage + " / PeerExchangeHandler=" + PeerExchangeHandler.this);
                        log.debug("timeoutTimer called on " + this);
                        handleFault(errorMessage, CloseConnectionReason.SEND_MSG_TIMEOUT, nodeAddress);
                    } else {
                        log.trace("We have stopped that handler already. We ignore that timeoutTimer.run call.");
                    }
                }, TIME_OUT_SEC, TimeUnit.SECONDS);
            }
            SettableFuture<Connection> future = networkNode.sendMessage(nodeAddress, getPeersRequest);
            Futures.addCallback(future, new FutureCallback<Connection>() {

                @Override
                public void onSuccess(Connection connection) {
                    if (!stopped) {
                        //TODO
                        /*if (!connection.getPeersNodeAddressOptional().isPresent()) {
                                connection.setPeersNodeAddress(nodeAddress);
                                log.warn("sendGetPeersRequest: !connection.getPeersNodeAddressOptional().isPresent()");
                            }*/
                        PeerExchangeHandler.this.connection = connection;
                        connection.addMessageListener(PeerExchangeHandler.this);
                        log.trace("Send " + getPeersRequest + " to " + nodeAddress + " succeeded.");
                    } else {
                        log.trace("We have stopped that handler already. We ignore that sendGetPeersRequest.onSuccess call.");
                    }
                }

                @Override
                public void onFailure(@NotNull Throwable throwable) {
                    if (!stopped) {
                        String errorMessage = "Sending getPeersRequest to " + nodeAddress + " failed. That is expected if the peer is offline.\n\tgetPeersRequest=" + getPeersRequest + ".\n\tException=" + throwable.getMessage();
                        log.debug(errorMessage);
                        handleFault(errorMessage, CloseConnectionReason.SEND_MSG_FAILURE, nodeAddress);
                    } else {
                        log.trace("We have stopped that handler already. We ignore that sendGetPeersRequest.onFailure call.");
                    }
                }
            });
        } else {
            log.debug("My node address is still null at sendGetPeersRequest. We ignore that call.");
        }
    } else {
        log.trace("We have stopped that handler already. We ignore that sendGetPeersRequest call.");
    }
}
Also used : Connection(io.bitsquare.p2p.network.Connection) GetPeersRequest(io.bitsquare.p2p.peers.peerexchange.messages.GetPeersRequest)

Aggregations

Connection (io.bitsquare.p2p.network.Connection)1 GetPeersRequest (io.bitsquare.p2p.peers.peerexchange.messages.GetPeersRequest)1