Search in sources :

Example 1 with DhtStorageHandler

use of i2p.bote.network.DhtStorageHandler in project i2p.i2p-bote by i2p.

the class KademliaDHT method storeLocally.

/**
 * Stores a DHT packet locally. The folder the packet is stored in depends on the packet type.
 * @param packetToStore
 * @param sender The peer that sent the store request; can be <code>null</code> for the local node
 */
private void storeLocally(DhtStorablePacket packetToStore, Destination sender) {
    if (packetToStore != null) {
        DhtStorageHandler storageHandler = storageHandlers.get(packetToStore.getClass());
        if (storageHandler != null) {
            // If another peer is trying to store a packet that we know has been deleted, let them know and don't store the packet.
            if (storageHandler instanceof DeletionAwareDhtFolder<?> && sender != null) {
                DeletionAwareDhtFolder<?> folder = (DeletionAwareDhtFolder<?>) storageHandler;
                DeleteRequest delRequest = folder.storeAndCreateDeleteRequest(packetToStore);
                if (delRequest != null)
                    sendQueue.send(delRequest, sender);
            } else
                storageHandler.store(packetToStore);
            replicateThread.packetStored(storageHandler, packetToStore);
        } else
            log.warn("No storage handler found for type " + packetToStore.getClass().getSimpleName() + ".");
    }
}
Also used : DhtStorageHandler(i2p.bote.network.DhtStorageHandler) DeleteRequest(i2p.bote.packet.dht.DeleteRequest) DeletionAwareDhtFolder(i2p.bote.folder.DeletionAwareDhtFolder)

Example 2 with DhtStorageHandler

use of i2p.bote.network.DhtStorageHandler in project i2p.i2p-bote by i2p.

the class KademliaDHT method findDeleteAuthorizationKey.

@Override
public UniqueId findDeleteAuthorizationKey(Hash dhtKey, Hash verificationHash) throws InterruptedException {
    final Collection<Destination> closeNodes = getClosestNodes(dhtKey);
    log.info("Querying " + closeNodes.size() + " peers with DeletionQueries for Kademlia key " + dhtKey);
    DhtStorageHandler storageHandler = storageHandlers.get(EncryptedEmailPacket.class);
    if (storageHandler instanceof DeletionAwareDhtFolder) {
        DeletionAwareDhtFolder<?> folder = (DeletionAwareDhtFolder<?>) storageHandler;
        UniqueId delAuthorization = folder.getDeleteAuthorization(dhtKey);
        if (delAuthorization != null)
            return delAuthorization;
    } else
        log.error("StorageHandler for EncryptedEmailPackets is not a DeletionAwareDhtFolder!");
    // Send the DeletionQueries
    PacketBatch batch = new PacketBatch();
    for (Destination node : closeNodes) if (// local has already been taken care of
    !localDestination.equals(node))
        batch.putPacket(new DeletionQuery(dhtKey), node);
    sendQueue.send(batch);
    batch.awaitSendCompletion();
    // wait for replies
    batch.awaitFirstReply(RESPONSE_TIMEOUT, TimeUnit.SECONDS);
    log.info(batch.getResponses().size() + " response packets received for deletion query for hash " + dhtKey);
    sendQueue.remove(batch);
    Map<Destination, DataPacket> responses = batch.getResponses();
    for (DataPacket response : responses.values()) if (response instanceof DeletionInfoPacket) {
        DeletionInfoPacket delInfo = (DeletionInfoPacket) response;
        DeletionRecord delRecord = delInfo.getEntry(dhtKey);
        if (delRecord != null) {
            boolean valid = Util.isDeleteAuthorizationValid(verificationHash, delRecord.delAuthorization);
            if (valid)
                return delRecord.delAuthorization;
        }
    }
    return null;
}
Also used : Destination(net.i2p.data.Destination) UniqueId(i2p.bote.UniqueId) DeletionQuery(i2p.bote.packet.dht.DeletionQuery) DeletionInfoPacket(i2p.bote.packet.dht.DeletionInfoPacket) DeletionRecord(i2p.bote.packet.dht.DeletionRecord) DhtStorageHandler(i2p.bote.network.DhtStorageHandler) PacketBatch(i2p.bote.network.PacketBatch) DataPacket(i2p.bote.packet.DataPacket) DeletionAwareDhtFolder(i2p.bote.folder.DeletionAwareDhtFolder)

