mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-18 05:39:53 +01:00
Run hooks when messages are validated.
This commit is contained in:
@@ -1,5 +1,7 @@
|
|||||||
package org.briarproject.api.sync;
|
package org.briarproject.api.sync;
|
||||||
|
|
||||||
|
import org.briarproject.api.db.Metadata;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Responsible for managing message validators and passing them messages to
|
* Responsible for managing message validators and passing them messages to
|
||||||
* validate.
|
* validate.
|
||||||
@@ -28,4 +30,11 @@ 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);
|
void registerMessageValidator(ClientId c, MessageValidator v);
|
||||||
|
|
||||||
|
/** Registers a hook to be called whenever a message is validated. */
|
||||||
|
void registerValidationHook(ValidationHook hook);
|
||||||
|
|
||||||
|
interface ValidationHook {
|
||||||
|
void validatingMessage(Message m, ClientId c, Metadata meta);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,8 +22,10 @@ import org.briarproject.api.sync.MessageValidator;
|
|||||||
import org.briarproject.api.sync.ValidationManager;
|
import org.briarproject.api.sync.ValidationManager;
|
||||||
import org.briarproject.util.ByteUtils;
|
import org.briarproject.util.ByteUtils;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
import java.util.concurrent.CopyOnWriteArrayList;
|
||||||
import java.util.concurrent.Executor;
|
import java.util.concurrent.Executor;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
@@ -40,6 +42,7 @@ class ValidationManagerImpl implements ValidationManager, Service,
|
|||||||
private final Executor dbExecutor;
|
private final Executor dbExecutor;
|
||||||
private final Executor cryptoExecutor;
|
private final Executor cryptoExecutor;
|
||||||
private final Map<ClientId, MessageValidator> validators;
|
private final Map<ClientId, MessageValidator> validators;
|
||||||
|
private final List<ValidationHook> hooks;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
ValidationManagerImpl(DatabaseComponent db,
|
ValidationManagerImpl(DatabaseComponent db,
|
||||||
@@ -49,6 +52,7 @@ class ValidationManagerImpl implements ValidationManager, Service,
|
|||||||
this.dbExecutor = dbExecutor;
|
this.dbExecutor = dbExecutor;
|
||||||
this.cryptoExecutor = cryptoExecutor;
|
this.cryptoExecutor = cryptoExecutor;
|
||||||
validators = new ConcurrentHashMap<ClientId, MessageValidator>();
|
validators = new ConcurrentHashMap<ClientId, MessageValidator>();
|
||||||
|
hooks = new CopyOnWriteArrayList<ValidationHook>();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -67,6 +71,11 @@ class ValidationManagerImpl implements ValidationManager, Service,
|
|||||||
validators.put(c, v);
|
validators.put(c, v);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void registerValidationHook(ValidationHook hook) {
|
||||||
|
hooks.add(hook);
|
||||||
|
}
|
||||||
|
|
||||||
private void getMessagesToValidate(final ClientId c) {
|
private void getMessagesToValidate(final ClientId c) {
|
||||||
dbExecutor.execute(new Runnable() {
|
dbExecutor.execute(new Runnable() {
|
||||||
public void run() {
|
public void run() {
|
||||||
@@ -119,6 +128,8 @@ class ValidationManagerImpl implements ValidationManager, Service,
|
|||||||
if (meta == null) {
|
if (meta == null) {
|
||||||
db.setMessageValid(m, c, false);
|
db.setMessageValid(m, c, false);
|
||||||
} else {
|
} else {
|
||||||
|
for (ValidationHook hook : hooks)
|
||||||
|
hook.validatingMessage(m, c, meta);
|
||||||
db.mergeMessageMetadata(m.getId(), meta);
|
db.mergeMessageMetadata(m.getId(), meta);
|
||||||
db.setMessageValid(m, c, true);
|
db.setMessageValid(m, c, true);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,14 @@
|
|||||||
|
package org.briarproject.sync;
|
||||||
|
|
||||||
|
import org.briarproject.BriarTestCase;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import static org.junit.Assert.fail;
|
||||||
|
|
||||||
|
public class ValidationManagerImplTest extends BriarTestCase {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testUnitTestsExist() {
|
||||||
|
fail(); // FIXME: Write tests
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user