Commit 6925dfcb authored by akwizgran's avatar akwizgran

Merge branch '1240-message-refactoring' into 'master'

Remove raw representation from Message class

See merge request !915
parents 817df9c7 ba592821
...@@ -16,7 +16,6 @@ import java.util.logging.Logger; ...@@ -16,7 +16,6 @@ import java.util.logging.Logger;
import javax.annotation.concurrent.Immutable; import javax.annotation.concurrent.Immutable;
import static org.briarproject.bramble.api.sync.SyncConstants.MESSAGE_HEADER_LENGTH;
import static org.briarproject.bramble.api.transport.TransportConstants.MAX_CLOCK_DIFFERENCE; import static org.briarproject.bramble.api.transport.TransportConstants.MAX_CLOCK_DIFFERENCE;
@Immutable @Immutable
...@@ -49,14 +48,13 @@ public abstract class BdfMessageValidator implements MessageValidator { ...@@ -49,14 +48,13 @@ public abstract class BdfMessageValidator implements MessageValidator {
throw new InvalidMessageException( throw new InvalidMessageException(
"Timestamp is too far in the future"); "Timestamp is too far in the future");
} }
byte[] raw = m.getRaw(); byte[] body = m.getBody();
if (raw.length <= MESSAGE_HEADER_LENGTH) { if (body.length == 0) {
throw new InvalidMessageException("Message is too short"); throw new InvalidMessageException("Message is too short");
} }
try { try {
BdfList body = clientHelper.toList(raw, MESSAGE_HEADER_LENGTH, BdfList bodyList = clientHelper.toList(body);
raw.length - MESSAGE_HEADER_LENGTH); BdfMessageContext result = validateMessage(m, g, bodyList);
BdfMessageContext result = validateMessage(m, g, body);
Metadata meta = metadataEncoder.encode(result.getDictionary()); Metadata meta = metadataEncoder.encode(result.getDictionary());
return new MessageContext(meta, result.getDependencies()); return new MessageContext(meta, result.getDependencies());
} catch (FormatException e) { } catch (FormatException e) {
......
package org.briarproject.bramble.api.sync; package org.briarproject.bramble.api.sync;
import static org.briarproject.bramble.api.sync.SyncConstants.MAX_MESSAGE_LENGTH; import static org.briarproject.bramble.api.sync.SyncConstants.MAX_MESSAGE_BODY_LENGTH;
import static org.briarproject.bramble.api.sync.SyncConstants.MESSAGE_HEADER_LENGTH; import static org.briarproject.bramble.api.sync.SyncConstants.MESSAGE_HEADER_LENGTH;
public class Message { public class Message {
...@@ -13,17 +13,15 @@ public class Message { ...@@ -13,17 +13,15 @@ public class Message {
private final MessageId id; private final MessageId id;
private final GroupId groupId; private final GroupId groupId;
private final long timestamp; private final long timestamp;
private final byte[] raw; private final byte[] body;
public Message(MessageId id, GroupId groupId, long timestamp, byte[] raw) { public Message(MessageId id, GroupId groupId, long timestamp, byte[] body) {
if (raw.length <= MESSAGE_HEADER_LENGTH) if (body.length > MAX_MESSAGE_BODY_LENGTH)
throw new IllegalArgumentException();
if (raw.length > MAX_MESSAGE_LENGTH)
throw new IllegalArgumentException(); throw new IllegalArgumentException();
this.id = id; this.id = id;
this.groupId = groupId; this.groupId = groupId;
this.timestamp = timestamp; this.timestamp = timestamp;
this.raw = raw; this.body = body;
} }
/** /**
...@@ -51,14 +49,14 @@ public class Message { ...@@ -51,14 +49,14 @@ public class Message {
* Returns the length of the raw message in bytes. * Returns the length of the raw message in bytes.
*/ */
public int getRawLength() { public int getRawLength() {
return raw.length; return MESSAGE_HEADER_LENGTH + body.length;
} }
/** /**
* Returns the raw message. * Returns the message body.
*/ */
public byte[] getRaw() { public byte[] getBody() {
return raw; return body;
} }
@Override @Override
......
...@@ -9,5 +9,5 @@ public interface MessageFactory { ...@@ -9,5 +9,5 @@ public interface MessageFactory {
Message createMessage(byte[] raw); Message createMessage(byte[] raw);
Message createMessage(MessageId m, byte[] raw); byte[] getRawMessage(Message m);
} }
...@@ -33,7 +33,6 @@ import static org.briarproject.bramble.api.properties.TransportPropertyConstants ...@@ -33,7 +33,6 @@ import static org.briarproject.bramble.api.properties.TransportPropertyConstants
import static org.briarproject.bramble.api.sync.ClientId.MAX_CLIENT_ID_LENGTH; import static org.briarproject.bramble.api.sync.ClientId.MAX_CLIENT_ID_LENGTH;
import static org.briarproject.bramble.api.sync.SyncConstants.MAX_GROUP_DESCRIPTOR_LENGTH; import static org.briarproject.bramble.api.sync.SyncConstants.MAX_GROUP_DESCRIPTOR_LENGTH;
import static org.briarproject.bramble.api.sync.SyncConstants.MAX_MESSAGE_BODY_LENGTH; import static org.briarproject.bramble.api.sync.SyncConstants.MAX_MESSAGE_BODY_LENGTH;
import static org.briarproject.bramble.api.sync.SyncConstants.MESSAGE_HEADER_LENGTH;
import static org.briarproject.bramble.util.StringUtils.getRandomString; import static org.briarproject.bramble.util.StringUtils.getRandomString;
public class TestUtils { public class TestUtils {
...@@ -132,13 +131,13 @@ public class TestUtils { ...@@ -132,13 +131,13 @@ public class TestUtils {
public static Message getMessage(GroupId groupId) { public static Message getMessage(GroupId groupId) {
int bodyLength = 1 + random.nextInt(MAX_MESSAGE_BODY_LENGTH); int bodyLength = 1 + random.nextInt(MAX_MESSAGE_BODY_LENGTH);
return getMessage(groupId, MESSAGE_HEADER_LENGTH + bodyLength); return getMessage(groupId, bodyLength);
} }
public static Message getMessage(GroupId groupId, int rawLength) { public static Message getMessage(GroupId groupId, int bodyLength) {
MessageId id = new MessageId(getRandomId()); MessageId id = new MessageId(getRandomId());
byte[] raw = getRandomBytes(rawLength); byte[] body = getRandomBytes(bodyLength);
return new Message(id, groupId, timestamp, raw); return new Message(id, groupId, timestamp, body);
} }
public static double getMedian(Collection<? extends Number> samples) { public static double getMedian(Collection<? extends Number> samples) {
......
...@@ -41,7 +41,6 @@ import static org.briarproject.bramble.api.identity.AuthorConstants.MAX_AUTHOR_N ...@@ -41,7 +41,6 @@ import static org.briarproject.bramble.api.identity.AuthorConstants.MAX_AUTHOR_N
import static org.briarproject.bramble.api.identity.AuthorConstants.MAX_PUBLIC_KEY_LENGTH; import static org.briarproject.bramble.api.identity.AuthorConstants.MAX_PUBLIC_KEY_LENGTH;
import static org.briarproject.bramble.api.properties.TransportPropertyConstants.MAX_PROPERTIES_PER_TRANSPORT; import static org.briarproject.bramble.api.properties.TransportPropertyConstants.MAX_PROPERTIES_PER_TRANSPORT;
import static org.briarproject.bramble.api.properties.TransportPropertyConstants.MAX_PROPERTY_LENGTH; import static org.briarproject.bramble.api.properties.TransportPropertyConstants.MAX_PROPERTY_LENGTH;
import static org.briarproject.bramble.api.sync.SyncConstants.MESSAGE_HEADER_LENGTH;
import static org.briarproject.bramble.util.ValidationUtils.checkLength; import static org.briarproject.bramble.util.ValidationUtils.checkLength;
import static org.briarproject.bramble.util.ValidationUtils.checkSize; import static org.briarproject.bramble.util.ValidationUtils.checkSize;
...@@ -147,9 +146,7 @@ class ClientHelperImpl implements ClientHelper { ...@@ -147,9 +146,7 @@ class ClientHelperImpl implements ClientHelper {
@Override @Override
public BdfList getMessageAsList(Transaction txn, MessageId m) public BdfList getMessageAsList(Transaction txn, MessageId m)
throws DbException, FormatException { throws DbException, FormatException {
byte[] raw = db.getMessage(txn, m).getRaw(); return toList(db.getMessage(txn, m).getBody());
return toList(raw, MESSAGE_HEADER_LENGTH,
raw.length - MESSAGE_HEADER_LENGTH);
} }
@Override @Override
...@@ -361,9 +358,7 @@ class ClientHelperImpl implements ClientHelper { ...@@ -361,9 +358,7 @@ class ClientHelperImpl implements ClientHelper {
@Override @Override
public BdfList toList(Message m) throws FormatException { public BdfList toList(Message m) throws FormatException {
byte[] raw = m.getRaw(); return toList(m.getBody());
return toList(raw, MESSAGE_HEADER_LENGTH,
raw.length - MESSAGE_HEADER_LENGTH);
} }
@Override @Override
......
...@@ -4,6 +4,7 @@ import org.briarproject.bramble.api.db.DatabaseComponent; ...@@ -4,6 +4,7 @@ import org.briarproject.bramble.api.db.DatabaseComponent;
import org.briarproject.bramble.api.db.DatabaseConfig; import org.briarproject.bramble.api.db.DatabaseConfig;
import org.briarproject.bramble.api.event.EventBus; import org.briarproject.bramble.api.event.EventBus;
import org.briarproject.bramble.api.lifecycle.ShutdownManager; import org.briarproject.bramble.api.lifecycle.ShutdownManager;
import org.briarproject.bramble.api.sync.MessageFactory;
import org.briarproject.bramble.api.system.Clock; import org.briarproject.bramble.api.system.Clock;
import java.sql.Connection; import java.sql.Connection;
...@@ -18,8 +19,9 @@ public class DatabaseModule { ...@@ -18,8 +19,9 @@ public class DatabaseModule {
@Provides @Provides
@Singleton @Singleton
Database<Connection> provideDatabase(DatabaseConfig config, Clock clock) { Database<Connection> provideDatabase(DatabaseConfig config,
return new H2Database(config, clock); MessageFactory messageFactory, Clock clock) {
return new H2Database(config, messageFactory, clock);
} }
@Provides @Provides
......
...@@ -5,6 +5,7 @@ import org.briarproject.bramble.api.db.DatabaseConfig; ...@@ -5,6 +5,7 @@ import org.briarproject.bramble.api.db.DatabaseConfig;
import org.briarproject.bramble.api.db.DbException; import org.briarproject.bramble.api.db.DbException;
import org.briarproject.bramble.api.db.MigrationListener; import org.briarproject.bramble.api.db.MigrationListener;
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.system.Clock; import org.briarproject.bramble.api.system.Clock;
import org.briarproject.bramble.util.StringUtils; import org.briarproject.bramble.util.StringUtils;
...@@ -36,9 +37,10 @@ class H2Database extends JdbcDatabase { ...@@ -36,9 +37,10 @@ class H2Database extends JdbcDatabase {
private volatile SecretKey key = null; private volatile SecretKey key = null;
@Inject @Inject
H2Database(DatabaseConfig config, Clock clock) { H2Database(DatabaseConfig config, MessageFactory messageFactory,
Clock clock) {
super(HASH_TYPE, SECRET_TYPE, BINARY_TYPE, COUNTER_TYPE, STRING_TYPE, super(HASH_TYPE, SECRET_TYPE, BINARY_TYPE, COUNTER_TYPE, STRING_TYPE,
clock); messageFactory, clock);
this.config = config; this.config = config;
File dir = config.getDatabaseDirectory(); File dir = config.getDatabaseDirectory();
String path = new File(dir, "db").getAbsolutePath(); String path = new File(dir, "db").getAbsolutePath();
......
...@@ -5,6 +5,7 @@ import org.briarproject.bramble.api.db.DatabaseConfig; ...@@ -5,6 +5,7 @@ import org.briarproject.bramble.api.db.DatabaseConfig;
import org.briarproject.bramble.api.db.DbException; import org.briarproject.bramble.api.db.DbException;
import org.briarproject.bramble.api.db.MigrationListener; import org.briarproject.bramble.api.db.MigrationListener;
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.system.Clock; import org.briarproject.bramble.api.system.Clock;
import org.briarproject.bramble.util.StringUtils; import org.briarproject.bramble.util.StringUtils;
...@@ -37,9 +38,10 @@ class HyperSqlDatabase extends JdbcDatabase { ...@@ -37,9 +38,10 @@ class HyperSqlDatabase extends JdbcDatabase {
private volatile SecretKey key = null; private volatile SecretKey key = null;
@Inject @Inject
HyperSqlDatabase(DatabaseConfig config, Clock clock) { HyperSqlDatabase(DatabaseConfig config, MessageFactory messageFactory,
Clock clock) {
super(HASH_TYPE, SECRET_TYPE, BINARY_TYPE, COUNTER_TYPE, STRING_TYPE, super(HASH_TYPE, SECRET_TYPE, BINARY_TYPE, COUNTER_TYPE, STRING_TYPE,
clock); messageFactory, clock);
this.config = config; this.config = config;
File dir = config.getDatabaseDirectory(); File dir = config.getDatabaseDirectory();
String path = new File(dir, "db").getAbsolutePath(); String path = new File(dir, "db").getAbsolutePath();
......
...@@ -21,6 +21,7 @@ import org.briarproject.bramble.api.sync.Group; ...@@ -21,6 +21,7 @@ import org.briarproject.bramble.api.sync.Group;
import org.briarproject.bramble.api.sync.Group.Visibility; import org.briarproject.bramble.api.sync.Group.Visibility;
import org.briarproject.bramble.api.sync.GroupId; import org.briarproject.bramble.api.sync.GroupId;
import org.briarproject.bramble.api.sync.Message; import org.briarproject.bramble.api.sync.Message;
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.MessageStatus; import org.briarproject.bramble.api.sync.MessageStatus;
import org.briarproject.bramble.api.sync.ValidationManager.State; import org.briarproject.bramble.api.sync.ValidationManager.State;
...@@ -61,6 +62,7 @@ import static org.briarproject.bramble.api.db.Metadata.REMOVE; ...@@ -61,6 +62,7 @@ import static org.briarproject.bramble.api.db.Metadata.REMOVE;
import static org.briarproject.bramble.api.sync.Group.Visibility.INVISIBLE; import static org.briarproject.bramble.api.sync.Group.Visibility.INVISIBLE;
import static org.briarproject.bramble.api.sync.Group.Visibility.SHARED; import static org.briarproject.bramble.api.sync.Group.Visibility.SHARED;
import static org.briarproject.bramble.api.sync.Group.Visibility.VISIBLE; import static org.briarproject.bramble.api.sync.Group.Visibility.VISIBLE;
import static org.briarproject.bramble.api.sync.SyncConstants.MESSAGE_HEADER_LENGTH;
import static org.briarproject.bramble.api.sync.ValidationManager.State.DELIVERED; import static org.briarproject.bramble.api.sync.ValidationManager.State.DELIVERED;
import static org.briarproject.bramble.api.sync.ValidationManager.State.PENDING; import static org.briarproject.bramble.api.sync.ValidationManager.State.PENDING;
import static org.briarproject.bramble.api.sync.ValidationManager.State.UNKNOWN; import static org.briarproject.bramble.api.sync.ValidationManager.State.UNKNOWN;
...@@ -305,6 +307,7 @@ abstract class JdbcDatabase implements Database<Connection> { ...@@ -305,6 +307,7 @@ abstract class JdbcDatabase implements Database<Connection> {
// Different database libraries use different names for certain types // Different database libraries use different names for certain types
private final String hashType, secretType, binaryType; private final String hashType, secretType, binaryType;
private final String counterType, stringType; private final String counterType, stringType;
private final MessageFactory messageFactory;
private final Clock clock; private final Clock clock;
// Locking: connectionsLock // Locking: connectionsLock
...@@ -320,12 +323,14 @@ abstract class JdbcDatabase implements Database<Connection> { ...@@ -320,12 +323,14 @@ abstract class JdbcDatabase implements Database<Connection> {
private final Condition connectionsChanged = connectionsLock.newCondition(); private final Condition connectionsChanged = connectionsLock.newCondition();
JdbcDatabase(String hashType, String secretType, String binaryType, JdbcDatabase(String hashType, String secretType, String binaryType,
String counterType, String stringType, Clock clock) { String counterType, String stringType,
MessageFactory messageFactory, Clock clock) {
this.hashType = hashType; this.hashType = hashType;
this.secretType = secretType; this.secretType = secretType;
this.binaryType = binaryType; this.binaryType = binaryType;
this.counterType = counterType; this.counterType = counterType;
this.stringType = stringType; this.stringType = stringType;
this.messageFactory = messageFactory;
this.clock = clock; this.clock = clock;
} }
...@@ -727,7 +732,7 @@ abstract class JdbcDatabase implements Database<Connection> { ...@@ -727,7 +732,7 @@ abstract class JdbcDatabase implements Database<Connection> {
ps.setLong(3, m.getTimestamp()); ps.setLong(3, m.getTimestamp());
ps.setInt(4, state.getValue()); ps.setInt(4, state.getValue());
ps.setBoolean(5, messageShared); ps.setBoolean(5, messageShared);
byte[] raw = m.getRaw(); byte[] raw = messageFactory.getRawMessage(m);
ps.setInt(6, raw.length); ps.setInt(6, raw.length);
ps.setBytes(7, raw); ps.setBytes(7, raw);
int affected = ps.executeUpdate(); int affected = ps.executeUpdate();
...@@ -741,7 +746,7 @@ abstract class JdbcDatabase implements Database<Connection> { ...@@ -741,7 +746,7 @@ abstract class JdbcDatabase implements Database<Connection> {
boolean offered = removeOfferedMessage(txn, c, m.getId()); boolean offered = removeOfferedMessage(txn, c, m.getId());
boolean seen = offered || (sender != null && c.equals(sender)); boolean seen = offered || (sender != null && c.equals(sender));
addStatus(txn, m.getId(), c, m.getGroupId(), m.getTimestamp(), addStatus(txn, m.getId(), c, m.getGroupId(), m.getTimestamp(),
m.getRawLength(), state, e.getValue(), messageShared, raw.length, state, e.getValue(), messageShared,
false, seen); false, seen);
} }
// Update denormalised column in messageDependencies if dependency // Update denormalised column in messageDependencies if dependency
...@@ -1501,7 +1506,10 @@ abstract class JdbcDatabase implements Database<Connection> { ...@@ -1501,7 +1506,10 @@ abstract class JdbcDatabase implements Database<Connection> {
rs.close(); rs.close();
ps.close(); ps.close();
if (raw == null) throw new MessageDeletedException(); if (raw == null) throw new MessageDeletedException();
return new Message(m, g, timestamp, raw); if (raw.length < MESSAGE_HEADER_LENGTH) throw new AssertionError();
byte[] body = new byte[raw.length - MESSAGE_HEADER_LENGTH];
System.arraycopy(raw, MESSAGE_HEADER_LENGTH, body, 0, body.length);
return new Message(m, g, timestamp, body);
} catch (SQLException e) { } catch (SQLException e) {
tryToClose(rs); tryToClose(rs);
tryToClose(ps); tryToClose(ps);
......
...@@ -39,11 +39,7 @@ class MessageFactoryImpl implements MessageFactory { ...@@ -39,11 +39,7 @@ class MessageFactoryImpl implements MessageFactory {
if (body.length > MAX_MESSAGE_BODY_LENGTH) if (body.length > MAX_MESSAGE_BODY_LENGTH)
throw new IllegalArgumentException(); throw new IllegalArgumentException();
MessageId id = getMessageId(g, timestamp, body); MessageId id = getMessageId(g, timestamp, body);
byte[] raw = new byte[MESSAGE_HEADER_LENGTH + body.length]; return new Message(id, g, timestamp, body);
System.arraycopy(g.getBytes(), 0, raw, 0, UniqueId.LENGTH);
ByteUtils.writeUint64(timestamp, raw, UniqueId.LENGTH);
System.arraycopy(body, 0, raw, MESSAGE_HEADER_LENGTH, body.length);
return new Message(id, g, timestamp, raw);
} }
private MessageId getMessageId(GroupId g, long timestamp, byte[] body) { private MessageId getMessageId(GroupId g, long timestamp, byte[] body) {
...@@ -69,18 +65,16 @@ class MessageFactoryImpl implements MessageFactory { ...@@ -69,18 +65,16 @@ class MessageFactoryImpl implements MessageFactory {
byte[] body = new byte[raw.length - MESSAGE_HEADER_LENGTH]; byte[] body = new byte[raw.length - MESSAGE_HEADER_LENGTH];
System.arraycopy(raw, MESSAGE_HEADER_LENGTH, body, 0, body.length); System.arraycopy(raw, MESSAGE_HEADER_LENGTH, body, 0, body.length);
MessageId id = getMessageId(g, timestamp, body); MessageId id = getMessageId(g, timestamp, body);
return new Message(id, g, timestamp, raw); return new Message(id, g, timestamp, body);
} }
@Override @Override
public Message createMessage(MessageId m, byte[] raw) { public byte[] getRawMessage(Message m) {
if (raw.length < MESSAGE_HEADER_LENGTH) byte[] body = m.getBody();
throw new IllegalArgumentException(); byte[] raw = new byte[MESSAGE_HEADER_LENGTH + body.length];
if (raw.length > MAX_MESSAGE_LENGTH) System.arraycopy(m.getGroupId().getBytes(), 0, raw, 0, UniqueId.LENGTH);
throw new IllegalArgumentException(); ByteUtils.writeUint64(m.getTimestamp(), raw, UniqueId.LENGTH);
byte[] groupId = new byte[UniqueId.LENGTH]; System.arraycopy(body, 0, raw, MESSAGE_HEADER_LENGTH, body.length);
System.arraycopy(raw, 0, groupId, 0, UniqueId.LENGTH); return raw;
long timestamp = ByteUtils.readUint64(raw, UniqueId.LENGTH);
return new Message(m, new GroupId(groupId), timestamp, raw);
} }
} }
...@@ -3,6 +3,7 @@ package org.briarproject.bramble.sync; ...@@ -3,6 +3,7 @@ package org.briarproject.bramble.sync;
import org.briarproject.bramble.api.nullsafety.NotNullByDefault; import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
import org.briarproject.bramble.api.record.RecordWriter; import org.briarproject.bramble.api.record.RecordWriter;
import org.briarproject.bramble.api.record.RecordWriterFactory; import org.briarproject.bramble.api.record.RecordWriterFactory;
import org.briarproject.bramble.api.sync.MessageFactory;
import org.briarproject.bramble.api.sync.SyncRecordWriter; import org.briarproject.bramble.api.sync.SyncRecordWriter;
import org.briarproject.bramble.api.sync.SyncRecordWriterFactory; import org.briarproject.bramble.api.sync.SyncRecordWriterFactory;
...@@ -13,16 +14,19 @@ import javax.inject.Inject; ...@@ -13,16 +14,19 @@ import javax.inject.Inject;
@NotNullByDefault @NotNullByDefault
class SyncRecordWriterFactoryImpl implements SyncRecordWriterFactory { class SyncRecordWriterFactoryImpl implements SyncRecordWriterFactory {
private final MessageFactory messageFactory;
private final RecordWriterFactory recordWriterFactory; private final RecordWriterFactory recordWriterFactory;
@Inject @Inject
SyncRecordWriterFactoryImpl(RecordWriterFactory recordWriterFactory) { SyncRecordWriterFactoryImpl(MessageFactory messageFactory,
RecordWriterFactory recordWriterFactory) {
this.messageFactory = messageFactory;
this.recordWriterFactory = recordWriterFactory; this.recordWriterFactory = recordWriterFactory;
} }
@Override @Override
public SyncRecordWriter createRecordWriter(OutputStream out) { public SyncRecordWriter createRecordWriter(OutputStream out) {
RecordWriter writer = recordWriterFactory.createRecordWriter(out); RecordWriter writer = recordWriterFactory.createRecordWriter(out);
return new SyncRecordWriterImpl(writer); return new SyncRecordWriterImpl(messageFactory, writer);
} }
} }
...@@ -5,6 +5,7 @@ import org.briarproject.bramble.api.record.Record; ...@@ -5,6 +5,7 @@ import org.briarproject.bramble.api.record.Record;
import org.briarproject.bramble.api.record.RecordWriter; import org.briarproject.bramble.api.record.RecordWriter;
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.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.Request; import org.briarproject.bramble.api.sync.Request;
...@@ -25,10 +26,12 @@ import static org.briarproject.bramble.api.sync.SyncConstants.PROTOCOL_VERSION; ...@@ -25,10 +26,12 @@ import static org.briarproject.bramble.api.sync.SyncConstants.PROTOCOL_VERSION;
@NotNullByDefault @NotNullByDefault
class SyncRecordWriterImpl implements SyncRecordWriter { class SyncRecordWriterImpl implements SyncRecordWriter {
private final MessageFactory messageFactory;
private final RecordWriter writer; private final RecordWriter writer;
private final ByteArrayOutputStream payload = new ByteArrayOutputStream(); private final ByteArrayOutputStream payload = new ByteArrayOutputStream();
SyncRecordWriterImpl(RecordWriter writer) { SyncRecordWriterImpl(MessageFactory messageFactory, RecordWriter writer) {
this.messageFactory = messageFactory;
this.writer = writer; this.writer = writer;
} }
...@@ -46,7 +49,8 @@ class SyncRecordWriterImpl implements SyncRecordWriter { ...@@ -46,7 +49,8 @@ class SyncRecordWriterImpl implements SyncRecordWriter {
@Override @Override
public void writeMessage(Message m) throws IOException { public void writeMessage(Message m) throws IOException {
writer.writeRecord(new Record(PROTOCOL_VERSION, MESSAGE, m.getRaw())); byte[] raw = messageFactory.getRawMessage(m);
writer.writeRecord(new Record(PROTOCOL_VERSION, MESSAGE, raw));
} }
@Override @Override
......
...@@ -16,10 +16,8 @@ import org.jmock.Expectations; ...@@ -16,10 +16,8 @@ import org.jmock.Expectations;
import org.jmock.lib.legacy.ClassImposteriser; import org.jmock.lib.legacy.ClassImposteriser;
import org.junit.Test; import org.junit.Test;
import static org.briarproject.bramble.api.sync.SyncConstants.MESSAGE_HEADER_LENGTH;
import static org.briarproject.bramble.api.transport.TransportConstants.MAX_CLOCK_DIFFERENCE; import static org.briarproject.bramble.api.transport.TransportConstants.MAX_CLOCK_DIFFERENCE;
import static org.briarproject.bramble.test.TestUtils.getMessage; import static org.briarproject.bramble.test.TestUtils.getMessage;
import static org.briarproject.bramble.test.TestUtils.getRandomBytes;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertSame; import static org.junit.Assert.assertSame;
...@@ -58,8 +56,7 @@ public class BdfMessageValidatorTest extends ValidatorTestCase { ...@@ -58,8 +56,7 @@ public class BdfMessageValidatorTest extends ValidatorTestCase {
context.checking(new Expectations() {{ context.checking(new Expectations() {{
oneOf(clock).currentTimeMillis(); oneOf(clock).currentTimeMillis();
will(returnValue(timestamp - MAX_CLOCK_DIFFERENCE)); will(returnValue(timestamp - MAX_CLOCK_DIFFERENCE));
oneOf(clientHelper).toList(raw, MESSAGE_HEADER_LENGTH, oneOf(clientHelper).toList(message.getBody());
raw.length - MESSAGE_HEADER_LENGTH);
will(returnValue(body)); will(returnValue(body));
oneOf(metadataEncoder).encode(dictionary); oneOf(metadataEncoder).encode(dictionary);
will(returnValue(meta)); will(returnValue(meta));
...@@ -84,18 +81,11 @@ public class BdfMessageValidatorTest extends ValidatorTestCase { ...@@ -84,18 +81,11 @@ public class BdfMessageValidatorTest extends ValidatorTestCase {