Search in sources :

Example 1 with CapabilityRequiringPayload

use of io.bitsquare.p2p.storage.payload.CapabilityRequiringPayload in project bitsquare by bitsquare.

the class Connection method isCapabilitySupported.

public boolean isCapabilitySupported(Message message) {
    if (message instanceof AddDataMessage) {
        final StoragePayload storagePayload = (((AddDataMessage) message).protectedStorageEntry).getStoragePayload();
        if (storagePayload instanceof CapabilityRequiringPayload) {
            final List<Integer> requiredCapabilities = ((CapabilityRequiringPayload) storagePayload).getRequiredCapabilities();
            final List<Integer> supportedCapabilities = sharedModel.getSupportedCapabilities();
            if (supportedCapabilities != null) {
                for (int messageCapability : requiredCapabilities) {
                    for (int connectionCapability : supportedCapabilities) {
                        if (messageCapability == connectionCapability)
                            return true;
                    }
                }
                log.debug("We do not send the message to the peer because he does not support the required capability for that message type.\n" + "Required capabilities is: " + requiredCapabilities.toString() + "\n" + "Supported capabilities is: " + supportedCapabilities.toString() + "\n" + "connection: " + this.toString() + "\n" + "storagePayload is: " + Utilities.toTruncatedString(storagePayload));
                return false;
            } else {
                log.debug("We do not send the message to the peer because he uses an old version which does not support capabilities.\n" + "Required capabilities is: " + requiredCapabilities.toString() + "\n" + "connection: " + this.toString() + "\n" + "storagePayload is: " + Utilities.toTruncatedString(storagePayload));
                return false;
            }
        } else {
            return true;
        }
    } else {
        return true;
    }
}
Also used : AddDataMessage(io.bitsquare.p2p.storage.messages.AddDataMessage) CapabilityRequiringPayload(io.bitsquare.p2p.storage.payload.CapabilityRequiringPayload) StoragePayload(io.bitsquare.p2p.storage.payload.StoragePayload)

Example 2 with CapabilityRequiringPayload

use of io.bitsquare.p2p.storage.payload.CapabilityRequiringPayload in project bitsquare by bitsquare.

the class GetDataRequestHandler method handle.

///////////////////////////////////////////////////////////////////////////////////////////
// API
///////////////////////////////////////////////////////////////////////////////////////////
public void handle(GetDataRequest getDataRequest, final Connection connection) {
    Log.traceCall(getDataRequest + "\n\tconnection=" + connection);
    final HashSet<ProtectedStorageEntry> filteredDataSet = new HashSet<>();
    final Set<Integer> lookupSet = new HashSet<>();
    Set<P2PDataStorage.ByteArray> excludedItems = getDataRequest.getExcludedKeys() != null ? getDataRequest.getExcludedKeys().stream().map(P2PDataStorage.ByteArray::new).collect(Collectors.toSet()) : new HashSet<>();
    for (ProtectedStorageEntry protectedStorageEntry : dataStorage.getFilteredValues(excludedItems)) {
        final StoragePayload storagePayload = protectedStorageEntry.getStoragePayload();
        boolean doAdd = false;
        if (storagePayload instanceof CapabilityRequiringPayload) {
            final List<Integer> requiredCapabilities = ((CapabilityRequiringPayload) storagePayload).getRequiredCapabilities();
            final List<Integer> supportedCapabilities = connection.getSupportedCapabilities();
            if (supportedCapabilities != null) {
                for (int messageCapability : requiredCapabilities) {
                    for (int connectionCapability : supportedCapabilities) {
                        if (messageCapability == connectionCapability) {
                            doAdd = true;
                            break;
                        }
                    }
                }
                if (!doAdd)
                    log.debug("We do not send the message to the peer because he does not support the required capability for that message type.\n" + "Required capabilities is: " + requiredCapabilities.toString() + "\n" + "Supported capabilities is: " + supportedCapabilities.toString() + "\n" + "storagePayload is: " + Utilities.toTruncatedString(storagePayload));
            } else {
                log.debug("We do not send the message to the peer because he uses an old version which does not support capabilities.\n" + "Required capabilities is: " + requiredCapabilities.toString() + "\n" + "storagePayload is: " + Utilities.toTruncatedString(storagePayload));
            }
        } else {
            doAdd = true;
        }
        if (doAdd) {
            // We have TradeStatistic data of both traders but we only send 1 item, 
            // so we use lookupSet as for a fast lookup. Using filteredDataSet would require a loop as it stores 
            // protectedStorageEntry not storagePayload. protectedStorageEntry is different for both traders but storagePayload not, 
            // as we ignore the pubKey and data there in the hashCode method.
            boolean notContained = lookupSet.add(storagePayload.hashCode());
            if (notContained)
                filteredDataSet.add(protectedStorageEntry);
        }
    }
    GetDataResponse getDataResponse = new GetDataResponse(filteredDataSet, getDataRequest.getNonce(), getDataRequest instanceof GetUpdatedDataRequest);
    if (timeoutTimer == null) {
        timeoutTimer = UserThread.runAfter(() -> {
            // setup before sending to avoid race conditions
            String errorMessage = "A timeout occurred for getDataResponse:" + getDataResponse + " on connection:" + connection;
            handleFault(errorMessage, CloseConnectionReason.SEND_MSG_TIMEOUT, connection);
        }, TIME_OUT_SEC, TimeUnit.SECONDS);
    }
    SettableFuture<Connection> future = networkNode.sendMessage(connection, getDataResponse);
    Futures.addCallback(future, new FutureCallback<Connection>() {

        @Override
        public void onSuccess(Connection connection) {
            if (!stopped) {
                log.trace("Send DataResponse to {} succeeded. getDataResponse={}", connection.getPeersNodeAddressOptional(), getDataResponse);
                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 getDataRequest to " + connection + " failed. That is expected if the peer is offline. getDataResponse=" + getDataResponse + "." + "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(io.bitsquare.p2p.network.Connection) CapabilityRequiringPayload(io.bitsquare.p2p.storage.payload.CapabilityRequiringPayload) GetDataResponse(io.bitsquare.p2p.peers.getdata.messages.GetDataResponse) GetUpdatedDataRequest(io.bitsquare.p2p.peers.getdata.messages.GetUpdatedDataRequest) StoragePayload(io.bitsquare.p2p.storage.payload.StoragePayload) HashSet(java.util.HashSet) ProtectedStorageEntry(io.bitsquare.p2p.storage.storageentry.ProtectedStorageEntry)

Aggregations

CapabilityRequiringPayload (io.bitsquare.p2p.storage.payload.CapabilityRequiringPayload)2 StoragePayload (io.bitsquare.p2p.storage.payload.StoragePayload)2 Connection (io.bitsquare.p2p.network.Connection)1 GetDataResponse (io.bitsquare.p2p.peers.getdata.messages.GetDataResponse)1 GetUpdatedDataRequest (io.bitsquare.p2p.peers.getdata.messages.GetUpdatedDataRequest)1 AddDataMessage (io.bitsquare.p2p.storage.messages.AddDataMessage)1 ProtectedStorageEntry (io.bitsquare.p2p.storage.storageentry.ProtectedStorageEntry)1 HashSet (java.util.HashSet)1