forked from I2P_Developers/i2p.i2p
GarlicClove: Store time as long rather than Date to save space
This commit is contained in:
@@ -11,7 +11,6 @@ package net.i2p.data.i2np;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
import java.util.Date;
|
|
||||||
|
|
||||||
import net.i2p.I2PAppContext;
|
import net.i2p.I2PAppContext;
|
||||||
import net.i2p.data.Certificate;
|
import net.i2p.data.Certificate;
|
||||||
@@ -35,7 +34,7 @@ public class GarlicClove extends DataStructureImpl {
|
|||||||
private DeliveryInstructions _instructions;
|
private DeliveryInstructions _instructions;
|
||||||
private I2NPMessage _msg;
|
private I2NPMessage _msg;
|
||||||
private long _cloveId;
|
private long _cloveId;
|
||||||
private Date _expiration;
|
private long _expiration;
|
||||||
private Certificate _certificate;
|
private Certificate _certificate;
|
||||||
|
|
||||||
public GarlicClove(I2PAppContext context) {
|
public GarlicClove(I2PAppContext context) {
|
||||||
@@ -49,8 +48,8 @@ public class GarlicClove extends DataStructureImpl {
|
|||||||
public void setData(I2NPMessage msg) { _msg = msg; }
|
public void setData(I2NPMessage msg) { _msg = msg; }
|
||||||
public long getCloveId() { return _cloveId; }
|
public long getCloveId() { return _cloveId; }
|
||||||
public void setCloveId(long id) { _cloveId = id; }
|
public void setCloveId(long id) { _cloveId = id; }
|
||||||
public Date getExpiration() { return _expiration; }
|
public long getExpiration() { return _expiration; }
|
||||||
public void setExpiration(Date exp) { _expiration = exp; }
|
public void setExpiration(long exp) { _expiration = exp; }
|
||||||
public Certificate getCertificate() { return _certificate; }
|
public Certificate getCertificate() { return _certificate; }
|
||||||
public void setCertificate(Certificate cert) { _certificate = cert; }
|
public void setCertificate(Certificate cert) { _certificate = cert; }
|
||||||
|
|
||||||
@@ -79,7 +78,7 @@ public class GarlicClove extends DataStructureImpl {
|
|||||||
}
|
}
|
||||||
_cloveId = DataHelper.fromLong(source, cur, 4);
|
_cloveId = DataHelper.fromLong(source, cur, 4);
|
||||||
cur += 4;
|
cur += 4;
|
||||||
_expiration = DataHelper.fromDate(source, cur);
|
_expiration = DataHelper.fromLong(source, cur, 8);
|
||||||
cur += DataHelper.DATE_LENGTH;
|
cur += DataHelper.DATE_LENGTH;
|
||||||
_certificate = Certificate.create(source, cur);
|
_certificate = Certificate.create(source, cur);
|
||||||
cur += _certificate.size();
|
cur += _certificate.size();
|
||||||
@@ -99,7 +98,7 @@ public class GarlicClove extends DataStructureImpl {
|
|||||||
I2NPMessageHandler handler = new I2NPMessageHandler(_context);
|
I2NPMessageHandler handler = new I2NPMessageHandler(_context);
|
||||||
_msg = I2NPMessageImpl.fromRawByteArrayNTCP2(_context, source, offset + isz, len - isz, handler);
|
_msg = I2NPMessageImpl.fromRawByteArrayNTCP2(_context, source, offset + isz, len - isz, handler);
|
||||||
_cloveId = _msg.getUniqueId();
|
_cloveId = _msg.getUniqueId();
|
||||||
_expiration = new Date(_msg.getMessageExpiration());
|
_expiration = _msg.getMessageExpiration();
|
||||||
_certificate = Certificate.NULL_CERT;
|
_certificate = Certificate.NULL_CERT;
|
||||||
} catch (I2NPMessageException ime) {
|
} catch (I2NPMessageException ime) {
|
||||||
throw new DataFormatException("Unable to read the message from a garlic clove", ime);
|
throw new DataFormatException("Unable to read the message from a garlic clove", ime);
|
||||||
@@ -125,7 +124,7 @@ public class GarlicClove extends DataStructureImpl {
|
|||||||
offset = _msg.toByteArray(rv, offset);
|
offset = _msg.toByteArray(rv, offset);
|
||||||
DataHelper.toLong(rv, offset, 4, _cloveId);
|
DataHelper.toLong(rv, offset, 4, _cloveId);
|
||||||
offset += 4;
|
offset += 4;
|
||||||
DataHelper.toDate(rv, offset, _expiration.getTime());
|
DataHelper.toLong(rv, offset, 8, _expiration);
|
||||||
offset += DataHelper.DATE_LENGTH;
|
offset += DataHelper.DATE_LENGTH;
|
||||||
offset += _certificate.writeBytes(rv, offset);
|
offset += _certificate.writeBytes(rv, offset);
|
||||||
if (offset != rv.length) {
|
if (offset != rv.length) {
|
||||||
@@ -173,7 +172,7 @@ public class GarlicClove extends DataStructureImpl {
|
|||||||
return DataHelper.eq(_certificate, clove._certificate) &&
|
return DataHelper.eq(_certificate, clove._certificate) &&
|
||||||
_cloveId == clove._cloveId &&
|
_cloveId == clove._cloveId &&
|
||||||
DataHelper.eq(_msg, clove._msg) &&
|
DataHelper.eq(_msg, clove._msg) &&
|
||||||
DataHelper.eq(_expiration, clove._expiration) &&
|
_expiration == clove._expiration &&
|
||||||
DataHelper.eq(_instructions, clove._instructions);
|
DataHelper.eq(_instructions, clove._instructions);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -182,7 +181,7 @@ public class GarlicClove extends DataStructureImpl {
|
|||||||
return DataHelper.hashCode(_certificate) ^
|
return DataHelper.hashCode(_certificate) ^
|
||||||
(int) _cloveId ^
|
(int) _cloveId ^
|
||||||
DataHelper.hashCode(_msg) ^
|
DataHelper.hashCode(_msg) ^
|
||||||
DataHelper.hashCode(_expiration) ^
|
(int) _expiration ^
|
||||||
DataHelper.hashCode(_instructions);
|
DataHelper.hashCode(_instructions);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -193,7 +192,7 @@ public class GarlicClove extends DataStructureImpl {
|
|||||||
buf.append("\n\tInstructions: ").append(_instructions);
|
buf.append("\n\tInstructions: ").append(_instructions);
|
||||||
buf.append("\n\tCertificate: ").append(_certificate);
|
buf.append("\n\tCertificate: ").append(_certificate);
|
||||||
buf.append("\n\tClove ID: ").append(_cloveId);
|
buf.append("\n\tClove ID: ").append(_cloveId);
|
||||||
buf.append("\n\tExpiration: ").append(_expiration);
|
buf.append("\n\tExpiration: ").append(DataHelper.formatTime(_expiration));
|
||||||
buf.append("\n\tData: ").append(_msg);
|
buf.append("\n\tData: ").append(_msg);
|
||||||
buf.append("]");
|
buf.append("]");
|
||||||
return buf.toString();
|
return buf.toString();
|
||||||
|
|||||||
@@ -467,7 +467,7 @@ public class GarlicMessageBuilder {
|
|||||||
private static byte[] buildCommonClove(GarlicClove clove, GarlicConfig config) {
|
private static byte[] buildCommonClove(GarlicClove clove, GarlicConfig config) {
|
||||||
clove.setCertificate(config.getCertificate());
|
clove.setCertificate(config.getCertificate());
|
||||||
clove.setCloveId(config.getId());
|
clove.setCloveId(config.getId());
|
||||||
clove.setExpiration(new Date(config.getExpiration()));
|
clove.setExpiration(config.getExpiration());
|
||||||
clove.setInstructions(config.getDeliveryInstructions());
|
clove.setInstructions(config.getDeliveryInstructions());
|
||||||
return clove.toByteArray();
|
return clove.toByteArray();
|
||||||
}
|
}
|
||||||
@@ -514,7 +514,7 @@ public class GarlicMessageBuilder {
|
|||||||
clove.setData(config.getPayload());
|
clove.setData(config.getPayload());
|
||||||
clove.setCertificate(Certificate.NULL_CERT);
|
clove.setCertificate(Certificate.NULL_CERT);
|
||||||
clove.setCloveId(0);
|
clove.setCloveId(0);
|
||||||
clove.setExpiration(new Date(config.getExpiration()));
|
clove.setExpiration(config.getExpiration());
|
||||||
clove.setInstructions(config.getDeliveryInstructions());
|
clove.setInstructions(config.getDeliveryInstructions());
|
||||||
return clove;
|
return clove;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -134,11 +134,11 @@ public class GarlicMessageReceiver {
|
|||||||
// is no longer a separate field for the clove ID in the transmission format.
|
// is no longer a separate field for the clove ID in the transmission format.
|
||||||
//String invalidReason = _context.messageValidator().validateMessage(clove.getCloveId(),
|
//String invalidReason = _context.messageValidator().validateMessage(clove.getCloveId(),
|
||||||
// clove.getExpiration().getTime());
|
// clove.getExpiration().getTime());
|
||||||
String invalidReason = _context.messageValidator().validateMessage(clove.getExpiration().getTime());
|
String invalidReason = _context.messageValidator().validateMessage(clove.getExpiration());
|
||||||
|
|
||||||
boolean rv = invalidReason == null;
|
boolean rv = invalidReason == null;
|
||||||
if (!rv) {
|
if (!rv) {
|
||||||
String howLongAgo = DataHelper.formatDuration(_context.clock().now()-clove.getExpiration().getTime());
|
String howLongAgo = DataHelper.formatDuration(_context.clock().now()-clove.getExpiration());
|
||||||
if (_log.shouldInfo())
|
if (_log.shouldInfo())
|
||||||
_log.info("Clove is NOT valid: id=" + clove.getCloveId()
|
_log.info("Clove is NOT valid: id=" + clove.getCloveId()
|
||||||
+ " expiration " + howLongAgo + " ago", new Exception("Invalid within..."));
|
+ " expiration " + howLongAgo + " ago", new Exception("Invalid within..."));
|
||||||
|
|||||||
Reference in New Issue
Block a user