Removed silly SerialComponent interface.

This commit is contained in:
akwizgran
2015-05-02 20:30:25 +01:00
parent 32c9ce50d9
commit 416719e3d9
7 changed files with 26 additions and 67 deletions

View File

@@ -6,22 +6,18 @@ import javax.inject.Inject;
import org.briarproject.api.messaging.PacketWriter;
import org.briarproject.api.messaging.PacketWriterFactory;
import org.briarproject.api.serial.SerialComponent;
import org.briarproject.api.serial.WriterFactory;
class PacketWriterFactoryImpl implements PacketWriterFactory {
private final SerialComponent serial;
private final WriterFactory writerFactory;
@Inject
PacketWriterFactoryImpl(SerialComponent serial,
WriterFactory writerFactory) {
this.serial = serial;
PacketWriterFactoryImpl(WriterFactory writerFactory) {
this.writerFactory = writerFactory;
}
public PacketWriter createPacketWriter(OutputStream out) {
return new PacketWriterImpl(serial, writerFactory, out);
return new PacketWriterImpl(writerFactory, out);
}
}

View File

@@ -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.TRANSPORT_ACK;
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.IOException;
@@ -30,7 +33,6 @@ import org.briarproject.api.messaging.SubscriptionAck;
import org.briarproject.api.messaging.SubscriptionUpdate;
import org.briarproject.api.messaging.TransportAck;
import org.briarproject.api.messaging.TransportUpdate;
import org.briarproject.api.serial.SerialComponent;
import org.briarproject.api.serial.Writer;
import org.briarproject.api.serial.WriterFactory;
import org.briarproject.util.ByteUtils;
@@ -38,15 +40,12 @@ import org.briarproject.util.ByteUtils;
// This class is not thread-safe
class PacketWriterImpl implements PacketWriter {
private final SerialComponent serial;
private final WriterFactory writerFactory;
private final OutputStream out;
private final byte[] header;
private final ByteArrayOutputStream payload;
PacketWriterImpl(SerialComponent serial, WriterFactory writerFactory,
OutputStream out) {
this.serial = serial;
PacketWriterImpl(WriterFactory writerFactory, OutputStream out) {
this.writerFactory = writerFactory;
this.out = out;
header = new byte[HEADER_LENGTH];
@@ -69,10 +68,8 @@ class PacketWriterImpl implements PacketWriter {
private int getMaxMessagesForPacket(long capacity) {
int payload = (int) Math.min(capacity - HEADER_LENGTH,
MAX_PAYLOAD_LENGTH);
int overhead = serial.getSerialisedListStartLength() * 2
+ serial.getSerialisedListEndLength() * 2;
int idLength = serial.getSerialisedUniqueIdLength();
return (payload - overhead) / idLength;
int overhead = LIST_START_LENGTH * 2 + LIST_END_LENGTH * 2;
return (payload - overhead) / UNIQUE_ID_LENGTH;
}
private void writePacket(byte packetType) throws IOException {

View File

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

View File

@@ -1,19 +1,15 @@
package org.briarproject.serial;
import javax.inject.Singleton;
import org.briarproject.api.serial.ReaderFactory;
import org.briarproject.api.serial.SerialComponent;
import org.briarproject.api.serial.WriterFactory;
import com.google.inject.AbstractModule;
public class SerialModule extends AbstractModule {
@Override
protected void configure() {
bind(ReaderFactory.class).to(ReaderFactoryImpl.class);
bind(SerialComponent.class).to(
SerialComponentImpl.class).in(Singleton.class);
bind(WriterFactory.class).to(WriterFactoryImpl.class);
}
}