Separated MessageHeader interface from Message interface.

This commit is contained in:
akwizgran
2011-10-21 18:42:27 +01:00
parent f2d80825bc
commit ef61eaa804
9 changed files with 69 additions and 44 deletions

View File

@@ -1,6 +1,8 @@
package net.sf.briar.api.protocol;
public interface Message {
import java.io.InputStream;
public interface Message extends MessageHeader {
/**
* The maximum length of a message body in bytes. To allow for future
@@ -19,30 +21,15 @@ public interface Message {
/** The length of the random salt in bytes. */
static final int SALT_LENGTH = 8;
/** Returns the message's unique identifier. */
MessageId getId();
/** Returns the length of the message in bytes. */
int getLength();
/** Returns the serialised representation of the entire message. */
byte[] getSerialisedBytes();
/**
* Returns the message's parent, or MessageId.NONE if this is the first
* message in a thread.
* Returns a stream for reading the serialised representation of the entire
* message.
*/
MessageId getParent();
/** Returns the group to which the message belongs. */
GroupId getGroup();
/** Returns the message's author. */
AuthorId getAuthor();
/** Returns the message's subject line. */
String getSubject();
/** Returns the timestamp created by the message's author. */
long getTimestamp();
/** Returns the size of the message in bytes. */
int getSize();
/** Returns the serialised representation of the message. */
byte[] getBytes();
InputStream getSerialisedStream();
}

View File

@@ -0,0 +1,25 @@
package net.sf.briar.api.protocol;
public interface MessageHeader {
/** Returns the message's unique identifier. */
MessageId getId();
/**
* Returns the message's parent, or MessageId.NONE if this is the first
* message in a thread.
*/
MessageId getParent();
/** Returns the group to which the message belongs. */
GroupId getGroup();
/** Returns the message's author. */
AuthorId getAuthor();
/** Returns the message's subject line. */
String getSubject();
/** Returns the timestamp created by the message's author. */
long getTimestamp();
}

View File

@@ -236,7 +236,7 @@ DatabaseCleaner.Callback {
if(sendability > 0) updateAncestorSendability(txn, id, true);
// Count the bytes stored
synchronized(spaceLock) {
bytesStoredSinceLastCheck += m.getSize();
bytesStoredSinceLastCheck += m.getLength();
}
}
return stored;
@@ -344,7 +344,7 @@ DatabaseCleaner.Callback {
else db.setStatus(txn, c, id, Status.NEW);
// Count the bytes stored
synchronized(spaceLock) {
bytesStoredSinceLastCheck += m.getSize();
bytesStoredSinceLastCheck += m.getLength();
}
return true;
}

View File

@@ -1,6 +1,5 @@
package net.sf.briar.db;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.sql.Connection;
@@ -549,9 +548,9 @@ abstract class JdbcDatabase implements Database<Connection> {
if(m.getAuthor() == null) ps.setNull(4, Types.BINARY);
else ps.setBytes(4, m.getAuthor().getBytes());
ps.setLong(5, m.getTimestamp());
ps.setInt(6, m.getSize());
byte[] raw = m.getBytes();
ps.setBinaryStream(7, new ByteArrayInputStream(raw), raw.length);
int length = m.getLength();
ps.setInt(6, length);
ps.setBinaryStream(7, m.getSerialisedStream(), length);
int affected = ps.executeUpdate();
if(affected != 1) throw new DbStateException();
ps.close();
@@ -635,9 +634,9 @@ abstract class JdbcDatabase implements Database<Connection> {
if(m.getParent() == null) ps.setNull(2, Types.BINARY);
else ps.setBytes(2, m.getParent().getBytes());
ps.setLong(3, m.getTimestamp());
ps.setInt(4, m.getSize());
byte[] raw = m.getBytes();
ps.setBinaryStream(5, new ByteArrayInputStream(raw), raw.length);
int length = m.getLength();
ps.setInt(4, length);
ps.setBinaryStream(5, m.getSerialisedStream(), length);
ps.setInt(6, c.getInt());
int affected = ps.executeUpdate();
if(affected != 1) throw new DbStateException();

View File

@@ -1,5 +1,8 @@
package net.sf.briar.protocol;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import net.sf.briar.api.protocol.AuthorId;
import net.sf.briar.api.protocol.GroupId;
import net.sf.briar.api.protocol.Message;
@@ -50,14 +53,18 @@ class MessageImpl implements Message {
return timestamp;
}
public int getSize() {
public int getLength() {
return raw.length;
}
public byte[] getBytes() {
public byte[] getSerialisedBytes() {
return raw;
}
public InputStream getSerialisedStream() {
return new ByteArrayInputStream(raw);
}
@Override
public boolean equals(Object o) {
return o instanceof Message && id.equals(((Message) o).getId());

View File

@@ -142,10 +142,10 @@ public class ProtocolIntegrationTest extends TestCase {
a.finish();
BatchWriter b = protocolWriterFactory.createBatchWriter(out1);
assertTrue(b.writeMessage(message.getBytes()));
assertTrue(b.writeMessage(message1.getBytes()));
assertTrue(b.writeMessage(message2.getBytes()));
assertTrue(b.writeMessage(message3.getBytes()));
assertTrue(b.writeMessage(message.getSerialisedBytes()));
assertTrue(b.writeMessage(message1.getSerialisedBytes()));
assertTrue(b.writeMessage(message2.getSerialisedBytes()));
assertTrue(b.writeMessage(message3.getSerialisedBytes()));
b.finish();
OfferWriter o = protocolWriterFactory.createOfferWriter(out1);
@@ -255,6 +255,6 @@ public class ProtocolIntegrationTest extends TestCase {
assertEquals(m1.getGroup(), m2.getGroup());
assertEquals(m1.getAuthor(), m2.getAuthor());
assertEquals(m1.getTimestamp(), m2.getTimestamp());
assertArrayEquals(m1.getBytes(), m2.getBytes());
assertArrayEquals(m1.getSerialisedBytes(), m2.getSerialisedBytes());
}
}

View File

@@ -1,5 +1,8 @@
package net.sf.briar.db;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import net.sf.briar.api.protocol.AuthorId;
import net.sf.briar.api.protocol.GroupId;
import net.sf.briar.api.protocol.Message;
@@ -49,14 +52,18 @@ class TestMessage implements Message {
return timestamp;
}
public int getSize() {
public int getLength() {
return raw.length;
}
public byte[] getBytes() {
public byte[] getSerialisedBytes() {
return raw;
}
public InputStream getSerialisedStream() {
return new ByteArrayInputStream(raw);
}
@Override
public boolean equals(Object o) {
return o instanceof Message && id.equals(((Message)o).getId());

View File

@@ -87,7 +87,7 @@ public class ProtocolReadWriteTest extends TestCase {
a.finish();
BatchWriter b = writerFactory.createBatchWriter(out);
b.writeMessage(message.getBytes());
b.writeMessage(message.getSerialisedBytes());
b.finish();
OfferWriter o = writerFactory.createOfferWriter(out);

View File

@@ -113,7 +113,7 @@ public class ConstantsTest extends TestCase {
ProtocolConstants.MAX_PACKET_LENGTH);
BatchWriter b = new BatchWriterImpl(out, serial, writerFactory,
crypto.getMessageDigest());
assertTrue(b.writeMessage(message.getBytes()));
assertTrue(b.writeMessage(message.getSerialisedBytes()));
b.finish();
// Check the size of the serialised batch
assertTrue(out.size() > UniqueId.LENGTH + Group.MAX_NAME_LENGTH +