Minor improvements

This commit is contained in:
Christian Basler 2017-04-12 17:18:09 +02:00
parent f50d7445c1
commit e5c956c6e5
5 changed files with 57 additions and 29 deletions

View File

@ -25,7 +25,6 @@ import ch.dissem.bitmessage.exception.DecryptionFailedException;
import ch.dissem.bitmessage.factory.Factory;
import ch.dissem.bitmessage.ports.*;
import ch.dissem.bitmessage.utils.Property;
import ch.dissem.bitmessage.utils.TTL;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -39,7 +38,8 @@ import static ch.dissem.bitmessage.InternalContext.NETWORK_EXTRA_BYTES;
import static ch.dissem.bitmessage.InternalContext.NETWORK_NONCE_TRIALS_PER_BYTE;
import static ch.dissem.bitmessage.entity.Plaintext.Type.BROADCAST;
import static ch.dissem.bitmessage.entity.Plaintext.Type.MSG;
import static ch.dissem.bitmessage.utils.UnixTime.*;
import static ch.dissem.bitmessage.utils.UnixTime.HOUR;
import static ch.dissem.bitmessage.utils.UnixTime.MINUTE;
/**
* <p>Use this class if you want to create a Bitmessage client.</p>
@ -404,23 +404,6 @@ public class BitmessageContext {
return this;
}
/**
* Time to live in seconds for public keys the client sends. Defaults to the maximum of 28 days,
* but on weak devices smaller values might be desirable.
* <p>
* Please be aware that this might cause some problems where you can't receive a message (the
* sender can't receive your public key) in some special situations. Also note that it's probably
* not a good idea to set it too low.
* </p>
*
* @deprecated use {@link TTL#pubkey(long)} instead.
*/
public Builder pubkeyTTL(long days) {
if (days < 0 || days > 28 * DAY) throw new IllegalArgumentException("TTL must be between 1 and 28 days");
TTL.pubkey(days);
return this;
}
public BitmessageContext build() {
nonNull("inventory", inventory);
nonNull("nodeRegistry", nodeRegistry);

View File

@ -81,7 +81,7 @@ public class ProofOfWorkService implements ProofOfWorkEngine.Callback, InternalC
public void onNonceCalculated(byte[] initialHash, byte[] nonce) {
Item item = powRepo.getItem(initialHash);
if (item.message == null) {
ObjectMessage object = powRepo.getItem(initialHash).object;
ObjectMessage object = item.object;
object.setNonce(nonce);
Plaintext plaintext = messageRepo.getMessage(initialHash);
if (plaintext != null) {

View File

@ -20,6 +20,8 @@ import ch.dissem.bitmessage.BitmessageContext;
import ch.dissem.bitmessage.entity.valueobject.NetworkAddress;
import ch.dissem.bitmessage.utils.Encode;
import ch.dissem.bitmessage.utils.UnixTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.io.OutputStream;
@ -30,6 +32,7 @@ import java.nio.ByteBuffer;
*/
public class Version implements MessagePayload {
private static final long serialVersionUID = 7219240857343176567L;
private static final Logger LOG = LoggerFactory.getLogger(Version.class);
/**
* Identifies protocol version being used by the node. Should equal 3. Nodes should disconnect if the remote node's
@ -93,6 +96,10 @@ public class Version implements MessagePayload {
return services;
}
public boolean provides(Service service) {
return service != null && service.isEnabled(services);
}
public long getTimestamp() {
return timestamp;
}
@ -159,7 +166,7 @@ public class Version implements MessagePayload {
public Builder defaults(long clientNonce) {
version = BitmessageContext.CURRENT_VERSION;
services = 1;
services = Service.getServiceFlag(Service.NODE_NETWORK);
timestamp = UnixTime.now();
userAgent = "/Jabit:0.0.1/";
streamNumbers = new long[]{1};
@ -172,6 +179,11 @@ public class Version implements MessagePayload {
return this;
}
public Builder services(Service... services) {
this.services = Service.getServiceFlag(services);
return this;
}
public Builder services(long services) {
this.services = services;
return this;
@ -211,4 +223,27 @@ public class Version implements MessagePayload {
return new Version(this);
}
}
public enum Service {
NODE_NETWORK(1);
// TODO: NODE_SSL(2);
long flag;
Service(long flag) {
this.flag = flag;
}
public boolean isEnabled(long flag) {
return (flag & this.flag) != 0;
}
public static long getServiceFlag(Service... services) {
long flag = 0;
for (Service service : services) {
flag |= service.flag;
}
return flag;
}
}
}

View File

@ -17,6 +17,7 @@
package ch.dissem.bitmessage.entity.valueobject;
import ch.dissem.bitmessage.entity.Streamable;
import ch.dissem.bitmessage.entity.Version;
import ch.dissem.bitmessage.exception.ApplicationException;
import ch.dissem.bitmessage.utils.Encode;
import ch.dissem.bitmessage.utils.UnixTime;
@ -75,6 +76,13 @@ public class NetworkAddress implements Streamable {
return services;
}
public boolean provides(Version.Service service) {
if (service == null) {
return false;
}
return service.isEnabled(services);
}
public long getStream() {
return stream;
}
@ -194,20 +202,20 @@ public class NetworkAddress implements Streamable {
int p08, int p09, int p10, int p11,
int p12, int p13, int p14, int p15) {
this.ipv6 = new byte[]{
(byte) p00, (byte) p01, (byte) p02, (byte) p03,
(byte) p04, (byte) p05, (byte) p06, (byte) p07,
(byte) p08, (byte) p09, (byte) p10, (byte) p11,
(byte) p12, (byte) p13, (byte) p14, (byte) p15
(byte) p00, (byte) p01, (byte) p02, (byte) p03,
(byte) p04, (byte) p05, (byte) p06, (byte) p07,
(byte) p08, (byte) p09, (byte) p10, (byte) p11,
(byte) p12, (byte) p13, (byte) p14, (byte) p15
};
return this;
}
public Builder ipv4(int p00, int p01, int p02, int p03) {
this.ipv6 = new byte[]{
(byte) 0, (byte) 0, (byte) 0x00, (byte) 0x00,
(byte) 0, (byte) 0, (byte) 0x00, (byte) 0x00,
(byte) 0, (byte) 0, (byte) 0xff, (byte) 0xff,
(byte) p00, (byte) p01, (byte) p02, (byte) p03
(byte) 0, (byte) 0, (byte) 0x00, (byte) 0x00,
(byte) 0, (byte) 0, (byte) 0x00, (byte) 0x00,
(byte) 0, (byte) 0, (byte) 0xff, (byte) 0xff,
(byte) p00, (byte) p01, (byte) p02, (byte) p03
};
return this;
}

View File

@ -24,6 +24,7 @@ import ch.dissem.bitmessage.entity.valueobject.NetworkAddress;
import ch.dissem.bitmessage.exception.NodeException;
import ch.dissem.bitmessage.utils.AccessCounter;
import ch.dissem.bitmessage.utils.Decode;
import ch.dissem.bitmessage.utils.Strings;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -103,6 +104,7 @@ class V3MessageFactory {
payload = Factory.getObjectPayload(objectType, version, stream, dataStream, data.length);
} catch (Exception e) {
LOG.trace("Could not parse object payload - using generic payload instead", e);
LOG.info(Strings.hex(data).toString());
payload = new GenericPayload(version, stream, data);
}