Example 3 with DhtStorageHandler

use of i2p.bote.network.DhtStorageHandler in project i2p.i2p-bote by i2p.

the class ReplicateThread method replicate.

private void replicate() throws InterruptedException {
    log.debug("Replicating DHT data...");
    replicationRunning = true;
    // refresh peers close to the local destination
    ClosestNodesLookupTask lookupTask = new ClosestNodesLookupTask(localDestination.calculateHash(), sendQueue, i2pReceiver, bucketManager);
    List<Destination> closestNodes = lookupTask.call();
    closestNodes.remove(localDestination);
    int numReplicated = 0;
    int numSkipped = 0;
    // If a peer responds with a delete request, replicate the delete request instead.
    for (DhtStorageHandler dhtStore : dhtStores) for (Iterator<? extends DhtStorablePacket> packetIterator = dhtStore.individualPackets(); packetIterator.hasNext(); ) {
        DhtStorablePacket packet = packetIterator.next();
        Hash dhtKey = packet.getDhtKey();
        if (!keysToSkip.contains(dhtKey)) {
            StoreRequest request = new StoreRequest(packet);
            List<Destination> closestPeers = bucketManager.getClosestPeers(dhtKey, KademliaConstants.K);
            for (Destination peer : closestPeers) {
                // Send the store request and give the peer time to respond with a delete request
                sendQueue.send(request, peer).await(SEND_TIMEOUT_MINUTES, TimeUnit.MINUTES);
                TimeUnit.SECONDS.sleep(WAIT_TIME_SECONDS);
                // If we received a delete request for the DHT item, notify the other close peers
                DeleteRequest delRequest = receivedDeleteRequests.get(dhtKey);
                if (delRequest != null) {
                    // KademliaDHT handles the delete request for local data, but we forward the
                    // request to the other nodes close to the DHT key.
                    // Note that the delete request contains only one entry, see packetReceived()
                    sendDeleteRequest(delRequest, closestPeers, peer);
                    break;
                }
            }
            numReplicated++;
        } else
            numSkipped++;
    }
    keysToSkip.clear();
    replicationRunning = false;
    receivedDeleteRequests.clear();
    log.debug("Replication finished. Replicated " + numReplicated + " packets, skipped: " + numSkipped);
}
Also used : DhtStorablePacket(i2p.bote.packet.dht.DhtStorablePacket) Destination(net.i2p.data.Destination) DhtStorageHandler(i2p.bote.network.DhtStorageHandler) StoreRequest(i2p.bote.packet.dht.StoreRequest) Iterator(java.util.Iterator) List(java.util.List) Hash(net.i2p.data.Hash) DeleteRequest(i2p.bote.packet.dht.DeleteRequest)

Example 4 with DhtStorageHandler

use of i2p.bote.network.DhtStorageHandler in project i2p.i2p-bote by i2p.

the class KademliaDHT method packetReceived.

