Search in sources :

Example 1 with GetDataRequest

use of io.bitsquare.p2p.peers.getdata.messages.GetDataRequest in project bitsquare by bitsquare.

the class RequestDataHandler method requestData.

///////////////////////////////////////////////////////////////////////////////////////////
// API
///////////////////////////////////////////////////////////////////////////////////////////
public void requestData(NodeAddress nodeAddress, boolean isPreliminaryDataRequest) {
    Log.traceCall("nodeAddress=" + nodeAddress);
    peersNodeAddress = nodeAddress;
    if (!stopped) {
        GetDataRequest getDataRequest;
        // We collect the keys of the PersistedStoragePayload items so we exclude them in our request.
        // PersistedStoragePayload items don't get removed, so we don't have an issue with the case that
        // an object gets removed in between PreliminaryGetDataRequest and the GetUpdatedDataRequest and we would 
        // miss that event if we do not load the full set or use some delta handling.
        Set<byte[]> excludedKeys = dataStorage.getMap().entrySet().stream().filter(e -> e.getValue().getStoragePayload() instanceof PersistedStoragePayload).map(e -> e.getKey().bytes).collect(Collectors.toSet());
        if (isPreliminaryDataRequest)
            getDataRequest = new PreliminaryGetDataRequest(nonce, excludedKeys);
        else
            getDataRequest = new GetUpdatedDataRequest(networkNode.getNodeAddress(), nonce, excludedKeys);
        if (timeoutTimer == null) {
            timeoutTimer = UserThread.runAfter(() -> {
                // setup before sending to avoid race conditions
                if (!stopped) {
                    String errorMessage = "A timeout occurred at sending getDataRequest:" + getDataRequest + " on nodeAddress:" + nodeAddress;
                    log.debug(errorMessage + " / RequestDataHandler=" + RequestDataHandler.this);
                    handleFault(errorMessage, nodeAddress, CloseConnectionReason.SEND_MSG_TIMEOUT);
                } else {
                    log.trace("We have stopped already. We ignore that timeoutTimer.run call. " + "Might be caused by an previous networkNode.sendMessage.onFailure.");
                }
            }, TIME_OUT_SEC);
        }
        log.debug("We send a {} to peer {}. ", getDataRequest.getClass().getSimpleName(), nodeAddress);
        networkNode.addMessageListener(this);
        SettableFuture<Connection> future = networkNode.sendMessage(nodeAddress, getDataRequest);
        Futures.addCallback(future, new FutureCallback<Connection>() {

            @Override
            public void onSuccess(Connection connection) {
                if (!stopped) {
                    RequestDataHandler.this.connection = connection;
                    log.trace("Send " + getDataRequest + " to " + nodeAddress + " succeeded.");
                } else {
                    log.trace("We have stopped already. We ignore that networkNode.sendMessage.onSuccess call." + "Might be caused by an previous timeout.");
                }
            }

            @Override
            public void onFailure(@NotNull Throwable throwable) {
                if (!stopped) {
                    String errorMessage = "Sending getDataRequest to " + nodeAddress + " failed. That is expected if the peer is offline.\n\t" + "getDataRequest=" + getDataRequest + "." + "\n\tException=" + throwable.getMessage();
                    log.debug(errorMessage);
                    handleFault(errorMessage, nodeAddress, CloseConnectionReason.SEND_MSG_FAILURE);
                } else {
                    log.trace("We have stopped already. We ignore that networkNode.sendMessage.onFailure call. " + "Might be caused by an previous timeout.");
                }
            }
        });
    } else {
        log.warn("We have stopped already. We ignore that requestData call.");
    }
}
Also used : java.util(java.util) ProtectedStorageEntry(io.bitsquare.p2p.storage.storageentry.ProtectedStorageEntry) LoggerFactory(org.slf4j.LoggerFactory) Connection(io.bitsquare.p2p.network.Connection) Timer(io.bitsquare.common.Timer) SettableFuture(com.google.common.util.concurrent.SettableFuture) GetDataRequest(io.bitsquare.p2p.peers.getdata.messages.GetDataRequest) PreliminaryGetDataRequest(io.bitsquare.p2p.peers.getdata.messages.PreliminaryGetDataRequest) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) PersistedStoragePayload(io.bitsquare.p2p.storage.payload.PersistedStoragePayload) PeerManager(io.bitsquare.p2p.peers.PeerManager) Log(io.bitsquare.app.Log) P2PDataStorage(io.bitsquare.p2p.storage.P2PDataStorage) GetUpdatedDataRequest(io.bitsquare.p2p.peers.getdata.messages.GetUpdatedDataRequest) Logger(org.slf4j.Logger) GetDataResponse(io.bitsquare.p2p.peers.getdata.messages.GetDataResponse) UserThread(io.bitsquare.common.UserThread) Message(io.bitsquare.p2p.Message) NodeAddress(io.bitsquare.p2p.NodeAddress) StoragePayload(io.bitsquare.p2p.storage.payload.StoragePayload) Collectors(java.util.stream.Collectors) FutureCallback(com.google.common.util.concurrent.FutureCallback) TimeUnit(java.util.concurrent.TimeUnit) Nullable(org.jetbrains.annotations.Nullable) Futures(com.google.common.util.concurrent.Futures) LazyProcessedStoragePayload(io.bitsquare.p2p.storage.payload.LazyProcessedStoragePayload) MessageListener(io.bitsquare.p2p.network.MessageListener) CloseConnectionReason(io.bitsquare.p2p.network.CloseConnectionReason) NotNull(org.jetbrains.annotations.NotNull) NetworkNode(io.bitsquare.p2p.network.NetworkNode) PreliminaryGetDataRequest(io.bitsquare.p2p.peers.getdata.messages.PreliminaryGetDataRequest) PersistedStoragePayload(io.bitsquare.p2p.storage.payload.PersistedStoragePayload) Connection(io.bitsquare.p2p.network.Connection) GetDataRequest(io.bitsquare.p2p.peers.getdata.messages.GetDataRequest) PreliminaryGetDataRequest(io.bitsquare.p2p.peers.getdata.messages.PreliminaryGetDataRequest) GetUpdatedDataRequest(io.bitsquare.p2p.peers.getdata.messages.GetUpdatedDataRequest)

