Replaced PacketWriter methods with a constant.

This commit is contained in:
akwizgran
2016-02-11 16:21:26 +00:00
parent 747d9678fe
commit 0c392e8b78
7 changed files with 15 additions and 125 deletions

View File

@@ -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;

View File

@@ -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;
}

View File

@@ -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));

View File

@@ -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);

View File

@@ -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();

View File

@@ -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);
}
}

View File

@@ -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)),