// PacketListener implementation
@Override
public void packetReceived(CommunicationPacket packet, Destination sender, long receiveTime) {
    if (packet instanceof FindClosePeersPacket)
        sendPeerList((FindClosePeersPacket) packet, sender);
    else if (packet instanceof StoreRequest) {
        DhtStorablePacket packetToStore = ((StoreRequest) packet).getPacketToStore();
        storeLocally(packetToStore, sender);
    } else if (packet instanceof RetrieveRequest) {
        RetrieveRequest retrieveRequest = (RetrieveRequest) packet;
        DhtStorageHandler storageHandler = storageHandlers.get(retrieveRequest.getDataType());
        if (storageHandler != null) {
            DhtStorablePacket storedPacket = storageHandler.retrieve(retrieveRequest.getKey());
            // if requested packet found, send it to the requester
            Collection<ResponsePacket> response = ResponsePacket.create(storedPacket, StatusCode.OK, retrieveRequest.getPacketId());
            if (storedPacket != null)
                log.debug("Packet found for retrieve request: [" + retrieveRequest + "], replying to sender: [" + Util.toBase32(sender) + "]");
            else
                log.debug("No matching packet found for retrieve request: [" + retrieveRequest + "]");
            sendQueue.send(response, sender);
        } else
            log.warn("No storage handler found for type " + packet.getClass().getSimpleName() + ".");
    } else if (packet instanceof DeleteRequest) {
        DeleteRequest delRequest = (DeleteRequest) packet;
        DhtStorageHandler storageHandler = storageHandlers.get(delRequest.getDataType());
        if (storageHandler instanceof DeletionAwareDhtFolder<?>)
            ((DeletionAwareDhtFolder<?>) storageHandler).process(delRequest);
    } else if (packet instanceof DeletionQuery) {
        DhtStorageHandler storageHandler = storageHandlers.get(EncryptedEmailPacket.class);
        if (storageHandler instanceof DeletionAwareDhtFolder) {
            Hash dhtKey = ((DeletionQuery) packet).getDhtKey();
            UniqueId delAuthorization = ((DeletionAwareDhtFolder<?>) storageHandler).getDeleteAuthorization(dhtKey);
            // If we know the Delete Authorization for the DHT key, send it to the peer
            if (delAuthorization != null) {
                DeletionInfoPacket delInfo = new DeletionInfoPacket();
                delInfo.put(dhtKey, delAuthorization);
                Collection<ResponsePacket> response = ResponsePacket.create(delInfo, StatusCode.OK, packet.getPacketId());
                sendQueue.send(response, sender);
            }
        }
    }
    // bucketManager is not registered as a PacketListener, so notify it here
    bucketManager.packetReceived(packet, sender, receiveTime);
}
Also used : DhtStorablePacket(i2p.bote.packet.dht.DhtStorablePacket) RetrieveRequest(i2p.bote.packet.dht.RetrieveRequest) UniqueId(i2p.bote.UniqueId) StoreRequest(i2p.bote.packet.dht.StoreRequest) Hash(net.i2p.data.Hash) FindClosePeersPacket(i2p.bote.packet.dht.FindClosePeersPacket) DeletionAwareDhtFolder(i2p.bote.folder.DeletionAwareDhtFolder) DeletionQuery(i2p.bote.packet.dht.DeletionQuery) DeletionInfoPacket(i2p.bote.packet.dht.DeletionInfoPacket) ResponsePacket(i2p.bote.packet.ResponsePacket) DhtStorageHandler(i2p.bote.network.DhtStorageHandler) DeleteRequest(i2p.bote.packet.dht.DeleteRequest)

Aggregations

DhtStorageHandler (i2p.bote.network.DhtStorageHandler)4 DeletionAwareDhtFolder (i2p.bote.folder.DeletionAwareDhtFolder)3 DeleteRequest (i2p.bote.packet.dht.DeleteRequest)3 UniqueId (i2p.bote.UniqueId)2 DeletionInfoPacket (i2p.bote.packet.dht.DeletionInfoPacket)2 DeletionQuery (i2p.bote.packet.dht.DeletionQuery)2 DhtStorablePacket (i2p.bote.packet.dht.DhtStorablePacket)2 StoreRequest (i2p.bote.packet.dht.StoreRequest)2 Destination (net.i2p.data.Destination)2 Hash (net.i2p.data.Hash)2 PacketBatch (i2p.bote.network.PacketBatch)1 DataPacket (i2p.bote.packet.DataPacket)1 ResponsePacket (i2p.bote.packet.ResponsePacket)1 DeletionRecord (i2p.bote.packet.dht.DeletionRecord)1 FindClosePeersPacket (i2p.bote.packet.dht.FindClosePeersPacket)1 RetrieveRequest (i2p.bote.packet.dht.RetrieveRequest)1 Iterator (java.util.Iterator)1 List (java.util.List)1