Aggregations

Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)1 FutureCallback (com.google.common.util.concurrent.FutureCallback)1 Futures (com.google.common.util.concurrent.Futures)1 SettableFuture (com.google.common.util.concurrent.SettableFuture)1 Log (io.bitsquare.app.Log)1 Timer (io.bitsquare.common.Timer)1 UserThread (io.bitsquare.common.UserThread)1 Message (io.bitsquare.p2p.Message)1 NodeAddress (io.bitsquare.p2p.NodeAddress)1 CloseConnectionReason (io.bitsquare.p2p.network.CloseConnectionReason)1 Connection (io.bitsquare.p2p.network.Connection)1 MessageListener (io.bitsquare.p2p.network.MessageListener)1 NetworkNode (io.bitsquare.p2p.network.NetworkNode)1 PeerManager (io.bitsquare.p2p.peers.PeerManager)1 GetDataRequest (io.bitsquare.p2p.peers.getdata.messages.GetDataRequest)1 GetDataResponse (io.bitsquare.p2p.peers.getdata.messages.GetDataResponse)1 GetUpdatedDataRequest (io.bitsquare.p2p.peers.getdata.messages.GetUpdatedDataRequest)1 PreliminaryGetDataRequest (io.bitsquare.p2p.peers.getdata.messages.PreliminaryGetDataRequest)1 P2PDataStorage (io.bitsquare.p2p.storage.P2PDataStorage)1 LazyProcessedStoragePayload (io.bitsquare.p2p.storage.payload.LazyProcessedStoragePayload)1