smarter network code, fixed various issues

- deciding on the stream at creation time is just silly - it should be done based on the identities (this part is TODO)
- changed NodeRegistry so it doesn't store nodes - this should help to connect faster
- inventory items shouldn't be advertised to nodes that are already aware of them (issue #13)
- objects shouldn't be requested more than once (issue #9)
This commit is contained in:
2015-07-01 06:57:30 +02:00
parent c8960df2b3
commit 2c4d95af2f
11 changed files with 248 additions and 205 deletions

View File

@ -267,7 +267,6 @@ public class BitmessageContext {
AddressRepository addressRepo;
MessageRepository messageRepo;
ProofOfWorkEngine proofOfWorkEngine;
TreeSet<Long> streams;
public Builder() {
}
@ -307,28 +306,12 @@ public class BitmessageContext {
return this;
}
public Builder streams(Collection<Long> streams) {
this.streams = new TreeSet<>(streams);
return this;
}
public Builder streams(long... streams) {
this.streams = new TreeSet<>();
for (long stream : streams) {
this.streams.add(stream);
}
return this;
}
public BitmessageContext build() {
nonNull("inventory", inventory);
nonNull("nodeRegistry", nodeRegistry);
nonNull("networkHandler", networkHandler);
nonNull("addressRepo", addressRepo);
nonNull("messageRepo", messageRepo);
if (streams == null) {
streams(1);
}
if (proofOfWorkEngine == null) {
proofOfWorkEngine = new MultiThreadedPOWEngine();
}

View File

@ -49,7 +49,7 @@ public class InternalContext {
private final MessageRepository messageRepository;
private final ProofOfWorkEngine proofOfWorkEngine;
private final TreeSet<Long> streams;
private final TreeSet<Long> streams = new TreeSet<>();
private final int port;
private long networkNonceTrialsPerByte = 1000;
private long networkExtraBytes = 1000;
@ -65,7 +65,7 @@ public class InternalContext {
this.clientNonce = Security.randomNonce();
port = builder.port;
streams = builder.streams;
streams.add(1L); // FIXME
init(inventory, nodeRegistry, networkHandler, addressRepository, messageRepository, proofOfWorkEngine);
}

View File

@ -16,14 +16,11 @@
package ch.dissem.bitmessage.ports;
import ch.dissem.bitmessage.BitmessageContext;
import ch.dissem.bitmessage.entity.ObjectMessage;
import ch.dissem.bitmessage.entity.payload.ObjectPayload;
import ch.dissem.bitmessage.entity.valueobject.InventoryVector;
import ch.dissem.bitmessage.utils.Property;
import java.io.IOException;
import java.util.Map;
/**
* Handles incoming messages

View File

@ -57,4 +57,15 @@ public class Collections {
}
return result;
}
public static <T> T selectRandom(Collection<T> collection) {
int index = RANDOM.nextInt(collection.size());
for (T item : collection) {
if (index == 0) {
return item;
}
index--;
}
throw new IllegalArgumentException("Empty collection? Size: " + collection.size());
}
}

View File

@ -20,10 +20,14 @@ package ch.dissem.bitmessage.utils;
* A simple utility class that simplifies using the second based time used in Bitmessage.
*/
public class UnixTime {
/**
* Length of a minute in seconds, intended for use with {@link #now(long)}.
*/
public static final int MINUTE = 60;
/**
* Length of an hour in seconds, intended for use with {@link #now(long)}.
*/
public static final long HOUR = 60 * 60;
public static final long HOUR = 60 * MINUTE;
/**
* Length of a day in seconds, intended for use with {@link #now(long)}.
*/