From 0c392e8b7826d8418e2657f6e5c8cb2226162b8f Mon Sep 17 00:00:00 2001 From: akwizgran <akwizgran@users.sourceforge.net> Date: Thu, 11 Feb 2016 16:21:26 +0000 Subject: [PATCH] Replaced PacketWriter methods with a constant. --- .../briarproject/api/sync/PacketWriter.java | 6 -- .../briarproject/api/sync/SyncConstants.java | 3 + .../sync/DuplexOutgoingSession.java | 13 ++- .../briarproject/sync/PacketWriterImpl.java | 19 ----- .../sync/SimplexOutgoingSession.java | 4 +- .../org/briarproject/sync/ConstantsTest.java | 79 ------------------- .../sync/SimplexOutgoingSessionTest.java | 16 ++-- 7 files changed, 15 insertions(+), 125 deletions(-) diff --git a/briar-api/src/org/briarproject/api/sync/PacketWriter.java b/briar-api/src/org/briarproject/api/sync/PacketWriter.java index 7cc4e7436e..a7e89bb64a 100644 --- a/briar-api/src/org/briarproject/api/sync/PacketWriter.java +++ b/briar-api/src/org/briarproject/api/sync/PacketWriter.java @@ -4,12 +4,6 @@ import java.io.IOException; public interface PacketWriter { - int getMaxMessagesForAck(long capacity); - - int getMaxMessagesForRequest(long capacity); - - int getMaxMessagesForOffer(long capacity); - void writeAck(Ack a) throws IOException; void writeMessage(byte[] raw) throws IOException; diff --git a/briar-api/src/org/briarproject/api/sync/SyncConstants.java b/briar-api/src/org/briarproject/api/sync/SyncConstants.java index 447a9aa9e5..f01279a4ba 100644 --- a/briar-api/src/org/briarproject/api/sync/SyncConstants.java +++ b/briar-api/src/org/briarproject/api/sync/SyncConstants.java @@ -24,4 +24,7 @@ public interface SyncConstants { /** The maximum length of a message body in bytes. */ int MAX_MESSAGE_BODY_LENGTH = MAX_MESSAGE_LENGTH - MESSAGE_HEADER_LENGTH; + + /** The maximum number of message IDs in an ack, offer or request packet. */ + int MAX_MESSAGE_IDS = MAX_PACKET_PAYLOAD_LENGTH / UniqueId.LENGTH; } diff --git a/briar-core/src/org/briarproject/sync/DuplexOutgoingSession.java b/briar-core/src/org/briarproject/sync/DuplexOutgoingSession.java index f6ec477959..d0af71201d 100644 --- a/briar-core/src/org/briarproject/sync/DuplexOutgoingSession.java +++ b/briar-core/src/org/briarproject/sync/DuplexOutgoingSession.java @@ -32,6 +32,7 @@ import java.util.logging.Logger; import static java.util.concurrent.TimeUnit.MILLISECONDS; import static java.util.logging.Level.INFO; import static java.util.logging.Level.WARNING; +import static org.briarproject.api.sync.SyncConstants.MAX_MESSAGE_IDS; import static org.briarproject.api.sync.SyncConstants.MAX_PACKET_PAYLOAD_LENGTH; /** @@ -176,9 +177,8 @@ class DuplexOutgoingSession implements SyncSession, EventListener { public void run() { if (interrupted) return; - int maxMessages = packetWriter.getMaxMessagesForAck(Long.MAX_VALUE); try { - Ack a = db.generateAck(contactId, maxMessages); + Ack a = db.generateAck(contactId, MAX_MESSAGE_IDS); if (LOG.isLoggable(INFO)) LOG.info("Generated ack: " + (a != null)); if (a != null) writerTasks.add(new WriteAck(a)); @@ -246,10 +246,9 @@ class DuplexOutgoingSession implements SyncSession, EventListener { public void run() { if (interrupted) return; - int maxMessages = packetWriter.getMaxMessagesForOffer( - Long.MAX_VALUE); try { - Offer o = db.generateOffer(contactId, maxMessages, maxLatency); + Offer o = db.generateOffer(contactId, MAX_MESSAGE_IDS, + maxLatency); if (LOG.isLoggable(INFO)) LOG.info("Generated offer: " + (o != null)); if (o != null) writerTasks.add(new WriteOffer(o)); @@ -282,10 +281,8 @@ class DuplexOutgoingSession implements SyncSession, EventListener { public void run() { if (interrupted) return; - int maxMessages = packetWriter.getMaxMessagesForRequest( - Long.MAX_VALUE); try { - Request r = db.generateRequest(contactId, maxMessages); + Request r = db.generateRequest(contactId, MAX_MESSAGE_IDS); if (LOG.isLoggable(INFO)) LOG.info("Generated request: " + (r != null)); if (r != null) writerTasks.add(new WriteRequest(r)); diff --git a/briar-core/src/org/briarproject/sync/PacketWriterImpl.java b/briar-core/src/org/briarproject/sync/PacketWriterImpl.java index f2e8817710..9bcbe570f3 100644 --- a/briar-core/src/org/briarproject/sync/PacketWriterImpl.java +++ b/briar-core/src/org/briarproject/sync/PacketWriterImpl.java @@ -1,6 +1,5 @@ package org.briarproject.sync; -import org.briarproject.api.UniqueId; import org.briarproject.api.sync.Ack; import org.briarproject.api.sync.MessageId; import org.briarproject.api.sync.Offer; @@ -34,24 +33,6 @@ class PacketWriterImpl implements PacketWriter { payload = new ByteArrayOutputStream(MAX_PACKET_PAYLOAD_LENGTH); } - public int getMaxMessagesForAck(long capacity) { - return getMaxMessagesForPacket(capacity); - } - - public int getMaxMessagesForRequest(long capacity) { - return getMaxMessagesForPacket(capacity); - } - - public int getMaxMessagesForOffer(long capacity) { - return getMaxMessagesForPacket(capacity); - } - - private int getMaxMessagesForPacket(long capacity) { - int payload = (int) Math.min(capacity - PACKET_HEADER_LENGTH, - MAX_PACKET_PAYLOAD_LENGTH); - return payload / UniqueId.LENGTH; - } - private void writePacket(byte packetType) throws IOException { header[1] = packetType; ByteUtils.writeUint16(payload.size(), header, 2); diff --git a/briar-core/src/org/briarproject/sync/SimplexOutgoingSession.java b/briar-core/src/org/briarproject/sync/SimplexOutgoingSession.java index 0290b7d835..620e23a4b7 100644 --- a/briar-core/src/org/briarproject/sync/SimplexOutgoingSession.java +++ b/briar-core/src/org/briarproject/sync/SimplexOutgoingSession.java @@ -24,6 +24,7 @@ import java.util.logging.Logger; import static java.util.logging.Level.INFO; import static java.util.logging.Level.WARNING; +import static org.briarproject.api.sync.SyncConstants.MAX_MESSAGE_IDS; import static org.briarproject.api.sync.SyncConstants.MAX_PACKET_PAYLOAD_LENGTH; /** @@ -117,9 +118,8 @@ class SimplexOutgoingSession implements SyncSession, EventListener { public void run() { if (interrupted) return; - int maxMessages = packetWriter.getMaxMessagesForAck(Long.MAX_VALUE); try { - Ack a = db.generateAck(contactId, maxMessages); + Ack a = db.generateAck(contactId, MAX_MESSAGE_IDS); if (LOG.isLoggable(INFO)) LOG.info("Generated ack: " + (a != null)); if (a == null) decrementOutstandingQueries(); diff --git a/briar-tests/src/org/briarproject/sync/ConstantsTest.java b/briar-tests/src/org/briarproject/sync/ConstantsTest.java index ac3d4334f3..9f31dce47d 100644 --- a/briar-tests/src/org/briarproject/sync/ConstantsTest.java +++ b/briar-tests/src/org/briarproject/sync/ConstantsTest.java @@ -21,13 +21,8 @@ import org.briarproject.api.identity.AuthorFactory; import org.briarproject.api.messaging.MessagingConstants; import org.briarproject.api.messaging.PrivateMessage; import org.briarproject.api.messaging.PrivateMessageFactory; -import org.briarproject.api.sync.Ack; import org.briarproject.api.sync.GroupId; import org.briarproject.api.sync.MessageId; -import org.briarproject.api.sync.Offer; -import org.briarproject.api.sync.PacketWriter; -import org.briarproject.api.sync.PacketWriterFactory; -import org.briarproject.api.sync.Request; import org.briarproject.contact.ContactModule; import org.briarproject.crypto.CryptoModule; import org.briarproject.data.DataModule; @@ -38,9 +33,6 @@ import org.briarproject.identity.IdentityModule; import org.briarproject.messaging.MessagingModule; import org.junit.Test; -import java.io.ByteArrayOutputStream; -import java.util.ArrayList; -import java.util.Collection; import java.util.Random; import static org.briarproject.api.forum.ForumConstants.MAX_FORUM_POST_BODY_LENGTH; @@ -59,7 +51,6 @@ public class ConstantsTest extends BriarTestCase { private final AuthorFactory authorFactory; private final PrivateMessageFactory privateMessageFactory; private final ForumPostFactory forumPostFactory; - private final PacketWriterFactory packetWriterFactory; public ConstantsTest() throws Exception { Injector i = Guice.createInjector(new TestDatabaseModule(), @@ -71,7 +62,6 @@ public class ConstantsTest extends BriarTestCase { authorFactory = i.getInstance(AuthorFactory.class); privateMessageFactory = i.getInstance(PrivateMessageFactory.class); forumPostFactory = i.getInstance(ForumPostFactory.class); - packetWriterFactory = i.getInstance(PacketWriterFactory.class); } @Test @@ -105,16 +95,6 @@ public class ConstantsTest extends BriarTestCase { } } - @Test - public void testMessageIdsFitIntoLargeAck() throws Exception { - testMessageIdsFitIntoAck(MAX_PACKET_PAYLOAD_LENGTH); - } - - @Test - public void testMessageIdsFitIntoSmallAck() throws Exception { - testMessageIdsFitIntoAck(1000); - } - @Test public void testPrivateMessageFitsIntoPacket() throws Exception { // Create a maximum-length private message @@ -159,63 +139,4 @@ public class ConstantsTest extends BriarTestCase { + MAX_FORUM_POST_BODY_LENGTH); assertTrue(length <= MAX_PACKET_PAYLOAD_LENGTH); } - - @Test - public void testMessageIdsFitIntoLargeOffer() throws Exception { - testMessageIdsFitIntoOffer(MAX_PACKET_PAYLOAD_LENGTH); - } - - @Test - public void testMessageIdsFitIntoSmallOffer() throws Exception { - testMessageIdsFitIntoOffer(1000); - } - - @Test - public void testMessageIdsFitIntoLargeRequest() throws Exception { - testMessageIdsFitIntoRequest(MAX_PACKET_PAYLOAD_LENGTH); - } - - @Test - public void testMessageIdsFitIntoSmallRequest() throws Exception { - testMessageIdsFitIntoRequest(1000); - } - - private void testMessageIdsFitIntoAck(int length) throws Exception { - // Create an ack with as many message IDs as possible - ByteArrayOutputStream out = new ByteArrayOutputStream(length); - PacketWriter writer = packetWriterFactory.createPacketWriter(out); - int maxMessages = writer.getMaxMessagesForAck(length); - Collection<MessageId> ids = new ArrayList<MessageId>(); - for (int i = 0; i < maxMessages; i++) - ids.add(new MessageId(TestUtils.getRandomId())); - writer.writeAck(new Ack(ids)); - // Check the size of the serialised ack - assertTrue(out.size() <= length); - } - - private void testMessageIdsFitIntoRequest(int length) throws Exception { - // Create a request with as many message IDs as possible - ByteArrayOutputStream out = new ByteArrayOutputStream(length); - PacketWriter writer = packetWriterFactory.createPacketWriter(out); - int maxMessages = writer.getMaxMessagesForRequest(length); - Collection<MessageId> ids = new ArrayList<MessageId>(); - for (int i = 0; i < maxMessages; i++) - ids.add(new MessageId(TestUtils.getRandomId())); - writer.writeRequest(new Request(ids)); - // Check the size of the serialised request - assertTrue(out.size() <= length); - } - - private void testMessageIdsFitIntoOffer(int length) throws Exception { - // Create an offer with as many message IDs as possible - ByteArrayOutputStream out = new ByteArrayOutputStream(length); - PacketWriter writer = packetWriterFactory.createPacketWriter(out); - int maxMessages = writer.getMaxMessagesForOffer(length); - Collection<MessageId> ids = new ArrayList<MessageId>(); - for (int i = 0; i < maxMessages; i++) - ids.add(new MessageId(TestUtils.getRandomId())); - writer.writeOffer(new Offer(ids)); - // Check the size of the serialised offer - assertTrue(out.size() <= length); - } } diff --git a/briar-tests/src/org/briarproject/sync/SimplexOutgoingSessionTest.java b/briar-tests/src/org/briarproject/sync/SimplexOutgoingSessionTest.java index ef2a58c476..7b49108ef5 100644 --- a/briar-tests/src/org/briarproject/sync/SimplexOutgoingSessionTest.java +++ b/briar-tests/src/org/briarproject/sync/SimplexOutgoingSessionTest.java @@ -18,9 +18,9 @@ import java.util.Arrays; import java.util.Collections; import java.util.concurrent.Executor; -public class SimplexOutgoingSessionTest extends BriarTestCase { +import static org.briarproject.api.sync.SyncConstants.MAX_MESSAGE_IDS; - private static final int MAX_MESSAGES_PER_ACK = 10; +public class SimplexOutgoingSessionTest extends BriarTestCase { private final Mockery context; private final DatabaseComponent db; @@ -53,9 +53,7 @@ public class SimplexOutgoingSessionTest extends BriarTestCase { // Add listener oneOf(eventBus).addListener(session); // No acks to send - oneOf(packetWriter).getMaxMessagesForAck(with(any(long.class))); - will(returnValue(MAX_MESSAGES_PER_ACK)); - oneOf(db).generateAck(contactId, MAX_MESSAGES_PER_ACK); + oneOf(db).generateAck(contactId, MAX_MESSAGE_IDS); will(returnValue(null)); // No messages to send oneOf(db).generateBatch(with(contactId), with(any(int.class)), @@ -81,15 +79,11 @@ public class SimplexOutgoingSessionTest extends BriarTestCase { // Add listener oneOf(eventBus).addListener(session); // One ack to send - oneOf(packetWriter).getMaxMessagesForAck(with(any(long.class))); - will(returnValue(MAX_MESSAGES_PER_ACK)); - oneOf(db).generateAck(contactId, MAX_MESSAGES_PER_ACK); + oneOf(db).generateAck(contactId, MAX_MESSAGE_IDS); will(returnValue(ack)); oneOf(packetWriter).writeAck(ack); // No more acks - oneOf(packetWriter).getMaxMessagesForAck(with(any(long.class))); - will(returnValue(MAX_MESSAGES_PER_ACK)); - oneOf(db).generateAck(contactId, MAX_MESSAGES_PER_ACK); + oneOf(db).generateAck(contactId, MAX_MESSAGE_IDS); will(returnValue(null)); // One message to send oneOf(db).generateBatch(with(contactId), with(any(int.class)), -- GitLab