use of i2p.bote.packet.relay.RelayRequest in project i2p.i2p-bote by i2p.
the class RelayPacketHandler method packetReceived.
@Override
public void packetReceived(CommunicationPacket packet, Destination sender, long receiveTime) {
if (packet instanceof RelayRequest && dht.isReady()) {
RelayRequest relayRequest = (RelayRequest) packet;
CommunicationPacket payload;
try {
payload = relayRequest.getStoredPacket(i2pSession);
} catch (DataFormatException e) {
log.error("Invalid RelayRequest received from peer " + Util.toBase32(sender), e);
return;
} catch (MalformedPacketException e) {
log.error("Invalid RelayRequest received from peer " + Util.toBase32(sender), e);
return;
}
log.debug("Received a relay request, payload: " + payload);
if (payload instanceof RelayRequest) {
log.debug("Relay packet is of type " + payload.getClass().getSimpleName() + ", storing it in the relay packet folder.");
relayPacketFolder.add((RelayRequest) payload);
confirm(sender, relayRequest);
} else if (payload instanceof StoreRequest) {
log.debug("Relay packet is of type " + payload.getClass().getSimpleName() + ", storing it in the DHT.");
final DhtStorablePacket dhtPacket = ((StoreRequest) payload).getPacketToStore();
// do dht.store() in a separate thread so we don't block the notifier thread
dhtTaskExecutor.submit(new Runnable() {
@Override
public void run() {
try {
dht.store(dhtPacket);
log.debug("Finished storing DHT packet: " + dhtPacket);
} catch (InterruptedException e) {
log.debug("Interrupted while storing packet in the DHT.");
} catch (DhtException e) {
log.error("Error storing packet in the DHT: " + dhtPacket, e);
}
}
});
confirm(sender, relayRequest);
} else
log.error("Don't know how to handle relay packet of type " + payload.getClass());
}
}
use of i2p.bote.packet.relay.RelayRequest in project i2p.i2p-bote by i2p.
the class RelayPacketFolder method createFolderElement.
@Override
protected RelayRequest createFolderElement(File file) throws IOException {
RelayRequest packet = super.createFolderElement(file);
if (packet != null) {
try {
long sendTime = getSendTime(file.getName());
packet.setSendTime(sendTime);
} catch (NumberFormatException e) {
log.error("Invalid send time in filename: <" + file.getAbsolutePath() + ">", e);
}
}
return packet;
}
use of i2p.bote.packet.relay.RelayRequest in project i2p.i2p-bote by i2p.
the class RelayPacketFolderTest method setUp.
@Before
public void setUp() throws Exception {
File tempDir = new File(System.getProperty("java.io.tmpdir"));
testDir = new File(tempDir, "RelayPacketFolderTest-" + System.currentTimeMillis());
folderDir = new File(testDir, "relay_pkt");
folder = new RelayPacketFolder(folderDir);
// make an EncryptedEmailPacket
String base64Destination = "X3oKYQJ~1EAz7B1ZYGSrOTIMCW5Rnn2Svoc38dx5D9~zvz8vqiWcH-pCqQDwLgPWl9RTBzHtTmZcGRPXIv54i0XWeUfX6rTPDQGuZsnBMM0xrkH2FNLNFaJa0NgW3uKXWpNj9AI1AXUXzK-2MYTYoaZHx5SBoCaKfAGMcFJvTON1~kopxBxdBF9Q7T4~PJ3I2LeU-ycmUlehe9N9bIu7adUGyPGVl8Ka-UxwQromoJ~vSWHHl8HkwcDkW--v9Aj~wvFqxqriFkB1EeBiThi3V4XtVY~GUP4IkRj9YZGTsSBf3eS4xwXgnYWlB7IvxAGBfHY9MCg3lbAa1Dg~1IH6rhtXxsXUtGcXsz9yMZTxXHd~rGo~JrXeM1y~Vcenpr6tJcum6pxevkKzzT0qDegGPH3Zhqz7sSeeIaJEcPBUAkX89csqyFWFIjTMm6yZp2rW-QYUnVNLNTjf7vndYUAEICogAkq~btqpIzrGEpm3Pr9F23br3SpbOmdxQxg51AMmAAAA";
Destination nextDestination = new Destination(base64Destination.substring(0, 516));
long delayMilliseconds = TimeUnit.MILLISECONDS.convert(111, TimeUnit.MINUTES);
String content = "Warum, warum, warum\n" + "Ist die Banane krumm?\n" + "Weil niemand in den Urwald zog\n" + "Und die Banane grade bog.\n";
byte[] messageIdBytes = new byte[] { -69, -24, -109, 1, 69, -122, -69, 113, -68, -90, 55, -28, 105, 97, 125, 70, 51, 58, 14, 2, -13, -53, 90, -29, 36, 67, 36, -94, -108, -125, 11, 123 };
UniqueId messageId = new UniqueId(messageIdBytes, 0);
int fragmentIndex = 0;
InputStream contentStream = new ByteArrayInputStream(content.getBytes());
UnencryptedEmailPacket unencryptedPacket = new UnencryptedEmailPacket(contentStream, messageId, fragmentIndex, I2PBotePacket.MAX_DATAGRAM_SIZE);
unencryptedPacket.setNumFragments(1);
String base64EmailDest = "rIbyUukqtsacD-MDJJ8KbIP9d3WQQo~t~zysc3bNcF1mSwz9PcGJnvWCNhnG2nzbdUAIDouESZjLRnBr7-mxNS";
EmailDestination recipient = new EmailDestination(base64EmailDest);
emailPacket = new EncryptedEmailPacket(unencryptedPacket, recipient);
// make a RelayRequest
StoreRequest storeRequest = new StoreRequest(emailPacket);
relayRequest = new RelayRequest(storeRequest, nextDestination, delayMilliseconds, 1000);
}
use of i2p.bote.packet.relay.RelayRequest in project i2p.i2p-bote by i2p.
the class RelayPacketFolderTest method testAddRemove.
/**
* Tests {@link RelayPacketFolder#add(RelayRequest)} and removal of the packet from the
* folder via an <code>Iterator</code>.
*/
@Test
public void testAddRemove() {
folder.add(relayRequest);
Iterator<RelayRequest> iterator = folder.iterator();
assertTrue("Folder is empty after a packet was added to it!", iterator.hasNext());
// Read the stored packet, convert to a byte array, and compare.
// This also verifies the send time which is not stored in the packet itself
RelayRequest storedPacket = iterator.next();
byte[] arrayA = relayRequest.toByteArray();
byte[] arrayB = storedPacket.toByteArray();
assertArrayEquals("The two arrays differ!", arrayA, arrayB);
assertFalse("Folder has more than one element!", iterator.hasNext());
iterator.remove();
assertFalse("Packet was not deleted!", folder.iterator().hasNext());
}
use of i2p.bote.packet.relay.RelayRequest in project i2p.i2p-bote by i2p.
the class OutboxProcessor method send.
/**
* Stores a packet in the DHT directly or via relay peers.
* @param hops The number of hops, or zero to store it directly in the DHT
* @throws DhtException
* @throws InterruptedException
*/
private void send(DhtStorablePacket dhtPacket, int hops, long minDelay, long maxDelay, int relayRedundancy) throws DhtException, InterruptedException {
if (hops > 0) {
StoreRequest storeRequest = new StoreRequest(dhtPacket);
for (int i = 0; i < relayRedundancy; i++) {
// TODO don't use the same relay peer twice if there are enough peers
RelayRequest packet = RelayRequest.create(storeRequest, peerManager, hops, minDelay, maxDelay);
relayPacketFolder.add(packet);
}
} else
dht.store(dhtPacket);
}
Aggregations