use of i2p.bote.UniqueId in project i2p.i2p-bote by i2p.
the class MessageIdCache method read.
private void read(File cacheFile) {
idList = Collections.synchronizedList(new ArrayList<UniqueId>());
if (!cacheFile.exists()) {
log.debug("Message ID cache file doesn't exist: <" + cacheFile.getAbsolutePath() + ">");
return;
}
log.debug("Reading message ID cache file: <" + cacheFile.getAbsolutePath() + ">");
BufferedReader input = null;
try {
input = new BufferedReader(new FileReader(cacheFile));
while (true) {
String idString = input.readLine();
if (// EOF
idString == null)
break;
UniqueId id = new UniqueId(idString);
idList.add(id);
}
} catch (IOException e) {
log.error("Can't read message ID cache file.", e);
} finally {
if (input != null)
try {
input.close();
} catch (IOException e) {
log.error("Error closing BufferedReader.", e);
}
}
}
use of i2p.bote.UniqueId in project i2p.i2p-bote by i2p.
the class EmailPacketFolder method process.
/**
* Deletes email packets.
* @param delRequest An instance of {@link EmailPacketDeleteRequest}
*/
@Override
public synchronized void process(DeleteRequest delRequest) {
log.debug("Processing delete request: " + delRequest);
if (!(delRequest instanceof EmailPacketDeleteRequest))
log.error("Invalid type of delete request for EmailPacketFolder: " + delRequest.getClass());
EmailPacketDeleteRequest emailPacketDelRequest = (EmailPacketDeleteRequest) delRequest;
// see if the packet exists
Hash dhtKey = emailPacketDelRequest.getDhtKey();
DhtStorablePacket storedPacket = retrieve(dhtKey);
if (storedPacket instanceof EncryptedEmailPacket) {
// verify
Hash verificationHash = ((EncryptedEmailPacket) storedPacket).getDeleteVerificationHash();
UniqueId delAuthorization = emailPacketDelRequest.getAuthorization();
boolean valid = Util.isDeleteAuthorizationValid(verificationHash, delAuthorization);
if (valid)
delete(dhtKey, delAuthorization);
else
log.debug("Invalid Delete Authorization in EmailPacketDeleteRequest. Should be: <" + verificationHash.toBase64() + ">");
} else if (storedPacket != null)
log.debug("EncryptedEmailPacket expected for DHT key <" + dhtKey + ">, found " + storedPacket.getClass().getSimpleName());
}
use of i2p.bote.UniqueId 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);
}
use of i2p.bote.UniqueId in project i2p.i2p-bote by i2p.
the class IncompleteEmailFolder method addEmailPacket.
/**
* Stores an <code>UnencryptedEmailPacket</code> in the folder and returns <code>true</code>
* if an email was completed as a result of adding the packet.
* @param packetToStore
* @see i2p.bote.folder.PacketFolder#add(I2PBotePacket, String)
*/
public synchronized boolean addEmailPacket(UnencryptedEmailPacket packetToStore) {
UniqueId messageId = packetToStore.getMessageId();
// if a previously assembled (completed) email contained the message ID, ignore the email packet
if (messageIdCache.contains(messageId)) {
log.debug("Discarding email packet because the message ID matches a previously received email. Packet: " + packetToStore);
return false;
}
add(packetToStore, getFilename(packetToStore));
// TODO possible optimization: if getNumFragments == 1, no need to check for other packet files
File[] finishedPacketFiles = getAllMatchingFiles(messageId);
// if all packets of the email are available, assemble them into an email
if (finishedPacketFiles.length == packetToStore.getNumFragments()) {
assemble(finishedPacketFiles);
messageIdCache.add(messageId);
return true;
}
return false;
}
use of i2p.bote.UniqueId in project i2p.i2p-bote by i2p.
the class IndexPacketFolder method process.
/**
* Deletes index packet entries.
* @param delRequest An instance of {@link IndexPacketDeleteRequest}
*/
@Override
public synchronized void process(DeleteRequest delRequest) {
log.debug("Processing delete request: " + delRequest);
if (!(delRequest instanceof IndexPacketDeleteRequest))
log.error("Invalid type of delete request for IndexPacketFolder: " + delRequest.getClass());
IndexPacketDeleteRequest indexPacketDelRequest = (IndexPacketDeleteRequest) delRequest;
Hash destHash = indexPacketDelRequest.getEmailDestHash();
DhtStorablePacket storedPacket = retrieve(destHash);
if (storedPacket instanceof IndexPacket) {
IndexPacket indexPacket = (IndexPacket) storedPacket;
Collection<Hash> keysToDelete = indexPacketDelRequest.getDhtKeys();
for (Hash keyToDelete : keysToDelete) {
// verify
Hash verificationHash = indexPacket.getDeleteVerificationHash(keyToDelete);
if (verificationHash == null)
log.debug("Email packet key " + keyToDelete + " from IndexPacketDeleteRequest not found in index packet for destination " + destHash);
else {
UniqueId delAuthorization = indexPacketDelRequest.getDeleteAuthorization(keyToDelete);
boolean valid = Util.isDeleteAuthorizationValid(verificationHash, delAuthorization);
if (valid)
remove(indexPacket, keyToDelete, delAuthorization);
else
log.debug("Invalid delete verification hash in IndexPacketDeleteRequest. Should be: <" + verificationHash.toBase64() + ">");
}
}
} else if (storedPacket != null)
log.debug("IndexPacket expected for DHT key <" + destHash + ">, found " + storedPacket.getClass().getSimpleName());
}
Aggregations