Rename sync record reader/writer.

This commit is contained in:
akwizgran
2018-04-19 12:01:08 +01:00
parent 6fa6ceb5ee
commit 4100daaa47
17 changed files with 110 additions and 91 deletions

View File

@@ -5,7 +5,7 @@ import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
import java.io.IOException; import java.io.IOException;
@NotNullByDefault @NotNullByDefault
public interface RecordReader { public interface SyncRecordReader {
boolean eof() throws IOException; boolean eof() throws IOException;

View File

@@ -5,7 +5,7 @@ import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
import java.io.InputStream; import java.io.InputStream;
@NotNullByDefault @NotNullByDefault
public interface RecordReaderFactory { public interface SyncRecordReaderFactory {
RecordReader createRecordReader(InputStream in); SyncRecordReader createRecordReader(InputStream in);
} }

View File

@@ -5,7 +5,7 @@ import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
import java.io.IOException; import java.io.IOException;
@NotNullByDefault @NotNullByDefault
public interface RecordWriter { public interface SyncRecordWriter {
void writeAck(Ack a) throws IOException; void writeAck(Ack a) throws IOException;

View File

@@ -5,7 +5,7 @@ import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
import java.io.OutputStream; import java.io.OutputStream;
@NotNullByDefault @NotNullByDefault
public interface RecordWriterFactory { public interface SyncRecordWriterFactory {
RecordWriter createRecordWriter(OutputStream out); SyncRecordWriter createRecordWriter(OutputStream out);
} }

View File

@@ -14,8 +14,8 @@ import org.briarproject.bramble.api.lifecycle.event.LifecycleEvent;
import org.briarproject.bramble.api.nullsafety.NotNullByDefault; import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
import org.briarproject.bramble.api.sync.Ack; import org.briarproject.bramble.api.sync.Ack;
import org.briarproject.bramble.api.sync.Offer; import org.briarproject.bramble.api.sync.Offer;
import org.briarproject.bramble.api.sync.RecordWriter;
import org.briarproject.bramble.api.sync.Request; import org.briarproject.bramble.api.sync.Request;
import org.briarproject.bramble.api.sync.SyncRecordWriter;
import org.briarproject.bramble.api.sync.SyncSession; import org.briarproject.bramble.api.sync.SyncSession;
import org.briarproject.bramble.api.sync.event.GroupVisibilityUpdatedEvent; import org.briarproject.bramble.api.sync.event.GroupVisibilityUpdatedEvent;
import org.briarproject.bramble.api.sync.event.MessageRequestedEvent; import org.briarproject.bramble.api.sync.event.MessageRequestedEvent;
@@ -67,7 +67,7 @@ class DuplexOutgoingSession implements SyncSession, EventListener {
private final Clock clock; private final Clock clock;
private final ContactId contactId; private final ContactId contactId;
private final int maxLatency, maxIdleTime; private final int maxLatency, maxIdleTime;
private final RecordWriter recordWriter; private final SyncRecordWriter recordWriter;
private final BlockingQueue<ThrowingRunnable<IOException>> writerTasks; private final BlockingQueue<ThrowingRunnable<IOException>> writerTasks;
private final AtomicBoolean generateAckQueued = new AtomicBoolean(false); private final AtomicBoolean generateAckQueued = new AtomicBoolean(false);
@@ -81,7 +81,7 @@ class DuplexOutgoingSession implements SyncSession, EventListener {
DuplexOutgoingSession(DatabaseComponent db, Executor dbExecutor, DuplexOutgoingSession(DatabaseComponent db, Executor dbExecutor,
EventBus eventBus, Clock clock, ContactId contactId, int maxLatency, EventBus eventBus, Clock clock, ContactId contactId, int maxLatency,
int maxIdleTime, RecordWriter recordWriter) { int maxIdleTime, SyncRecordWriter recordWriter) {
this.db = db; this.db = db;
this.dbExecutor = dbExecutor; this.dbExecutor = dbExecutor;
this.eventBus = eventBus; this.eventBus = eventBus;

View File

@@ -16,8 +16,8 @@ import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
import org.briarproject.bramble.api.sync.Ack; import org.briarproject.bramble.api.sync.Ack;
import org.briarproject.bramble.api.sync.Message; import org.briarproject.bramble.api.sync.Message;
import org.briarproject.bramble.api.sync.Offer; import org.briarproject.bramble.api.sync.Offer;
import org.briarproject.bramble.api.sync.RecordReader;
import org.briarproject.bramble.api.sync.Request; import org.briarproject.bramble.api.sync.Request;
import org.briarproject.bramble.api.sync.SyncRecordReader;
import org.briarproject.bramble.api.sync.SyncSession; import org.briarproject.bramble.api.sync.SyncSession;
import java.io.IOException; import java.io.IOException;
@@ -43,13 +43,13 @@ class IncomingSession implements SyncSession, EventListener {
private final Executor dbExecutor; private final Executor dbExecutor;
private final EventBus eventBus; private final EventBus eventBus;
private final ContactId contactId; private final ContactId contactId;
private final RecordReader recordReader; private final SyncRecordReader recordReader;
private volatile boolean interrupted = false; private volatile boolean interrupted = false;
IncomingSession(DatabaseComponent db, Executor dbExecutor, IncomingSession(DatabaseComponent db, Executor dbExecutor,
EventBus eventBus, ContactId contactId, EventBus eventBus, ContactId contactId,
RecordReader recordReader) { SyncRecordReader recordReader) {
this.db = db; this.db = db;
this.dbExecutor = dbExecutor; this.dbExecutor = dbExecutor;
this.eventBus = eventBus; this.eventBus = eventBus;

View File

@@ -1,16 +0,0 @@
package org.briarproject.bramble.sync;
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
import org.briarproject.bramble.api.sync.RecordWriter;
import org.briarproject.bramble.api.sync.RecordWriterFactory;
import java.io.OutputStream;
@NotNullByDefault
class RecordWriterFactoryImpl implements RecordWriterFactory {
@Override
public RecordWriter createRecordWriter(OutputStream out) {
return new RecordWriterImpl(out);
}
}

View File

@@ -13,7 +13,7 @@ import org.briarproject.bramble.api.lifecycle.IoExecutor;
import org.briarproject.bramble.api.lifecycle.event.LifecycleEvent; import org.briarproject.bramble.api.lifecycle.event.LifecycleEvent;
import org.briarproject.bramble.api.nullsafety.NotNullByDefault; import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
import org.briarproject.bramble.api.sync.Ack; import org.briarproject.bramble.api.sync.Ack;
import org.briarproject.bramble.api.sync.RecordWriter; import org.briarproject.bramble.api.sync.SyncRecordWriter;
import org.briarproject.bramble.api.sync.SyncSession; import org.briarproject.bramble.api.sync.SyncSession;
import java.io.IOException; import java.io.IOException;
@@ -51,7 +51,7 @@ class SimplexOutgoingSession implements SyncSession, EventListener {
private final EventBus eventBus; private final EventBus eventBus;
private final ContactId contactId; private final ContactId contactId;
private final int maxLatency; private final int maxLatency;
private final RecordWriter recordWriter; private final SyncRecordWriter recordWriter;
private final AtomicInteger outstandingQueries; private final AtomicInteger outstandingQueries;
private final BlockingQueue<ThrowingRunnable<IOException>> writerTasks; private final BlockingQueue<ThrowingRunnable<IOException>> writerTasks;
@@ -59,7 +59,7 @@ class SimplexOutgoingSession implements SyncSession, EventListener {
SimplexOutgoingSession(DatabaseComponent db, Executor dbExecutor, SimplexOutgoingSession(DatabaseComponent db, Executor dbExecutor,
EventBus eventBus, ContactId contactId, EventBus eventBus, ContactId contactId,
int maxLatency, RecordWriter recordWriter) { int maxLatency, SyncRecordWriter recordWriter) {
this.db = db; this.db = db;
this.dbExecutor = dbExecutor; this.dbExecutor = dbExecutor;
this.eventBus = eventBus; this.eventBus = eventBus;

View File

@@ -9,8 +9,8 @@ import org.briarproject.bramble.api.event.EventBus;
import org.briarproject.bramble.api.lifecycle.LifecycleManager; import org.briarproject.bramble.api.lifecycle.LifecycleManager;
import org.briarproject.bramble.api.sync.GroupFactory; import org.briarproject.bramble.api.sync.GroupFactory;
import org.briarproject.bramble.api.sync.MessageFactory; import org.briarproject.bramble.api.sync.MessageFactory;
import org.briarproject.bramble.api.sync.RecordReaderFactory; import org.briarproject.bramble.api.sync.SyncRecordReaderFactory;
import org.briarproject.bramble.api.sync.RecordWriterFactory; import org.briarproject.bramble.api.sync.SyncRecordWriterFactory;
import org.briarproject.bramble.api.sync.SyncSessionFactory; import org.briarproject.bramble.api.sync.SyncSessionFactory;
import org.briarproject.bramble.api.sync.ValidationManager; import org.briarproject.bramble.api.sync.ValidationManager;
import org.briarproject.bramble.api.system.Clock; import org.briarproject.bramble.api.system.Clock;
@@ -52,22 +52,22 @@ public class SyncModule {
} }
@Provides @Provides
RecordReaderFactory provideRecordReaderFactory( SyncRecordReaderFactory provideRecordReaderFactory(
RecordReaderFactoryImpl recordReaderFactory) { SyncRecordReaderFactoryImpl recordReaderFactory) {
return recordReaderFactory; return recordReaderFactory;
} }
@Provides @Provides
RecordWriterFactory provideRecordWriterFactory() { SyncRecordWriterFactory provideRecordWriterFactory() {
return new RecordWriterFactoryImpl(); return new SyncRecordWriterFactoryImpl();
} }
@Provides @Provides
@Singleton @Singleton
SyncSessionFactory provideSyncSessionFactory(DatabaseComponent db, SyncSessionFactory provideSyncSessionFactory(DatabaseComponent db,
@DatabaseExecutor Executor dbExecutor, EventBus eventBus, @DatabaseExecutor Executor dbExecutor, EventBus eventBus,
Clock clock, RecordReaderFactory recordReaderFactory, Clock clock, SyncRecordReaderFactory recordReaderFactory,
RecordWriterFactory recordWriterFactory) { SyncRecordWriterFactory recordWriterFactory) {
return new SyncSessionFactoryImpl(db, dbExecutor, eventBus, clock, return new SyncSessionFactoryImpl(db, dbExecutor, eventBus, clock,
recordReaderFactory, recordWriterFactory); recordReaderFactory, recordWriterFactory);
} }

View File

@@ -2,8 +2,8 @@ package org.briarproject.bramble.sync;
import org.briarproject.bramble.api.nullsafety.NotNullByDefault; import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
import org.briarproject.bramble.api.sync.MessageFactory; import org.briarproject.bramble.api.sync.MessageFactory;
import org.briarproject.bramble.api.sync.RecordReader; import org.briarproject.bramble.api.sync.SyncRecordReader;
import org.briarproject.bramble.api.sync.RecordReaderFactory; import org.briarproject.bramble.api.sync.SyncRecordReaderFactory;
import java.io.InputStream; import java.io.InputStream;
@@ -12,17 +12,17 @@ import javax.inject.Inject;
@Immutable @Immutable
@NotNullByDefault @NotNullByDefault
class RecordReaderFactoryImpl implements RecordReaderFactory { class SyncRecordReaderFactoryImpl implements SyncRecordReaderFactory {
private final MessageFactory messageFactory; private final MessageFactory messageFactory;
@Inject @Inject
RecordReaderFactoryImpl(MessageFactory messageFactory) { SyncRecordReaderFactoryImpl(MessageFactory messageFactory) {
this.messageFactory = messageFactory; this.messageFactory = messageFactory;
} }
@Override @Override
public RecordReader createRecordReader(InputStream in) { public SyncRecordReader createRecordReader(InputStream in) {
return new RecordReaderImpl(messageFactory, in); return new SyncRecordReaderImpl(messageFactory, in);
} }
} }

View File

@@ -9,8 +9,8 @@ import org.briarproject.bramble.api.sync.Message;
import org.briarproject.bramble.api.sync.MessageFactory; import org.briarproject.bramble.api.sync.MessageFactory;
import org.briarproject.bramble.api.sync.MessageId; import org.briarproject.bramble.api.sync.MessageId;
import org.briarproject.bramble.api.sync.Offer; import org.briarproject.bramble.api.sync.Offer;
import org.briarproject.bramble.api.sync.RecordReader;
import org.briarproject.bramble.api.sync.Request; import org.briarproject.bramble.api.sync.Request;
import org.briarproject.bramble.api.sync.SyncRecordReader;
import org.briarproject.bramble.util.ByteUtils; import org.briarproject.bramble.util.ByteUtils;
import java.io.IOException; import java.io.IOException;
@@ -31,7 +31,7 @@ import static org.briarproject.bramble.api.sync.SyncConstants.RECORD_HEADER_LENG
@NotThreadSafe @NotThreadSafe
@NotNullByDefault @NotNullByDefault
class RecordReaderImpl implements RecordReader { class SyncRecordReaderImpl implements SyncRecordReader {
private enum State {BUFFER_EMPTY, BUFFER_FULL, EOF} private enum State {BUFFER_EMPTY, BUFFER_FULL, EOF}
@@ -42,7 +42,7 @@ class RecordReaderImpl implements RecordReader {
private State state = State.BUFFER_EMPTY; private State state = State.BUFFER_EMPTY;
private int payloadLength = 0; private int payloadLength = 0;
RecordReaderImpl(MessageFactory messageFactory, InputStream in) { SyncRecordReaderImpl(MessageFactory messageFactory, InputStream in) {
this.messageFactory = messageFactory; this.messageFactory = messageFactory;
this.in = in; this.in = in;
header = new byte[RECORD_HEADER_LENGTH]; header = new byte[RECORD_HEADER_LENGTH];

View File

@@ -0,0 +1,16 @@
package org.briarproject.bramble.sync;
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
import org.briarproject.bramble.api.sync.SyncRecordWriter;
import org.briarproject.bramble.api.sync.SyncRecordWriterFactory;
import java.io.OutputStream;
@NotNullByDefault
class SyncRecordWriterFactoryImpl implements SyncRecordWriterFactory {
@Override
public SyncRecordWriter createRecordWriter(OutputStream out) {
return new SyncRecordWriterImpl(out);
}
}

View File

@@ -5,8 +5,8 @@ import org.briarproject.bramble.api.sync.Ack;
import org.briarproject.bramble.api.sync.MessageId; import org.briarproject.bramble.api.sync.MessageId;
import org.briarproject.bramble.api.sync.Offer; import org.briarproject.bramble.api.sync.Offer;
import org.briarproject.bramble.api.sync.RecordTypes; import org.briarproject.bramble.api.sync.RecordTypes;
import org.briarproject.bramble.api.sync.RecordWriter;
import org.briarproject.bramble.api.sync.Request; import org.briarproject.bramble.api.sync.Request;
import org.briarproject.bramble.api.sync.SyncRecordWriter;
import org.briarproject.bramble.util.ByteUtils; import org.briarproject.bramble.util.ByteUtils;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
@@ -19,18 +19,18 @@ import static org.briarproject.bramble.api.sync.RecordTypes.ACK;
import static org.briarproject.bramble.api.sync.RecordTypes.OFFER; import static org.briarproject.bramble.api.sync.RecordTypes.OFFER;
import static org.briarproject.bramble.api.sync.RecordTypes.REQUEST; import static org.briarproject.bramble.api.sync.RecordTypes.REQUEST;
import static org.briarproject.bramble.api.sync.SyncConstants.MAX_RECORD_PAYLOAD_LENGTH; import static org.briarproject.bramble.api.sync.SyncConstants.MAX_RECORD_PAYLOAD_LENGTH;
import static org.briarproject.bramble.api.sync.SyncConstants.RECORD_HEADER_LENGTH;
import static org.briarproject.bramble.api.sync.SyncConstants.PROTOCOL_VERSION; import static org.briarproject.bramble.api.sync.SyncConstants.PROTOCOL_VERSION;
import static org.briarproject.bramble.api.sync.SyncConstants.RECORD_HEADER_LENGTH;
@NotThreadSafe @NotThreadSafe
@NotNullByDefault @NotNullByDefault
class RecordWriterImpl implements RecordWriter { class SyncRecordWriterImpl implements SyncRecordWriter {
private final OutputStream out; private final OutputStream out;
private final byte[] header; private final byte[] header;
private final ByteArrayOutputStream payload; private final ByteArrayOutputStream payload;
RecordWriterImpl(OutputStream out) { SyncRecordWriterImpl(OutputStream out) {
this.out = out; this.out = out;
header = new byte[RECORD_HEADER_LENGTH]; header = new byte[RECORD_HEADER_LENGTH];
header[0] = PROTOCOL_VERSION; header[0] = PROTOCOL_VERSION;

View File

@@ -5,10 +5,10 @@ import org.briarproject.bramble.api.db.DatabaseComponent;
import org.briarproject.bramble.api.db.DatabaseExecutor; import org.briarproject.bramble.api.db.DatabaseExecutor;
import org.briarproject.bramble.api.event.EventBus; import org.briarproject.bramble.api.event.EventBus;
import org.briarproject.bramble.api.nullsafety.NotNullByDefault; import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
import org.briarproject.bramble.api.sync.RecordReader; import org.briarproject.bramble.api.sync.SyncRecordReader;
import org.briarproject.bramble.api.sync.RecordReaderFactory; import org.briarproject.bramble.api.sync.SyncRecordReaderFactory;
import org.briarproject.bramble.api.sync.RecordWriter; import org.briarproject.bramble.api.sync.SyncRecordWriter;
import org.briarproject.bramble.api.sync.RecordWriterFactory; import org.briarproject.bramble.api.sync.SyncRecordWriterFactory;
import org.briarproject.bramble.api.sync.SyncSession; import org.briarproject.bramble.api.sync.SyncSession;
import org.briarproject.bramble.api.sync.SyncSessionFactory; import org.briarproject.bramble.api.sync.SyncSessionFactory;
import org.briarproject.bramble.api.system.Clock; import org.briarproject.bramble.api.system.Clock;
@@ -28,14 +28,14 @@ class SyncSessionFactoryImpl implements SyncSessionFactory {
private final Executor dbExecutor; private final Executor dbExecutor;
private final EventBus eventBus; private final EventBus eventBus;
private final Clock clock; private final Clock clock;
private final RecordReaderFactory recordReaderFactory; private final SyncRecordReaderFactory recordReaderFactory;
private final RecordWriterFactory recordWriterFactory; private final SyncRecordWriterFactory recordWriterFactory;
@Inject @Inject
SyncSessionFactoryImpl(DatabaseComponent db, SyncSessionFactoryImpl(DatabaseComponent db,
@DatabaseExecutor Executor dbExecutor, EventBus eventBus, @DatabaseExecutor Executor dbExecutor, EventBus eventBus,
Clock clock, RecordReaderFactory recordReaderFactory, Clock clock, SyncRecordReaderFactory recordReaderFactory,
RecordWriterFactory recordWriterFactory) { SyncRecordWriterFactory recordWriterFactory) {
this.db = db; this.db = db;
this.dbExecutor = dbExecutor; this.dbExecutor = dbExecutor;
this.eventBus = eventBus; this.eventBus = eventBus;
@@ -46,14 +46,16 @@ class SyncSessionFactoryImpl implements SyncSessionFactory {
@Override @Override
public SyncSession createIncomingSession(ContactId c, InputStream in) { public SyncSession createIncomingSession(ContactId c, InputStream in) {
RecordReader recordReader = recordReaderFactory.createRecordReader(in); SyncRecordReader recordReader =
recordReaderFactory.createRecordReader(in);
return new IncomingSession(db, dbExecutor, eventBus, c, recordReader); return new IncomingSession(db, dbExecutor, eventBus, c, recordReader);
} }
@Override @Override
public SyncSession createSimplexOutgoingSession(ContactId c, public SyncSession createSimplexOutgoingSession(ContactId c,
int maxLatency, OutputStream out) { int maxLatency, OutputStream out) {
RecordWriter recordWriter = recordWriterFactory.createRecordWriter(out); SyncRecordWriter recordWriter =
recordWriterFactory.createRecordWriter(out);
return new SimplexOutgoingSession(db, dbExecutor, eventBus, c, return new SimplexOutgoingSession(db, dbExecutor, eventBus, c,
maxLatency, recordWriter); maxLatency, recordWriter);
} }
@@ -61,7 +63,8 @@ class SyncSessionFactoryImpl implements SyncSessionFactory {
@Override @Override
public SyncSession createDuplexOutgoingSession(ContactId c, int maxLatency, public SyncSession createDuplexOutgoingSession(ContactId c, int maxLatency,
int maxIdleTime, OutputStream out) { int maxIdleTime, OutputStream out) {
RecordWriter recordWriter = recordWriterFactory.createRecordWriter(out); SyncRecordWriter recordWriter =
recordWriterFactory.createRecordWriter(out);
return new DuplexOutgoingSession(db, dbExecutor, eventBus, clock, c, return new DuplexOutgoingSession(db, dbExecutor, eventBus, clock, c,
maxLatency, maxIdleTime, recordWriter); maxLatency, maxIdleTime, recordWriter);
} }

View File

@@ -6,7 +6,7 @@ import org.briarproject.bramble.api.db.Transaction;
import org.briarproject.bramble.api.event.EventBus; import org.briarproject.bramble.api.event.EventBus;
import org.briarproject.bramble.api.sync.Ack; import org.briarproject.bramble.api.sync.Ack;
import org.briarproject.bramble.api.sync.MessageId; import org.briarproject.bramble.api.sync.MessageId;
import org.briarproject.bramble.api.sync.RecordWriter; import org.briarproject.bramble.api.sync.SyncRecordWriter;
import org.briarproject.bramble.test.BrambleTestCase; import org.briarproject.bramble.test.BrambleTestCase;
import org.briarproject.bramble.test.ImmediateExecutor; import org.briarproject.bramble.test.ImmediateExecutor;
import org.briarproject.bramble.test.TestUtils; import org.briarproject.bramble.test.TestUtils;
@@ -29,14 +29,14 @@ public class SimplexOutgoingSessionTest extends BrambleTestCase {
private final ContactId contactId; private final ContactId contactId;
private final MessageId messageId; private final MessageId messageId;
private final int maxLatency; private final int maxLatency;
private final RecordWriter recordWriter; private final SyncRecordWriter recordWriter;
public SimplexOutgoingSessionTest() { public SimplexOutgoingSessionTest() {
context = new Mockery(); context = new Mockery();
db = context.mock(DatabaseComponent.class); db = context.mock(DatabaseComponent.class);
dbExecutor = new ImmediateExecutor(); dbExecutor = new ImmediateExecutor();
eventBus = context.mock(EventBus.class); eventBus = context.mock(EventBus.class);
recordWriter = context.mock(RecordWriter.class); recordWriter = context.mock(SyncRecordWriter.class);
contactId = new ContactId(234); contactId = new ContactId(234);
messageId = new MessageId(TestUtils.getRandomId()); messageId = new MessageId(TestUtils.getRandomId());
maxLatency = Integer.MAX_VALUE; maxLatency = Integer.MAX_VALUE;

View File

@@ -12,11 +12,11 @@ import org.briarproject.bramble.api.sync.Message;
import org.briarproject.bramble.api.sync.MessageFactory; import org.briarproject.bramble.api.sync.MessageFactory;
import org.briarproject.bramble.api.sync.MessageId; import org.briarproject.bramble.api.sync.MessageId;
import org.briarproject.bramble.api.sync.Offer; import org.briarproject.bramble.api.sync.Offer;
import org.briarproject.bramble.api.sync.RecordReader;
import org.briarproject.bramble.api.sync.RecordReaderFactory;
import org.briarproject.bramble.api.sync.RecordWriter;
import org.briarproject.bramble.api.sync.RecordWriterFactory;
import org.briarproject.bramble.api.sync.Request; import org.briarproject.bramble.api.sync.Request;
import org.briarproject.bramble.api.sync.SyncRecordReader;
import org.briarproject.bramble.api.sync.SyncRecordReaderFactory;
import org.briarproject.bramble.api.sync.SyncRecordWriter;
import org.briarproject.bramble.api.sync.SyncRecordWriterFactory;
import org.briarproject.bramble.api.transport.StreamContext; import org.briarproject.bramble.api.transport.StreamContext;
import org.briarproject.bramble.api.transport.StreamReaderFactory; import org.briarproject.bramble.api.transport.StreamReaderFactory;
import org.briarproject.bramble.api.transport.StreamWriterFactory; import org.briarproject.bramble.api.transport.StreamWriterFactory;
@@ -54,9 +54,9 @@ public class SyncIntegrationTest extends BrambleTestCase {
@Inject @Inject
StreamWriterFactory streamWriterFactory; StreamWriterFactory streamWriterFactory;
@Inject @Inject
RecordReaderFactory recordReaderFactory; SyncRecordReaderFactory recordReaderFactory;
@Inject @Inject
RecordWriterFactory recordWriterFactory; SyncRecordWriterFactory recordWriterFactory;
@Inject @Inject
TransportCrypto transportCrypto; TransportCrypto transportCrypto;
@@ -104,7 +104,7 @@ public class SyncIntegrationTest extends BrambleTestCase {
headerKey, streamNumber); headerKey, streamNumber);
OutputStream streamWriter = streamWriterFactory.createStreamWriter(out, OutputStream streamWriter = streamWriterFactory.createStreamWriter(out,
ctx); ctx);
RecordWriter recordWriter = recordWriterFactory.createRecordWriter( SyncRecordWriter recordWriter = recordWriterFactory.createRecordWriter(
streamWriter); streamWriter);
recordWriter.writeAck(new Ack(messageIds)); recordWriter.writeAck(new Ack(messageIds));
@@ -134,7 +134,7 @@ public class SyncIntegrationTest extends BrambleTestCase {
headerKey, streamNumber); headerKey, streamNumber);
InputStream streamReader = streamReaderFactory.createStreamReader(in, InputStream streamReader = streamReaderFactory.createStreamReader(in,
ctx); ctx);
RecordReader recordReader = recordReaderFactory.createRecordReader( SyncRecordReader recordReader = recordReaderFactory.createRecordReader(
streamReader); streamReader);
// Read the ack // Read the ack

View File

@@ -23,7 +23,7 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
public class RecordReaderImplTest extends BrambleMockTestCase { public class SyncRecordReaderImplTest extends BrambleMockTestCase {
private final MessageFactory messageFactory = private final MessageFactory messageFactory =
context.mock(MessageFactory.class); context.mock(MessageFactory.class);
@@ -32,7 +32,8 @@ public class RecordReaderImplTest extends BrambleMockTestCase {
public void testFormatExceptionIfAckIsTooLarge() throws Exception { public void testFormatExceptionIfAckIsTooLarge() throws Exception {
byte[] b = createAck(true); byte[] b = createAck(true);
ByteArrayInputStream in = new ByteArrayInputStream(b); ByteArrayInputStream in = new ByteArrayInputStream(b);
RecordReaderImpl reader = new RecordReaderImpl(messageFactory, in); SyncRecordReaderImpl
reader = new SyncRecordReaderImpl(messageFactory, in);
reader.readAck(); reader.readAck();
} }
@@ -40,7 +41,8 @@ public class RecordReaderImplTest extends BrambleMockTestCase {
public void testNoFormatExceptionIfAckIsMaximumSize() throws Exception { public void testNoFormatExceptionIfAckIsMaximumSize() throws Exception {
byte[] b = createAck(false); byte[] b = createAck(false);
ByteArrayInputStream in = new ByteArrayInputStream(b); ByteArrayInputStream in = new ByteArrayInputStream(b);
RecordReaderImpl reader = new RecordReaderImpl(messageFactory, in); SyncRecordReaderImpl
reader = new SyncRecordReaderImpl(messageFactory, in);
reader.readAck(); reader.readAck();
} }
@@ -48,7 +50,8 @@ public class RecordReaderImplTest extends BrambleMockTestCase {
public void testFormatExceptionIfAckIsEmpty() throws Exception { public void testFormatExceptionIfAckIsEmpty() throws Exception {
byte[] b = createEmptyAck(); byte[] b = createEmptyAck();
ByteArrayInputStream in = new ByteArrayInputStream(b); ByteArrayInputStream in = new ByteArrayInputStream(b);
RecordReaderImpl reader = new RecordReaderImpl(messageFactory, in); SyncRecordReaderImpl
reader = new SyncRecordReaderImpl(messageFactory, in);
reader.readAck(); reader.readAck();
} }
@@ -56,7 +59,8 @@ public class RecordReaderImplTest extends BrambleMockTestCase {
public void testFormatExceptionIfOfferIsTooLarge() throws Exception { public void testFormatExceptionIfOfferIsTooLarge() throws Exception {
byte[] b = createOffer(true); byte[] b = createOffer(true);
ByteArrayInputStream in = new ByteArrayInputStream(b); ByteArrayInputStream in = new ByteArrayInputStream(b);
RecordReaderImpl reader = new RecordReaderImpl(messageFactory, in); SyncRecordReaderImpl
reader = new SyncRecordReaderImpl(messageFactory, in);
reader.readOffer(); reader.readOffer();
} }
@@ -64,7 +68,8 @@ public class RecordReaderImplTest extends BrambleMockTestCase {
public void testNoFormatExceptionIfOfferIsMaximumSize() throws Exception { public void testNoFormatExceptionIfOfferIsMaximumSize() throws Exception {
byte[] b = createOffer(false); byte[] b = createOffer(false);
ByteArrayInputStream in = new ByteArrayInputStream(b); ByteArrayInputStream in = new ByteArrayInputStream(b);
RecordReaderImpl reader = new RecordReaderImpl(messageFactory, in); SyncRecordReaderImpl
reader = new SyncRecordReaderImpl(messageFactory, in);
reader.readOffer(); reader.readOffer();
} }
@@ -72,7 +77,8 @@ public class RecordReaderImplTest extends BrambleMockTestCase {
public void testFormatExceptionIfOfferIsEmpty() throws Exception { public void testFormatExceptionIfOfferIsEmpty() throws Exception {
byte[] b = createEmptyOffer(); byte[] b = createEmptyOffer();
ByteArrayInputStream in = new ByteArrayInputStream(b); ByteArrayInputStream in = new ByteArrayInputStream(b);
RecordReaderImpl reader = new RecordReaderImpl(messageFactory, in); SyncRecordReaderImpl
reader = new SyncRecordReaderImpl(messageFactory, in);
reader.readOffer(); reader.readOffer();
} }
@@ -80,7 +86,8 @@ public class RecordReaderImplTest extends BrambleMockTestCase {
public void testFormatExceptionIfRequestIsTooLarge() throws Exception { public void testFormatExceptionIfRequestIsTooLarge() throws Exception {
byte[] b = createRequest(true); byte[] b = createRequest(true);
ByteArrayInputStream in = new ByteArrayInputStream(b); ByteArrayInputStream in = new ByteArrayInputStream(b);
RecordReaderImpl reader = new RecordReaderImpl(messageFactory, in); SyncRecordReaderImpl
reader = new SyncRecordReaderImpl(messageFactory, in);
reader.readRequest(); reader.readRequest();
} }
@@ -88,7 +95,8 @@ public class RecordReaderImplTest extends BrambleMockTestCase {
public void testNoFormatExceptionIfRequestIsMaximumSize() throws Exception { public void testNoFormatExceptionIfRequestIsMaximumSize() throws Exception {
byte[] b = createRequest(false); byte[] b = createRequest(false);
ByteArrayInputStream in = new ByteArrayInputStream(b); ByteArrayInputStream in = new ByteArrayInputStream(b);
RecordReaderImpl reader = new RecordReaderImpl(messageFactory, in); SyncRecordReaderImpl
reader = new SyncRecordReaderImpl(messageFactory, in);
reader.readRequest(); reader.readRequest();
} }
@@ -96,14 +104,16 @@ public class RecordReaderImplTest extends BrambleMockTestCase {
public void testFormatExceptionIfRequestIsEmpty() throws Exception { public void testFormatExceptionIfRequestIsEmpty() throws Exception {
byte[] b = createEmptyRequest(); byte[] b = createEmptyRequest();
ByteArrayInputStream in = new ByteArrayInputStream(b); ByteArrayInputStream in = new ByteArrayInputStream(b);
RecordReaderImpl reader = new RecordReaderImpl(messageFactory, in); SyncRecordReaderImpl
reader = new SyncRecordReaderImpl(messageFactory, in);
reader.readRequest(); reader.readRequest();
} }
@Test @Test
public void testEofReturnsTrueWhenAtEndOfStream() throws Exception { public void testEofReturnsTrueWhenAtEndOfStream() throws Exception {
ByteArrayInputStream in = new ByteArrayInputStream(new byte[0]); ByteArrayInputStream in = new ByteArrayInputStream(new byte[0]);
RecordReaderImpl reader = new RecordReaderImpl(messageFactory, in); SyncRecordReaderImpl
reader = new SyncRecordReaderImpl(messageFactory, in);
assertTrue(reader.eof()); assertTrue(reader.eof());
} }
@@ -111,7 +121,8 @@ public class RecordReaderImplTest extends BrambleMockTestCase {
public void testEofReturnsFalseWhenNotAtEndOfStream() throws Exception { public void testEofReturnsFalseWhenNotAtEndOfStream() throws Exception {
byte[] b = createAck(false); byte[] b = createAck(false);
ByteArrayInputStream in = new ByteArrayInputStream(b); ByteArrayInputStream in = new ByteArrayInputStream(b);
RecordReaderImpl reader = new RecordReaderImpl(messageFactory, in); SyncRecordReaderImpl
reader = new SyncRecordReaderImpl(messageFactory, in);
assertFalse(reader.eof()); assertFalse(reader.eof());
} }
@@ -121,7 +132,8 @@ public class RecordReaderImplTest extends BrambleMockTestCase {
b[0] = PROTOCOL_VERSION; b[0] = PROTOCOL_VERSION;
b[1] = ACK; b[1] = ACK;
ByteArrayInputStream in = new ByteArrayInputStream(b); ByteArrayInputStream in = new ByteArrayInputStream(b);
RecordReaderImpl reader = new RecordReaderImpl(messageFactory, in); SyncRecordReaderImpl
reader = new SyncRecordReaderImpl(messageFactory, in);
reader.eof(); reader.eof();
} }
@@ -133,7 +145,8 @@ public class RecordReaderImplTest extends BrambleMockTestCase {
b[1] = ACK; b[1] = ACK;
ByteUtils.writeUint16(payloadLength, b, 2); ByteUtils.writeUint16(payloadLength, b, 2);
ByteArrayInputStream in = new ByteArrayInputStream(b); ByteArrayInputStream in = new ByteArrayInputStream(b);
RecordReaderImpl reader = new RecordReaderImpl(messageFactory, in); SyncRecordReaderImpl
reader = new SyncRecordReaderImpl(messageFactory, in);
reader.eof(); reader.eof();
} }
@@ -143,7 +156,8 @@ public class RecordReaderImplTest extends BrambleMockTestCase {
byte version = (byte) (PROTOCOL_VERSION + 1); byte version = (byte) (PROTOCOL_VERSION + 1);
byte[] b = createRecord(version, ACK, new byte[0]); byte[] b = createRecord(version, ACK, new byte[0]);
ByteArrayInputStream in = new ByteArrayInputStream(b); ByteArrayInputStream in = new ByteArrayInputStream(b);
RecordReaderImpl reader = new RecordReaderImpl(messageFactory, in); SyncRecordReaderImpl
reader = new SyncRecordReaderImpl(messageFactory, in);
reader.eof(); reader.eof();
} }
@@ -152,7 +166,8 @@ public class RecordReaderImplTest extends BrambleMockTestCase {
byte[] payload = new byte[MAX_RECORD_PAYLOAD_LENGTH + 1]; byte[] payload = new byte[MAX_RECORD_PAYLOAD_LENGTH + 1];
byte[] b = createRecord(PROTOCOL_VERSION, ACK, payload); byte[] b = createRecord(PROTOCOL_VERSION, ACK, payload);
ByteArrayInputStream in = new ByteArrayInputStream(b); ByteArrayInputStream in = new ByteArrayInputStream(b);
RecordReaderImpl reader = new RecordReaderImpl(messageFactory, in); SyncRecordReaderImpl
reader = new SyncRecordReaderImpl(messageFactory, in);
reader.eof(); reader.eof();
} }
@@ -168,7 +183,8 @@ public class RecordReaderImplTest extends BrambleMockTestCase {
input.write(skip2); input.write(skip2);
input.write(ack); input.write(ack);
ByteArrayInputStream in = new ByteArrayInputStream(input.toByteArray()); ByteArrayInputStream in = new ByteArrayInputStream(input.toByteArray());
RecordReaderImpl reader = new RecordReaderImpl(messageFactory, in); SyncRecordReaderImpl
reader = new SyncRecordReaderImpl(messageFactory, in);
assertTrue(reader.hasAck()); assertTrue(reader.hasAck());
Ack a = reader.readAck(); Ack a = reader.readAck();
assertEquals(MAX_MESSAGE_IDS, a.getMessageIds().size()); assertEquals(MAX_MESSAGE_IDS, a.getMessageIds().size());