mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-18 13:49:53 +01:00
Removed silly SerialComponent interface.
This commit is contained in:
@@ -1,10 +0,0 @@
|
|||||||
package org.briarproject.api.serial;
|
|
||||||
|
|
||||||
public interface SerialComponent {
|
|
||||||
|
|
||||||
int getSerialisedListStartLength();
|
|
||||||
|
|
||||||
int getSerialisedListEndLength();
|
|
||||||
|
|
||||||
int getSerialisedUniqueIdLength();
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
package org.briarproject.api.serial;
|
||||||
|
|
||||||
|
import org.briarproject.api.UniqueId;
|
||||||
|
|
||||||
|
public interface SerialConstants {
|
||||||
|
|
||||||
|
int LIST_START_LENGTH = 1;
|
||||||
|
|
||||||
|
int LIST_END_LENGTH = 1;
|
||||||
|
|
||||||
|
int UNIQUE_ID_LENGTH = 2 + UniqueId.LENGTH;
|
||||||
|
}
|
||||||
@@ -6,22 +6,18 @@ import javax.inject.Inject;
|
|||||||
|
|
||||||
import org.briarproject.api.messaging.PacketWriter;
|
import org.briarproject.api.messaging.PacketWriter;
|
||||||
import org.briarproject.api.messaging.PacketWriterFactory;
|
import org.briarproject.api.messaging.PacketWriterFactory;
|
||||||
import org.briarproject.api.serial.SerialComponent;
|
|
||||||
import org.briarproject.api.serial.WriterFactory;
|
import org.briarproject.api.serial.WriterFactory;
|
||||||
|
|
||||||
class PacketWriterFactoryImpl implements PacketWriterFactory {
|
class PacketWriterFactoryImpl implements PacketWriterFactory {
|
||||||
|
|
||||||
private final SerialComponent serial;
|
|
||||||
private final WriterFactory writerFactory;
|
private final WriterFactory writerFactory;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
PacketWriterFactoryImpl(SerialComponent serial,
|
PacketWriterFactoryImpl(WriterFactory writerFactory) {
|
||||||
WriterFactory writerFactory) {
|
|
||||||
this.serial = serial;
|
|
||||||
this.writerFactory = writerFactory;
|
this.writerFactory = writerFactory;
|
||||||
}
|
}
|
||||||
|
|
||||||
public PacketWriter createPacketWriter(OutputStream out) {
|
public PacketWriter createPacketWriter(OutputStream out) {
|
||||||
return new PacketWriterImpl(serial, writerFactory, out);
|
return new PacketWriterImpl(writerFactory, out);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,6 +12,9 @@ import static org.briarproject.api.messaging.PacketTypes.SUBSCRIPTION_ACK;
|
|||||||
import static org.briarproject.api.messaging.PacketTypes.SUBSCRIPTION_UPDATE;
|
import static org.briarproject.api.messaging.PacketTypes.SUBSCRIPTION_UPDATE;
|
||||||
import static org.briarproject.api.messaging.PacketTypes.TRANSPORT_ACK;
|
import static org.briarproject.api.messaging.PacketTypes.TRANSPORT_ACK;
|
||||||
import static org.briarproject.api.messaging.PacketTypes.TRANSPORT_UPDATE;
|
import static org.briarproject.api.messaging.PacketTypes.TRANSPORT_UPDATE;
|
||||||
|
import static org.briarproject.api.serial.SerialConstants.LIST_END_LENGTH;
|
||||||
|
import static org.briarproject.api.serial.SerialConstants.LIST_START_LENGTH;
|
||||||
|
import static org.briarproject.api.serial.SerialConstants.UNIQUE_ID_LENGTH;
|
||||||
|
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
@@ -30,7 +33,6 @@ import org.briarproject.api.messaging.SubscriptionAck;
|
|||||||
import org.briarproject.api.messaging.SubscriptionUpdate;
|
import org.briarproject.api.messaging.SubscriptionUpdate;
|
||||||
import org.briarproject.api.messaging.TransportAck;
|
import org.briarproject.api.messaging.TransportAck;
|
||||||
import org.briarproject.api.messaging.TransportUpdate;
|
import org.briarproject.api.messaging.TransportUpdate;
|
||||||
import org.briarproject.api.serial.SerialComponent;
|
|
||||||
import org.briarproject.api.serial.Writer;
|
import org.briarproject.api.serial.Writer;
|
||||||
import org.briarproject.api.serial.WriterFactory;
|
import org.briarproject.api.serial.WriterFactory;
|
||||||
import org.briarproject.util.ByteUtils;
|
import org.briarproject.util.ByteUtils;
|
||||||
@@ -38,15 +40,12 @@ import org.briarproject.util.ByteUtils;
|
|||||||
// This class is not thread-safe
|
// This class is not thread-safe
|
||||||
class PacketWriterImpl implements PacketWriter {
|
class PacketWriterImpl implements PacketWriter {
|
||||||
|
|
||||||
private final SerialComponent serial;
|
|
||||||
private final WriterFactory writerFactory;
|
private final WriterFactory writerFactory;
|
||||||
private final OutputStream out;
|
private final OutputStream out;
|
||||||
private final byte[] header;
|
private final byte[] header;
|
||||||
private final ByteArrayOutputStream payload;
|
private final ByteArrayOutputStream payload;
|
||||||
|
|
||||||
PacketWriterImpl(SerialComponent serial, WriterFactory writerFactory,
|
PacketWriterImpl(WriterFactory writerFactory, OutputStream out) {
|
||||||
OutputStream out) {
|
|
||||||
this.serial = serial;
|
|
||||||
this.writerFactory = writerFactory;
|
this.writerFactory = writerFactory;
|
||||||
this.out = out;
|
this.out = out;
|
||||||
header = new byte[HEADER_LENGTH];
|
header = new byte[HEADER_LENGTH];
|
||||||
@@ -69,10 +68,8 @@ class PacketWriterImpl implements PacketWriter {
|
|||||||
private int getMaxMessagesForPacket(long capacity) {
|
private int getMaxMessagesForPacket(long capacity) {
|
||||||
int payload = (int) Math.min(capacity - HEADER_LENGTH,
|
int payload = (int) Math.min(capacity - HEADER_LENGTH,
|
||||||
MAX_PAYLOAD_LENGTH);
|
MAX_PAYLOAD_LENGTH);
|
||||||
int overhead = serial.getSerialisedListStartLength() * 2
|
int overhead = LIST_START_LENGTH * 2 + LIST_END_LENGTH * 2;
|
||||||
+ serial.getSerialisedListEndLength() * 2;
|
return (payload - overhead) / UNIQUE_ID_LENGTH;
|
||||||
int idLength = serial.getSerialisedUniqueIdLength();
|
|
||||||
return (payload - overhead) / idLength;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void writePacket(byte packetType) throws IOException {
|
private void writePacket(byte packetType) throws IOException {
|
||||||
|
|||||||
@@ -1,28 +0,0 @@
|
|||||||
package org.briarproject.serial;
|
|
||||||
|
|
||||||
import org.briarproject.api.UniqueId;
|
|
||||||
import org.briarproject.api.serial.SerialComponent;
|
|
||||||
|
|
||||||
class SerialComponentImpl implements SerialComponent {
|
|
||||||
|
|
||||||
public int getSerialisedListStartLength() {
|
|
||||||
// LIST tag
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getSerialisedListEndLength() {
|
|
||||||
// END tag
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getSerialisedUniqueIdLength() {
|
|
||||||
// BYTES_8, BYTES_16 or BYTES_32 tag, length, bytes
|
|
||||||
return 1 + getLengthBytes(UniqueId.LENGTH) + UniqueId.LENGTH;
|
|
||||||
}
|
|
||||||
|
|
||||||
private int getLengthBytes(int length) {
|
|
||||||
if(length <= Byte.MAX_VALUE) return 1;
|
|
||||||
if(length <= Short.MAX_VALUE) return 2;
|
|
||||||
return 4;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,19 +1,15 @@
|
|||||||
package org.briarproject.serial;
|
package org.briarproject.serial;
|
||||||
|
|
||||||
import javax.inject.Singleton;
|
|
||||||
|
|
||||||
import org.briarproject.api.serial.ReaderFactory;
|
import org.briarproject.api.serial.ReaderFactory;
|
||||||
import org.briarproject.api.serial.SerialComponent;
|
|
||||||
import org.briarproject.api.serial.WriterFactory;
|
import org.briarproject.api.serial.WriterFactory;
|
||||||
|
|
||||||
import com.google.inject.AbstractModule;
|
import com.google.inject.AbstractModule;
|
||||||
|
|
||||||
public class SerialModule extends AbstractModule {
|
public class SerialModule extends AbstractModule {
|
||||||
|
|
||||||
|
@Override
|
||||||
protected void configure() {
|
protected void configure() {
|
||||||
bind(ReaderFactory.class).to(ReaderFactoryImpl.class);
|
bind(ReaderFactory.class).to(ReaderFactoryImpl.class);
|
||||||
bind(SerialComponent.class).to(
|
|
||||||
SerialComponentImpl.class).in(Singleton.class);
|
|
||||||
bind(WriterFactory.class).to(WriterFactoryImpl.class);
|
bind(WriterFactory.class).to(WriterFactoryImpl.class);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,6 +5,8 @@ import static org.briarproject.api.messaging.MessagingConstants.MAX_PAYLOAD_LENG
|
|||||||
import static org.briarproject.api.messaging.PacketTypes.ACK;
|
import static org.briarproject.api.messaging.PacketTypes.ACK;
|
||||||
import static org.briarproject.api.messaging.PacketTypes.OFFER;
|
import static org.briarproject.api.messaging.PacketTypes.OFFER;
|
||||||
import static org.briarproject.api.messaging.PacketTypes.REQUEST;
|
import static org.briarproject.api.messaging.PacketTypes.REQUEST;
|
||||||
|
import static org.briarproject.api.serial.SerialConstants.LIST_END_LENGTH;
|
||||||
|
import static org.briarproject.api.serial.SerialConstants.UNIQUE_ID_LENGTH;
|
||||||
|
|
||||||
import java.io.ByteArrayInputStream;
|
import java.io.ByteArrayInputStream;
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
@@ -13,7 +15,6 @@ import org.briarproject.BriarTestCase;
|
|||||||
import org.briarproject.TestUtils;
|
import org.briarproject.TestUtils;
|
||||||
import org.briarproject.api.FormatException;
|
import org.briarproject.api.FormatException;
|
||||||
import org.briarproject.api.serial.ReaderFactory;
|
import org.briarproject.api.serial.ReaderFactory;
|
||||||
import org.briarproject.api.serial.SerialComponent;
|
|
||||||
import org.briarproject.api.serial.Writer;
|
import org.briarproject.api.serial.Writer;
|
||||||
import org.briarproject.api.serial.WriterFactory;
|
import org.briarproject.api.serial.WriterFactory;
|
||||||
import org.briarproject.serial.SerialModule;
|
import org.briarproject.serial.SerialModule;
|
||||||
@@ -27,13 +28,11 @@ public class PacketReaderImplTest extends BriarTestCase {
|
|||||||
|
|
||||||
// FIXME: This is an integration test, not a unit test
|
// FIXME: This is an integration test, not a unit test
|
||||||
|
|
||||||
private final SerialComponent serial;
|
|
||||||
private final ReaderFactory readerFactory;
|
private final ReaderFactory readerFactory;
|
||||||
private final WriterFactory writerFactory;
|
private final WriterFactory writerFactory;
|
||||||
|
|
||||||
public PacketReaderImplTest() throws Exception {
|
public PacketReaderImplTest() throws Exception {
|
||||||
Injector i = Guice.createInjector(new SerialModule());
|
Injector i = Guice.createInjector(new SerialModule());
|
||||||
serial = i.getInstance(SerialComponent.class);
|
|
||||||
readerFactory = i.getInstance(ReaderFactory.class);
|
readerFactory = i.getInstance(ReaderFactory.class);
|
||||||
writerFactory = i.getInstance(WriterFactory.class);
|
writerFactory = i.getInstance(WriterFactory.class);
|
||||||
}
|
}
|
||||||
@@ -143,8 +142,7 @@ public class PacketReaderImplTest extends BriarTestCase {
|
|||||||
Writer w = writerFactory.createWriter(out);
|
Writer w = writerFactory.createWriter(out);
|
||||||
w.writeListStart();
|
w.writeListStart();
|
||||||
w.writeListStart();
|
w.writeListStart();
|
||||||
while(out.size() + serial.getSerialisedUniqueIdLength()
|
while(out.size() + UNIQUE_ID_LENGTH + LIST_END_LENGTH * 2
|
||||||
+ serial.getSerialisedListEndLength() * 2
|
|
||||||
< HEADER_LENGTH + MAX_PAYLOAD_LENGTH) {
|
< HEADER_LENGTH + MAX_PAYLOAD_LENGTH) {
|
||||||
w.writeBytes(TestUtils.getRandomId());
|
w.writeBytes(TestUtils.getRandomId());
|
||||||
}
|
}
|
||||||
@@ -178,8 +176,7 @@ public class PacketReaderImplTest extends BriarTestCase {
|
|||||||
Writer w = writerFactory.createWriter(out);
|
Writer w = writerFactory.createWriter(out);
|
||||||
w.writeListStart();
|
w.writeListStart();
|
||||||
w.writeListStart();
|
w.writeListStart();
|
||||||
while(out.size() + serial.getSerialisedUniqueIdLength()
|
while(out.size() + UNIQUE_ID_LENGTH + LIST_END_LENGTH * 2
|
||||||
+ serial.getSerialisedListEndLength() * 2
|
|
||||||
< HEADER_LENGTH + MAX_PAYLOAD_LENGTH) {
|
< HEADER_LENGTH + MAX_PAYLOAD_LENGTH) {
|
||||||
w.writeBytes(TestUtils.getRandomId());
|
w.writeBytes(TestUtils.getRandomId());
|
||||||
}
|
}
|
||||||
@@ -213,8 +210,7 @@ public class PacketReaderImplTest extends BriarTestCase {
|
|||||||
Writer w = writerFactory.createWriter(out);
|
Writer w = writerFactory.createWriter(out);
|
||||||
w.writeListStart();
|
w.writeListStart();
|
||||||
w.writeListStart();
|
w.writeListStart();
|
||||||
while(out.size() + serial.getSerialisedUniqueIdLength()
|
while(out.size() + UNIQUE_ID_LENGTH + LIST_END_LENGTH * 2
|
||||||
+ serial.getSerialisedListEndLength() * 2
|
|
||||||
< HEADER_LENGTH + MAX_PAYLOAD_LENGTH) {
|
< HEADER_LENGTH + MAX_PAYLOAD_LENGTH) {
|
||||||
w.writeBytes(TestUtils.getRandomId());
|
w.writeBytes(TestUtils.getRandomId());
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user