Search in sources :

Example 1 with GetBsqBlocksResponse

use of bisq.core.dao.node.messages.GetBsqBlocksResponse in project bisq-core by bisq-network.

the class RequestBlocksHandler method onMessage.

// /////////////////////////////////////////////////////////////////////////////////////////
// MessageListener implementation
// /////////////////////////////////////////////////////////////////////////////////////////
@Override
public void onMessage(NetworkEnvelope networkEnvelop, Connection connection) {
    if (networkEnvelop instanceof GetBsqBlocksResponse) {
        if (connection.getPeersNodeAddressOptional().isPresent() && connection.getPeersNodeAddressOptional().get().equals(nodeAddress)) {
            Log.traceCall(networkEnvelop.toString() + "\n\tconnection=" + connection);
            if (!stopped) {
                GetBsqBlocksResponse getBsqBlocksResponse = (GetBsqBlocksResponse) networkEnvelop;
                if (getBsqBlocksResponse.getRequestNonce() == nonce) {
                    stopTimeoutTimer();
                    checkArgument(connection.getPeersNodeAddressOptional().isPresent(), "RequestDataHandler.onMessage: connection.getPeersNodeAddressOptional() must be present " + "at that moment");
                    cleanup();
                    listener.onComplete(getBsqBlocksResponse);
                } else {
                    log.warn("Nonce not matching. That can happen rarely if we get a response after a canceled " + "handshake (timeout causes connection close but peer might have sent a msg before " + "connection was closed).\n\t" + "We drop that message. nonce={} / requestNonce={}", nonce, getBsqBlocksResponse.getRequestNonce());
                }
            } else {
                log.warn("We have stopped already. We ignore that onDataRequest call.");
            }
        } else {
            log.warn("We got a message from another connection and ignore it. That should never happen.");
        }
    }
}
Also used : GetBsqBlocksResponse(bisq.core.dao.node.messages.GetBsqBlocksResponse)

Example 2 with GetBsqBlocksResponse

use of bisq.core.dao.node.messages.GetBsqBlocksResponse in project bisq-core by bisq-network.

the class GetBsqBlocksRequestHandler method onGetBsqBlocksRequest.

// /////////////////////////////////////////////////////////////////////////////////////////
// API
// /////////////////////////////////////////////////////////////////////////////////////////
public void onGetBsqBlocksRequest(GetBsqBlocksRequest getBsqBlocksRequest, final Connection connection) {
    Log.traceCall(getBsqBlocksRequest + "\n\tconnection=" + connection);
    List<BsqBlock> bsqBlocks = readableBsqBlockChain.getClonedBlocksFrom(getBsqBlocksRequest.getFromBlockHeight());
    final GetBsqBlocksResponse bsqBlocksResponse = new GetBsqBlocksResponse(bsqBlocks, getBsqBlocksRequest.getNonce());
    log.debug("bsqBlocksResponse " + bsqBlocksResponse.getRequestNonce());
    if (timeoutTimer == null) {
        timeoutTimer = UserThread.runAfter(() -> {
            // setup before sending to avoid race conditions
            String errorMessage = "A timeout occurred for bsqBlocksResponse:" + bsqBlocksResponse + " on connection:" + connection;
            handleFault(errorMessage, CloseConnectionReason.SEND_MSG_TIMEOUT, connection);
        }, TIMEOUT, TimeUnit.SECONDS);
    }
    SettableFuture<Connection> future = networkNode.sendMessage(connection, bsqBlocksResponse);
    Futures.addCallback(future, new FutureCallback<Connection>() {

        @Override
        public void onSuccess(Connection connection) {
            if (!stopped) {
                log.trace("Send DataResponse to {} succeeded. bsqBlocksResponse={}", connection.getPeersNodeAddressOptional(), bsqBlocksResponse);
                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 bsqBlocksResponse to " + connection + " failed. That is expected if the peer is offline. bsqBlocksResponse=" + bsqBlocksResponse + "." + "Exception: " + throwable.getMessage();
                handleFault(errorMessage, CloseConnectionReason.SEND_MSG_FAILURE, connection);
            } else {
                log.trace("We have stopped already. We ignore that networkNode.sendMessage.onFailure call.");
            }
        }
    });
}
Also used : Connection(bisq.network.p2p.network.Connection) GetBsqBlocksResponse(bisq.core.dao.node.messages.GetBsqBlocksResponse) BsqBlock(bisq.core.dao.blockchain.vo.BsqBlock)

Aggregations

GetBsqBlocksResponse (bisq.core.dao.node.messages.GetBsqBlocksResponse)2 BsqBlock (bisq.core.dao.blockchain.vo.BsqBlock)1 Connection (bisq.network.p2p.network.Connection)1