Moved classes from messaging package to sync package.

This commit is contained in:
akwizgran
2015-12-15 14:36:45 +00:00
parent d99df73380
commit e370cafb12
94 changed files with 905 additions and 801 deletions

View File

@@ -0,0 +1,18 @@
package org.briarproject.api.sync;
import java.util.Collection;
/** A packet acknowledging receipt of one or more {@link Message}s. */
public class Ack {
private final Collection<MessageId> acked;
public Ack(Collection<MessageId> acked) {
this.acked = acked;
}
/** Returns the identifiers of the acknowledged messages. */
public Collection<MessageId> getMessageIds() {
return acked;
}
}

View File

@@ -0,0 +1,55 @@
package org.briarproject.api.sync;
import java.io.UnsupportedEncodingException;
/** A group to which users may subscribe. */
public class Group {
private final GroupId id;
private final String name;
private final byte[] salt;
public Group(GroupId id, String name, byte[] salt) {
int length;
try {
length = name.getBytes("UTF-8").length;
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
if (length == 0 || length > MessagingConstants.MAX_GROUP_NAME_LENGTH)
throw new IllegalArgumentException();
if (salt.length != MessagingConstants.GROUP_SALT_LENGTH)
throw new IllegalArgumentException();
this.id = id;
this.name = name;
this.salt = salt;
}
/** Returns the group's unique identifier. */
public GroupId getId() {
return id;
}
/** Returns the group's name. */
public String getName() {
return name;
}
/**
* Returns the salt used to distinguish the group from other groups with
* the same name.
*/
public byte[] getSalt() {
return salt;
}
@Override
public int hashCode() {
return id.hashCode();
}
@Override
public boolean equals(Object o) {
return o instanceof Group && id.equals(((Group) o).id);
}
}

View File

@@ -0,0 +1,10 @@
package org.briarproject.api.sync;
public interface GroupFactory {
/** Creates a group with the given name and a random salt. */
Group createGroup(String name);
/** Creates a group with the given name and salt. */
Group createGroup(String name, byte[] salt);
}

View File

@@ -0,0 +1,22 @@
package org.briarproject.api.sync;
import org.briarproject.api.UniqueId;
import java.util.Arrays;
/**
* Type-safe wrapper for a byte array that uniquely identifies a {@link Group}.
*/
public class GroupId extends UniqueId {
public GroupId(byte[] id) {
super(id);
}
@Override
public boolean equals(Object o) {
if (o instanceof GroupId)
return Arrays.equals(id, ((GroupId) o).id);
return false;
}
}

View File

@@ -0,0 +1,42 @@
package org.briarproject.api.sync;
import org.briarproject.api.Author;
public interface Message {
/** Returns the message's unique identifier. */
MessageId getId();
/**
* Returns the identifier of the message's parent, or null if this is the
* first message in a thread.
*/
MessageId getParent();
/**
* Returns the {@link Group} to which the message belongs, or null if this
* is a private message.
*/
Group getGroup();
/**
* Returns the message's {@link org.briarproject.api.Author Author}, or null
* if this is an anonymous message.
*/
Author getAuthor();
/** Returns the message's content type. */
String getContentType();
/** Returns the message's timestamp in milliseconds since the Unix epoch. */
long getTimestamp();
/** Returns the serialised message. */
byte[] getSerialised();
/** Returns the offset of the message body within the serialised message. */
int getBodyStart();
/** Returns the length of the message body in bytes. */
int getBodyLength();
}

View File

@@ -0,0 +1,19 @@
package org.briarproject.api.sync;
import org.briarproject.api.Author;
import org.briarproject.api.crypto.PrivateKey;
import java.io.IOException;
import java.security.GeneralSecurityException;
public interface MessageFactory {
Message createAnonymousMessage(MessageId parent, org.briarproject.api.sync.Group group,
String contentType, long timestamp, byte[] body) throws IOException,
GeneralSecurityException;
Message createPseudonymousMessage(MessageId parent, org.briarproject.api.sync.Group group,
Author author, PrivateKey privateKey, String contentType,
long timestamp, byte[] body) throws IOException,
GeneralSecurityException;
}

View File

@@ -0,0 +1,23 @@
package org.briarproject.api.sync;
import org.briarproject.api.UniqueId;
import java.util.Arrays;
/**
* Type-safe wrapper for a byte array that uniquely identifies a
* {@link Message}.
*/
public class MessageId extends UniqueId {
public MessageId(byte[] id) {
super(id);
}
@Override
public boolean equals(Object o) {
if (o instanceof MessageId)
return Arrays.equals(id, ((MessageId) o).id);
return false;
}
}

View File

@@ -0,0 +1,9 @@
package org.briarproject.api.sync;
import java.security.GeneralSecurityException;
/** Verifies the signatures on an {@link UnverifiedMessage}. */
public interface MessageVerifier {
Message verifyMessage(UnverifiedMessage m) throws GeneralSecurityException;
}

View File

@@ -0,0 +1,46 @@
package org.briarproject.api.sync;
public interface MessagingConstants {
/** The current version of the messaging protocol. */
byte PROTOCOL_VERSION = 0;
/** The length of the packet header in bytes. */
int HEADER_LENGTH = 4;
/** The maximum length of the packet payload in bytes. */
int MAX_PAYLOAD_LENGTH = 32 * 1024; // 32 KiB
/** The maximum number of public groups a user may subscribe to. */
int MAX_SUBSCRIPTIONS = 300;
/** The maximum length of a group's name in UTF-8 bytes. */
int MAX_GROUP_NAME_LENGTH = 50;
/** The length of a group's random salt in bytes. */
int GROUP_SALT_LENGTH = 32;
/**
* The maximum length of a message body in bytes. To allow for future
* changes in the protocol, this is smaller than the maximum payload length
* even when all the message's other fields have their maximum lengths.
*/
int MAX_BODY_LENGTH = MAX_PAYLOAD_LENGTH - 1024;
/** The maximum length of a message's content type in UTF-8 bytes. */
int MAX_CONTENT_TYPE_LENGTH = 50;
/** The maximum length of a message's subject line in UTF-8 bytes. */
int MAX_SUBJECT_LENGTH = 100;
/** The length of a message's random salt in bytes. */
int MESSAGE_SALT_LENGTH = 32;
/**
* When calculating the retention time of the database, the timestamp of
* the oldest message in the database is rounded down to a multiple of
* this value to avoid revealing the presence of any particular message.
*/
int RETENTION_GRANULARITY = 60 * 1000; // 1 minute
}

View File

@@ -0,0 +1,18 @@
package org.briarproject.api.sync;
import java.io.IOException;
public interface MessagingSession {
/**
* Runs the session. This method returns when there are no more packets to
* send or receive, or when the {@link #interrupt()} method has been called.
*/
void run() throws IOException;
/**
* Interrupts the session, causing the {@link #run()} method to return at
* the next opportunity.
*/
void interrupt();
}

View File

@@ -0,0 +1,19 @@
package org.briarproject.api.sync;
import org.briarproject.api.ContactId;
import org.briarproject.api.TransportId;
import java.io.InputStream;
import java.io.OutputStream;
public interface MessagingSessionFactory {
MessagingSession createIncomingSession(ContactId c, TransportId t,
InputStream in);
MessagingSession createSimplexOutgoingSession(ContactId c, TransportId t,
int maxLatency, OutputStream out);
MessagingSession createDuplexOutgoingSession(ContactId c, TransportId t,
int maxLatency, int maxIdleTime, OutputStream out);
}

View File

@@ -0,0 +1,18 @@
package org.briarproject.api.sync;
import java.util.Collection;
/** A packet offering the recipient one or more {@link Message}s. */
public class Offer {
private final Collection<MessageId> offered;
public Offer(Collection<MessageId> offered) {
this.offered = offered;
}
/** Returns the identifiers of the offered messages. */
public Collection<MessageId> getMessageIds() {
return offered;
}
}

View File

@@ -0,0 +1,38 @@
package org.briarproject.api.sync;
import java.io.IOException;
public interface PacketReader {
boolean eof() throws IOException;
boolean hasAck() throws IOException;
Ack readAck() throws IOException;
boolean hasMessage() throws IOException;
UnverifiedMessage readMessage() throws IOException;
boolean hasOffer() throws IOException;
Offer readOffer() throws IOException;
boolean hasRequest() throws IOException;
Request readRequest() throws IOException;
boolean hasRetentionAck() throws IOException;
org.briarproject.api.sync.RetentionAck readRetentionAck() throws IOException;
boolean hasRetentionUpdate() throws IOException;
org.briarproject.api.sync.RetentionUpdate readRetentionUpdate() throws IOException;
boolean hasSubscriptionAck() throws IOException;
org.briarproject.api.sync.SubscriptionAck readSubscriptionAck() throws IOException;
boolean hasSubscriptionUpdate() throws IOException;
org.briarproject.api.sync.SubscriptionUpdate readSubscriptionUpdate() throws IOException;
boolean hasTransportAck() throws IOException;
TransportAck readTransportAck() throws IOException;
boolean hasTransportUpdate() throws IOException;
org.briarproject.api.sync.TransportUpdate readTransportUpdate() throws IOException;
}

View File

@@ -0,0 +1,8 @@
package org.briarproject.api.sync;
import java.io.InputStream;
public interface PacketReaderFactory {
PacketReader createPacketReader(InputStream in);
}

View File

@@ -0,0 +1,16 @@
package org.briarproject.api.sync;
/** Packet types for the messaging protocol. */
public interface PacketTypes {
byte ACK = 0;
byte MESSAGE = 1;
byte OFFER = 2;
byte REQUEST = 3;
byte RETENTION_ACK = 4;
byte RETENTION_UPDATE = 5;
byte SUBSCRIPTION_ACK = 6;
byte SUBSCRIPTION_UPDATE = 7;
byte TRANSPORT_ACK = 8;
byte TRANSPORT_UPDATE = 9;
}

View File

@@ -0,0 +1,34 @@
package org.briarproject.api.sync;
import java.io.IOException;
public interface PacketWriter {
int getMaxMessagesForAck(long capacity);
int getMaxMessagesForRequest(long capacity);
int getMaxMessagesForOffer(long capacity);
void writeAck(Ack a) throws IOException;
void writeMessage(byte[] raw) throws IOException;
void writeOffer(org.briarproject.api.sync.Offer o) throws IOException;
void writeRequest(Request r) throws IOException;
void writeRetentionAck(org.briarproject.api.sync.RetentionAck a) throws IOException;
void writeRetentionUpdate(org.briarproject.api.sync.RetentionUpdate u) throws IOException;
void writeSubscriptionAck(org.briarproject.api.sync.SubscriptionAck a) throws IOException;
void writeSubscriptionUpdate(org.briarproject.api.sync.SubscriptionUpdate u) throws IOException;
void writeTransportAck(org.briarproject.api.sync.TransportAck a) throws IOException;
void writeTransportUpdate(org.briarproject.api.sync.TransportUpdate u) throws IOException;
void flush() throws IOException;
}

View File

@@ -0,0 +1,8 @@
package org.briarproject.api.sync;
import java.io.OutputStream;
public interface PacketWriterFactory {
PacketWriter createPacketWriter(OutputStream out);
}

View File

@@ -0,0 +1,18 @@
package org.briarproject.api.sync;
import java.util.Collection;
/** A packet requesting one or more {@link Message}s from the recipient. */
public class Request {
private final Collection<org.briarproject.api.sync.MessageId> requested;
public Request(Collection<org.briarproject.api.sync.MessageId> requested) {
this.requested = requested;
}
/** Returns the identifiers of the requested messages. */
public Collection<org.briarproject.api.sync.MessageId> getMessageIds() {
return requested;
}
}

View File

@@ -0,0 +1,16 @@
package org.briarproject.api.sync;
/** A packet acknowledging a (@link RetentionUpdate} */
public class RetentionAck {
private final long version;
public RetentionAck(long version) {
this.version = version;
}
/** Returns the version number of the acknowledged update. */
public long getVersion() {
return version;
}
}

View File

@@ -0,0 +1,23 @@
package org.briarproject.api.sync;
/**
* A packet updating the recipient's view of the retention time of the sender's
* database.
*/
public class RetentionUpdate {
private final long retention, version;
public RetentionUpdate(long retention, long version) {
this.retention = retention;
this.version = version;
}
public long getRetentionTime() {
return retention;
}
public long getVersion() {
return version;
}
}

View File

@@ -0,0 +1,16 @@
package org.briarproject.api.sync;
/** A packet acknowledging a {@link SubscriptionUpdate}. */
public class SubscriptionAck {
private final long version;
public SubscriptionAck(long version) {
this.version = version;
}
/** Returns the version number of the acknowledged update. */
public long getVersion() {
return version;
}
}

View File

@@ -0,0 +1,28 @@
package org.briarproject.api.sync;
import java.util.Collection;
/** A packet updating the recipient's view of the sender's subscriptions. */
public class SubscriptionUpdate {
private final Collection<Group> groups;
private final long version;
public SubscriptionUpdate(Collection<Group> groups, long version) {
this.groups = groups;
this.version = version;
}
/**
* Returns the groups to which the sender subscribes, and which the sender
* has made visible to the recipient.
*/
public Collection<Group> getGroups() {
return groups;
}
/** Returns the update's version number. */
public long getVersion() {
return version;
}
}

View File

@@ -0,0 +1,25 @@
package org.briarproject.api.sync;
import org.briarproject.api.TransportId;
/** A packet acknowledging a {@link TransportUpdate}. */
public class TransportAck {
private final TransportId id;
private final long version;
public TransportAck(TransportId id, long version) {
this.id = id;
this.version = version;
}
/** Returns the identifier of the updated transport. */
public TransportId getId() {
return id;
}
/** Returns the version number of the acknowledged update. */
public long getVersion() {
return version;
}
}

View File

@@ -0,0 +1,36 @@
package org.briarproject.api.sync;
import org.briarproject.api.TransportId;
import org.briarproject.api.TransportProperties;
/**
* A packet updating the recipient's view of the sender's transport properties.
*/
public class TransportUpdate {
private final TransportId id;
private final TransportProperties properties;
private final long version;
public TransportUpdate(TransportId id, TransportProperties properties,
long version) {
this.id = id;
this.properties = properties;
this.version = version;
}
/** Returns the identifier of the updated transport. */
public TransportId getId() {
return id;
}
/** Returns the transport's updated properties. */
public TransportProperties getProperties() {
return properties;
}
/** Returns the update's version number. */
public long getVersion() {
return version;
}
}

View File

@@ -0,0 +1,94 @@
package org.briarproject.api.sync;
import org.briarproject.api.Author;
/** A {@link Message} that has not yet had its signatures (if any) verified. */
public class UnverifiedMessage {
private final MessageId parent;
private final org.briarproject.api.sync.Group group;
private final Author author;
private final String contentType;
private final long timestamp;
private final byte[] raw, signature;
private final int bodyStart, bodyLength, signedLength;
public UnverifiedMessage(MessageId parent, org.briarproject.api.sync.Group group, Author author,
String contentType, long timestamp, byte[] raw, byte[] signature,
int bodyStart, int bodyLength, int signedLength) {
this.parent = parent;
this.group = group;
this.author = author;
this.contentType = contentType;
this.timestamp = timestamp;
this.raw = raw;
this.signature = signature;
this.bodyStart = bodyStart;
this.bodyLength = bodyLength;
this.signedLength = signedLength;
}
/**
* Returns the identifier of the message's parent, or null if this is the
* first message in a thread.
*/
public MessageId getParent() {
return parent;
}
/**
* Returns the {@link org.briarproject.api.sync.Group} to which the message belongs, or null if this
* is a private message.
*/
public org.briarproject.api.sync.Group getGroup() {
return group;
}
/**
* Returns the message's {@link org.briarproject.api.Author Author}, or null
* if this is an anonymous message.
*/
public Author getAuthor() {
return author;
}
/** Returns the message's content type. */
public String getContentType() {
return contentType;
}
/** Returns the message's timestamp. */
public long getTimestamp() {
return timestamp;
}
/** Returns the serialised message. */
public byte[] getSerialised() {
return raw;
}
/**
* Returns the author's signature, or null if this is an anonymous message.
*/
public byte[] getSignature() {
return signature;
}
/** Returns the offset of the message body within the serialised message. */
public int getBodyStart() {
return bodyStart;
}
/** Returns the length of the message body in bytes. */
public int getBodyLength() {
return bodyLength;
}
/**
* Returns the length in bytes of the data covered by the author's
* signature.
*/
public int getSignedLength() {
return signedLength;
}
}