Search in sources :

Example 1 with GetPeersResponse

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

the class PeerExchangeHandler method onMessage.

///////////////////////////////////////////////////////////////////////////////////////////
// MessageListener implementation
///////////////////////////////////////////////////////////////////////////////////////////
@Override
public void onMessage(Message message, Connection connection) {
    if (message instanceof GetPeersResponse) {
        if (!stopped) {
            Log.traceCall(message.toString() + "\n\tconnection=" + connection);
            GetPeersResponse getPeersResponse = (GetPeersResponse) message;
            if (peerManager.isSeedNode(connection))
                connection.setPeerType(Connection.PeerType.SEED_NODE);
            // Check if the response is for our request
            if (getPeersResponse.requestNonce == nonce) {
                peerManager.addToReportedPeers(getPeersResponse.reportedPeers, connection);
                cleanup();
                listener.onComplete();
            } else {
                log.warn("Nonce not matching. That should never happen.\n\t" + "We drop that message. nonce={} / requestNonce={}", nonce, getPeersResponse.requestNonce);
            }
        } else {
            log.trace("We have stopped that handler already. We ignore that onMessage call.");
        }
    }
}
Also used : GetPeersResponse(io.bitsquare.p2p.peers.peerexchange.messages.GetPeersResponse)

Example 2 with GetPeersResponse

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

the class GetPeersRequestHandler method handle.

///////////////////////////////////////////////////////////////////////////////////////////
// API
///////////////////////////////////////////////////////////////////////////////////////////
public void handle(GetPeersRequest getPeersRequest, final Connection connection) {
    Log.traceCall("getPeersRequest=" + getPeersRequest + "\n\tconnection=" + connection + "\n\tthis=" + this);
    checkArgument(connection.getPeersNodeAddressOptional().isPresent(), "The peers address must have been already set at the moment");
    GetPeersResponse getPeersResponse = new GetPeersResponse(getPeersRequest.nonce, peerManager.getConnectedNonSeedNodeReportedPeers(connection.getPeersNodeAddressOptional().get()));
    checkArgument(timeoutTimer == null, "onGetPeersRequest must not be called twice.");
    timeoutTimer = UserThread.runAfter(() -> {
        // setup before sending to avoid race conditions
        if (!stopped) {
            String errorMessage = "A timeout occurred at sending getPeersResponse:" + getPeersResponse + " on connection:" + connection;
            log.debug(errorMessage + " / PeerExchangeHandshake=" + GetPeersRequestHandler.this);
            log.debug("timeoutTimer called. this=" + this);
            handleFault(errorMessage, CloseConnectionReason.SEND_MSG_TIMEOUT, connection);
        } else {
            log.trace("We have stopped already. We ignore that timeoutTimer.run call.");
        }
    }, TIME_OUT_SEC, TimeUnit.SECONDS);
    SettableFuture<Connection> future = networkNode.sendMessage(connection, getPeersResponse);
    Futures.addCallback(future, new FutureCallback<Connection>() {

        @Override
        public void onSuccess(Connection connection) {
            if (!stopped) {
                log.trace("GetPeersResponse sent successfully");
                cleanup();
                listener.onComplete();
            } else {
                log.trace("We have stopped already. We ignore that networkNode.sendMessage.onSuccess call.");
            }
        }

        @Override
        public void onFailure(@NotNull Throwable throwable) {
            if (!stopped) {
                String errorMessage = "Sending getPeersResponse to " + connection + " failed. That is expected if the peer is offline. getPeersResponse=" + getPeersResponse + "." + "Exception: " + throwable.getMessage();
                log.debug(errorMessage);
                handleFault(errorMessage, CloseConnectionReason.SEND_MSG_FAILURE, connection);
            } else {
                log.trace("We have stopped already. We ignore that networkNode.sendMessage.onFailure call.");
            }
        }
    });
    peerManager.addToReportedPeers(getPeersRequest.reportedPeers, connection);
}
Also used : GetPeersResponse(io.bitsquare.p2p.peers.peerexchange.messages.GetPeersResponse) Connection(io.bitsquare.p2p.network.Connection)

Aggregations

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