Search in sources :

Example 1 with RelayRequest

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());
    }
}
Also used : DhtStorablePacket(i2p.bote.packet.dht.DhtStorablePacket) DataFormatException(net.i2p.data.DataFormatException) StoreRequest(i2p.bote.packet.dht.StoreRequest) CommunicationPacket(i2p.bote.packet.CommunicationPacket) MalformedPacketException(i2p.bote.packet.MalformedPacketException) RelayRequest(i2p.bote.packet.relay.RelayRequest)

Example 2 with RelayRequest

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;
}
Also used : RelayRequest(i2p.bote.packet.relay.RelayRequest)

Example 3 with RelayRequest

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);
}
Also used : Destination(net.i2p.data.Destination) EmailDestination(i2p.bote.email.EmailDestination) UniqueId(i2p.bote.UniqueId) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) EncryptedEmailPacket(i2p.bote.packet.dht.EncryptedEmailPacket) StoreRequest(i2p.bote.packet.dht.StoreRequest) ByteArrayInputStream(java.io.ByteArrayInputStream) EmailDestination(i2p.bote.email.EmailDestination) UnencryptedEmailPacket(i2p.bote.packet.dht.UnencryptedEmailPacket) File(java.io.File) RelayRequest(i2p.bote.packet.relay.RelayRequest) Before(org.junit.Before)

Example 4 with RelayRequest

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());
}
Also used : RelayRequest(i2p.bote.packet.relay.RelayRequest) Test(org.junit.Test)

Example 5 with RelayRequest

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);
}
Also used : StoreRequest(i2p.bote.packet.dht.StoreRequest) RelayRequest(i2p.bote.packet.relay.RelayRequest)

Aggregations

RelayRequest (i2p.bote.packet.relay.RelayRequest)6 StoreRequest (i2p.bote.packet.dht.StoreRequest)3 Destination (net.i2p.data.Destination)2 UniqueId (i2p.bote.UniqueId)1 EmailDestination (i2p.bote.email.EmailDestination)1 CommunicationPacket (i2p.bote.packet.CommunicationPacket)1 MalformedPacketException (i2p.bote.packet.MalformedPacketException)1 DhtStorablePacket (i2p.bote.packet.dht.DhtStorablePacket)1 EncryptedEmailPacket (i2p.bote.packet.dht.EncryptedEmailPacket)1 UnencryptedEmailPacket (i2p.bote.packet.dht.UnencryptedEmailPacket)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 File (java.io.File)1 InputStream (java.io.InputStream)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 DataFormatException (net.i2p.data.DataFormatException)1 Before (org.junit.Before)1 Test (org.junit.Test)1