Refactor ValidationManager and fix some bugs. #619

This commit is contained in:
akwizgran
2016-09-08 14:57:41 +01:00
parent fd4275733f
commit 8a3e5bfb50
34 changed files with 978 additions and 922 deletions

View File

@@ -3,22 +3,24 @@ package org.briarproject.api.clients;
import org.briarproject.api.data.BdfDictionary;
import org.briarproject.api.sync.BaseMessageContext;
import org.briarproject.api.sync.MessageId;
import org.jetbrains.annotations.NotNull;
import java.util.Collection;
import java.util.Collections;
public class BdfMessageContext extends BaseMessageContext {
private final BdfDictionary dictionary;
public BdfMessageContext(BdfDictionary dictionary,
Collection<MessageId> dependencies) {
public BdfMessageContext(@NotNull BdfDictionary dictionary,
@NotNull Collection<MessageId> dependencies) {
super(dependencies);
this.dictionary = dictionary;
}
public BdfMessageContext(BdfDictionary dictionary) {
this(dictionary, null);
public BdfMessageContext(@NotNull BdfDictionary dictionary) {
this(dictionary, Collections.<MessageId>emptyList());
}
public BdfDictionary getDictionary() {

View File

@@ -5,7 +5,6 @@ import org.briarproject.api.data.BdfDictionary;
import org.briarproject.api.data.BdfList;
import org.briarproject.api.db.DbException;
import org.briarproject.api.db.Transaction;
import org.briarproject.api.sync.ClientId;
import org.briarproject.api.sync.GroupId;
import org.briarproject.api.sync.Message;
import org.briarproject.api.sync.MessageId;
@@ -15,12 +14,11 @@ import java.util.Map;
public interface ClientHelper {
void addLocalMessage(Message m, ClientId c, BdfDictionary metadata,
boolean shared) throws DbException, FormatException;
void addLocalMessage(Message m, BdfDictionary metadata, boolean shared)
throws DbException, FormatException;
void addLocalMessage(Transaction txn, Message m, ClientId c,
BdfDictionary metadata, boolean shared) throws DbException,
FormatException;
void addLocalMessage(Transaction txn, Message m, BdfDictionary metadata,
boolean shared) throws DbException, FormatException;
Message createMessage(GroupId g, long timestamp, BdfDictionary body)
throws FormatException;

View File

@@ -18,6 +18,7 @@ import org.briarproject.api.sync.MessageStatus;
import org.briarproject.api.sync.Offer;
import org.briarproject.api.sync.Request;
import org.briarproject.api.transport.TransportKeys;
import org.jetbrains.annotations.Nullable;
import java.util.Collection;
import java.util.Map;
@@ -77,7 +78,7 @@ public interface DatabaseComponent {
/**
* Stores a local message.
*/
void addLocalMessage(Transaction txn, Message m, ClientId c, Metadata meta,
void addLocalMessage(Transaction txn, Message m, Metadata meta,
boolean shared) throws DbException;
/**
@@ -125,6 +126,7 @@ public interface DatabaseComponent {
* Returns an acknowledgement for the given contact, or null if there are
* no messages to acknowledge.
*/
@Nullable
Ack generateAck(Transaction txn, ContactId c, int maxMessages)
throws DbException;
@@ -134,6 +136,7 @@ public interface DatabaseComponent {
* transport with the given maximum latency. Returns null if there are no
* sendable messages that fit in the given length.
*/
@Nullable
Collection<byte[]> generateBatch(Transaction txn, ContactId c,
int maxLength, int maxLatency) throws DbException;
@@ -142,6 +145,7 @@ public interface DatabaseComponent {
* transport with the given maximum latency, or null if there are no
* messages to offer.
*/
@Nullable
Offer generateOffer(Transaction txn, ContactId c, int maxMessages,
int maxLatency) throws DbException;
@@ -149,6 +153,7 @@ public interface DatabaseComponent {
* Returns a request for the given contact, or null if there are no
* messages to request.
*/
@Nullable
Request generateRequest(Transaction txn, ContactId c, int maxMessages)
throws DbException;
@@ -159,6 +164,7 @@ public interface DatabaseComponent {
* requested by the contact are returned. Returns null if there are no
* sendable messages that fit in the given length.
*/
@Nullable
Collection<byte[]> generateRequestedBatch(Transaction txn, ContactId c,
int maxLength, int maxLatency) throws DbException;
@@ -244,17 +250,8 @@ public interface DatabaseComponent {
throws DbException;
/**
* Returns the IDs of any messages that need to be delivered to the given
* client.
* <p/>
* Read-only.
*/
Collection<MessageId> getMessagesToDeliver(Transaction txn, ClientId c)
throws DbException;
/**
* Returns the IDs of any messages that are still pending due to
* dependencies to other messages for the given client.
* Returns the IDs of any messages that are valid but pending delivery due
* to dependencies on other messages for the given client.
* <p/>
* Read-only.
*/
@@ -267,6 +264,7 @@ public interface DatabaseComponent {
* <p/>
* Read-only.
*/
@Nullable
byte[] getRawMessage(Transaction txn, MessageId m) throws DbException;
/**
@@ -314,7 +312,13 @@ public interface DatabaseComponent {
GroupId g) throws DbException;
/**
* Returns the dependencies of the given message.
* Returns the IDs and states of all dependencies of the given message.
* Missing dependencies have the state {@link
* org.briarproject.api.sync.ValidationManager.State UNKNOWN}.
* Dependencies in other groups have the state {@link
* org.briarproject.api.sync.ValidationManager.State INVALID}.
* Note that these states are not set on the dependencies themselves; the
* returned states should only be taken in the context of the given message.
* <p/>
* Read-only.
*/
@@ -323,12 +327,21 @@ public interface DatabaseComponent {
/**
* Returns all IDs of messages that depend on the given message.
* Messages in other groups that declare a dependency on the given message
* will be returned even though such dependencies are invalid.
* <p/>
* Read-only.
*/
Map<MessageId, State> getMessageDependents(Transaction txn, MessageId m)
throws DbException;
/**
* Gets the validation and delivery state of the given message.
* <p/>
* Read-only.
*/
State getMessageState(Transaction txn, MessageId m) throws DbException;
/**
* Returns the status of the given message with respect to the given
* contact.
@@ -449,9 +462,9 @@ public interface DatabaseComponent {
throws DbException;
/**
* Sets the state of the message with respect to validation and delivery.
* Sets the validation and delivery state of the given message.
*/
void setMessageState(Transaction txn, Message m, ClientId c, State valid)
void setMessageState(Transaction txn, MessageId m, State state)
throws DbException;
/**

View File

@@ -1,8 +1,7 @@
package org.briarproject.api.event;
import org.briarproject.api.sync.ClientId;
import org.briarproject.api.sync.Message;
import org.briarproject.api.sync.ValidationManager;
import org.briarproject.api.sync.MessageId;
import static org.briarproject.api.sync.ValidationManager.State;
/**
@@ -10,25 +9,19 @@ import static org.briarproject.api.sync.ValidationManager.State;
*/
public class MessageStateChangedEvent extends Event {
private final Message message;
private final ClientId clientId;
private final MessageId messageId;
private final boolean local;
private final State state;
public MessageStateChangedEvent(Message message, ClientId clientId,
boolean local, State state) {
this.message = message;
this.clientId = clientId;
public MessageStateChangedEvent(MessageId messageId, boolean local,
State state) {
this.messageId = messageId;
this.local = local;
this.state = state;
}
public Message getMessage() {
return message;
}
public ClientId getClientId() {
return clientId;
public MessageId getMessageId() {
return messageId;
}
public boolean isLocal() {

View File

@@ -1,12 +1,14 @@
package org.briarproject.api.sync;
import org.jetbrains.annotations.NotNull;
import java.util.Collection;
public abstract class BaseMessageContext {
private final Collection<MessageId> dependencies;
public BaseMessageContext(Collection<MessageId> dependencies) {
public BaseMessageContext(@NotNull Collection<MessageId> dependencies) {
this.dependencies = dependencies;
}

View File

@@ -1,22 +1,24 @@
package org.briarproject.api.sync;
import org.briarproject.api.db.Metadata;
import org.jetbrains.annotations.NotNull;
import java.util.Collection;
import java.util.Collections;
public class MessageContext extends BaseMessageContext {
private final Metadata metadata;
public MessageContext(Metadata metadata,
Collection<MessageId> dependencies) {
public MessageContext(@NotNull Metadata metadata,
@NotNull Collection<MessageId> dependencies) {
super(dependencies);
this.metadata = metadata;
}
public MessageContext(Metadata metadata) {
this(metadata, null);
public MessageContext(@NotNull Metadata metadata) {
this(metadata, Collections.<MessageId>emptyList());
}
public Metadata getMetadata() {

View File

@@ -12,7 +12,7 @@ public interface ValidationManager {
enum State {
UNKNOWN(0), INVALID(1), PENDING(2), VALID(3), DELIVERED(4);
UNKNOWN(0), INVALID(1), PENDING(2), DELIVERED(3);
private final int value;