Skip to content
Snippets Groups Projects
Commit 6af3c54c authored by akwizgran's avatar akwizgran
Browse files

Removed shouldFlush() from plugins, added missing PacketWriter method.

parent c146da2e
No related branches found
No related tags found
No related merge requests found
Showing
with 54 additions and 68 deletions
......@@ -34,10 +34,6 @@ class DroidtoothTransportConnection implements DuplexTransportConnection {
return socket.getOutputStream();
}
public boolean shouldFlush() {
return true;
}
public void dispose(boolean exception, boolean recognised)
throws IOException {
socket.close();
......
......@@ -34,10 +34,6 @@ class TorTransportConnection implements DuplexTransportConnection {
return socket.getOutputStream();
}
public boolean shouldFlush() {
return true;
}
public void dispose(boolean exception, boolean recognised)
throws IOException {
socket.close();
......
......@@ -4,6 +4,8 @@ import java.io.IOException;
public interface PacketWriter {
int getMaxMessagesForAck(long capacity);
int getMaxMessagesForRequest(long capacity);
int getMaxMessagesForOffer(long capacity);
......
......@@ -23,11 +23,6 @@ public interface DuplexTransportConnection {
/** Returns an output stream for writing to the connection. */
OutputStream getOutputStream() throws IOException;
/**
* Returns true if the output stream should be flushed after each packet.
*/
boolean shouldFlush();
/**
* Closes the connection and disposes of any associated resources. The
* first argument indicates whether the connection is being closed because
......
......@@ -21,11 +21,6 @@ public interface SimplexTransportWriter {
/** Returns an output stream for writing to the transport. */
OutputStream getOutputStream() throws IOException;
/**
* Returns true if the output stream should be flushed after each packet.
*/
boolean shouldFlush();
/**
* Closes the writer and disposes of any associated resources. The
* argument indicates whether the writer is being closed because of an
......
......@@ -21,8 +21,7 @@ class PacketWriterFactoryImpl implements PacketWriterFactory {
this.writerFactory = writerFactory;
}
public PacketWriter createPacketWriter(OutputStream out,
boolean flush) {
public PacketWriter createPacketWriter(OutputStream out, boolean flush) {
return new PacketWriterImpl(serial, writerFactory, out, flush);
}
}
......@@ -47,19 +47,21 @@ class PacketWriterImpl implements PacketWriter {
w = writerFactory.createWriter(out);
}
public int getMaxMessagesForAck(long capacity) {
return getMaxMessagesForPacket(capacity, ACK);
}
public int getMaxMessagesForRequest(long capacity) {
int packet = (int) Math.min(capacity, MAX_PACKET_LENGTH);
int overhead = serial.getSerialisedStructStartLength(ACK)
+ serial.getSerialisedListStartLength()
+ serial.getSerialisedListEndLength()
+ serial.getSerialisedStructEndLength();
int idLength = serial.getSerialisedUniqueIdLength();
return (packet - overhead) / idLength;
return getMaxMessagesForPacket(capacity, REQUEST);
}
public int getMaxMessagesForOffer(long capacity) {
return getMaxMessagesForPacket(capacity, OFFER);
}
private int getMaxMessagesForPacket(long capacity, int structId) {
int packet = (int) Math.min(capacity, MAX_PACKET_LENGTH);
int overhead = serial.getSerialisedStructStartLength(OFFER)
int overhead = serial.getSerialisedStructStartLength(structId)
+ serial.getSerialisedListStartLength()
+ serial.getSerialisedListEndLength()
+ serial.getSerialisedStructEndLength();
......
......@@ -226,8 +226,7 @@ abstract class DuplexConnection implements EventListener {
db.addListener(this);
try {
OutputStream out = createConnectionWriter().getOutputStream();
writer = packetWriterFactory.createPacketWriter(out,
transport.shouldFlush());
writer = packetWriterFactory.createPacketWriter(out, true);
if(LOG.isLoggable(INFO)) LOG.info("Starting to write");
// Send the initial packets
dbExecutor.execute(new GenerateTransportAcks());
......@@ -500,7 +499,7 @@ abstract class DuplexConnection implements EventListener {
public void run() {
assert writer != null;
int maxMessages = writer.getMaxMessagesForRequest(Long.MAX_VALUE);
int maxMessages = writer.getMaxMessagesForAck(Long.MAX_VALUE);
try {
Ack a = db.generateAck(contactId, maxMessages);
if(LOG.isLoggable(INFO))
......
......@@ -72,7 +72,7 @@ class OutgoingSimplexConnection {
if(conn.getRemainingCapacity() < MAX_PACKET_LENGTH)
throw new EOFException();
PacketWriter writer = packetWriterFactory.createPacketWriter(out,
transport.shouldFlush());
false);
// Send the initial packets: updates and acks
boolean hasSpace = writeTransportAcks(conn, writer);
if(hasSpace) hasSpace = writeTransportUpdates(conn, writer);
......@@ -82,12 +82,12 @@ class OutgoingSimplexConnection {
if(hasSpace) hasSpace = writeRetentionUpdate(conn, writer);
// Write acks until you can't write acks no more
capacity = conn.getRemainingCapacity();
int maxMessages = writer.getMaxMessagesForRequest(capacity);
int maxMessages = writer.getMaxMessagesForAck(capacity);
Ack a = db.generateAck(contactId, maxMessages);
while(a != null) {
writer.writeAck(a);
capacity = conn.getRemainingCapacity();
maxMessages = writer.getMaxMessagesForRequest(capacity);
maxMessages = writer.getMaxMessagesForAck(capacity);
a = db.generateAck(contactId, maxMessages);
}
// Write messages until you can't write messages no more
......
......@@ -43,10 +43,6 @@ class FileTransportWriter implements SimplexTransportWriter {
return out;
}
public boolean shouldFlush() {
return false;
}
public void dispose(boolean exception) {
try {
out.close();
......
......@@ -34,10 +34,6 @@ class TcpTransportConnection implements DuplexTransportConnection {
return socket.getOutputStream();
}
public boolean shouldFlush() {
return true;
}
public void dispose(boolean exception, boolean recognised)
throws IOException {
socket.close();
......
......@@ -35,10 +35,6 @@ class BluetoothTransportConnection implements DuplexTransportConnection {
return stream.openOutputStream();
}
public boolean shouldFlush() {
return true;
}
public void dispose(boolean exception, boolean recognised)
throws IOException {
stream.close();
......
......@@ -250,10 +250,6 @@ class ModemPlugin implements DuplexPlugin, Modem.Callback {
return modem.getOutputStream();
}
public boolean shouldFlush() {
return true;
}
public void dispose(boolean exception, boolean recognised) {
if(LOG.isLoggable(INFO)) LOG.info("Call disconnected");
try {
......
......@@ -39,6 +39,7 @@ import org.briarproject.api.messaging.MessageId;
import org.briarproject.api.messaging.Offer;
import org.briarproject.api.messaging.PacketWriter;
import org.briarproject.api.messaging.PacketWriterFactory;
import org.briarproject.api.messaging.Request;
import org.briarproject.api.messaging.SubscriptionUpdate;
import org.briarproject.api.messaging.TransportUpdate;
import org.briarproject.crypto.CryptoModule;
......@@ -153,6 +154,16 @@ public class ConstantsTest extends BriarTestCase {
testMessageIdsFitIntoOffer(1000);
}
@Test
public void testMessageIdsFitIntoLargeRequest() throws Exception {
testMessageIdsFitIntoRequest(MAX_PACKET_LENGTH);
}
@Test
public void testMessageIdsFitIntoSmallRequest() throws Exception {
testMessageIdsFitIntoRequest(1000);
}
@Test
public void testPropertiesFitIntoTransportUpdate() throws Exception {
// Create the maximum number of properties with the maximum length
......@@ -195,24 +206,37 @@ public class ConstantsTest extends BriarTestCase {
// Create an ack with as many message IDs as possible
ByteArrayOutputStream out = new ByteArrayOutputStream(length);
PacketWriter writer = packetWriterFactory.createPacketWriter(out, true);
int maxMessages = writer.getMaxMessagesForRequest(length);
Collection<MessageId> acked = new ArrayList<MessageId>();
int maxMessages = writer.getMaxMessagesForAck(length);
Collection<MessageId> ids = new ArrayList<MessageId>();
for(int i = 0; i < maxMessages; i++)
acked.add(new MessageId(TestUtils.getRandomId()));
writer.writeAck(new Ack(acked));
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, true);
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, true);
int maxMessages = writer.getMaxMessagesForOffer(length);
Collection<MessageId> offered = new ArrayList<MessageId>();
Collection<MessageId> ids = new ArrayList<MessageId>();
for(int i = 0; i < maxMessages; i++)
offered.add(new MessageId(TestUtils.getRandomId()));
writer.writeOffer(new Offer(offered));
ids.add(new MessageId(TestUtils.getRandomId()));
writer.writeOffer(new Offer(ids));
// Check the size of the serialised offer
assertTrue(out.size() <= length);
}
......
......@@ -85,7 +85,7 @@ public class OutgoingSimplexConnectionTest extends BriarTestCase {
public void testConnectionTooShort() throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream();
TestSimplexTransportWriter transport = new TestSimplexTransportWriter(
out, MAX_PACKET_LENGTH, Long.MAX_VALUE, true);
out, MAX_PACKET_LENGTH, Long.MAX_VALUE);
ConnectionContext ctx = new ConnectionContext(contactId, transportId,
secret, 0, true);
OutgoingSimplexConnection connection = new OutgoingSimplexConnection(db,
......@@ -103,7 +103,7 @@ public class OutgoingSimplexConnectionTest extends BriarTestCase {
public void testNothingToSend() throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream();
TestSimplexTransportWriter transport = new TestSimplexTransportWriter(
out, MIN_CONNECTION_LENGTH, Long.MAX_VALUE, true);
out, MIN_CONNECTION_LENGTH, Long.MAX_VALUE);
ConnectionContext ctx = new ConnectionContext(contactId, transportId,
secret, 0, true);
OutgoingSimplexConnection connection = new OutgoingSimplexConnection(db,
......@@ -152,7 +152,7 @@ public class OutgoingSimplexConnectionTest extends BriarTestCase {
public void testSomethingToSend() throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream();
TestSimplexTransportWriter transport = new TestSimplexTransportWriter(
out, MIN_CONNECTION_LENGTH, Long.MAX_VALUE, true);
out, MIN_CONNECTION_LENGTH, Long.MAX_VALUE);
ConnectionContext ctx = new ConnectionContext(contactId, transportId,
secret, 0, true);
OutgoingSimplexConnection connection = new OutgoingSimplexConnection(db,
......
......@@ -146,7 +146,7 @@ public class SimplexMessagingIntegrationTest extends BriarTestCase {
PacketWriterFactory packetWriterFactory =
alice.getInstance(PacketWriterFactory.class);
TestSimplexTransportWriter transport = new TestSimplexTransportWriter(
out, Long.MAX_VALUE, Long.MAX_VALUE, false);
out, Long.MAX_VALUE, Long.MAX_VALUE);
ConnectionContext ctx = km.getConnectionContext(contactId, transportId);
assertNotNull(ctx);
OutgoingSimplexConnection simplex = new OutgoingSimplexConnection(db,
......
......@@ -11,16 +11,14 @@ class TestSimplexTransportWriter implements SimplexTransportWriter {
private final ByteArrayOutputStream out;
private final long capacity, maxLatency;
private final boolean flush;
private boolean disposed = false, exception = false;
TestSimplexTransportWriter(ByteArrayOutputStream out, long capacity,
long maxLatency, boolean flush) {
long maxLatency) {
this.out = out;
this.capacity = capacity;
this.maxLatency = maxLatency;
this.flush = flush;
}
public long getCapacity() {
......@@ -39,10 +37,6 @@ class TestSimplexTransportWriter implements SimplexTransportWriter {
return out;
}
public boolean shouldFlush() {
return flush;
}
public void dispose(boolean exception) {
assert !disposed;
disposed = true;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment