Unverified Commit c9276de3 authored by akwizgran's avatar akwizgran
Browse files

Renamed validation hooks to incoming message hooks.

parent 89606442
......@@ -30,14 +30,19 @@ public interface ValidationManager {
}
}
/** Sets the message validator for the given client. */
/**
* Sets the message validator for the given client.
*/
void registerMessageValidator(ClientId c, MessageValidator v);
/** Registers a hook to be called whenever a message is validated. */
void registerValidationHook(ValidationHook hook);
/**
* Sets the incoming message hook for the given client. The hook will be
* called once for each incoming message that passes validation.
*/
void registerIncomingMessageHook(ClientId c, IncomingMessageHook hook);
interface ValidationHook {
void validatingMessage(Transaction txn, Message m, ClientId c,
Metadata meta) throws DbException;
interface IncomingMessageHook {
void incomingMessage(Transaction txn, Message m, Metadata meta)
throws DbException;
}
}
......@@ -53,7 +53,8 @@ public class ForumModule extends AbstractModule {
ForumSharingManagerImpl forumSharingManager) {
contactManager.registerAddContactHook(forumSharingManager);
contactManager.registerRemoveContactHook(forumSharingManager);
validationManager.registerValidationHook(forumSharingManager);
validationManager.registerIncomingMessageHook(
ForumSharingManagerImpl.CLIENT_ID, forumSharingManager);
return forumSharingManager;
}
}
......@@ -24,7 +24,7 @@ import org.briarproject.api.sync.GroupFactory;
import org.briarproject.api.sync.GroupId;
import org.briarproject.api.sync.Message;
import org.briarproject.api.sync.MessageId;
import org.briarproject.api.sync.ValidationManager.ValidationHook;
import org.briarproject.api.sync.ValidationManager.IncomingMessageHook;
import org.briarproject.api.system.Clock;
import org.briarproject.util.StringUtils;
......@@ -44,7 +44,7 @@ import static org.briarproject.api.forum.ForumConstants.MAX_FORUM_NAME_LENGTH;
import static org.briarproject.api.sync.SyncConstants.MESSAGE_HEADER_LENGTH;
class ForumSharingManagerImpl implements ForumSharingManager, AddContactHook,
RemoveContactHook, ValidationHook {
RemoveContactHook, IncomingMessageHook {
static final ClientId CLIENT_ID = new ClientId(StringUtils.fromHexString(
"cd11a5d04dccd9e2931d6fc3df456313"
......@@ -103,15 +103,13 @@ class ForumSharingManagerImpl implements ForumSharingManager, AddContactHook,
}
@Override
public void validatingMessage(Transaction txn, Message m, ClientId c,
Metadata meta) throws DbException {
if (c.equals(CLIENT_ID)) {
try {
ContactId contactId = getContactId(txn, m.getGroupId());
setForumVisibility(txn, contactId, getVisibleForums(txn, m));
} catch (FormatException e) {
throw new DbException(e);
}
public void incomingMessage(Transaction txn, Message m, Metadata meta)
throws DbException {
try {
ContactId contactId = getContactId(txn, m.getGroupId());
setForumVisibility(txn, contactId, getVisibleForums(txn, m));
} catch (FormatException e) {
throw new DbException(e);
}
}
......
......@@ -25,11 +25,9 @@ import org.briarproject.api.sync.ValidationManager;
import org.briarproject.util.ByteUtils;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.Executor;
import java.util.logging.Logger;
......@@ -46,7 +44,7 @@ class ValidationManagerImpl implements ValidationManager, Service,
private final Executor dbExecutor;
private final Executor cryptoExecutor;
private final Map<ClientId, MessageValidator> validators;
private final List<ValidationHook> hooks;
private final Map<ClientId, IncomingMessageHook> hooks;
@Inject
ValidationManagerImpl(DatabaseComponent db,
......@@ -56,7 +54,7 @@ class ValidationManagerImpl implements ValidationManager, Service,
this.dbExecutor = dbExecutor;
this.cryptoExecutor = cryptoExecutor;
validators = new ConcurrentHashMap<ClientId, MessageValidator>();
hooks = new CopyOnWriteArrayList<ValidationHook>();
hooks = new ConcurrentHashMap<ClientId, IncomingMessageHook>();
}
@Override
......@@ -76,8 +74,8 @@ class ValidationManagerImpl implements ValidationManager, Service,
}
@Override
public void registerValidationHook(ValidationHook hook) {
hooks.add(hook);
public void registerIncomingMessageHook(ClientId c, IncomingMessageHook hook) {
hooks.put(c, hook);
}
private void getMessagesToValidate(final ClientId c) {
......@@ -170,8 +168,9 @@ class ValidationManagerImpl implements ValidationManager, Service,
db.mergeMessageMetadata(txn, m.getId(), meta);
db.setMessageValid(txn, m, c, true);
db.setMessageShared(txn, m, true);
for (ValidationHook hook : hooks)
hook.validatingMessage(txn, m, c, meta);
IncomingMessageHook hook = hooks.get(c);
if (hook != null)
hook.incomingMessage(txn, m, meta);
}
txn.setComplete();
} finally {
......
......@@ -17,7 +17,7 @@ import org.briarproject.api.sync.GroupId;
import org.briarproject.api.sync.Message;
import org.briarproject.api.sync.MessageId;
import org.briarproject.api.sync.MessageValidator;
import org.briarproject.api.sync.ValidationManager.ValidationHook;
import org.briarproject.api.sync.ValidationManager.IncomingMessageHook;
import org.briarproject.util.ByteUtils;
import org.jmock.Expectations;
import org.jmock.Mockery;
......@@ -56,7 +56,7 @@ public class ValidationManagerImplTest extends BriarTestCase {
final Executor dbExecutor = new ImmediateExecutor();
final Executor cryptoExecutor = new ImmediateExecutor();
final MessageValidator validator = context.mock(MessageValidator.class);
final ValidationHook hook = context.mock(ValidationHook.class);
final IncomingMessageHook hook = context.mock(IncomingMessageHook.class);
final Transaction txn = new Transaction(null);
final Transaction txn1 = new Transaction(null);
final Transaction txn2 = new Transaction(null);
......@@ -87,7 +87,7 @@ public class ValidationManagerImplTest extends BriarTestCase {
oneOf(db).setMessageValid(txn2, message, clientId, true);
oneOf(db).setMessageShared(txn2, message, true);
// Call the hook for the first message
oneOf(hook).validatingMessage(txn2, message, clientId, metadata);
oneOf(hook).incomingMessage(txn2, message, metadata);
oneOf(db).endTransaction(txn2);
// Load the second raw message and group
oneOf(db).startTransaction();
......@@ -110,7 +110,7 @@ public class ValidationManagerImplTest extends BriarTestCase {
ValidationManagerImpl vm = new ValidationManagerImpl(db, dbExecutor,
cryptoExecutor);
vm.registerMessageValidator(clientId, validator);
vm.registerValidationHook(hook);
vm.registerIncomingMessageHook(clientId, hook);
vm.start();
context.assertIsSatisfied();
......@@ -124,7 +124,7 @@ public class ValidationManagerImplTest extends BriarTestCase {
final Executor dbExecutor = new ImmediateExecutor();
final Executor cryptoExecutor = new ImmediateExecutor();
final MessageValidator validator = context.mock(MessageValidator.class);
final ValidationHook hook = context.mock(ValidationHook.class);
final IncomingMessageHook hook = context.mock(IncomingMessageHook.class);
final Transaction txn = new Transaction(null);
final Transaction txn1 = new Transaction(null);
final Transaction txn2 = new Transaction(null);
......@@ -163,7 +163,7 @@ public class ValidationManagerImplTest extends BriarTestCase {
ValidationManagerImpl vm = new ValidationManagerImpl(db, dbExecutor,
cryptoExecutor);
vm.registerMessageValidator(clientId, validator);
vm.registerValidationHook(hook);
vm.registerIncomingMessageHook(clientId, hook);
vm.start();
context.assertIsSatisfied();
......@@ -177,7 +177,7 @@ public class ValidationManagerImplTest extends BriarTestCase {
final Executor dbExecutor = new ImmediateExecutor();
final Executor cryptoExecutor = new ImmediateExecutor();
final MessageValidator validator = context.mock(MessageValidator.class);
final ValidationHook hook = context.mock(ValidationHook.class);
final IncomingMessageHook hook = context.mock(IncomingMessageHook.class);
final Transaction txn = new Transaction(null);
final Transaction txn1 = new Transaction(null);
final Transaction txn2 = new Transaction(null);
......@@ -219,7 +219,7 @@ public class ValidationManagerImplTest extends BriarTestCase {
ValidationManagerImpl vm = new ValidationManagerImpl(db, dbExecutor,
cryptoExecutor);
vm.registerMessageValidator(clientId, validator);
vm.registerValidationHook(hook);
vm.registerIncomingMessageHook(clientId, hook);
vm.start();
context.assertIsSatisfied();
......@@ -232,7 +232,7 @@ public class ValidationManagerImplTest extends BriarTestCase {
final Executor dbExecutor = new ImmediateExecutor();
final Executor cryptoExecutor = new ImmediateExecutor();
final MessageValidator validator = context.mock(MessageValidator.class);
final ValidationHook hook = context.mock(ValidationHook.class);
final IncomingMessageHook hook = context.mock(IncomingMessageHook.class);
final Transaction txn = new Transaction(null);
final Transaction txn1 = new Transaction(null);
context.checking(new Expectations() {{
......@@ -252,14 +252,14 @@ public class ValidationManagerImplTest extends BriarTestCase {
oneOf(db).setMessageValid(txn1, message, clientId, true);
oneOf(db).setMessageShared(txn1, message, true);
// Call the hook
oneOf(hook).validatingMessage(txn1, message, clientId, metadata);
oneOf(hook).incomingMessage(txn1, message, metadata);
oneOf(db).endTransaction(txn1);
}});
ValidationManagerImpl vm = new ValidationManagerImpl(db, dbExecutor,
cryptoExecutor);
vm.registerMessageValidator(clientId, validator);
vm.registerValidationHook(hook);
vm.registerIncomingMessageHook(clientId, hook);
vm.eventOccurred(new MessageAddedEvent(message, contactId));
context.assertIsSatisfied();
......@@ -272,12 +272,12 @@ public class ValidationManagerImplTest extends BriarTestCase {
final Executor dbExecutor = new ImmediateExecutor();
final Executor cryptoExecutor = new ImmediateExecutor();
final MessageValidator validator = context.mock(MessageValidator.class);
final ValidationHook hook = context.mock(ValidationHook.class);
final IncomingMessageHook hook = context.mock(IncomingMessageHook.class);
ValidationManagerImpl vm = new ValidationManagerImpl(db, dbExecutor,
cryptoExecutor);
vm.registerMessageValidator(clientId, validator);
vm.registerValidationHook(hook);
vm.registerIncomingMessageHook(clientId, hook);
vm.eventOccurred(new MessageAddedEvent(message, null));
context.assertIsSatisfied();
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment