Commit f6611daf authored by akwizgran's avatar akwizgran

Replace Client interface with OpenDatabaseHook.

parent 00bc8ac7
package org.briarproject.bramble.api.identity; package org.briarproject.bramble.api.identity;
import org.briarproject.bramble.api.crypto.CryptoExecutor; import org.briarproject.bramble.api.crypto.CryptoExecutor;
import org.briarproject.bramble.api.crypto.SecretKey;
import org.briarproject.bramble.api.db.DbException; import org.briarproject.bramble.api.db.DbException;
import org.briarproject.bramble.api.db.Transaction; import org.briarproject.bramble.api.db.Transaction;
import org.briarproject.bramble.api.lifecycle.LifecycleManager;
import org.briarproject.bramble.api.nullsafety.NotNullByDefault; import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
@NotNullByDefault @NotNullByDefault
...@@ -15,17 +17,13 @@ public interface IdentityManager { ...@@ -15,17 +17,13 @@ public interface IdentityManager {
LocalAuthor createLocalAuthor(String name); LocalAuthor createLocalAuthor(String name);
/** /**
* Registers the given local identity with the manager. The identity is * Registers the given local identity with the manager. This method should
* not stored until {@link #storeLocalAuthor()} is called. * be called before {@link LifecycleManager#startServices(SecretKey)}. The
* identity is stored when {@link LifecycleManager#startServices(SecretKey)}
* is called.
*/ */
void registerLocalAuthor(LocalAuthor a); void registerLocalAuthor(LocalAuthor a);
/**
* Stores the local identity registered with
* {@link #registerLocalAuthor(LocalAuthor)}, if any.
*/
void storeLocalAuthor() throws DbException;
/** /**
* Returns the cached local identity or loads it from the database. * Returns the cached local identity or loads it from the database.
*/ */
......
...@@ -2,16 +2,17 @@ package org.briarproject.bramble.api.lifecycle; ...@@ -2,16 +2,17 @@ package org.briarproject.bramble.api.lifecycle;
import org.briarproject.bramble.api.crypto.SecretKey; import org.briarproject.bramble.api.crypto.SecretKey;
import org.briarproject.bramble.api.db.DatabaseComponent; import org.briarproject.bramble.api.db.DatabaseComponent;
import org.briarproject.bramble.api.db.DbException;
import org.briarproject.bramble.api.db.Transaction;
import org.briarproject.bramble.api.lifecycle.LifecycleManager.OpenDatabaseHook.Priority;
import org.briarproject.bramble.api.nullsafety.NotNullByDefault; import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
import org.briarproject.bramble.api.sync.Client;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
/** /**
* Manages the lifecycle of the app, starting {@link Client Clients}, starting * Manages the lifecycle of the app: opening and closing the
* and stopping {@link Service Services}, shutting down * {@link DatabaseComponent} starting and stopping {@link Service Services},
* {@link ExecutorService ExecutorServices}, and opening and closing the * and shutting down {@link ExecutorService ExecutorServices}.
* {@link DatabaseComponent}.
*/ */
@NotNullByDefault @NotNullByDefault
public interface LifecycleManager { public interface LifecycleManager {
...@@ -43,16 +44,17 @@ public interface LifecycleManager { ...@@ -43,16 +44,17 @@ public interface LifecycleManager {
} }
/** /**
* Registers a {@link Service} to be started and stopped. This method * Registers a hook to be called after the database is opened and before
* should be called before {@link #startServices(SecretKey)}. * {@link Service services} are started. This method should be called
* before {@link #startServices(SecretKey)}.
*/ */
void registerService(Service s); void registerOpenDatabaseHook(OpenDatabaseHook hook, Priority p);
/** /**
* Registers a {@link Client} to be started. This method should be called * Registers a {@link Service} to be started and stopped. This method
* before {@link #startServices(SecretKey)}. * should be called before {@link #startServices(SecretKey)}.
*/ */
void registerClient(Client c); void registerService(Service s);
/** /**
* Registers an {@link ExecutorService} to be shut down. This method * Registers an {@link ExecutorService} to be shut down. This method
...@@ -62,7 +64,7 @@ public interface LifecycleManager { ...@@ -62,7 +64,7 @@ public interface LifecycleManager {
/** /**
* Opens the {@link DatabaseComponent} using the given key and starts any * Opens the {@link DatabaseComponent} using the given key and starts any
* registered {@link Client Clients} and {@link Service Services}. * registered {@link Service Services}.
*/ */
StartResult startServices(SecretKey dbKey); StartResult startServices(SecretKey dbKey);
...@@ -80,8 +82,7 @@ public interface LifecycleManager { ...@@ -80,8 +82,7 @@ public interface LifecycleManager {
/** /**
* Waits for the {@link DatabaseComponent} to be opened and all registered * Waits for the {@link DatabaseComponent} to be opened and all registered
* {@link Client Clients} and {@link Service Services} to start before * {@link Service Services} to start before returning.
* returning.
*/ */
void waitForStartup() throws InterruptedException; void waitForStartup() throws InterruptedException;
...@@ -97,4 +98,10 @@ public interface LifecycleManager { ...@@ -97,4 +98,10 @@ public interface LifecycleManager {
*/ */
LifecycleState getLifecycleState(); LifecycleState getLifecycleState();
interface OpenDatabaseHook {
enum Priority {EARLY, NORMAL, LATE}
void onDatabaseOpened(Transaction txn) throws DbException;
}
} }
\ No newline at end of file
package org.briarproject.bramble.api.sync;
import org.briarproject.bramble.api.db.DbException;
import org.briarproject.bramble.api.db.Transaction;
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
@NotNullByDefault
public interface Client {
/**
* Called at startup to create any local state needed by the client.
*/
void createLocalState(Transaction txn) throws DbException;
}
...@@ -6,6 +6,7 @@ import org.briarproject.bramble.api.db.Transaction; ...@@ -6,6 +6,7 @@ import org.briarproject.bramble.api.db.Transaction;
import org.briarproject.bramble.api.identity.AuthorFactory; import org.briarproject.bramble.api.identity.AuthorFactory;
import org.briarproject.bramble.api.identity.IdentityManager; import org.briarproject.bramble.api.identity.IdentityManager;
import org.briarproject.bramble.api.identity.LocalAuthor; import org.briarproject.bramble.api.identity.LocalAuthor;
import org.briarproject.bramble.api.lifecycle.LifecycleManager.OpenDatabaseHook;
import org.briarproject.bramble.api.nullsafety.NotNullByDefault; import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
import java.util.logging.Logger; import java.util.logging.Logger;
...@@ -14,15 +15,16 @@ import javax.annotation.Nullable; ...@@ -14,15 +15,16 @@ import javax.annotation.Nullable;
import javax.annotation.concurrent.ThreadSafe; import javax.annotation.concurrent.ThreadSafe;
import javax.inject.Inject; import javax.inject.Inject;
import static java.util.logging.Logger.getLogger;
import static org.briarproject.bramble.util.LogUtils.logDuration; import static org.briarproject.bramble.util.LogUtils.logDuration;
import static org.briarproject.bramble.util.LogUtils.now; import static org.briarproject.bramble.util.LogUtils.now;
@ThreadSafe @ThreadSafe
@NotNullByDefault @NotNullByDefault
class IdentityManagerImpl implements IdentityManager { class IdentityManagerImpl implements IdentityManager, OpenDatabaseHook {
private static final Logger LOG = private static final Logger LOG =
Logger.getLogger(IdentityManagerImpl.class.getName()); getLogger(IdentityManagerImpl.class.getName());
private final DatabaseComponent db; private final DatabaseComponent db;
private final AuthorFactory authorFactory; private final AuthorFactory authorFactory;
...@@ -52,13 +54,13 @@ class IdentityManagerImpl implements IdentityManager { ...@@ -52,13 +54,13 @@ class IdentityManagerImpl implements IdentityManager {
} }
@Override @Override
public void storeLocalAuthor() throws DbException { public void onDatabaseOpened(Transaction txn) throws DbException {
LocalAuthor cached = cachedAuthor; LocalAuthor cached = cachedAuthor;
if (cached == null) { if (cached == null) {
LOG.info("No local author to store"); LOG.info("No local author to store");
return; return;
} }
db.transaction(false, txn -> db.addLocalAuthor(txn, cached)); db.addLocalAuthor(txn, cached);
LOG.info("Local author stored"); LOG.info("Local author stored");
} }
...@@ -89,5 +91,4 @@ class IdentityManagerImpl implements IdentityManager { ...@@ -89,5 +91,4 @@ class IdentityManagerImpl implements IdentityManager {
private LocalAuthor loadLocalAuthor(Transaction txn) throws DbException { private LocalAuthor loadLocalAuthor(Transaction txn) throws DbException {
return db.getLocalAuthors(txn).iterator().next(); return db.getLocalAuthors(txn).iterator().next();
} }
} }
...@@ -2,6 +2,7 @@ package org.briarproject.bramble.identity; ...@@ -2,6 +2,7 @@ package org.briarproject.bramble.identity;
import org.briarproject.bramble.api.identity.AuthorFactory; import org.briarproject.bramble.api.identity.AuthorFactory;
import org.briarproject.bramble.api.identity.IdentityManager; import org.briarproject.bramble.api.identity.IdentityManager;
import org.briarproject.bramble.api.lifecycle.LifecycleManager;
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Singleton; import javax.inject.Singleton;
...@@ -9,6 +10,8 @@ import javax.inject.Singleton; ...@@ -9,6 +10,8 @@ import javax.inject.Singleton;
import dagger.Module; import dagger.Module;
import dagger.Provides; import dagger.Provides;
import static org.briarproject.bramble.api.lifecycle.LifecycleManager.OpenDatabaseHook.Priority.EARLY;
@Module @Module
public class IdentityModule { public class IdentityModule {
...@@ -24,8 +27,9 @@ public class IdentityModule { ...@@ -24,8 +27,9 @@ public class IdentityModule {
@Provides @Provides
@Singleton @Singleton
IdentityManager provideIdentityManager( IdentityManager provideIdentityManager(LifecycleManager lifecycleManager,
IdentityManagerImpl identityManager) { IdentityManagerImpl identityManager) {
lifecycleManager.registerOpenDatabaseHook(identityManager, EARLY);
return identityManager; return identityManager;
} }
} }
package org.briarproject.bramble.lifecycle; package org.briarproject.bramble.lifecycle;
import org.briarproject.bramble.api.Pair;
import org.briarproject.bramble.api.crypto.SecretKey; import org.briarproject.bramble.api.crypto.SecretKey;
import org.briarproject.bramble.api.db.DataTooNewException; import org.briarproject.bramble.api.db.DataTooNewException;
import org.briarproject.bramble.api.db.DataTooOldException; import org.briarproject.bramble.api.db.DataTooOldException;
...@@ -7,14 +8,14 @@ import org.briarproject.bramble.api.db.DatabaseComponent; ...@@ -7,14 +8,14 @@ import org.briarproject.bramble.api.db.DatabaseComponent;
import org.briarproject.bramble.api.db.DbException; import org.briarproject.bramble.api.db.DbException;
import org.briarproject.bramble.api.db.MigrationListener; import org.briarproject.bramble.api.db.MigrationListener;
import org.briarproject.bramble.api.event.EventBus; import org.briarproject.bramble.api.event.EventBus;
import org.briarproject.bramble.api.identity.IdentityManager;
import org.briarproject.bramble.api.lifecycle.LifecycleManager; import org.briarproject.bramble.api.lifecycle.LifecycleManager;
import org.briarproject.bramble.api.lifecycle.LifecycleManager.OpenDatabaseHook.Priority;
import org.briarproject.bramble.api.lifecycle.Service; import org.briarproject.bramble.api.lifecycle.Service;
import org.briarproject.bramble.api.lifecycle.ServiceException; import org.briarproject.bramble.api.lifecycle.ServiceException;
import org.briarproject.bramble.api.lifecycle.event.LifecycleEvent; import org.briarproject.bramble.api.lifecycle.event.LifecycleEvent;
import org.briarproject.bramble.api.nullsafety.NotNullByDefault; import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
import org.briarproject.bramble.api.sync.Client;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CountDownLatch; import java.util.concurrent.CountDownLatch;
...@@ -25,9 +26,11 @@ import java.util.logging.Logger; ...@@ -25,9 +26,11 @@ import java.util.logging.Logger;
import javax.annotation.concurrent.ThreadSafe; import javax.annotation.concurrent.ThreadSafe;
import javax.inject.Inject; import javax.inject.Inject;
import static java.util.Collections.sort;
import static java.util.logging.Level.FINE; import static java.util.logging.Level.FINE;
import static java.util.logging.Level.INFO; import static java.util.logging.Level.INFO;
import static java.util.logging.Level.WARNING; import static java.util.logging.Level.WARNING;
import static java.util.logging.Logger.getLogger;
import static org.briarproject.bramble.api.lifecycle.LifecycleManager.LifecycleState.COMPACTING_DATABASE; import static org.briarproject.bramble.api.lifecycle.LifecycleManager.LifecycleState.COMPACTING_DATABASE;
import static org.briarproject.bramble.api.lifecycle.LifecycleManager.LifecycleState.MIGRATING_DATABASE; import static org.briarproject.bramble.api.lifecycle.LifecycleManager.LifecycleState.MIGRATING_DATABASE;
import static org.briarproject.bramble.api.lifecycle.LifecycleManager.LifecycleState.RUNNING; import static org.briarproject.bramble.api.lifecycle.LifecycleManager.LifecycleState.RUNNING;
...@@ -49,14 +52,13 @@ import static org.briarproject.bramble.util.LogUtils.now; ...@@ -49,14 +52,13 @@ import static org.briarproject.bramble.util.LogUtils.now;
class LifecycleManagerImpl implements LifecycleManager, MigrationListener { class LifecycleManagerImpl implements LifecycleManager, MigrationListener {
private static final Logger LOG = private static final Logger LOG =
Logger.getLogger(LifecycleManagerImpl.class.getName()); getLogger(LifecycleManagerImpl.class.getName());
private final DatabaseComponent db; private final DatabaseComponent db;
private final EventBus eventBus; private final EventBus eventBus;
private final List<Service> services; private final List<Service> services;
private final List<Client> clients; private final List<Pair<OpenDatabaseHook, Priority>> openDatabaseHooks;
private final List<ExecutorService> executors; private final List<ExecutorService> executors;
private final IdentityManager identityManager;
private final Semaphore startStopSemaphore = new Semaphore(1); private final Semaphore startStopSemaphore = new Semaphore(1);
private final CountDownLatch dbLatch = new CountDownLatch(1); private final CountDownLatch dbLatch = new CountDownLatch(1);
private final CountDownLatch startupLatch = new CountDownLatch(1); private final CountDownLatch startupLatch = new CountDownLatch(1);
...@@ -65,13 +67,11 @@ class LifecycleManagerImpl implements LifecycleManager, MigrationListener { ...@@ -65,13 +67,11 @@ class LifecycleManagerImpl implements LifecycleManager, MigrationListener {
private volatile LifecycleState state = STARTING; private volatile LifecycleState state = STARTING;
@Inject @Inject
LifecycleManagerImpl(DatabaseComponent db, EventBus eventBus, LifecycleManagerImpl(DatabaseComponent db, EventBus eventBus) {
IdentityManager identityManager) {
this.db = db; this.db = db;
this.eventBus = eventBus; this.eventBus = eventBus;
this.identityManager = identityManager;
services = new CopyOnWriteArrayList<>(); services = new CopyOnWriteArrayList<>();
clients = new CopyOnWriteArrayList<>(); openDatabaseHooks = new CopyOnWriteArrayList<>();
executors = new CopyOnWriteArrayList<>(); executors = new CopyOnWriteArrayList<>();
} }
...@@ -83,10 +83,12 @@ class LifecycleManagerImpl implements LifecycleManager, MigrationListener { ...@@ -83,10 +83,12 @@ class LifecycleManagerImpl implements LifecycleManager, MigrationListener {
} }
@Override @Override
public void registerClient(Client c) { public void registerOpenDatabaseHook(OpenDatabaseHook hook, Priority p) {
if (LOG.isLoggable(INFO)) if (LOG.isLoggable(INFO)) {
LOG.info("Registering client " + c.getClass().getSimpleName()); LOG.info("Registering open database hook "
clients.add(c); + hook.getClass().getSimpleName());
}
openDatabaseHooks.add(new Pair<>(hook, p));
} }
@Override @Override
...@@ -102,28 +104,31 @@ class LifecycleManagerImpl implements LifecycleManager, MigrationListener { ...@@ -102,28 +104,31 @@ class LifecycleManagerImpl implements LifecycleManager, MigrationListener {
return ALREADY_RUNNING; return ALREADY_RUNNING;
} }
try { try {
LOG.info("Starting services"); LOG.info("Opening database");
long start = now(); long start = now();
boolean reopened = db.open(dbKey, this); boolean reopened = db.open(dbKey, this);
if (reopened) logDuration(LOG, "Reopening database", start); if (reopened) logDuration(LOG, "Reopening database", start);
else logDuration(LOG, "Creating database", start); else logDuration(LOG, "Creating database", start);
identityManager.storeLocalAuthor(); List<Pair<OpenDatabaseHook, Priority>> hooks =
new ArrayList<>(openDatabaseHooks);
state = STARTING_SERVICES; sort(hooks, (a, b) -> a.getSecond().compareTo(b.getSecond()));
dbLatch.countDown();
eventBus.broadcast(new LifecycleEvent(STARTING_SERVICES));
db.transaction(false, txn -> { db.transaction(false, txn -> {
for (Client c : clients) { for (Pair<OpenDatabaseHook, Priority> pair : hooks) {
long start1 = now(); long start1 = now();
c.createLocalState(txn); OpenDatabaseHook hook = pair.getFirst();
hook.onDatabaseOpened(txn);
if (LOG.isLoggable(FINE)) { if (LOG.isLoggable(FINE)) {
logDuration(LOG, "Starting client " logDuration(LOG, "Calling open database hook "
+ c.getClass().getSimpleName(), start1); + hook.getClass().getSimpleName(), start1);
} }
} }
}); });
LOG.info("Starting services");
state = STARTING_SERVICES;
dbLatch.countDown();
eventBus.broadcast(new LifecycleEvent(STARTING_SERVICES));
for (Service s : services) { for (Service s : services) {
start = now(); start = now();
s.startService(); s.startService();
......
...@@ -15,6 +15,7 @@ import javax.inject.Singleton; ...@@ -15,6 +15,7 @@ import javax.inject.Singleton;
import dagger.Module; import dagger.Module;
import dagger.Provides; import dagger.Provides;
import static org.briarproject.bramble.api.lifecycle.LifecycleManager.OpenDatabaseHook.Priority.NORMAL;
import static org.briarproject.bramble.api.properties.TransportPropertyManager.CLIENT_ID; import static org.briarproject.bramble.api.properties.TransportPropertyManager.CLIENT_ID;
import static org.briarproject.bramble.api.properties.TransportPropertyManager.MAJOR_VERSION; import static org.briarproject.bramble.api.properties.TransportPropertyManager.MAJOR_VERSION;
import static org.briarproject.bramble.api.properties.TransportPropertyManager.MINOR_VERSION; import static org.briarproject.bramble.api.properties.TransportPropertyManager.MINOR_VERSION;
...@@ -48,7 +49,8 @@ public class PropertiesModule { ...@@ -48,7 +49,8 @@ public class PropertiesModule {
ValidationManager validationManager, ContactManager contactManager, ValidationManager validationManager, ContactManager contactManager,
ClientVersioningManager clientVersioningManager, ClientVersioningManager clientVersioningManager,
TransportPropertyManagerImpl transportPropertyManager) { TransportPropertyManagerImpl transportPropertyManager) {
lifecycleManager.registerClient(transportPropertyManager); lifecycleManager.registerOpenDatabaseHook(transportPropertyManager,
NORMAL);
validationManager.registerIncomingMessageHook(CLIENT_ID, MAJOR_VERSION, validationManager.registerIncomingMessageHook(CLIENT_ID, MAJOR_VERSION,
transportPropertyManager); transportPropertyManager);
contactManager.registerContactHook(transportPropertyManager); contactManager.registerContactHook(transportPropertyManager);
......
...@@ -13,11 +13,11 @@ import org.briarproject.bramble.api.db.DatabaseComponent; ...@@ -13,11 +13,11 @@ import org.briarproject.bramble.api.db.DatabaseComponent;
import org.briarproject.bramble.api.db.DbException; import org.briarproject.bramble.api.db.DbException;
import org.briarproject.bramble.api.db.Metadata; import org.briarproject.bramble.api.db.Metadata;
import org.briarproject.bramble.api.db.Transaction; import org.briarproject.bramble.api.db.Transaction;
import org.briarproject.bramble.api.lifecycle.LifecycleManager.OpenDatabaseHook;
import org.briarproject.bramble.api.nullsafety.NotNullByDefault; import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
import org.briarproject.bramble.api.plugin.TransportId; import org.briarproject.bramble.api.plugin.TransportId;
import org.briarproject.bramble.api.properties.TransportProperties; import org.briarproject.bramble.api.properties.TransportProperties;
import org.briarproject.bramble.api.properties.TransportPropertyManager; import org.briarproject.bramble.api.properties.TransportPropertyManager;
import org.briarproject.bramble.api.sync.Client;
import org.briarproject.bramble.api.sync.Group; import org.briarproject.bramble.api.sync.Group;
import org.briarproject.bramble.api.sync.Group.Visibility; import org.briarproject.bramble.api.sync.Group.Visibility;
import org.briarproject.bramble.api.sync.GroupId; import org.briarproject.bramble.api.sync.GroupId;
...@@ -40,7 +40,8 @@ import javax.inject.Inject; ...@@ -40,7 +40,8 @@ import javax.inject.Inject;
@Immutable @Immutable
@NotNullByDefault @NotNullByDefault
class TransportPropertyManagerImpl implements TransportPropertyManager, class TransportPropertyManagerImpl implements TransportPropertyManager,
Client, ContactHook, ClientVersioningHook, IncomingMessageHook { OpenDatabaseHook, ContactHook, ClientVersioningHook,
IncomingMessageHook {
private final DatabaseComponent db; private final DatabaseComponent db;
private final ClientHelper clientHelper; private final ClientHelper clientHelper;
...@@ -67,7 +68,7 @@ class TransportPropertyManagerImpl implements TransportPropertyManager, ...@@ -67,7 +68,7 @@ class TransportPropertyManagerImpl implements TransportPropertyManager,
} }
@Override @Override
public void createLocalState(Transaction txn) throws DbException { public void onDatabaseOpened(Transaction txn) throws DbException {
if (db.containsGroup(txn, localGroup.getId())) return; if (db.containsGroup(txn, localGroup.getId())) return;
db.addGroup(txn, localGroup); db.addGroup(txn, localGroup);
// Set things up for any pre-existing contacts // Set things up for any pre-existing contacts
......
...@@ -12,10 +12,10 @@ import org.briarproject.bramble.api.db.DatabaseComponent; ...@@ -12,10 +12,10 @@ import org.briarproject.bramble.api.db.DatabaseComponent;
import org.briarproject.bramble.api.db.DbException; import org.briarproject.bramble.api.db.DbException;
import org.briarproject.bramble.api.db.Metadata; import org.briarproject.bramble.api.db.Metadata;
import org.briarproject.bramble.api.db.Transaction; import org.briarproject.bramble.api.db.Transaction;
import org.briarproject.bramble.api.lifecycle.LifecycleManager.OpenDatabaseHook;
import org.briarproject.bramble.api.lifecycle.Service; import org.briarproject.bramble.api.lifecycle.Service;
import org.briarproject.bramble.api.lifecycle.ServiceException; import org.briarproject.bramble.api.lifecycle.ServiceException;
import org.briarproject.bramble.api.nullsafety.NotNullByDefault; import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
import org.briarproject.bramble.api.sync.Client;
import org.briarproject.bramble.api.sync.ClientId; import org.briarproject.bramble.api.sync.ClientId;
import org.briarproject.bramble.api.sync.Group; import org.briarproject.bramble.api.sync.Group;
import org.briarproject.bramble.api.sync.Group.Visibility; import org.briarproject.bramble.api.sync.Group.Visibility;
...@@ -53,8 +53,8 @@ import static org.briarproject.bramble.versioning.ClientVersioningConstants.MSG_ ...@@ -53,8 +53,8 @@ import static org.briarproject.bramble.versioning.ClientVersioningConstants.MSG_
import static org.briarproject.bramble.versioning.ClientVersioningConstants.MSG_KEY_UPDATE_VERSION; import static org.briarproject.bramble.versioning.ClientVersioningConstants.MSG_KEY_UPDATE_VERSION;
@NotNullByDefault @NotNullByDefault
class ClientVersioningManagerImpl implements ClientVersioningManager, Client, class ClientVersioningManagerImpl implements ClientVersioningManager,
Service, ContactHook, IncomingMessageHook { Service, OpenDatabaseHook, ContactHook, IncomingMessageHook {
private final DatabaseComponent db; private final DatabaseComponent db;
private final ClientHelper clientHelper; private final ClientHelper clientHelper;
...@@ -124,7 +124,7 @@ class ClientVersioningManagerImpl implements ClientVersioningManager, Client, ...@@ -124,7 +124,7 @@ class ClientVersioningManagerImpl implements ClientVersioningManager, Client,
} }
@Override @Override
public void createLocalState(Transaction txn) throws DbException { public void onDatabaseOpened(Transaction txn) throws DbException {
if (db.containsGroup(txn, localGroup.getId())) return; if (db.containsGroup(txn, localGroup.getId())) return;
db.addGroup(txn, localGroup); db.addGroup(txn, localGroup);
// Set things up for any pre-existing contacts // Set things up for any pre-existing contacts
......
...@@ -14,6 +14,7 @@ import javax.inject.Singleton; ...@@ -14,6 +14,7 @@ import javax.inject.Singleton;
import dagger.Module; import dagger.Module;
import dagger.Provides; import dagger.Provides;
import static org.briarproject.bramble.api.lifecycle.LifecycleManager.OpenDatabaseHook.Priority.NORMAL;
import static org.briarproject.bramble.api.versioning.ClientVersioningManager.CLIENT_ID; import static org.briarproject.bramble.api.versioning.ClientVersioningManager.CLIENT_ID;
import static org.briarproject.bramble.api.versioning.ClientVersioningManager.MAJOR_VERSION; import static org.briarproject.bramble.api.versioning.ClientVersioningManager.MAJOR_VERSION;
...@@ -34,7 +35,8 @@ public class VersioningModule { ...@@ -34,7 +35,8 @@ public class VersioningModule {
ClientVersioningManagerImpl clientVersioningManager, ClientVersioningManagerImpl clientVersioningManager,
LifecycleManager lifecycleManager, ContactManager contactManager, LifecycleManager lifecycleManager, ContactManager contactManager,
ValidationManager validationManager) { ValidationManager validationManager) {
lifecycleManager.registerClient(clientVersioningManager); lifecycleManager.registerOpenDatabaseHook(clientVersioningManager,
NORMAL);
lifecycleManager.registerService(clientVersioningManager); lifecycleManager.registerService(clientVersioningManager);
contactManager.registerContactHook(clientVersioningManager); contactManager.registerContactHook(clientVersioningManager);
validationManager.registerIncomingMessageHook(CLIENT_ID, MAJOR_VERSION, validationManager.registerIncomingMessageHook(CLIENT_ID, MAJOR_VERSION,
......
...@@ -4,7 +4,6 @@ import org.briarproject.bramble.api.db.DatabaseComponent; ...@@ -4,7 +4,6 @@ import org.briarproject.bramble.api.db.DatabaseComponent;
import org.briarproject.bramble.api.db.DbException; import org.briarproject.bramble.api.db.DbException;
import org.briarproject.bramble.api.db.Transaction; import org.briarproject.bramble.api.db.Transaction;
import org.briarproject.bramble.api.identity.AuthorFactory; import org.briarproject.bramble.api.identity.AuthorFactory;
import org.briarproject.bramble.api.identity.IdentityManager;
import org.briarproject.bramble.api.identity.LocalAuthor; import org.briarproject.bramble.api.identity.LocalAuthor;
import org.briarproject.bramble.test.BrambleMockTestCase; import org.briarproject.bramble.test.BrambleMockTestCase;
import org.briarproject.bramble.test.DbExpectations; import org.briarproject.bramble.test.DbExpectations;
...@@ -27,7 +26,7 @@ public class IdentityManagerImplTest extends BrambleMockTestCase { ...@@ -27,7 +26,7 @@ public class IdentityManagerImplTest extends BrambleMockTestCase {
private final LocalAuthor localAuthor = getLocalAuthor(); private final LocalAuthor localAuthor = getLocalAuthor();
private final Collection<LocalAuthor> localAuthors = private final Collection<LocalAuthor> localAuthors =
singletonList(localAuthor); singletonList(localAuthor);
private IdentityManager identityManager; private IdentityManagerImpl identityManager;
@Before @Before
public void setUp() { public void setUp() {
...@@ -35,15 +34,20 @@ public class IdentityManagerImplTest extends BrambleMockTestCase { ...@@ -35,15 +34,20 @@ public class IdentityManagerImplTest extends BrambleMockTestCase {
} }
@Test @Test
public void testRegisterAndStoreLocalAuthor() throws Exception { public void testOpenDatabaseHookWithoutLocalAuthorRegistered()
throws Exception {
identityManager.onDatabaseOpened(txn);
}
@Test
public void testOpenDatabaseHookWithLocalAuthorRegistered()
throws Exception {
context.checking(new DbExpectations() {{ context.checking(new DbExpectations() {{
oneOf(db).transaction(with(false), withDbRunnable(txn));
oneOf(db).addLocalAuthor(txn, localAuthor); oneOf(db).addLocalAuthor(txn, localAuthor);
}}); }});
identityManager.registerLocalAuthor(localAuthor); identityManager.registerLocalAuthor(localAuthor);