Skip to content

Commit

Permalink
Fix serialization size computation.
Browse files Browse the repository at this point in the history
  • Loading branch information
zapek committed May 10, 2024
1 parent 54712e9 commit 7a8aefa
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .run/All Tests.run.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<ExternalSystemDebugServerProcess>true</ExternalSystemDebugServerProcess>
<ExternalSystemReattachDebugProcess>true</ExternalSystemReattachDebugProcess>
<DebugAllEnabled>false</DebugAllEnabled>
<ForceTestExec>false</ForceTestExec>
<RunAsTest>false</RunAsTest>
<method v="2"/>
</configuration>
</component>
2 changes: 1 addition & 1 deletion app/src/main/java/io/xeres/app/xrs/item/Item.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ else if (RsSerializable.class.isAssignableFrom(getClass()))

var rawItem = new RawItem(buf, getPriority());
log.trace("Serialized buffer ==> {}", rawItem);
if (flags.contains(SerializationFlags.SIGNATURE))
if (flags.contains(SerializationFlags.SIGNATURE) || flags.contains(SerializationFlags.SIZE))
{
buf = backupBuf;
backupBuf = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@

public enum SerializationFlags
{
SIGNATURE
SIGNATURE,
SIZE
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@

package io.xeres.app.xrs.serialization;

import io.netty.buffer.Unpooled;
import io.xeres.app.xrs.item.Item;
import io.xeres.app.xrs.service.RsService;

import java.util.EnumSet;
import java.util.HashMap;
Expand All @@ -44,8 +46,11 @@ private SerializerSizeCache()
* @param item the item
* @return the size of the item after serialization, header included
*/
public static int getItemSize(Item item)
public static int getItemSize(Item item, RsService service)
{
return cache.computeIfAbsent(item.getClass(), aClass -> item.serializeItem(EnumSet.noneOf(SerializationFlags.class)).getSize());
return cache.computeIfAbsent(item.getClass(), aClass -> {
item.setSerialization(Unpooled.buffer().alloc(), service);
return item.serializeItem(EnumSet.of(SerializationFlags.SIZE)).getSize();
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

package io.xeres.app.xrs.service.turtle;

import io.netty.buffer.Unpooled;
import io.xeres.app.database.DatabaseSession;
import io.xeres.app.database.DatabaseSessionManager;
import io.xeres.app.database.model.location.Location;
Expand Down Expand Up @@ -223,7 +224,8 @@ public void sendTurtleData(LocationId virtualPeerId, TurtleGenericTunnelItem ite

item.setTunnelId(tunnelId);

var itemSerializedSize = item.serializeItem(EnumSet.noneOf(SerializationFlags.class)).getSize(); // XXX: optimize... we're doing it twice
item.setSerialization(Unpooled.buffer().alloc(), this);
var itemSerializedSize = item.serializeItem(EnumSet.of(SerializationFlags.SIZE)).getSize(); // XXX: optimize... we're doing it twice

// XXX: timestamp the tunnel

Expand Down Expand Up @@ -272,7 +274,8 @@ private void routeGenericTunnel(PeerConnection sender, TurtleGenericTunnelItem i

// XXX: add time stamp logic

var serializedSize = item.serializeItem(EnumSet.noneOf(SerializationFlags.class)).getSize(); // XXX: maybe find a flag to do the size serialization only because it's a bit of a CPU waste
item.setSerialization(Unpooled.buffer().alloc(), this);
var serializedSize = item.serializeItem(EnumSet.of(SerializationFlags.SIZE)).getSize(); // XXX: maybe find a flag to do the size serialization only because it's a bit of a CPU waste
tunnel.addTransferredBytes(serializedSize);

if (sender.getLocation().equals(tunnel.getDestination()))
Expand Down Expand Up @@ -358,7 +361,7 @@ private void handleTunnelRequest(PeerConnection sender, TurtleTunnelRequestItem
return;
}

trafficStatisticsBuffer.addToTunnelRequestsDownload(SerializerSizeCache.getItemSize(item));
trafficStatisticsBuffer.addToTunnelRequestsDownload(SerializerSizeCache.getItemSize(item, this));

if (isBanned(item.getFileHash()))
{
Expand Down Expand Up @@ -403,7 +406,7 @@ private void handleTunnelRequest(PeerConnection sender, TurtleTunnelRequestItem
tunnelProbability.incrementDepth(itemToSend);
if (SecureRandomUtils.nextDouble() <= probability)
{
trafficStatistics.addToTunnelRequestsUpload(SerializerSizeCache.getItemSize(itemToSend));
trafficStatistics.addToTunnelRequestsUpload(SerializerSizeCache.getItemSize(itemToSend, this));
peerConnectionManager.writeItem(peerConnection, itemToSend, this);
}
},
Expand Down
11 changes: 9 additions & 2 deletions common/src/test/java/io/xeres/common/protocol/dns/DNSTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@

package io.xeres.common.protocol.dns;

import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable;

import java.io.IOException;
import java.net.InetAddress;
Expand All @@ -29,8 +29,15 @@

class DNSTest
{
/**
* This test verifies that myip.opendns.com works for finding one's own IP when UPNP is not working.
* It also tests that akamai works, in case opendns is removed, and we need to fall back to something else.
* It only runs on my machine because of the chicken & egg problem on knowing one's own IP.
*
* @throws IOException
*/
@Test
@Disabled
@EnabledIfEnvironmentVariable(named = "COMPUTERNAME", matches = "B650")
void DNS_OK() throws IOException
{
var ip1 = DNS.resolve("myip.opendns.com", "208.67.222.222"); // resolver1.opendns.com
Expand Down

0 comments on commit 7a8aefa

Please sign in to comment.