mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-20 22:59:54 +01:00
Calculate the timestamp outside the subscription/transport update
writer - this will allow it to be saved so new connections can work out whether they should send updates.
This commit is contained in:
@@ -9,5 +9,6 @@ import net.sf.briar.api.protocol.Group;
|
|||||||
public interface SubscriptionWriter {
|
public interface SubscriptionWriter {
|
||||||
|
|
||||||
/** Writes the contents of the update. */
|
/** Writes the contents of the update. */
|
||||||
void writeSubscriptions(Map<Group, Long> subs) throws IOException;
|
void writeSubscriptionUpdate(Map<Group, Long> subs, long timestamp)
|
||||||
|
throws IOException;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,6 +7,6 @@ import java.util.Map;
|
|||||||
public interface TransportWriter {
|
public interface TransportWriter {
|
||||||
|
|
||||||
/** Writes the contents of the update. */
|
/** Writes the contents of the update. */
|
||||||
void writeTransports(Map<String, Map<String, String>> transports)
|
void writeTransportUpdate(Map<String, Map<String, String>> transports,
|
||||||
throws IOException;
|
long timestamp) throws IOException;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -445,7 +445,7 @@ class ReadWriteLockDatabaseComponent<Txn> extends DatabaseComponentImpl<Txn> {
|
|||||||
Txn txn = db.startTransaction();
|
Txn txn = db.startTransaction();
|
||||||
try {
|
try {
|
||||||
Map<Group, Long> subs = db.getVisibleSubscriptions(txn, c);
|
Map<Group, Long> subs = db.getVisibleSubscriptions(txn, c);
|
||||||
s.writeSubscriptions(subs);
|
s.writeSubscriptionUpdate(subs, System.currentTimeMillis());
|
||||||
if(LOG.isLoggable(Level.FINE))
|
if(LOG.isLoggable(Level.FINE))
|
||||||
LOG.fine("Added " + subs.size() + " subscriptions");
|
LOG.fine("Added " + subs.size() + " subscriptions");
|
||||||
db.commitTransaction(txn);
|
db.commitTransaction(txn);
|
||||||
@@ -475,7 +475,8 @@ class ReadWriteLockDatabaseComponent<Txn> extends DatabaseComponentImpl<Txn> {
|
|||||||
try {
|
try {
|
||||||
Map<String, Map<String, String>> transports =
|
Map<String, Map<String, String>> transports =
|
||||||
db.getTransports(txn);
|
db.getTransports(txn);
|
||||||
t.writeTransports(transports);
|
long timestamp = System.currentTimeMillis();
|
||||||
|
t.writeTransportUpdate(transports, timestamp);
|
||||||
if(LOG.isLoggable(Level.FINE))
|
if(LOG.isLoggable(Level.FINE))
|
||||||
LOG.fine("Added " + transports.size() + " transports");
|
LOG.fine("Added " + transports.size() + " transports");
|
||||||
db.commitTransaction(txn);
|
db.commitTransaction(txn);
|
||||||
|
|||||||
@@ -339,7 +339,7 @@ class SynchronizedDatabaseComponent<Txn> extends DatabaseComponentImpl<Txn> {
|
|||||||
Txn txn = db.startTransaction();
|
Txn txn = db.startTransaction();
|
||||||
try {
|
try {
|
||||||
Map<Group, Long> subs = db.getVisibleSubscriptions(txn, c);
|
Map<Group, Long> subs = db.getVisibleSubscriptions(txn, c);
|
||||||
s.writeSubscriptions(subs);
|
s.writeSubscriptionUpdate(subs, System.currentTimeMillis());
|
||||||
if(LOG.isLoggable(Level.FINE))
|
if(LOG.isLoggable(Level.FINE))
|
||||||
LOG.fine("Added " + subs.size() + " subscriptions");
|
LOG.fine("Added " + subs.size() + " subscriptions");
|
||||||
db.commitTransaction(txn);
|
db.commitTransaction(txn);
|
||||||
@@ -363,7 +363,8 @@ class SynchronizedDatabaseComponent<Txn> extends DatabaseComponentImpl<Txn> {
|
|||||||
try {
|
try {
|
||||||
Map<String, Map<String, String>> transports =
|
Map<String, Map<String, String>> transports =
|
||||||
db.getTransports(txn);
|
db.getTransports(txn);
|
||||||
t.writeTransports(transports);
|
long timestamp = System.currentTimeMillis();
|
||||||
|
t.writeTransportUpdate(transports, timestamp);
|
||||||
if(LOG.isLoggable(Level.FINE))
|
if(LOG.isLoggable(Level.FINE))
|
||||||
LOG.fine("Added " + transports.size() + " transports");
|
LOG.fine("Added " + transports.size() + " transports");
|
||||||
db.commitTransaction(txn);
|
db.commitTransaction(txn);
|
||||||
|
|||||||
@@ -20,10 +20,11 @@ class SubscriptionWriterImpl implements SubscriptionWriter {
|
|||||||
w = writerFactory.createWriter(out);
|
w = writerFactory.createWriter(out);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void writeSubscriptions(Map<Group, Long> subs) throws IOException {
|
public void writeSubscriptionUpdate(Map<Group, Long> subs, long timestamp)
|
||||||
|
throws IOException {
|
||||||
w.writeUserDefinedTag(Tags.SUBSCRIPTION_UPDATE);
|
w.writeUserDefinedTag(Tags.SUBSCRIPTION_UPDATE);
|
||||||
w.writeMap(subs);
|
w.writeMap(subs);
|
||||||
w.writeInt64(System.currentTimeMillis());
|
w.writeInt64(timestamp);
|
||||||
out.flush();
|
out.flush();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,7 +20,8 @@ class TransportWriterImpl implements TransportWriter {
|
|||||||
w = writerFactory.createWriter(out);
|
w = writerFactory.createWriter(out);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void writeTransports(Map<String, Map<String, String>> transports)
|
public void writeTransportUpdate(
|
||||||
|
Map<String, Map<String, String>> transports, long timestamp)
|
||||||
throws IOException {
|
throws IOException {
|
||||||
w.writeUserDefinedTag(Tags.TRANSPORT_UPDATE);
|
w.writeUserDefinedTag(Tags.TRANSPORT_UPDATE);
|
||||||
w.writeListStart();
|
w.writeListStart();
|
||||||
@@ -30,7 +31,7 @@ class TransportWriterImpl implements TransportWriter {
|
|||||||
w.writeMap(e.getValue());
|
w.writeMap(e.getValue());
|
||||||
}
|
}
|
||||||
w.writeListEnd();
|
w.writeListEnd();
|
||||||
w.writeInt64(System.currentTimeMillis());
|
w.writeInt64(timestamp);
|
||||||
out.flush();
|
out.flush();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -64,7 +64,7 @@ public class FileReadWriteTest extends TestCase {
|
|||||||
private final File file = new File(testDir, "foo");
|
private final File file = new File(testDir, "foo");
|
||||||
|
|
||||||
private final BatchId ack = new BatchId(TestUtils.getRandomId());
|
private final BatchId ack = new BatchId(TestUtils.getRandomId());
|
||||||
private final long start = System.currentTimeMillis();
|
private final long timestamp = System.currentTimeMillis();
|
||||||
|
|
||||||
private final PacketReaderFactory packetReaderFactory;
|
private final PacketReaderFactory packetReaderFactory;
|
||||||
private final PacketWriterFactory packetWriterFactory;
|
private final PacketWriterFactory packetWriterFactory;
|
||||||
@@ -172,11 +172,11 @@ public class FileReadWriteTest extends TestCase {
|
|||||||
Map<Group, Long> subs = new LinkedHashMap<Group, Long>();
|
Map<Group, Long> subs = new LinkedHashMap<Group, Long>();
|
||||||
subs.put(group, 0L);
|
subs.put(group, 0L);
|
||||||
subs.put(group1, 0L);
|
subs.put(group1, 0L);
|
||||||
s.writeSubscriptions(subs);
|
s.writeSubscriptionUpdate(subs, timestamp);
|
||||||
packetWriter.finishPacket();
|
packetWriter.finishPacket();
|
||||||
|
|
||||||
TransportWriter t = protocolWriterFactory.createTransportWriter(out);
|
TransportWriter t = protocolWriterFactory.createTransportWriter(out);
|
||||||
t.writeTransports(transports);
|
t.writeTransportUpdate(transports, timestamp);
|
||||||
packetWriter.finishPacket();
|
packetWriter.finishPacket();
|
||||||
|
|
||||||
out.flush();
|
out.flush();
|
||||||
@@ -257,16 +257,14 @@ public class FileReadWriteTest extends TestCase {
|
|||||||
assertEquals(2, subs.size());
|
assertEquals(2, subs.size());
|
||||||
assertEquals(Long.valueOf(0L), subs.get(group));
|
assertEquals(Long.valueOf(0L), subs.get(group));
|
||||||
assertEquals(Long.valueOf(0L), subs.get(group1));
|
assertEquals(Long.valueOf(0L), subs.get(group1));
|
||||||
assertTrue(s.getTimestamp() > start);
|
assertTrue(s.getTimestamp() == timestamp);
|
||||||
assertTrue(s.getTimestamp() <= System.currentTimeMillis());
|
|
||||||
|
|
||||||
// Read the transport update
|
// Read the transport update
|
||||||
assertTrue(protocolReader.hasTransportUpdate());
|
assertTrue(protocolReader.hasTransportUpdate());
|
||||||
TransportUpdate t = protocolReader.readTransportUpdate();
|
TransportUpdate t = protocolReader.readTransportUpdate();
|
||||||
packetReader.finishPacket();
|
packetReader.finishPacket();
|
||||||
assertEquals(transports, t.getTransports());
|
assertEquals(transports, t.getTransports());
|
||||||
assertTrue(t.getTimestamp() > start);
|
assertTrue(t.getTimestamp() == timestamp);
|
||||||
assertTrue(t.getTimestamp() <= System.currentTimeMillis());
|
|
||||||
|
|
||||||
in.close();
|
in.close();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -779,8 +779,9 @@ public abstract class DatabaseComponentTest extends TestCase {
|
|||||||
oneOf(database).getVisibleSubscriptions(txn, contactId);
|
oneOf(database).getVisibleSubscriptions(txn, contactId);
|
||||||
will(returnValue(Collections.singletonMap(group, 0L)));
|
will(returnValue(Collections.singletonMap(group, 0L)));
|
||||||
// Add the subscriptions to the writer
|
// Add the subscriptions to the writer
|
||||||
oneOf(subscriptionWriter).writeSubscriptions(
|
oneOf(subscriptionWriter).writeSubscriptionUpdate(
|
||||||
Collections.singletonMap(group, 0L));
|
with(Collections.singletonMap(group, 0L)),
|
||||||
|
with(any(long.class)));
|
||||||
}});
|
}});
|
||||||
DatabaseComponent db = createDatabaseComponent(database, cleaner);
|
DatabaseComponent db = createDatabaseComponent(database, cleaner);
|
||||||
|
|
||||||
@@ -811,7 +812,8 @@ public abstract class DatabaseComponentTest extends TestCase {
|
|||||||
oneOf(database).getTransports(txn);
|
oneOf(database).getTransports(txn);
|
||||||
will(returnValue(transports));
|
will(returnValue(transports));
|
||||||
// Add the properties to the writer
|
// Add the properties to the writer
|
||||||
oneOf(transportWriter).writeTransports(transports);
|
oneOf(transportWriter).writeTransportUpdate(with(transports),
|
||||||
|
with(any(long.class)));
|
||||||
}});
|
}});
|
||||||
DatabaseComponent db = createDatabaseComponent(database, cleaner);
|
DatabaseComponent db = createDatabaseComponent(database, cleaner);
|
||||||
|
|
||||||
|
|||||||
@@ -51,6 +51,7 @@ public class ProtocolReadWriteTest extends TestCase {
|
|||||||
private final BitSet bitSet;
|
private final BitSet bitSet;
|
||||||
private final Map<Group, Long> subscriptions;
|
private final Map<Group, Long> subscriptions;
|
||||||
private final Map<String, Map<String, String>> transports;
|
private final Map<String, Map<String, String>> transports;
|
||||||
|
private final long timestamp = System.currentTimeMillis();
|
||||||
|
|
||||||
public ProtocolReadWriteTest() throws Exception {
|
public ProtocolReadWriteTest() throws Exception {
|
||||||
super();
|
super();
|
||||||
@@ -75,8 +76,6 @@ public class ProtocolReadWriteTest extends TestCase {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testWriteAndRead() throws Exception {
|
public void testWriteAndRead() throws Exception {
|
||||||
long start = System.currentTimeMillis();
|
|
||||||
|
|
||||||
// Write
|
// Write
|
||||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||||
|
|
||||||
@@ -96,10 +95,10 @@ public class ProtocolReadWriteTest extends TestCase {
|
|||||||
r.writeRequest(offerId, bitSet, 10);
|
r.writeRequest(offerId, bitSet, 10);
|
||||||
|
|
||||||
SubscriptionWriter s = writerFactory.createSubscriptionWriter(out);
|
SubscriptionWriter s = writerFactory.createSubscriptionWriter(out);
|
||||||
s.writeSubscriptions(subscriptions);
|
s.writeSubscriptionUpdate(subscriptions, timestamp);
|
||||||
|
|
||||||
TransportWriter t = writerFactory.createTransportWriter(out);
|
TransportWriter t = writerFactory.createTransportWriter(out);
|
||||||
t.writeTransports(transports);
|
t.writeTransportUpdate(transports, timestamp);
|
||||||
|
|
||||||
// Read
|
// Read
|
||||||
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
||||||
@@ -120,10 +119,10 @@ public class ProtocolReadWriteTest extends TestCase {
|
|||||||
|
|
||||||
SubscriptionUpdate subscriptionUpdate = reader.readSubscriptionUpdate();
|
SubscriptionUpdate subscriptionUpdate = reader.readSubscriptionUpdate();
|
||||||
assertEquals(subscriptions, subscriptionUpdate.getSubscriptions());
|
assertEquals(subscriptions, subscriptionUpdate.getSubscriptions());
|
||||||
assertTrue(subscriptionUpdate.getTimestamp() >= start);
|
assertTrue(subscriptionUpdate.getTimestamp() == timestamp);
|
||||||
|
|
||||||
TransportUpdate transportUpdate = reader.readTransportUpdate();
|
TransportUpdate transportUpdate = reader.readTransportUpdate();
|
||||||
assertEquals(transports, transportUpdate.getTransports());
|
assertEquals(transports, transportUpdate.getTransports());
|
||||||
assertTrue(transportUpdate.getTimestamp() >= start);
|
assertTrue(transportUpdate.getTimestamp() == timestamp);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user