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:
akwizgran
2011-08-14 13:36:21 +02:00
parent 4497774311
commit 2c13e35dc4
9 changed files with 31 additions and 27 deletions

View File

@@ -9,5 +9,6 @@ import net.sf.briar.api.protocol.Group;
public interface SubscriptionWriter {
/** Writes the contents of the update. */
void writeSubscriptions(Map<Group, Long> subs) throws IOException;
void writeSubscriptionUpdate(Map<Group, Long> subs, long timestamp)
throws IOException;
}

View File

@@ -7,6 +7,6 @@ import java.util.Map;
public interface TransportWriter {
/** Writes the contents of the update. */
void writeTransports(Map<String, Map<String, String>> transports)
throws IOException;
void writeTransportUpdate(Map<String, Map<String, String>> transports,
long timestamp) throws IOException;
}

View File

@@ -445,7 +445,7 @@ class ReadWriteLockDatabaseComponent<Txn> extends DatabaseComponentImpl<Txn> {
Txn txn = db.startTransaction();
try {
Map<Group, Long> subs = db.getVisibleSubscriptions(txn, c);
s.writeSubscriptions(subs);
s.writeSubscriptionUpdate(subs, System.currentTimeMillis());
if(LOG.isLoggable(Level.FINE))
LOG.fine("Added " + subs.size() + " subscriptions");
db.commitTransaction(txn);
@@ -475,7 +475,8 @@ class ReadWriteLockDatabaseComponent<Txn> extends DatabaseComponentImpl<Txn> {
try {
Map<String, Map<String, String>> transports =
db.getTransports(txn);
t.writeTransports(transports);
long timestamp = System.currentTimeMillis();
t.writeTransportUpdate(transports, timestamp);
if(LOG.isLoggable(Level.FINE))
LOG.fine("Added " + transports.size() + " transports");
db.commitTransaction(txn);

View File

@@ -339,7 +339,7 @@ class SynchronizedDatabaseComponent<Txn> extends DatabaseComponentImpl<Txn> {
Txn txn = db.startTransaction();
try {
Map<Group, Long> subs = db.getVisibleSubscriptions(txn, c);
s.writeSubscriptions(subs);
s.writeSubscriptionUpdate(subs, System.currentTimeMillis());
if(LOG.isLoggable(Level.FINE))
LOG.fine("Added " + subs.size() + " subscriptions");
db.commitTransaction(txn);
@@ -363,7 +363,8 @@ class SynchronizedDatabaseComponent<Txn> extends DatabaseComponentImpl<Txn> {
try {
Map<String, Map<String, String>> transports =
db.getTransports(txn);
t.writeTransports(transports);
long timestamp = System.currentTimeMillis();
t.writeTransportUpdate(transports, timestamp);
if(LOG.isLoggable(Level.FINE))
LOG.fine("Added " + transports.size() + " transports");
db.commitTransaction(txn);

View File

@@ -20,10 +20,11 @@ class SubscriptionWriterImpl implements SubscriptionWriter {
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.writeMap(subs);
w.writeInt64(System.currentTimeMillis());
w.writeInt64(timestamp);
out.flush();
}
}

View File

@@ -20,7 +20,8 @@ class TransportWriterImpl implements TransportWriter {
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 {
w.writeUserDefinedTag(Tags.TRANSPORT_UPDATE);
w.writeListStart();
@@ -30,7 +31,7 @@ class TransportWriterImpl implements TransportWriter {
w.writeMap(e.getValue());
}
w.writeListEnd();
w.writeInt64(System.currentTimeMillis());
w.writeInt64(timestamp);
out.flush();
}
}

View File

@@ -64,7 +64,7 @@ public class FileReadWriteTest extends TestCase {
private final File file = new File(testDir, "foo");
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 PacketWriterFactory packetWriterFactory;
@@ -172,11 +172,11 @@ public class FileReadWriteTest extends TestCase {
Map<Group, Long> subs = new LinkedHashMap<Group, Long>();
subs.put(group, 0L);
subs.put(group1, 0L);
s.writeSubscriptions(subs);
s.writeSubscriptionUpdate(subs, timestamp);
packetWriter.finishPacket();
TransportWriter t = protocolWriterFactory.createTransportWriter(out);
t.writeTransports(transports);
t.writeTransportUpdate(transports, timestamp);
packetWriter.finishPacket();
out.flush();
@@ -257,16 +257,14 @@ public class FileReadWriteTest extends TestCase {
assertEquals(2, subs.size());
assertEquals(Long.valueOf(0L), subs.get(group));
assertEquals(Long.valueOf(0L), subs.get(group1));
assertTrue(s.getTimestamp() > start);
assertTrue(s.getTimestamp() <= System.currentTimeMillis());
assertTrue(s.getTimestamp() == timestamp);
// Read the transport update
assertTrue(protocolReader.hasTransportUpdate());
TransportUpdate t = protocolReader.readTransportUpdate();
packetReader.finishPacket();
assertEquals(transports, t.getTransports());
assertTrue(t.getTimestamp() > start);
assertTrue(t.getTimestamp() <= System.currentTimeMillis());
assertTrue(t.getTimestamp() == timestamp);
in.close();
}

View File

@@ -779,8 +779,9 @@ public abstract class DatabaseComponentTest extends TestCase {
oneOf(database).getVisibleSubscriptions(txn, contactId);
will(returnValue(Collections.singletonMap(group, 0L)));
// Add the subscriptions to the writer
oneOf(subscriptionWriter).writeSubscriptions(
Collections.singletonMap(group, 0L));
oneOf(subscriptionWriter).writeSubscriptionUpdate(
with(Collections.singletonMap(group, 0L)),
with(any(long.class)));
}});
DatabaseComponent db = createDatabaseComponent(database, cleaner);
@@ -811,7 +812,8 @@ public abstract class DatabaseComponentTest extends TestCase {
oneOf(database).getTransports(txn);
will(returnValue(transports));
// Add the properties to the writer
oneOf(transportWriter).writeTransports(transports);
oneOf(transportWriter).writeTransportUpdate(with(transports),
with(any(long.class)));
}});
DatabaseComponent db = createDatabaseComponent(database, cleaner);

View File

@@ -51,6 +51,7 @@ public class ProtocolReadWriteTest extends TestCase {
private final BitSet bitSet;
private final Map<Group, Long> subscriptions;
private final Map<String, Map<String, String>> transports;
private final long timestamp = System.currentTimeMillis();
public ProtocolReadWriteTest() throws Exception {
super();
@@ -75,8 +76,6 @@ public class ProtocolReadWriteTest extends TestCase {
@Test
public void testWriteAndRead() throws Exception {
long start = System.currentTimeMillis();
// Write
ByteArrayOutputStream out = new ByteArrayOutputStream();
@@ -96,10 +95,10 @@ public class ProtocolReadWriteTest extends TestCase {
r.writeRequest(offerId, bitSet, 10);
SubscriptionWriter s = writerFactory.createSubscriptionWriter(out);
s.writeSubscriptions(subscriptions);
s.writeSubscriptionUpdate(subscriptions, timestamp);
TransportWriter t = writerFactory.createTransportWriter(out);
t.writeTransports(transports);
t.writeTransportUpdate(transports, timestamp);
// Read
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
@@ -120,10 +119,10 @@ public class ProtocolReadWriteTest extends TestCase {
SubscriptionUpdate subscriptionUpdate = reader.readSubscriptionUpdate();
assertEquals(subscriptions, subscriptionUpdate.getSubscriptions());
assertTrue(subscriptionUpdate.getTimestamp() >= start);
assertTrue(subscriptionUpdate.getTimestamp() == timestamp);
TransportUpdate transportUpdate = reader.readTransportUpdate();
assertEquals(transports, transportUpdate.getTransports());
assertTrue(transportUpdate.getTimestamp() >= start);
assertTrue(transportUpdate.getTimestamp() == timestamp);
}
}