From 33ef09a6bf2ab063faeb048a8e01ad69b95db3f5 Mon Sep 17 00:00:00 2001 From: akwizgran Date: Tue, 19 Jan 2016 15:50:29 +0000 Subject: [PATCH 1/4] Implement Service interface rather than extending it. Whether or not a class needs to run as a service is an implementation decision. --- .../AndroidNotificationManagerImpl.java | 5 +- .../android/AndroidNotificationManager.java | 3 +- .../api/plugins/PluginManager.java | 7 +- .../api/sync/MessageValidator.java | 3 +- .../api/sync/ValidationManager.java | 4 +- .../api/transport/KeyManager.java | 3 +- .../forum/ForumPostValidator.java | 3 +- .../messaging/PrivateMessageValidator.java | 3 +- .../plugins/PluginManagerImpl.java | 5 +- .../sync/ValidationManagerImpl.java | 4 +- .../transport/KeyManagerImpl.java | 5 +- .../sync/SimplexMessagingIntegrationTest.java | 117 ++++++++++-------- 12 files changed, 90 insertions(+), 72 deletions(-) diff --git a/briar-android/src/org/briarproject/android/AndroidNotificationManagerImpl.java b/briar-android/src/org/briarproject/android/AndroidNotificationManagerImpl.java index 8a5bcba15..ca3ed6a4c 100644 --- a/briar-android/src/org/briarproject/android/AndroidNotificationManagerImpl.java +++ b/briar-android/src/org/briarproject/android/AndroidNotificationManagerImpl.java @@ -25,6 +25,7 @@ import org.briarproject.api.event.EventListener; import org.briarproject.api.event.MessageValidatedEvent; import org.briarproject.api.event.SettingsUpdatedEvent; import org.briarproject.api.forum.ForumManager; +import org.briarproject.api.lifecycle.Service; import org.briarproject.api.messaging.MessagingManager; import org.briarproject.api.sync.ClientId; import org.briarproject.api.sync.GroupId; @@ -46,7 +47,7 @@ import static android.content.Intent.FLAG_ACTIVITY_SINGLE_TOP; import static java.util.logging.Level.WARNING; class AndroidNotificationManagerImpl implements AndroidNotificationManager, - EventListener { + Service, EventListener { private static final int PRIVATE_MESSAGE_NOTIFICATION_ID = 3; private static final int FORUM_POST_NOTIFICATION_ID = 4; @@ -91,6 +92,7 @@ class AndroidNotificationManagerImpl implements AndroidNotificationManager, appContext = app.getApplicationContext(); } + @Override public boolean start() { eventBus.addListener(this); loadSettings(); @@ -110,6 +112,7 @@ class AndroidNotificationManagerImpl implements AndroidNotificationManager, }); } + @Override public boolean stop() { eventBus.removeListener(this); clearNotifications(); diff --git a/briar-api/src/org/briarproject/api/android/AndroidNotificationManager.java b/briar-api/src/org/briarproject/api/android/AndroidNotificationManager.java index ed3223c4b..3aad189dc 100644 --- a/briar-api/src/org/briarproject/api/android/AndroidNotificationManager.java +++ b/briar-api/src/org/briarproject/api/android/AndroidNotificationManager.java @@ -1,10 +1,9 @@ package org.briarproject.api.android; -import org.briarproject.api.lifecycle.Service; import org.briarproject.api.sync.GroupId; /** Manages notifications for private messages and forum posts. */ -public interface AndroidNotificationManager extends Service { +public interface AndroidNotificationManager { void showPrivateMessageNotification(GroupId g); diff --git a/briar-api/src/org/briarproject/api/plugins/PluginManager.java b/briar-api/src/org/briarproject/api/plugins/PluginManager.java index efc336cf0..35248962d 100644 --- a/briar-api/src/org/briarproject/api/plugins/PluginManager.java +++ b/briar-api/src/org/briarproject/api/plugins/PluginManager.java @@ -1,16 +1,15 @@ package org.briarproject.api.plugins; -import java.util.Collection; - import org.briarproject.api.TransportId; -import org.briarproject.api.lifecycle.Service; import org.briarproject.api.plugins.duplex.DuplexPlugin; +import java.util.Collection; + /** * Responsible for starting transport plugins at startup, stopping them at * shutdown, and providing access to plugins for exchanging invitations. */ -public interface PluginManager extends Service { +public interface PluginManager { /** * Returns the plugin for the given transport, or null if no such plugin diff --git a/briar-api/src/org/briarproject/api/sync/MessageValidator.java b/briar-api/src/org/briarproject/api/sync/MessageValidator.java index d318421a4..d3d7ef520 100644 --- a/briar-api/src/org/briarproject/api/sync/MessageValidator.java +++ b/briar-api/src/org/briarproject/api/sync/MessageValidator.java @@ -1,9 +1,8 @@ package org.briarproject.api.sync; import org.briarproject.api.db.Metadata; -import org.briarproject.api.lifecycle.Service; -public interface MessageValidator extends Service { +public interface MessageValidator { /** * Validates the given message and returns its metadata if the message diff --git a/briar-api/src/org/briarproject/api/sync/ValidationManager.java b/briar-api/src/org/briarproject/api/sync/ValidationManager.java index 729e96ddc..ee1d7e967 100644 --- a/briar-api/src/org/briarproject/api/sync/ValidationManager.java +++ b/briar-api/src/org/briarproject/api/sync/ValidationManager.java @@ -1,12 +1,10 @@ package org.briarproject.api.sync; -import org.briarproject.api.lifecycle.Service; - /** * Responsible for managing message validators and passing them messages to * validate. */ -public interface ValidationManager extends Service { +public interface ValidationManager { /** Sets the message validator for the given client. */ void setMessageValidator(ClientId c, MessageValidator v); diff --git a/briar-api/src/org/briarproject/api/transport/KeyManager.java b/briar-api/src/org/briarproject/api/transport/KeyManager.java index e2f76f081..32800d7af 100644 --- a/briar-api/src/org/briarproject/api/transport/KeyManager.java +++ b/briar-api/src/org/briarproject/api/transport/KeyManager.java @@ -3,7 +3,6 @@ package org.briarproject.api.transport; import org.briarproject.api.TransportId; import org.briarproject.api.contact.ContactId; import org.briarproject.api.crypto.SecretKey; -import org.briarproject.api.lifecycle.Service; import java.util.Collection; @@ -11,7 +10,7 @@ import java.util.Collection; * Responsible for managing transport keys and recognising the pseudo-random * tags of incoming streams. */ -public interface KeyManager extends Service { +public interface KeyManager { /** * Informs the key manager that a new contact has been added. Derives and diff --git a/briar-core/src/org/briarproject/forum/ForumPostValidator.java b/briar-core/src/org/briarproject/forum/ForumPostValidator.java index 205b290f4..5f321d33d 100644 --- a/briar-core/src/org/briarproject/forum/ForumPostValidator.java +++ b/briar-core/src/org/briarproject/forum/ForumPostValidator.java @@ -15,6 +15,7 @@ import org.briarproject.api.data.MetadataEncoder; import org.briarproject.api.data.ObjectReader; import org.briarproject.api.db.Metadata; import org.briarproject.api.identity.Author; +import org.briarproject.api.lifecycle.Service; import org.briarproject.api.sync.Message; import org.briarproject.api.sync.MessageId; import org.briarproject.api.sync.MessageValidator; @@ -36,7 +37,7 @@ import static org.briarproject.api.sync.SyncConstants.MESSAGE_HEADER_LENGTH; import static org.briarproject.api.transport.TransportConstants.MAX_CLOCK_DIFFERENCE; import static org.briarproject.forum.ForumManagerImpl.CLIENT_ID; -class ForumPostValidator implements MessageValidator { +class ForumPostValidator implements MessageValidator, Service { private static final Logger LOG = Logger.getLogger(ForumPostValidator.class.getName()); diff --git a/briar-core/src/org/briarproject/messaging/PrivateMessageValidator.java b/briar-core/src/org/briarproject/messaging/PrivateMessageValidator.java index a3b491938..c97c66b6a 100644 --- a/briar-core/src/org/briarproject/messaging/PrivateMessageValidator.java +++ b/briar-core/src/org/briarproject/messaging/PrivateMessageValidator.java @@ -7,6 +7,7 @@ import org.briarproject.api.data.BdfReader; import org.briarproject.api.data.BdfReaderFactory; import org.briarproject.api.data.MetadataEncoder; import org.briarproject.api.db.Metadata; +import org.briarproject.api.lifecycle.Service; import org.briarproject.api.sync.Message; import org.briarproject.api.sync.MessageId; import org.briarproject.api.sync.MessageValidator; @@ -25,7 +26,7 @@ import static org.briarproject.api.sync.SyncConstants.MESSAGE_HEADER_LENGTH; import static org.briarproject.api.transport.TransportConstants.MAX_CLOCK_DIFFERENCE; import static org.briarproject.messaging.MessagingManagerImpl.CLIENT_ID; -class PrivateMessageValidator implements MessageValidator { +class PrivateMessageValidator implements MessageValidator, Service { private static final Logger LOG = Logger.getLogger(PrivateMessageValidator.class.getName()); diff --git a/briar-core/src/org/briarproject/plugins/PluginManagerImpl.java b/briar-core/src/org/briarproject/plugins/PluginManagerImpl.java index abdf16578..0625b2d24 100644 --- a/briar-core/src/org/briarproject/plugins/PluginManagerImpl.java +++ b/briar-core/src/org/briarproject/plugins/PluginManagerImpl.java @@ -10,6 +10,7 @@ import org.briarproject.api.event.EventBus; import org.briarproject.api.event.TransportDisabledEvent; import org.briarproject.api.event.TransportEnabledEvent; import org.briarproject.api.lifecycle.IoExecutor; +import org.briarproject.api.lifecycle.Service; import org.briarproject.api.plugins.ConnectionManager; import org.briarproject.api.plugins.Plugin; import org.briarproject.api.plugins.PluginCallback; @@ -46,7 +47,7 @@ import javax.inject.Inject; import static java.util.logging.Level.INFO; import static java.util.logging.Level.WARNING; -class PluginManagerImpl implements PluginManager { +class PluginManagerImpl implements PluginManager, Service { private static final Logger LOG = Logger.getLogger(PluginManagerImpl.class.getName()); @@ -88,6 +89,7 @@ class PluginManagerImpl implements PluginManager { duplexPlugins = new CopyOnWriteArrayList(); } + @Override public boolean start() { // Instantiate and start the simplex plugins LOG.info("Starting simplex plugins"); @@ -115,6 +117,7 @@ class PluginManagerImpl implements PluginManager { return true; } + @Override public boolean stop() { // Stop the poller LOG.info("Stopping poller"); diff --git a/briar-core/src/org/briarproject/sync/ValidationManagerImpl.java b/briar-core/src/org/briarproject/sync/ValidationManagerImpl.java index 68040be94..d3e861266 100644 --- a/briar-core/src/org/briarproject/sync/ValidationManagerImpl.java +++ b/briar-core/src/org/briarproject/sync/ValidationManagerImpl.java @@ -14,6 +14,7 @@ import org.briarproject.api.event.Event; import org.briarproject.api.event.EventBus; import org.briarproject.api.event.EventListener; import org.briarproject.api.event.MessageAddedEvent; +import org.briarproject.api.lifecycle.Service; import org.briarproject.api.sync.ClientId; import org.briarproject.api.sync.GroupId; import org.briarproject.api.sync.Message; @@ -30,7 +31,8 @@ import java.util.logging.Logger; import static java.util.logging.Level.WARNING; import static org.briarproject.api.sync.SyncConstants.MESSAGE_HEADER_LENGTH; -class ValidationManagerImpl implements ValidationManager, EventListener { +class ValidationManagerImpl implements ValidationManager, Service, + EventListener { private static final Logger LOG = Logger.getLogger(ValidationManagerImpl.class.getName()); diff --git a/briar-core/src/org/briarproject/transport/KeyManagerImpl.java b/briar-core/src/org/briarproject/transport/KeyManagerImpl.java index c36e844d2..aa50d8e5f 100644 --- a/briar-core/src/org/briarproject/transport/KeyManagerImpl.java +++ b/briar-core/src/org/briarproject/transport/KeyManagerImpl.java @@ -13,6 +13,7 @@ import org.briarproject.api.event.EventBus; import org.briarproject.api.event.EventListener; import org.briarproject.api.event.TransportAddedEvent; import org.briarproject.api.event.TransportRemovedEvent; +import org.briarproject.api.lifecycle.Service; import org.briarproject.api.system.Clock; import org.briarproject.api.system.Timer; import org.briarproject.api.transport.KeyManager; @@ -29,7 +30,7 @@ import javax.inject.Inject; import static java.util.logging.Level.WARNING; -class KeyManagerImpl implements KeyManager, EventListener { +class KeyManagerImpl implements KeyManager, Service, EventListener { private static final Logger LOG = Logger.getLogger(KeyManagerImpl.class.getName()); @@ -55,6 +56,7 @@ class KeyManagerImpl implements KeyManager, EventListener { managers = new ConcurrentHashMap(); } + @Override public boolean start() { eventBus.addListener(this); try { @@ -68,6 +70,7 @@ class KeyManagerImpl implements KeyManager, EventListener { return true; } + @Override public boolean stop() { eventBus.removeListener(this); return true; diff --git a/briar-tests/src/org/briarproject/sync/SimplexMessagingIntegrationTest.java b/briar-tests/src/org/briarproject/sync/SimplexMessagingIntegrationTest.java index 3a7be3b74..07eb61d57 100644 --- a/briar-tests/src/org/briarproject/sync/SimplexMessagingIntegrationTest.java +++ b/briar-tests/src/org/briarproject/sync/SimplexMessagingIntegrationTest.java @@ -5,7 +5,6 @@ import com.google.inject.Injector; import org.briarproject.BriarTestCase; import org.briarproject.TestDatabaseModule; -import org.briarproject.TestLifecycleModule; import org.briarproject.TestSystemModule; import org.briarproject.TestUtils; import org.briarproject.api.TransportId; @@ -21,6 +20,7 @@ import org.briarproject.api.identity.Author; import org.briarproject.api.identity.AuthorId; import org.briarproject.api.identity.IdentityManager; import org.briarproject.api.identity.LocalAuthor; +import org.briarproject.api.lifecycle.LifecycleManager; import org.briarproject.api.messaging.MessagingManager; import org.briarproject.api.messaging.PrivateMessage; import org.briarproject.api.messaging.PrivateMessageFactory; @@ -40,6 +40,7 @@ import org.briarproject.data.DataModule; import org.briarproject.db.DatabaseModule; import org.briarproject.event.EventModule; import org.briarproject.identity.IdentityModule; +import org.briarproject.lifecycle.LifecycleModule; import org.briarproject.messaging.MessagingModule; import org.briarproject.plugins.ImmediateExecutor; import org.briarproject.transport.TransportModule; @@ -78,17 +79,17 @@ public class SimplexMessagingIntegrationTest extends BriarTestCase { @Before public void setUp() { - testDir.mkdirs(); + assertTrue(testDir.mkdirs()); alice = createInjector(aliceDir); bob = createInjector(bobDir); } private Injector createInjector(File dir) { return Guice.createInjector(new TestDatabaseModule(dir), - new TestLifecycleModule(), new TestSystemModule(), - new ContactModule(), new CryptoModule(), new DatabaseModule(), - new DataModule(), new EventModule(), new IdentityModule(), - new SyncModule(), new MessagingModule(), new TransportModule()); + new TestSystemModule(), new ContactModule(), new CryptoModule(), + new DatabaseModule(), new DataModule(), new EventModule(), + new IdentityModule(), new LifecycleModule(), + new MessagingModule(), new SyncModule(), new TransportModule()); } @Test @@ -97,35 +98,44 @@ public class SimplexMessagingIntegrationTest extends BriarTestCase { } private byte[] write() throws Exception { - // Open Alice's database + // Instantiate Alice's services + LifecycleManager lifecycleManager = + alice.getInstance(LifecycleManager.class); DatabaseComponent db = alice.getInstance(DatabaseComponent.class); - assertFalse(db.open()); - // Add the transport - db.addTransport(transportId, MAX_LATENCY); - // Start Alice's key manager + IdentityManager identityManager = + alice.getInstance(IdentityManager.class); + ContactManager contactManager = alice.getInstance(ContactManager.class); + MessagingManager messagingManager = + alice.getInstance(MessagingManager.class); KeyManager keyManager = alice.getInstance(KeyManager.class); - keyManager.start(); + PrivateMessageFactory privateMessageFactory = + alice.getInstance(PrivateMessageFactory.class); + PacketWriterFactory packetWriterFactory = + alice.getInstance(PacketWriterFactory.class); + EventBus eventBus = alice.getInstance(EventBus.class); + StreamWriterFactory streamWriterFactory = + alice.getInstance(StreamWriterFactory.class); + + // Start the lifecycle manager + lifecycleManager.startServices(); + lifecycleManager.waitForStartup(); + // Add a transport + db.addTransport(transportId, MAX_LATENCY); // Add an identity for Alice LocalAuthor aliceAuthor = new LocalAuthor(aliceId, "Alice", new byte[MAX_PUBLIC_KEY_LENGTH], new byte[100], timestamp); - IdentityManager identityManager = - alice.getInstance(IdentityManager.class); identityManager.addLocalAuthor(aliceAuthor); // Add Bob as a contact Author bobAuthor = new Author(bobId, "Bob", new byte[MAX_PUBLIC_KEY_LENGTH]); - ContactManager contactManager = alice.getInstance(ContactManager.class); ContactId contactId = contactManager.addContact(bobAuthor, aliceId); - // Create the private conversation - MessagingManager messagingManager = - alice.getInstance(MessagingManager.class); + // Create a private conversation messagingManager.addContact(contactId); // Derive and store the transport keys keyManager.addContact(contactId, Collections.singletonList(transportId), master, timestamp, true); + // Send Bob a message - PrivateMessageFactory privateMessageFactory = - alice.getInstance(PrivateMessageFactory.class); GroupId groupId = messagingManager.getConversationId(contactId); byte[] body = "Hi Bob!".getBytes("UTF-8"); PrivateMessage message = privateMessageFactory.createPrivateMessage( @@ -136,14 +146,9 @@ public class SimplexMessagingIntegrationTest extends BriarTestCase { assertNotNull(ctx); // Create a stream writer ByteArrayOutputStream out = new ByteArrayOutputStream(); - StreamWriterFactory streamWriterFactory = - alice.getInstance(StreamWriterFactory.class); - OutputStream streamWriter = - streamWriterFactory.createStreamWriter(out, ctx); + OutputStream streamWriter = streamWriterFactory.createStreamWriter( + out, ctx); // Create an outgoing sync session - EventBus eventBus = alice.getInstance(EventBus.class); - PacketWriterFactory packetWriterFactory = - alice.getInstance(PacketWriterFactory.class); PacketWriter packetWriter = packetWriterFactory.createPacketWriter( streamWriter); SyncSession session = new SimplexOutgoingSession(db, @@ -152,40 +157,51 @@ public class SimplexMessagingIntegrationTest extends BriarTestCase { // Write whatever needs to be written session.run(); streamWriter.close(); + // Clean up - keyManager.stop(); - db.close(); + lifecycleManager.stopServices(); + lifecycleManager.waitForShutdown(); + // Return the contents of the stream return out.toByteArray(); } private void read(byte[] stream) throws Exception { - // Open Bob's database + // Instantiate Bob's services + LifecycleManager lifecycleManager = + bob.getInstance(LifecycleManager.class); DatabaseComponent db = bob.getInstance(DatabaseComponent.class); - assertFalse(db.open()); - // Add the transport - db.addTransport(transportId, MAX_LATENCY); - // Start Bob's key manager + IdentityManager identityManager = + bob.getInstance(IdentityManager.class); + ContactManager contactManager = bob.getInstance(ContactManager.class); + MessagingManager messagingManager = + bob.getInstance(MessagingManager.class); KeyManager keyManager = bob.getInstance(KeyManager.class); - keyManager.start(); + StreamReaderFactory streamReaderFactory = + bob.getInstance(StreamReaderFactory.class); + PacketReaderFactory packetReaderFactory = + bob.getInstance(PacketReaderFactory.class); + EventBus eventBus = bob.getInstance(EventBus.class); + + // Start the lifecyle manager + lifecycleManager.startServices(); + lifecycleManager.waitForStartup(); + // Add a transport + db.addTransport(transportId, MAX_LATENCY); // Add an identity for Bob LocalAuthor bobAuthor = new LocalAuthor(bobId, "Bob", new byte[MAX_PUBLIC_KEY_LENGTH], new byte[100], timestamp); - IdentityManager identityManager = - bob.getInstance(IdentityManager.class); identityManager.addLocalAuthor(bobAuthor); // Add Alice as a contact Author aliceAuthor = new Author(aliceId, "Alice", new byte[MAX_PUBLIC_KEY_LENGTH]); - ContactManager contactManager = bob.getInstance(ContactManager.class); ContactId contactId = contactManager.addContact(aliceAuthor, bobId); - // Create the private conversation - MessagingManager messagingManager = - bob.getInstance(MessagingManager.class); + // Create a private conversation messagingManager.addContact(contactId); // Derive and store the transport keys keyManager.addContact(contactId, Collections.singletonList(transportId), master, timestamp, false); + // Set up an event listener MessageListener listener = new MessageListener(); bob.getInstance(EventBus.class).addListener(listener); @@ -197,19 +213,13 @@ public class SimplexMessagingIntegrationTest extends BriarTestCase { StreamContext ctx = keyManager.getStreamContext(transportId, tag); assertNotNull(ctx); // Create a stream reader - StreamReaderFactory streamReaderFactory = - bob.getInstance(StreamReaderFactory.class); - InputStream streamReader = - streamReaderFactory.createStreamReader(in, ctx); + InputStream streamReader = streamReaderFactory.createStreamReader( + in, ctx); // Create an incoming sync session - EventBus eventBus = bob.getInstance(EventBus.class); - PacketReaderFactory packetReaderFactory = - bob.getInstance(PacketReaderFactory.class); PacketReader packetReader = packetReaderFactory.createPacketReader( streamReader); - SyncSession session = new IncomingSession(db, - new ImmediateExecutor(), eventBus, contactId, transportId, - packetReader); + SyncSession session = new IncomingSession(db, new ImmediateExecutor(), + eventBus, contactId, transportId, packetReader); // No messages should have been added yet assertFalse(listener.messageAdded); // Read whatever needs to be read @@ -217,9 +227,10 @@ public class SimplexMessagingIntegrationTest extends BriarTestCase { streamReader.close(); // The private message from Alice should have been added assertTrue(listener.messageAdded); + // Clean up - keyManager.stop(); - db.close(); + lifecycleManager.stopServices(); + lifecycleManager.waitForShutdown(); } @After From 82cf12040ff945d9ec51c78fcc2b0dc09e9f844a Mon Sep 17 00:00:00 2001 From: akwizgran Date: Tue, 19 Jan 2016 19:16:35 +0000 Subject: [PATCH 2/4] Contact manager hooks. #209 --- .../briarproject/android/AndroidModule.java | 18 ++-- .../AndroidNotificationManagerImpl.java | 7 +- .../org/briarproject/api/contact/Contact.java | 29 ++++++- .../api/contact/ContactManager.java | 14 +++ .../api/db/DatabaseComponent.java | 3 + .../api/messaging/MessagingManager.java | 6 -- .../api/sync/ValidationManager.java | 20 +++++ .../contact/ContactManagerImpl.java | 87 +++++++++++++++++-- .../briarproject/contact/ContactModule.java | 13 ++- .../src/org/briarproject/db/Database.java | 14 ++- .../db/DatabaseComponentImpl.java | 27 ++++-- .../src/org/briarproject/db/JdbcDatabase.java | 65 +++++++++----- .../org/briarproject/forum/ForumModule.java | 12 +-- .../forum/ForumPostValidator.java | 19 +--- .../invitation/AliceConnector.java | 8 +- .../briarproject/invitation/BobConnector.java | 8 +- .../briarproject/invitation/Connector.java | 7 +- .../invitation/ConnectorGroup.java | 12 +-- .../invitation/InvitationTaskFactoryImpl.java | 9 +- .../messaging/MessagingManagerImpl.java | 44 +++++++--- .../messaging/MessagingModule.java | 20 +++-- .../messaging/PrivateMessageValidator.java | 23 +---- .../src/org/briarproject/sync/SyncModule.java | 6 +- .../sync/ValidationManagerImpl.java | 21 +---- .../transport/KeyManagerImpl.java | 9 +- .../transport/TransportModule.java | 4 +- .../db/DatabaseComponentImplTest.java | 8 +- .../org/briarproject/sync/ConstantsTest.java | 7 +- .../sync/SimplexMessagingIntegrationTest.java | 8 +- 29 files changed, 333 insertions(+), 195 deletions(-) diff --git a/briar-android/src/org/briarproject/android/AndroidModule.java b/briar-android/src/org/briarproject/android/AndroidModule.java index 1adf33094..1a46ec730 100644 --- a/briar-android/src/org/briarproject/android/AndroidModule.java +++ b/briar-android/src/org/briarproject/android/AndroidModule.java @@ -1,23 +1,24 @@ package org.briarproject.android; -import static android.content.Context.MODE_PRIVATE; +import android.app.Application; -import java.io.File; - -import javax.inject.Singleton; +import com.google.inject.AbstractModule; +import com.google.inject.Provides; import org.briarproject.api.android.AndroidExecutor; import org.briarproject.api.android.AndroidNotificationManager; import org.briarproject.api.android.ReferenceManager; import org.briarproject.api.crypto.SecretKey; import org.briarproject.api.db.DatabaseConfig; +import org.briarproject.api.event.EventBus; import org.briarproject.api.lifecycle.LifecycleManager; import org.briarproject.api.ui.UiCallback; -import android.app.Application; +import java.io.File; -import com.google.inject.AbstractModule; -import com.google.inject.Provides; +import javax.inject.Singleton; + +import static android.content.Context.MODE_PRIVATE; public class AndroidModule extends AbstractModule { @@ -81,9 +82,10 @@ public class AndroidModule extends AbstractModule { @Provides @Singleton AndroidNotificationManager getAndroidNotificationManager( - LifecycleManager lifecycleManager, + LifecycleManager lifecycleManager, EventBus eventBus, AndroidNotificationManagerImpl notificationManager) { lifecycleManager.register(notificationManager); + eventBus.addListener(notificationManager); return notificationManager; } } diff --git a/briar-android/src/org/briarproject/android/AndroidNotificationManagerImpl.java b/briar-android/src/org/briarproject/android/AndroidNotificationManagerImpl.java index ca3ed6a4c..f83f83082 100644 --- a/briar-android/src/org/briarproject/android/AndroidNotificationManagerImpl.java +++ b/briar-android/src/org/briarproject/android/AndroidNotificationManagerImpl.java @@ -20,7 +20,6 @@ import org.briarproject.api.db.DatabaseComponent; import org.briarproject.api.db.DatabaseExecutor; import org.briarproject.api.db.DbException; import org.briarproject.api.event.Event; -import org.briarproject.api.event.EventBus; import org.briarproject.api.event.EventListener; import org.briarproject.api.event.MessageValidatedEvent; import org.briarproject.api.event.SettingsUpdatedEvent; @@ -61,7 +60,6 @@ class AndroidNotificationManagerImpl implements AndroidNotificationManager, private final DatabaseComponent db; private final Executor dbExecutor; - private final EventBus eventBus; private final MessagingManager messagingManager; private final ForumManager forumManager; private final AndroidExecutor androidExecutor; @@ -80,12 +78,11 @@ class AndroidNotificationManagerImpl implements AndroidNotificationManager, @Inject public AndroidNotificationManagerImpl(DatabaseComponent db, - @DatabaseExecutor Executor dbExecutor, EventBus eventBus, + @DatabaseExecutor Executor dbExecutor, MessagingManager messagingManager, ForumManager forumManager, AndroidExecutor androidExecutor, Application app) { this.db = db; this.dbExecutor = dbExecutor; - this.eventBus = eventBus; this.messagingManager = messagingManager; this.forumManager = forumManager; this.androidExecutor = androidExecutor; @@ -94,7 +91,6 @@ class AndroidNotificationManagerImpl implements AndroidNotificationManager, @Override public boolean start() { - eventBus.addListener(this); loadSettings(); return true; } @@ -114,7 +110,6 @@ class AndroidNotificationManagerImpl implements AndroidNotificationManager, @Override public boolean stop() { - eventBus.removeListener(this); clearNotifications(); return true; } diff --git a/briar-api/src/org/briarproject/api/contact/Contact.java b/briar-api/src/org/briarproject/api/contact/Contact.java index 5c65c57d2..585010ef7 100644 --- a/briar-api/src/org/briarproject/api/contact/Contact.java +++ b/briar-api/src/org/briarproject/api/contact/Contact.java @@ -5,14 +5,37 @@ import org.briarproject.api.identity.AuthorId; public class Contact { + public enum Status { + + ADDING(0), ACTIVE(1), REMOVING(2); + + private final int value; + + Status(int value) { + this.value = value; + } + + public int getValue() { + return value; + } + + public static Status fromValue(int value) { + for (Status s : values()) if (s.value == value) return s; + throw new IllegalArgumentException(); + } + } + private final ContactId id; private final Author author; private final AuthorId localAuthorId; + private final Status status; - public Contact(ContactId id, Author author, AuthorId localAuthorId) { + public Contact(ContactId id, Author author, AuthorId localAuthorId, + Status status) { this.id = id; this.author = author; this.localAuthorId = localAuthorId; + this.status = status; } public ContactId getId() { @@ -27,6 +50,10 @@ public class Contact { return localAuthorId; } + public Status getStatus() { + return status; + } + @Override public int hashCode() { return id.hashCode(); diff --git a/briar-api/src/org/briarproject/api/contact/ContactManager.java b/briar-api/src/org/briarproject/api/contact/ContactManager.java index f323c0737..9637d1491 100644 --- a/briar-api/src/org/briarproject/api/contact/ContactManager.java +++ b/briar-api/src/org/briarproject/api/contact/ContactManager.java @@ -8,6 +8,12 @@ import java.util.Collection; public interface ContactManager { + /** Registers a hook to be called whenever a contact is added. */ + void registerContactAddedHook(ContactAddedHook hook); + + /** Registers a hook to be called whenever a contact is removed. */ + void registerContactRemovedHook(ContactRemovedHook hook); + /** * Stores a contact associated with the given local and remote pseudonyms, * and returns an ID for the contact. @@ -22,4 +28,12 @@ public interface ContactManager { /** Removes a contact and all associated state. */ void removeContact(ContactId c) throws DbException; + + interface ContactAddedHook { + void contactAdded(ContactId c); + } + + interface ContactRemovedHook { + void contactRemoved(ContactId c); + } } diff --git a/briar-api/src/org/briarproject/api/db/DatabaseComponent.java b/briar-api/src/org/briarproject/api/db/DatabaseComponent.java index 84cca32ea..476198231 100644 --- a/briar-api/src/org/briarproject/api/db/DatabaseComponent.java +++ b/briar-api/src/org/briarproject/api/db/DatabaseComponent.java @@ -297,6 +297,9 @@ public interface DatabaseComponent { */ void removeTransport(TransportId t) throws DbException; + /** Sets the status of the given contact. */ + void setContactStatus(ContactId c, Contact.Status s) throws DbException; + /** Marks the given message as valid or invalid. */ void setMessageValidity(Message m, ClientId c, boolean valid) throws DbException; diff --git a/briar-api/src/org/briarproject/api/messaging/MessagingManager.java b/briar-api/src/org/briarproject/api/messaging/MessagingManager.java index 7ce3241f0..f22a5131a 100644 --- a/briar-api/src/org/briarproject/api/messaging/MessagingManager.java +++ b/briar-api/src/org/briarproject/api/messaging/MessagingManager.java @@ -13,12 +13,6 @@ public interface MessagingManager { /** Returns the unique ID of the messaging client. */ ClientId getClientId(); - /** - * Informs the messaging manager that a new contact has been added. - * Creates a private conversation with the contact. - */ - void addContact(ContactId c) throws DbException; - /** Stores a local private message. */ void addLocalMessage(PrivateMessage m) throws DbException; diff --git a/briar-api/src/org/briarproject/api/sync/ValidationManager.java b/briar-api/src/org/briarproject/api/sync/ValidationManager.java index ee1d7e967..35fcea79c 100644 --- a/briar-api/src/org/briarproject/api/sync/ValidationManager.java +++ b/briar-api/src/org/briarproject/api/sync/ValidationManager.java @@ -6,6 +6,26 @@ package org.briarproject.api.sync; */ public interface ValidationManager { + enum Status { + + UNKNOWN(0), INVALID(1), VALID(2); + + private final int value; + + Status(int value) { + this.value = value; + } + + public int getValue() { + return value; + } + + public static Status fromValue(int value) { + for (Status s : values()) if (s.value == value) return s; + throw new IllegalArgumentException(); + } + } + /** Sets the message validator for the given client. */ void setMessageValidator(ClientId c, MessageValidator v); } diff --git a/briar-core/src/org/briarproject/contact/ContactManagerImpl.java b/briar-core/src/org/briarproject/contact/ContactManagerImpl.java index 3a1648f16..3eb37298d 100644 --- a/briar-core/src/org/briarproject/contact/ContactManagerImpl.java +++ b/briar-core/src/org/briarproject/contact/ContactManagerImpl.java @@ -7,38 +7,115 @@ import org.briarproject.api.contact.ContactId; import org.briarproject.api.contact.ContactManager; import org.briarproject.api.db.DatabaseComponent; import org.briarproject.api.db.DbException; +import org.briarproject.api.db.NoSuchContactException; +import org.briarproject.api.event.ContactAddedEvent; +import org.briarproject.api.event.ContactRemovedEvent; +import org.briarproject.api.event.EventBus; import org.briarproject.api.identity.Author; import org.briarproject.api.identity.AuthorId; +import org.briarproject.api.lifecycle.Service; +import java.util.ArrayList; import java.util.Collection; +import java.util.Collections; +import java.util.List; +import java.util.concurrent.CopyOnWriteArrayList; +import java.util.logging.Logger; -class ContactManagerImpl implements ContactManager { +import static java.util.logging.Level.WARNING; +import static org.briarproject.api.contact.Contact.Status.ACTIVE; +import static org.briarproject.api.contact.Contact.Status.ADDING; +import static org.briarproject.api.contact.Contact.Status.REMOVING; + +class ContactManagerImpl implements ContactManager, Service { + + private static final Logger LOG = + Logger.getLogger(ContactManagerImpl.class.getName()); private final DatabaseComponent db; + private final EventBus eventBus; + private final List addHooks; + private final List removeHooks; @Inject - ContactManagerImpl(DatabaseComponent db) { + ContactManagerImpl(DatabaseComponent db, EventBus eventBus) { this.db = db; + this.eventBus = eventBus; + addHooks = new CopyOnWriteArrayList(); + removeHooks = new CopyOnWriteArrayList(); + } + + @Override + public boolean start() { + // Finish adding/removing any partly added/removed contacts + try { + for (Contact c : db.getContacts()) { + if (c.getStatus().equals(ADDING)) { + for (ContactAddedHook hook : addHooks) + hook.contactAdded(c.getId()); + db.setContactStatus(c.getId(), ACTIVE); + eventBus.broadcast(new ContactAddedEvent(c.getId())); + } else if (c.getStatus().equals(REMOVING)) { + for (ContactRemovedHook hook : removeHooks) + hook.contactRemoved(c.getId()); + db.removeContact(c.getId()); + eventBus.broadcast(new ContactRemovedEvent(c.getId())); + } + } + return true; + } catch (DbException e) { + if (LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e); + return false; + } + } + + @Override + public boolean stop() { + return true; + } + + @Override + public void registerContactAddedHook(ContactAddedHook hook) { + addHooks.add(hook); + } + + @Override + public void registerContactRemovedHook(ContactRemovedHook hook) { + removeHooks.add(hook); } @Override public ContactId addContact(Author remote, AuthorId local) throws DbException { - return db.addContact(remote, local); + ContactId c = db.addContact(remote, local); + for (ContactAddedHook hook : addHooks) hook.contactAdded(c); + db.setContactStatus(c, ACTIVE); + eventBus.broadcast(new ContactAddedEvent(c)); + return c; } @Override public Contact getContact(ContactId c) throws DbException { - return db.getContact(c); + Contact contact = db.getContact(c); + if (contact.getStatus().equals(ACTIVE)) return contact; + throw new NoSuchContactException(); } @Override public Collection getContacts() throws DbException { - return db.getContacts(); + Collection contacts = db.getContacts(); + // Filter out any contacts that are being added or removed + List active = new ArrayList(contacts.size()); + for (Contact c : contacts) + if (c.getStatus().equals(ACTIVE)) active.add(c); + return Collections.unmodifiableList(active); } @Override public void removeContact(ContactId c) throws DbException { + db.setContactStatus(c, REMOVING); + for (ContactRemovedHook hook : removeHooks) hook.contactRemoved(c); db.removeContact(c); + eventBus.broadcast(new ContactRemovedEvent(c)); } } diff --git a/briar-core/src/org/briarproject/contact/ContactModule.java b/briar-core/src/org/briarproject/contact/ContactModule.java index 6bd786aa3..105aa9ef1 100644 --- a/briar-core/src/org/briarproject/contact/ContactModule.java +++ b/briar-core/src/org/briarproject/contact/ContactModule.java @@ -1,13 +1,22 @@ package org.briarproject.contact; import com.google.inject.AbstractModule; +import com.google.inject.Provides; import org.briarproject.api.contact.ContactManager; +import org.briarproject.api.lifecycle.LifecycleManager; + +import javax.inject.Singleton; public class ContactModule extends AbstractModule { @Override - protected void configure() { - bind(ContactManager.class).to(ContactManagerImpl.class); + protected void configure() {} + + @Provides @Singleton + ContactManager getContactManager(LifecycleManager lifecycleManager, + ContactManagerImpl contactManager) { + lifecycleManager.register(contactManager); + return contactManager; } } diff --git a/briar-core/src/org/briarproject/db/Database.java b/briar-core/src/org/briarproject/db/Database.java index f512b8553..39790deed 100644 --- a/briar-core/src/org/briarproject/db/Database.java +++ b/briar-core/src/org/briarproject/db/Database.java @@ -631,7 +631,19 @@ interface Database { */ void resetExpiryTime(T txn, ContactId c, MessageId m) throws DbException; - /** Marks the given message as valid or invalid. */ + /** + * Sets the status of the given contact. + *

+ * Locking: write. + */ + void setContactStatus(T txn, ContactId c, Contact.Status s) + throws DbException; + + /** + * Marks the given message as valid or invalid. + *

+ * Locking: write. + */ void setMessageValidity(T txn, MessageId m, boolean valid) throws DbException; diff --git a/briar-core/src/org/briarproject/db/DatabaseComponentImpl.java b/briar-core/src/org/briarproject/db/DatabaseComponentImpl.java index 3378b8ff5..546434ff5 100644 --- a/briar-core/src/org/briarproject/db/DatabaseComponentImpl.java +++ b/briar-core/src/org/briarproject/db/DatabaseComponentImpl.java @@ -16,7 +16,6 @@ import org.briarproject.api.db.NoSuchLocalAuthorException; import org.briarproject.api.db.NoSuchMessageException; import org.briarproject.api.db.NoSuchSubscriptionException; import org.briarproject.api.db.NoSuchTransportException; -import org.briarproject.api.event.ContactAddedEvent; import org.briarproject.api.event.ContactRemovedEvent; import org.briarproject.api.event.EventBus; import org.briarproject.api.event.LocalAuthorAddedEvent; @@ -147,7 +146,6 @@ class DatabaseComponentImpl implements DatabaseComponent { public ContactId addContact(Author remote, AuthorId local) throws DbException { - ContactId c; lock.writeLock().lock(); try { T txn = db.startTransaction(); @@ -156,8 +154,9 @@ class DatabaseComponentImpl implements DatabaseComponent { throw new ContactExistsException(); if (!db.containsLocalAuthor(txn, local)) throw new NoSuchLocalAuthorException(); - c = db.addContact(txn, remote, local); + ContactId c = db.addContact(txn, remote, local); db.commitTransaction(txn); + return c; } catch (DbException e) { db.abortTransaction(txn); throw e; @@ -165,8 +164,6 @@ class DatabaseComponentImpl implements DatabaseComponent { } finally { lock.writeLock().unlock(); } - eventBus.broadcast(new ContactAddedEvent(c)); - return c; } public void addContactGroup(ContactId c, Group g) throws DbException { @@ -1219,7 +1216,6 @@ class DatabaseComponentImpl implements DatabaseComponent { } finally { lock.writeLock().unlock(); } - eventBus.broadcast(new ContactRemovedEvent(c)); } public void removeGroup(Group g) throws DbException { @@ -1287,6 +1283,25 @@ class DatabaseComponentImpl implements DatabaseComponent { eventBus.broadcast(new TransportRemovedEvent(t)); } + public void setContactStatus(ContactId c, Contact.Status s) + throws DbException { + lock.writeLock().lock(); + try { + T txn = db.startTransaction(); + try { + if (!db.containsContact(txn, c)) + throw new NoSuchContactException(); + db.setContactStatus(txn, c, s); + db.commitTransaction(txn); + } catch (DbException e) { + db.abortTransaction(txn); + throw e; + } + } finally { + lock.writeLock().unlock(); + } + } + public void setMessageValidity(Message m, ClientId c, boolean valid) throws DbException { lock.writeLock().lock(); diff --git a/briar-core/src/org/briarproject/db/JdbcDatabase.java b/briar-core/src/org/briarproject/db/JdbcDatabase.java index 80c9a1dd9..1faf11bda 100644 --- a/briar-core/src/org/briarproject/db/JdbcDatabase.java +++ b/briar-core/src/org/briarproject/db/JdbcDatabase.java @@ -50,8 +50,12 @@ import java.util.concurrent.locks.ReentrantLock; import java.util.logging.Logger; import static java.util.logging.Level.WARNING; +import static org.briarproject.api.contact.Contact.Status.ADDING; import static org.briarproject.api.db.Metadata.REMOVE; import static org.briarproject.api.sync.SyncConstants.MAX_SUBSCRIPTIONS; +import static org.briarproject.api.sync.ValidationManager.Status.INVALID; +import static org.briarproject.api.sync.ValidationManager.Status.UNKNOWN; +import static org.briarproject.api.sync.ValidationManager.Status.VALID; import static org.briarproject.db.ExponentialBackoff.calculateExpiry; /** @@ -60,12 +64,8 @@ import static org.briarproject.db.ExponentialBackoff.calculateExpiry; */ abstract class JdbcDatabase implements Database { - private static final int SCHEMA_VERSION = 14; - private static final int MIN_SCHEMA_VERSION = 14; - - private static final int VALIDATION_UNKNOWN = 0; - private static final int VALIDATION_INVALID = 1; - private static final int VALIDATION_VALID = 2; + private static final int SCHEMA_VERSION = 15; + private static final int MIN_SCHEMA_VERSION = 15; private static final String CREATE_SETTINGS = "CREATE TABLE settings" @@ -90,6 +90,7 @@ abstract class JdbcDatabase implements Database { + " name VARCHAR NOT NULL," + " publicKey BINARY NOT NULL," + " localAuthorId HASH NOT NULL," + + " status INT NOT NULL," + " PRIMARY KEY (contactId)," + " UNIQUE (authorId)," + " FOREIGN KEY (localAuthorId)" @@ -533,13 +534,14 @@ abstract class JdbcDatabase implements Database { try { // Create a contact row String sql = "INSERT INTO contacts" - + " (authorId, name, publicKey, localAuthorId)" - + " VALUES (?, ?, ?, ?)"; + + " (authorId, name, publicKey, localAuthorId, status)" + + " VALUES (?, ?, ?, ?, ?)"; ps = txn.prepareStatement(sql); ps.setBytes(1, remote.getId().getBytes()); ps.setString(2, remote.getName()); ps.setBytes(3, remote.getPublicKey()); ps.setBytes(4, local.getBytes()); + ps.setInt(5, ADDING.getValue()); int affected = ps.executeUpdate(); if (affected != 1) throw new DbStateException(); ps.close(); @@ -747,7 +749,7 @@ abstract class JdbcDatabase implements Database { ps.setBytes(2, m.getGroupId().getBytes()); ps.setLong(3, m.getTimestamp()); ps.setBoolean(4, local); - ps.setInt(5, local ? VALIDATION_VALID : VALIDATION_UNKNOWN); + ps.setInt(5, local ? VALID.getValue() : UNKNOWN.getValue()); byte[] raw = m.getRaw(); ps.setInt(6, raw.length); ps.setBytes(7, raw); @@ -1192,7 +1194,8 @@ abstract class JdbcDatabase implements Database { PreparedStatement ps = null; ResultSet rs = null; try { - String sql = "SELECT authorId, name, publicKey, localAuthorId" + String sql = "SELECT authorId, name, publicKey, localAuthorId," + + " status" + " FROM contacts" + " WHERE contactId = ?"; ps = txn.prepareStatement(sql); @@ -1203,10 +1206,11 @@ abstract class JdbcDatabase implements Database { String name = rs.getString(2); byte[] publicKey = rs.getBytes(3); AuthorId localAuthorId = new AuthorId(rs.getBytes(4)); + Contact.Status status = Contact.Status.fromValue(rs.getInt(5)); rs.close(); ps.close(); Author author = new Author(authorId, name, publicKey); - return new Contact(c, author, localAuthorId); + return new Contact(c, author, localAuthorId, status); } catch (SQLException e) { tryToClose(rs); tryToClose(ps); @@ -1240,7 +1244,7 @@ abstract class JdbcDatabase implements Database { ResultSet rs = null; try { String sql = "SELECT contactId, authorId, name, publicKey," - + " localAuthorId" + + " localAuthorId, status" + " FROM contacts"; ps = txn.prepareStatement(sql); rs = ps.executeQuery(); @@ -1252,7 +1256,9 @@ abstract class JdbcDatabase implements Database { byte[] publicKey = rs.getBytes(4); Author author = new Author(authorId, name, publicKey); AuthorId localAuthorId = new AuthorId(rs.getBytes(5)); - contacts.add(new Contact(contactId, author, localAuthorId)); + Contact.Status status = Contact.Status.fromValue(rs.getInt(6)); + contacts.add(new Contact(contactId, author, localAuthorId, + status)); } rs.close(); ps.close(); @@ -1612,7 +1618,7 @@ abstract class JdbcDatabase implements Database { + " ORDER BY timestamp DESC LIMIT ?"; ps = txn.prepareStatement(sql); ps.setInt(1, c.getInt()); - ps.setInt(2, VALIDATION_VALID); + ps.setInt(2, VALID.getValue()); ps.setLong(3, now); ps.setInt(4, maxMessages); rs = ps.executeQuery(); @@ -1674,7 +1680,7 @@ abstract class JdbcDatabase implements Database { + " ORDER BY timestamp DESC"; ps = txn.prepareStatement(sql); ps.setInt(1, c.getInt()); - ps.setInt(2, VALIDATION_VALID); + ps.setInt(2, VALID.getValue()); ps.setLong(3, now); rs = ps.executeQuery(); List ids = new ArrayList(); @@ -1704,7 +1710,7 @@ abstract class JdbcDatabase implements Database { + " JOIN groups AS g ON m.groupId = g.groupId" + " WHERE valid = ? AND clientId = ?"; ps = txn.prepareStatement(sql); - ps.setInt(1, VALIDATION_UNKNOWN); + ps.setInt(1, UNKNOWN.getValue()); ps.setBytes(2, c.getBytes()); rs = ps.executeQuery(); List ids = new ArrayList(); @@ -1799,7 +1805,7 @@ abstract class JdbcDatabase implements Database { + " ORDER BY timestamp DESC"; ps = txn.prepareStatement(sql); ps.setInt(1, c.getInt()); - ps.setInt(2, VALIDATION_VALID); + ps.setInt(2, VALID.getValue()); ps.setLong(3, now); rs = ps.executeQuery(); List ids = new ArrayList(); @@ -1846,7 +1852,7 @@ abstract class JdbcDatabase implements Database { ResultSet rs = null; try { String sql = "SELECT c.contactId, authorId, c.name, publicKey," - + " localAuthorId" + + " localAuthorId, status" + " FROM contacts AS c" + " JOIN contactGroups AS cg" + " ON c.contactId = cg.contactId" @@ -1862,7 +1868,9 @@ abstract class JdbcDatabase implements Database { byte[] publicKey = rs.getBytes(4); Author author = new Author(authorId, name, publicKey); AuthorId localAuthorId = new AuthorId(rs.getBytes(5)); - contacts.add(new Contact(contactId, author, localAuthorId)); + Contact.Status status = Contact.Status.fromValue(rs.getInt(6)); + contacts.add(new Contact(contactId, author, localAuthorId, + status)); } rs.close(); ps.close(); @@ -2687,13 +2695,30 @@ abstract class JdbcDatabase implements Database { } } + public void setContactStatus(Connection txn, ContactId c, Contact.Status s) + throws DbException { + PreparedStatement ps = null; + try { + String sql = "UPDATE contacts SET status = ? WHERE contactId = ?"; + ps = txn.prepareStatement(sql); + ps.setInt(1, s.getValue()); + ps.setInt(2, c.getInt()); + int affected = ps.executeUpdate(); + if (affected < 0 || affected > 1) throw new DbStateException(); + ps.close(); + } catch (SQLException e) { + tryToClose(ps); + throw new DbException(e); + } + } + public void setMessageValidity(Connection txn, MessageId m, boolean valid) throws DbException { PreparedStatement ps = null; try { String sql = "UPDATE messages SET valid = ? WHERE messageId = ?"; ps = txn.prepareStatement(sql); - ps.setInt(1, valid ? VALIDATION_VALID : VALIDATION_INVALID); + ps.setInt(1, valid ? VALID.getValue() : INVALID.getValue()); ps.setBytes(2, m.getBytes()); int affected = ps.executeUpdate(); if (affected < 0) throw new DbStateException(); diff --git a/briar-core/src/org/briarproject/forum/ForumModule.java b/briar-core/src/org/briarproject/forum/ForumModule.java index 60ac05bbe..94f0f829c 100644 --- a/briar-core/src/org/briarproject/forum/ForumModule.java +++ b/briar-core/src/org/briarproject/forum/ForumModule.java @@ -11,7 +11,6 @@ import org.briarproject.api.data.ObjectReader; import org.briarproject.api.forum.ForumManager; import org.briarproject.api.forum.ForumPostFactory; import org.briarproject.api.identity.Author; -import org.briarproject.api.lifecycle.LifecycleManager; import org.briarproject.api.sync.ValidationManager; import org.briarproject.api.system.Clock; @@ -26,16 +25,17 @@ public class ForumModule extends AbstractModule { } @Provides @Singleton - ForumPostValidator getValidator(LifecycleManager lifecycleManager, - CryptoComponent crypto, ValidationManager validationManager, + ForumPostValidator getValidator(ValidationManager validationManager, + ForumManager forumManager, CryptoComponent crypto, BdfReaderFactory bdfReaderFactory, BdfWriterFactory bdfWriterFactory, ObjectReader authorReader, MetadataEncoder metadataEncoder, Clock clock) { ForumPostValidator validator = new ForumPostValidator(crypto, - validationManager, bdfReaderFactory, bdfWriterFactory, - authorReader, metadataEncoder, clock); - lifecycleManager.register(validator); + bdfReaderFactory, bdfWriterFactory, authorReader, + metadataEncoder, clock); + validationManager.setMessageValidator(forumManager.getClientId(), + validator); return validator; } } diff --git a/briar-core/src/org/briarproject/forum/ForumPostValidator.java b/briar-core/src/org/briarproject/forum/ForumPostValidator.java index 5f321d33d..1ee770cf1 100644 --- a/briar-core/src/org/briarproject/forum/ForumPostValidator.java +++ b/briar-core/src/org/briarproject/forum/ForumPostValidator.java @@ -15,11 +15,9 @@ import org.briarproject.api.data.MetadataEncoder; import org.briarproject.api.data.ObjectReader; import org.briarproject.api.db.Metadata; import org.briarproject.api.identity.Author; -import org.briarproject.api.lifecycle.Service; import org.briarproject.api.sync.Message; import org.briarproject.api.sync.MessageId; import org.briarproject.api.sync.MessageValidator; -import org.briarproject.api.sync.ValidationManager; import org.briarproject.api.system.Clock; import java.io.ByteArrayInputStream; @@ -35,15 +33,13 @@ import static org.briarproject.api.forum.ForumConstants.MAX_FORUM_POST_BODY_LENG import static org.briarproject.api.identity.AuthorConstants.MAX_SIGNATURE_LENGTH; import static org.briarproject.api.sync.SyncConstants.MESSAGE_HEADER_LENGTH; import static org.briarproject.api.transport.TransportConstants.MAX_CLOCK_DIFFERENCE; -import static org.briarproject.forum.ForumManagerImpl.CLIENT_ID; -class ForumPostValidator implements MessageValidator, Service { +class ForumPostValidator implements MessageValidator { private static final Logger LOG = Logger.getLogger(ForumPostValidator.class.getName()); private final CryptoComponent crypto; - private final ValidationManager validationManager; private final BdfReaderFactory bdfReaderFactory; private final BdfWriterFactory bdfWriterFactory; private final ObjectReader authorReader; @@ -53,13 +49,11 @@ class ForumPostValidator implements MessageValidator, Service { @Inject ForumPostValidator(CryptoComponent crypto, - ValidationManager validationManager, BdfReaderFactory bdfReaderFactory, BdfWriterFactory bdfWriterFactory, ObjectReader authorReader, MetadataEncoder metadataEncoder, Clock clock) { this.crypto = crypto; - this.validationManager = validationManager; this.bdfReaderFactory = bdfReaderFactory; this.bdfWriterFactory = bdfWriterFactory; this.authorReader = authorReader; @@ -68,17 +62,6 @@ class ForumPostValidator implements MessageValidator, Service { keyParser = crypto.getSignatureKeyParser(); } - @Override - public boolean start() { - validationManager.setMessageValidator(CLIENT_ID, this); - return true; - } - - @Override - public boolean stop() { - return true; - } - @Override public Metadata validateMessage(Message m) { // Reject the message if it's too far in the future diff --git a/briar-core/src/org/briarproject/invitation/AliceConnector.java b/briar-core/src/org/briarproject/invitation/AliceConnector.java index 794f542ec..590fc5458 100644 --- a/briar-core/src/org/briarproject/invitation/AliceConnector.java +++ b/briar-core/src/org/briarproject/invitation/AliceConnector.java @@ -14,7 +14,6 @@ import org.briarproject.api.db.DbException; import org.briarproject.api.identity.Author; import org.briarproject.api.identity.AuthorFactory; import org.briarproject.api.identity.LocalAuthor; -import org.briarproject.api.messaging.MessagingManager; import org.briarproject.api.plugins.ConnectionManager; import org.briarproject.api.plugins.duplex.DuplexPlugin; import org.briarproject.api.plugins.duplex.DuplexTransportConnection; @@ -48,7 +47,7 @@ class AliceConnector extends Connector { StreamWriterFactory streamWriterFactory, AuthorFactory authorFactory, GroupFactory groupFactory, KeyManager keyManager, ConnectionManager connectionManager, - ContactManager contactManager, MessagingManager messagingManager, + ContactManager contactManager, TransportPropertyManager transportPropertyManager, Clock clock, boolean reuseConnection, ConnectorGroup group, DuplexPlugin plugin, LocalAuthor localAuthor, @@ -57,9 +56,8 @@ class AliceConnector extends Connector { super(crypto, bdfReaderFactory, bdfWriterFactory, streamReaderFactory, streamWriterFactory, authorFactory, groupFactory, keyManager, connectionManager, contactManager, - messagingManager, transportPropertyManager, clock, - reuseConnection, group, plugin, localAuthor, localProps, - random); + transportPropertyManager, clock, reuseConnection, group, + plugin, localAuthor, localProps, random); } @Override diff --git a/briar-core/src/org/briarproject/invitation/BobConnector.java b/briar-core/src/org/briarproject/invitation/BobConnector.java index 270ecfbf1..584a06a3d 100644 --- a/briar-core/src/org/briarproject/invitation/BobConnector.java +++ b/briar-core/src/org/briarproject/invitation/BobConnector.java @@ -14,7 +14,6 @@ import org.briarproject.api.db.DbException; import org.briarproject.api.identity.Author; import org.briarproject.api.identity.AuthorFactory; import org.briarproject.api.identity.LocalAuthor; -import org.briarproject.api.messaging.MessagingManager; import org.briarproject.api.plugins.ConnectionManager; import org.briarproject.api.plugins.duplex.DuplexPlugin; import org.briarproject.api.plugins.duplex.DuplexTransportConnection; @@ -48,7 +47,7 @@ class BobConnector extends Connector { StreamWriterFactory streamWriterFactory, AuthorFactory authorFactory, GroupFactory groupFactory, KeyManager keyManager, ConnectionManager connectionManager, - ContactManager contactManager, MessagingManager messagingManager, + ContactManager contactManager, TransportPropertyManager transportPropertyManager, Clock clock, boolean reuseConnection, ConnectorGroup group, DuplexPlugin plugin, LocalAuthor localAuthor, @@ -57,9 +56,8 @@ class BobConnector extends Connector { super(crypto, bdfReaderFactory, bdfWriterFactory, streamReaderFactory, streamWriterFactory, authorFactory, groupFactory, keyManager, connectionManager, contactManager, - messagingManager, transportPropertyManager, clock, - reuseConnection, group, plugin, localAuthor, localProps, - random); + transportPropertyManager, clock, reuseConnection, group, + plugin, localAuthor, localProps, random); } @Override diff --git a/briar-core/src/org/briarproject/invitation/Connector.java b/briar-core/src/org/briarproject/invitation/Connector.java index deb68a9d8..21f84ef2f 100644 --- a/briar-core/src/org/briarproject/invitation/Connector.java +++ b/briar-core/src/org/briarproject/invitation/Connector.java @@ -20,7 +20,6 @@ import org.briarproject.api.db.DbException; import org.briarproject.api.identity.Author; import org.briarproject.api.identity.AuthorFactory; import org.briarproject.api.identity.LocalAuthor; -import org.briarproject.api.messaging.MessagingManager; import org.briarproject.api.plugins.ConnectionManager; import org.briarproject.api.plugins.duplex.DuplexPlugin; import org.briarproject.api.plugins.duplex.DuplexTransportConnection; @@ -65,7 +64,6 @@ abstract class Connector extends Thread { protected final KeyManager keyManager; protected final ConnectionManager connectionManager; protected final ContactManager contactManager; - protected final MessagingManager messagingManager; protected final TransportPropertyManager transportPropertyManager; protected final Clock clock; protected final boolean reuseConnection; @@ -89,7 +87,7 @@ abstract class Connector extends Thread { StreamWriterFactory streamWriterFactory, AuthorFactory authorFactory, GroupFactory groupFactory, KeyManager keyManager, ConnectionManager connectionManager, - ContactManager contactManager, MessagingManager messagingManager, + ContactManager contactManager, TransportPropertyManager transportPropertyManager, Clock clock, boolean reuseConnection, ConnectorGroup group, DuplexPlugin plugin, LocalAuthor localAuthor, @@ -106,7 +104,6 @@ abstract class Connector extends Thread { this.keyManager = keyManager; this.connectionManager = connectionManager; this.contactManager = contactManager; - this.messagingManager = messagingManager; this.transportPropertyManager = transportPropertyManager; this.clock = clock; this.reuseConnection = reuseConnection; @@ -287,8 +284,6 @@ abstract class Connector extends Thread { // Derive transport keys for each transport shared with the contact keyManager.addContact(contactId, remoteProps.keySet(), master, timestamp, alice); - // Create a private messaging conversation - messagingManager.addContact(contactId); } protected void tryToClose(DuplexTransportConnection conn, diff --git a/briar-core/src/org/briarproject/invitation/ConnectorGroup.java b/briar-core/src/org/briarproject/invitation/ConnectorGroup.java index 378fc54a5..1a24b782c 100644 --- a/briar-core/src/org/briarproject/invitation/ConnectorGroup.java +++ b/briar-core/src/org/briarproject/invitation/ConnectorGroup.java @@ -16,7 +16,6 @@ import org.briarproject.api.identity.LocalAuthor; import org.briarproject.api.invitation.InvitationListener; import org.briarproject.api.invitation.InvitationState; import org.briarproject.api.invitation.InvitationTask; -import org.briarproject.api.messaging.MessagingManager; import org.briarproject.api.plugins.ConnectionManager; import org.briarproject.api.plugins.PluginManager; import org.briarproject.api.plugins.duplex.DuplexPlugin; @@ -58,7 +57,6 @@ class ConnectorGroup extends Thread implements InvitationTask { private final ConnectionManager connectionManager; private final IdentityManager identityManager; private final ContactManager contactManager; - private final MessagingManager messagingManager; private final TransportPropertyManager transportPropertyManager; private final Clock clock; private final PluginManager pluginManager; @@ -85,7 +83,6 @@ class ConnectorGroup extends Thread implements InvitationTask { AuthorFactory authorFactory, GroupFactory groupFactory, KeyManager keyManager, ConnectionManager connectionManager, IdentityManager identityManager, ContactManager contactManager, - MessagingManager messagingManager, TransportPropertyManager transportPropertyManager, Clock clock, PluginManager pluginManager, AuthorId localAuthorId, int localInvitationCode, int remoteInvitationCode, @@ -102,7 +99,6 @@ class ConnectorGroup extends Thread implements InvitationTask { this.connectionManager = connectionManager; this.identityManager = identityManager; this.contactManager = contactManager; - this.messagingManager = messagingManager; this.transportPropertyManager = transportPropertyManager; this.clock = clock; this.pluginManager = pluginManager; @@ -201,8 +197,8 @@ class ConnectorGroup extends Thread implements InvitationTask { return new AliceConnector(crypto, bdfReaderFactory, bdfWriterFactory, streamReaderFactory, streamWriterFactory, authorFactory, groupFactory, keyManager, connectionManager, contactManager, - messagingManager, transportPropertyManager, clock, - reuseConnection, this, plugin, localAuthor, localProps, random); + transportPropertyManager, clock, reuseConnection, this, plugin, + localAuthor, localProps, random); } private Connector createBobConnector(DuplexPlugin plugin, @@ -213,8 +209,8 @@ class ConnectorGroup extends Thread implements InvitationTask { return new BobConnector(crypto, bdfReaderFactory, bdfWriterFactory, streamReaderFactory, streamWriterFactory, authorFactory, groupFactory, keyManager, connectionManager, contactManager, - messagingManager, transportPropertyManager, clock, - reuseConnection, this, plugin, localAuthor, localProps, random); + transportPropertyManager, clock, reuseConnection, this, plugin, + localAuthor, localProps, random); } public void localConfirmationSucceeded() { diff --git a/briar-core/src/org/briarproject/invitation/InvitationTaskFactoryImpl.java b/briar-core/src/org/briarproject/invitation/InvitationTaskFactoryImpl.java index 9879f7f55..db131b376 100644 --- a/briar-core/src/org/briarproject/invitation/InvitationTaskFactoryImpl.java +++ b/briar-core/src/org/briarproject/invitation/InvitationTaskFactoryImpl.java @@ -9,7 +9,6 @@ import org.briarproject.api.identity.AuthorId; import org.briarproject.api.identity.IdentityManager; import org.briarproject.api.invitation.InvitationTask; import org.briarproject.api.invitation.InvitationTaskFactory; -import org.briarproject.api.messaging.MessagingManager; import org.briarproject.api.plugins.ConnectionManager; import org.briarproject.api.plugins.PluginManager; import org.briarproject.api.property.TransportPropertyManager; @@ -34,7 +33,6 @@ class InvitationTaskFactoryImpl implements InvitationTaskFactory { private final ConnectionManager connectionManager; private final IdentityManager identityManager; private final ContactManager contactManager; - private final MessagingManager messagingManager; private final TransportPropertyManager transportPropertyManager; private final Clock clock; private final PluginManager pluginManager; @@ -47,7 +45,6 @@ class InvitationTaskFactoryImpl implements InvitationTaskFactory { AuthorFactory authorFactory, GroupFactory groupFactory, KeyManager keyManager, ConnectionManager connectionManager, IdentityManager identityManager, ContactManager contactManager, - MessagingManager messagingManager, TransportPropertyManager transportPropertyManager, Clock clock, PluginManager pluginManager) { this.crypto = crypto; @@ -61,7 +58,6 @@ class InvitationTaskFactoryImpl implements InvitationTaskFactory { this.connectionManager = connectionManager; this.identityManager = identityManager; this.contactManager = contactManager; - this.messagingManager = messagingManager; this.transportPropertyManager = transportPropertyManager; this.clock = clock; this.pluginManager = pluginManager; @@ -72,8 +68,7 @@ class InvitationTaskFactoryImpl implements InvitationTaskFactory { return new ConnectorGroup(crypto, bdfReaderFactory, bdfWriterFactory, streamReaderFactory, streamWriterFactory, authorFactory, groupFactory, keyManager, connectionManager, identityManager, - contactManager, messagingManager, transportPropertyManager, - clock, pluginManager, localAuthorId, localCode, remoteCode, - reuseConnection); + contactManager, transportPropertyManager, clock, pluginManager, + localAuthorId, localCode, remoteCode, reuseConnection); } } diff --git a/briar-core/src/org/briarproject/messaging/MessagingManagerImpl.java b/briar-core/src/org/briarproject/messaging/MessagingManagerImpl.java index 39ba71647..37f5acafe 100644 --- a/briar-core/src/org/briarproject/messaging/MessagingManagerImpl.java +++ b/briar-core/src/org/briarproject/messaging/MessagingManagerImpl.java @@ -6,6 +6,8 @@ import org.briarproject.api.FormatException; import org.briarproject.api.UniqueId; import org.briarproject.api.contact.Contact; import org.briarproject.api.contact.ContactId; +import org.briarproject.api.contact.ContactManager.ContactAddedHook; +import org.briarproject.api.contact.ContactManager.ContactRemovedHook; import org.briarproject.api.data.BdfDictionary; import org.briarproject.api.data.BdfReader; import org.briarproject.api.data.BdfReaderFactory; @@ -42,7 +44,8 @@ import static java.util.logging.Level.WARNING; import static org.briarproject.api.messaging.MessagingConstants.MAX_PRIVATE_MESSAGE_BODY_LENGTH; import static org.briarproject.api.sync.SyncConstants.MESSAGE_HEADER_LENGTH; -class MessagingManagerImpl implements MessagingManager { +class MessagingManagerImpl implements MessagingManager, ContactAddedHook, + ContactRemovedHook { static final ClientId CLIENT_ID = new ClientId(StringUtils.fromHexString( "6bcdc006c0910b0f44e40644c3b31f1a" @@ -72,18 +75,17 @@ class MessagingManagerImpl implements MessagingManager { } @Override - public ClientId getClientId() { - return CLIENT_ID; - } - - @Override - public void addContact(ContactId c) throws DbException { - // Create the conversation group - Group conversation = createConversationGroup(db.getContact(c)); - // Subscribe to the group and share it with the contact - db.addGroup(conversation); - db.addContactGroup(c, conversation); - db.setVisibility(conversation.getId(), Collections.singletonList(c)); + public void contactAdded(ContactId c) { + try { + // Create the conversation group + Group g = createConversationGroup(db.getContact(c)); + // Subscribe to the group and share it with the contact + db.addGroup(g); + db.addContactGroup(c, g); + db.setVisibility(g.getId(), Collections.singletonList(c)); + } catch (DbException e) { + if (LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e); + } } private Group createConversationGroup(Contact c) { @@ -113,6 +115,20 @@ class MessagingManagerImpl implements MessagingManager { return out.toByteArray(); } + @Override + public void contactRemoved(ContactId c) { + try { + db.removeGroup(createConversationGroup(db.getContact(c))); + } catch (DbException e) { + if (LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e); + } + } + + @Override + public ClientId getClientId() { + return CLIENT_ID; + } + @Override public void addLocalMessage(PrivateMessage m) throws DbException { BdfDictionary d = new BdfDictionary(); @@ -131,7 +147,7 @@ class MessagingManagerImpl implements MessagingManager { @Override public ContactId getContactId(GroupId g) throws DbException { - // TODO: Make this more efficient + // TODO: Use metadata to attach the contact ID to the group for (Contact c : db.getContacts()) { Group conversation = createConversationGroup(c); if (conversation.getId().equals(g)) return c.getId(); diff --git a/briar-core/src/org/briarproject/messaging/MessagingModule.java b/briar-core/src/org/briarproject/messaging/MessagingModule.java index 95a28a3f1..42827c2c0 100644 --- a/briar-core/src/org/briarproject/messaging/MessagingModule.java +++ b/briar-core/src/org/briarproject/messaging/MessagingModule.java @@ -3,9 +3,9 @@ package org.briarproject.messaging; import com.google.inject.AbstractModule; import com.google.inject.Provides; +import org.briarproject.api.contact.ContactManager; import org.briarproject.api.data.BdfReaderFactory; import org.briarproject.api.data.MetadataEncoder; -import org.briarproject.api.lifecycle.LifecycleManager; import org.briarproject.api.messaging.MessagingManager; import org.briarproject.api.messaging.PrivateMessageFactory; import org.briarproject.api.sync.ValidationManager; @@ -17,18 +17,26 @@ public class MessagingModule extends AbstractModule { @Override protected void configure() { - bind(MessagingManager.class).to(MessagingManagerImpl.class); bind(PrivateMessageFactory.class).to(PrivateMessageFactoryImpl.class); } @Provides @Singleton - PrivateMessageValidator getValidator(LifecycleManager lifecycleManager, - ValidationManager validationManager, + PrivateMessageValidator getValidator(ValidationManager validationManager, + MessagingManager messagingManager, BdfReaderFactory bdfReaderFactory, MetadataEncoder metadataEncoder, Clock clock) { PrivateMessageValidator validator = new PrivateMessageValidator( - validationManager, bdfReaderFactory, metadataEncoder, clock); - lifecycleManager.register(validator); + bdfReaderFactory, metadataEncoder, clock); + validationManager.setMessageValidator(messagingManager.getClientId(), + validator); return validator; } + + @Provides @Singleton + MessagingManager getMessagingManager(ContactManager contactManager, + MessagingManagerImpl messagingManager) { + contactManager.registerContactAddedHook(messagingManager); + contactManager.registerContactRemovedHook(messagingManager); + return messagingManager; + } } diff --git a/briar-core/src/org/briarproject/messaging/PrivateMessageValidator.java b/briar-core/src/org/briarproject/messaging/PrivateMessageValidator.java index c97c66b6a..c0134934f 100644 --- a/briar-core/src/org/briarproject/messaging/PrivateMessageValidator.java +++ b/briar-core/src/org/briarproject/messaging/PrivateMessageValidator.java @@ -7,11 +7,9 @@ import org.briarproject.api.data.BdfReader; import org.briarproject.api.data.BdfReaderFactory; import org.briarproject.api.data.MetadataEncoder; import org.briarproject.api.db.Metadata; -import org.briarproject.api.lifecycle.Service; import org.briarproject.api.sync.Message; import org.briarproject.api.sync.MessageId; import org.briarproject.api.sync.MessageValidator; -import org.briarproject.api.sync.ValidationManager; import org.briarproject.api.system.Clock; import java.io.ByteArrayInputStream; @@ -24,39 +22,24 @@ import static org.briarproject.api.messaging.MessagingConstants.MAX_CONTENT_TYPE import static org.briarproject.api.messaging.MessagingConstants.MAX_PRIVATE_MESSAGE_BODY_LENGTH; import static org.briarproject.api.sync.SyncConstants.MESSAGE_HEADER_LENGTH; import static org.briarproject.api.transport.TransportConstants.MAX_CLOCK_DIFFERENCE; -import static org.briarproject.messaging.MessagingManagerImpl.CLIENT_ID; -class PrivateMessageValidator implements MessageValidator, Service { +class PrivateMessageValidator implements MessageValidator { private static final Logger LOG = Logger.getLogger(PrivateMessageValidator.class.getName()); - private final ValidationManager validationManager; private final BdfReaderFactory bdfReaderFactory; private final MetadataEncoder metadataEncoder; private final Clock clock; @Inject - PrivateMessageValidator(ValidationManager validationManager, - BdfReaderFactory bdfReaderFactory, MetadataEncoder metadataEncoder, - Clock clock) { - this.validationManager = validationManager; + PrivateMessageValidator(BdfReaderFactory bdfReaderFactory, + MetadataEncoder metadataEncoder, Clock clock) { this.bdfReaderFactory = bdfReaderFactory; this.metadataEncoder = metadataEncoder; this.clock = clock; } - @Override - public boolean start() { - validationManager.setMessageValidator(CLIENT_ID, this); - return true; - } - - @Override - public boolean stop() { - return true; - } - @Override public Metadata validateMessage(Message m) { // Reject the message if it's too far in the future diff --git a/briar-core/src/org/briarproject/sync/SyncModule.java b/briar-core/src/org/briarproject/sync/SyncModule.java index cce031fd9..77633c7d2 100644 --- a/briar-core/src/org/briarproject/sync/SyncModule.java +++ b/briar-core/src/org/briarproject/sync/SyncModule.java @@ -4,9 +4,9 @@ import com.google.inject.AbstractModule; import com.google.inject.Provides; import org.briarproject.api.data.ObjectReader; +import org.briarproject.api.event.EventBus; import org.briarproject.api.identity.Author; import org.briarproject.api.identity.AuthorFactory; -import org.briarproject.api.lifecycle.LifecycleManager; import org.briarproject.api.sync.Group; import org.briarproject.api.sync.GroupFactory; import org.briarproject.api.sync.MessageFactory; @@ -48,9 +48,9 @@ public class SyncModule extends AbstractModule { } @Provides @Singleton - ValidationManager getValidationManager(LifecycleManager lifecycleManager, + ValidationManager getValidationManager(EventBus eventBus, ValidationManagerImpl validationManager) { - lifecycleManager.register(validationManager); + eventBus.addListener(validationManager); return validationManager; } } diff --git a/briar-core/src/org/briarproject/sync/ValidationManagerImpl.java b/briar-core/src/org/briarproject/sync/ValidationManagerImpl.java index d3e861266..9f977ef15 100644 --- a/briar-core/src/org/briarproject/sync/ValidationManagerImpl.java +++ b/briar-core/src/org/briarproject/sync/ValidationManagerImpl.java @@ -11,10 +11,8 @@ import org.briarproject.api.db.Metadata; import org.briarproject.api.db.NoSuchMessageException; import org.briarproject.api.db.NoSuchSubscriptionException; import org.briarproject.api.event.Event; -import org.briarproject.api.event.EventBus; import org.briarproject.api.event.EventListener; import org.briarproject.api.event.MessageAddedEvent; -import org.briarproject.api.lifecycle.Service; import org.briarproject.api.sync.ClientId; import org.briarproject.api.sync.GroupId; import org.briarproject.api.sync.Message; @@ -31,8 +29,7 @@ import java.util.logging.Logger; import static java.util.logging.Level.WARNING; import static org.briarproject.api.sync.SyncConstants.MESSAGE_HEADER_LENGTH; -class ValidationManagerImpl implements ValidationManager, Service, - EventListener { +class ValidationManagerImpl implements ValidationManager, EventListener { private static final Logger LOG = Logger.getLogger(ValidationManagerImpl.class.getName()); @@ -40,32 +37,18 @@ class ValidationManagerImpl implements ValidationManager, Service, private final DatabaseComponent db; private final Executor dbExecutor; private final Executor cryptoExecutor; - private final EventBus eventBus; private final Map validators; @Inject ValidationManagerImpl(DatabaseComponent db, @DatabaseExecutor Executor dbExecutor, - @CryptoExecutor Executor cryptoExecutor, EventBus eventBus) { + @CryptoExecutor Executor cryptoExecutor) { this.db = db; this.dbExecutor = dbExecutor; this.cryptoExecutor = cryptoExecutor; - this.eventBus = eventBus; validators = new ConcurrentHashMap(); } - @Override - public boolean start() { - eventBus.addListener(this); - return true; - } - - @Override - public boolean stop() { - eventBus.removeListener(this); - return true; - } - @Override public void setMessageValidator(ClientId c, MessageValidator v) { validators.put(c, v); diff --git a/briar-core/src/org/briarproject/transport/KeyManagerImpl.java b/briar-core/src/org/briarproject/transport/KeyManagerImpl.java index aa50d8e5f..9804cab61 100644 --- a/briar-core/src/org/briarproject/transport/KeyManagerImpl.java +++ b/briar-core/src/org/briarproject/transport/KeyManagerImpl.java @@ -9,7 +9,6 @@ import org.briarproject.api.db.DatabaseExecutor; import org.briarproject.api.db.DbException; import org.briarproject.api.event.ContactRemovedEvent; import org.briarproject.api.event.Event; -import org.briarproject.api.event.EventBus; import org.briarproject.api.event.EventListener; import org.briarproject.api.event.TransportAddedEvent; import org.briarproject.api.event.TransportRemovedEvent; @@ -38,19 +37,17 @@ class KeyManagerImpl implements KeyManager, Service, EventListener { private final DatabaseComponent db; private final CryptoComponent crypto; private final ExecutorService dbExecutor; - private final EventBus eventBus; private final Timer timer; private final Clock clock; private final ConcurrentHashMap managers; @Inject KeyManagerImpl(DatabaseComponent db, CryptoComponent crypto, - @DatabaseExecutor ExecutorService dbExecutor, EventBus eventBus, - Timer timer, Clock clock) { + @DatabaseExecutor ExecutorService dbExecutor, Timer timer, + Clock clock) { this.db = db; this.crypto = crypto; this.dbExecutor = dbExecutor; - this.eventBus = eventBus; this.timer = timer; this.clock = clock; managers = new ConcurrentHashMap(); @@ -58,7 +55,6 @@ class KeyManagerImpl implements KeyManager, Service, EventListener { @Override public boolean start() { - eventBus.addListener(this); try { Map latencies = db.getTransportLatencies(); for (Entry e : latencies.entrySet()) @@ -72,7 +68,6 @@ class KeyManagerImpl implements KeyManager, Service, EventListener { @Override public boolean stop() { - eventBus.removeListener(this); return true; } diff --git a/briar-core/src/org/briarproject/transport/TransportModule.java b/briar-core/src/org/briarproject/transport/TransportModule.java index 36a7b3e3e..367340a4c 100644 --- a/briar-core/src/org/briarproject/transport/TransportModule.java +++ b/briar-core/src/org/briarproject/transport/TransportModule.java @@ -3,6 +3,7 @@ package org.briarproject.transport; import com.google.inject.AbstractModule; import com.google.inject.Provides; +import org.briarproject.api.event.EventBus; import org.briarproject.api.lifecycle.LifecycleManager; import org.briarproject.api.transport.KeyManager; import org.briarproject.api.transport.StreamReaderFactory; @@ -20,8 +21,9 @@ public class TransportModule extends AbstractModule { @Provides @Singleton KeyManager getKeyManager(LifecycleManager lifecycleManager, - KeyManagerImpl keyManager) { + EventBus eventBus, KeyManagerImpl keyManager) { lifecycleManager.register(keyManager); + eventBus.addListener(keyManager); return keyManager; } } diff --git a/briar-tests/src/org/briarproject/db/DatabaseComponentImplTest.java b/briar-tests/src/org/briarproject/db/DatabaseComponentImplTest.java index a6bd31ab3..b73ac3ee0 100644 --- a/briar-tests/src/org/briarproject/db/DatabaseComponentImplTest.java +++ b/briar-tests/src/org/briarproject/db/DatabaseComponentImplTest.java @@ -16,8 +16,6 @@ import org.briarproject.api.db.NoSuchLocalAuthorException; import org.briarproject.api.db.NoSuchMessageException; import org.briarproject.api.db.NoSuchSubscriptionException; import org.briarproject.api.db.NoSuchTransportException; -import org.briarproject.api.event.ContactAddedEvent; -import org.briarproject.api.event.ContactRemovedEvent; import org.briarproject.api.event.EventBus; import org.briarproject.api.event.LocalAuthorAddedEvent; import org.briarproject.api.event.LocalAuthorRemovedEvent; @@ -59,6 +57,7 @@ import java.util.Arrays; import java.util.Collection; import java.util.Collections; +import static org.briarproject.api.contact.Contact.Status.ACTIVE; import static org.briarproject.api.identity.AuthorConstants.MAX_PUBLIC_KEY_LENGTH; import static org.briarproject.api.sync.SyncConstants.MAX_GROUP_DESCRIPTOR_LENGTH; import static org.briarproject.db.DatabaseConstants.MAX_OFFERED_MESSAGES; @@ -113,7 +112,7 @@ public class DatabaseComponentImplTest extends BriarTestCase { "bar", "baz")); maxLatency = Integer.MAX_VALUE; contactId = new ContactId(234); - contact = new Contact(contactId, author, localAuthorId); + contact = new Contact(contactId, author, localAuthorId, ACTIVE); } private DatabaseComponent createDatabaseComponent(Database database, @@ -150,7 +149,6 @@ public class DatabaseComponentImplTest extends BriarTestCase { will(returnValue(true)); oneOf(database).addContact(txn, author, localAuthorId); will(returnValue(contactId)); - oneOf(eventBus).broadcast(with(any(ContactAddedEvent.class))); // getContacts() oneOf(database).getContacts(txn); will(returnValue(Collections.singletonList(contact))); @@ -183,7 +181,6 @@ public class DatabaseComponentImplTest extends BriarTestCase { oneOf(database).containsContact(txn, contactId); will(returnValue(true)); oneOf(database).removeContact(txn, contactId); - oneOf(eventBus).broadcast(with(any(ContactRemovedEvent.class))); // removeLocalAuthor() oneOf(database).containsLocalAuthor(txn, localAuthorId); will(returnValue(true)); @@ -671,7 +668,6 @@ public class DatabaseComponentImplTest extends BriarTestCase { oneOf(database).addContact(txn, author, localAuthorId); will(returnValue(contactId)); oneOf(database).commitTransaction(txn); - oneOf(eventBus).broadcast(with(any(ContactAddedEvent.class))); // Check whether the transport is in the DB (which it's not) exactly(6).of(database).startTransaction(); will(returnValue(txn)); diff --git a/briar-tests/src/org/briarproject/sync/ConstantsTest.java b/briar-tests/src/org/briarproject/sync/ConstantsTest.java index 6b92a0269..c2fa6bf5e 100644 --- a/briar-tests/src/org/briarproject/sync/ConstantsTest.java +++ b/briar-tests/src/org/briarproject/sync/ConstantsTest.java @@ -35,6 +35,7 @@ import org.briarproject.api.sync.PacketWriterFactory; import org.briarproject.api.sync.Request; import org.briarproject.api.sync.SubscriptionUpdate; import org.briarproject.api.sync.TransportUpdate; +import org.briarproject.contact.ContactModule; import org.briarproject.crypto.CryptoModule; import org.briarproject.data.DataModule; import org.briarproject.db.DatabaseModule; @@ -73,9 +74,9 @@ public class ConstantsTest extends BriarTestCase { public ConstantsTest() throws Exception { Injector i = Guice.createInjector(new TestDatabaseModule(), new TestLifecycleModule(), new TestSystemModule(), - new CryptoModule(), new DatabaseModule(), new DataModule(), - new EventModule(), new ForumModule(), new MessagingModule(), - new SyncModule()); + new ContactModule(), new CryptoModule(), new DatabaseModule(), + new DataModule(), new EventModule(), new ForumModule(), + new MessagingModule(), new SyncModule()); crypto = i.getInstance(CryptoComponent.class); groupFactory = i.getInstance(GroupFactory.class); authorFactory = i.getInstance(AuthorFactory.class); diff --git a/briar-tests/src/org/briarproject/sync/SimplexMessagingIntegrationTest.java b/briar-tests/src/org/briarproject/sync/SimplexMessagingIntegrationTest.java index 07eb61d57..eb0db4854 100644 --- a/briar-tests/src/org/briarproject/sync/SimplexMessagingIntegrationTest.java +++ b/briar-tests/src/org/briarproject/sync/SimplexMessagingIntegrationTest.java @@ -129,8 +129,6 @@ public class SimplexMessagingIntegrationTest extends BriarTestCase { Author bobAuthor = new Author(bobId, "Bob", new byte[MAX_PUBLIC_KEY_LENGTH]); ContactId contactId = contactManager.addContact(bobAuthor, aliceId); - // Create a private conversation - messagingManager.addContact(contactId); // Derive and store the transport keys keyManager.addContact(contactId, Collections.singletonList(transportId), master, timestamp, true); @@ -174,14 +172,14 @@ public class SimplexMessagingIntegrationTest extends BriarTestCase { IdentityManager identityManager = bob.getInstance(IdentityManager.class); ContactManager contactManager = bob.getInstance(ContactManager.class); - MessagingManager messagingManager = - bob.getInstance(MessagingManager.class); KeyManager keyManager = bob.getInstance(KeyManager.class); StreamReaderFactory streamReaderFactory = bob.getInstance(StreamReaderFactory.class); PacketReaderFactory packetReaderFactory = bob.getInstance(PacketReaderFactory.class); EventBus eventBus = bob.getInstance(EventBus.class); + // Bob needs a MessagingManager even though we're not using it directly + bob.getInstance(MessagingManager.class); // Start the lifecyle manager lifecycleManager.startServices(); @@ -196,8 +194,6 @@ public class SimplexMessagingIntegrationTest extends BriarTestCase { Author aliceAuthor = new Author(aliceId, "Alice", new byte[MAX_PUBLIC_KEY_LENGTH]); ContactId contactId = contactManager.addContact(aliceAuthor, bobId); - // Create a private conversation - messagingManager.addContact(contactId); // Derive and store the transport keys keyManager.addContact(contactId, Collections.singletonList(transportId), master, timestamp, false); From c4692a7007ecb8a43e0a334fff1ba027c2be4f1c Mon Sep 17 00:00:00 2001 From: akwizgran Date: Wed, 20 Jan 2016 12:03:15 +0000 Subject: [PATCH 3/4] Identity manager hooks. #209 --- .../org/briarproject/api/contact/Contact.java | 3 +- .../briarproject/api/contact/ContactId.java | 3 +- .../api/db/DatabaseComponent.java | 7 ++ .../api/identity/IdentityManager.java | 14 ++++ .../api/identity/LocalAuthor.java | 33 +++++++- .../contact/ContactManagerImpl.java | 14 +++- .../briarproject/contact/ContactModule.java | 3 + .../src/org/briarproject/db/Database.java | 14 +++- .../db/DatabaseComponentImpl.java | 47 +++++++++-- .../src/org/briarproject/db/JdbcDatabase.java | 46 +++++++--- .../identity/IdentityManagerImpl.java | 84 ++++++++++++++++++- .../briarproject/sync/AuthorFactoryImpl.java | 4 +- .../db/DatabaseComponentImplTest.java | 17 ++-- .../org/briarproject/db/H2DatabaseTest.java | 11 +-- .../org/briarproject/sync/ConstantsTest.java | 5 +- .../sync/SimplexMessagingIntegrationTest.java | 6 +- 16 files changed, 258 insertions(+), 53 deletions(-) diff --git a/briar-api/src/org/briarproject/api/contact/Contact.java b/briar-api/src/org/briarproject/api/contact/Contact.java index 585010ef7..62d7efa64 100644 --- a/briar-api/src/org/briarproject/api/contact/Contact.java +++ b/briar-api/src/org/briarproject/api/contact/Contact.java @@ -61,7 +61,6 @@ public class Contact { @Override public boolean equals(Object o) { - if (o instanceof Contact) return id.equals(((Contact) o).id); - return false; + return o instanceof Contact && id.equals(((Contact) o).id); } } diff --git a/briar-api/src/org/briarproject/api/contact/ContactId.java b/briar-api/src/org/briarproject/api/contact/ContactId.java index 1805818d7..736597a38 100644 --- a/briar-api/src/org/briarproject/api/contact/ContactId.java +++ b/briar-api/src/org/briarproject/api/contact/ContactId.java @@ -23,7 +23,6 @@ public class ContactId { @Override public boolean equals(Object o) { - if (o instanceof ContactId) return id == ((ContactId) o).id; - return false; + return o instanceof ContactId && id == ((ContactId) o).id; } } diff --git a/briar-api/src/org/briarproject/api/db/DatabaseComponent.java b/briar-api/src/org/briarproject/api/db/DatabaseComponent.java index 476198231..b2ee63bde 100644 --- a/briar-api/src/org/briarproject/api/db/DatabaseComponent.java +++ b/briar-api/src/org/briarproject/api/db/DatabaseComponent.java @@ -152,6 +152,9 @@ public interface DatabaseComponent { /** Returns all contacts. */ Collection getContacts() throws DbException; + /** Returns all contacts associated with the given local pseudonym. */ + Collection getContacts(AuthorId a) throws DbException; + /** Returns the group with the given ID, if the user subscribes to it. */ Group getGroup(GroupId g) throws DbException; @@ -300,6 +303,10 @@ public interface DatabaseComponent { /** Sets the status of the given contact. */ void setContactStatus(ContactId c, Contact.Status s) throws DbException; + /** Sets the status of the given local pseudonym. */ + void setLocalAuthorStatus(AuthorId a, LocalAuthor.Status s) + throws DbException; + /** Marks the given message as valid or invalid. */ void setMessageValidity(Message m, ClientId c, boolean valid) throws DbException; diff --git a/briar-api/src/org/briarproject/api/identity/IdentityManager.java b/briar-api/src/org/briarproject/api/identity/IdentityManager.java index 207fea87d..ec2383371 100644 --- a/briar-api/src/org/briarproject/api/identity/IdentityManager.java +++ b/briar-api/src/org/briarproject/api/identity/IdentityManager.java @@ -6,6 +6,12 @@ import java.util.Collection; public interface IdentityManager { + /** Registers a hook to be called whenever a local pseudonym is added. */ + void registerIdentityAddedHook(IdentityAddedHook hook); + + /** Registers a hook to be called whenever a local pseudonym is removed. */ + void registerIdentityRemovedHook(IdentityRemovedHook hook); + /** Stores a local pseudonym. */ void addLocalAuthor(LocalAuthor a) throws DbException; @@ -17,4 +23,12 @@ public interface IdentityManager { /** Removes a local pseudonym and all associated state. */ void removeLocalAuthor(AuthorId a) throws DbException; + + interface IdentityAddedHook { + void identityAdded(AuthorId a); + } + + interface IdentityRemovedHook { + void identityRemoved(AuthorId a); + } } diff --git a/briar-api/src/org/briarproject/api/identity/LocalAuthor.java b/briar-api/src/org/briarproject/api/identity/LocalAuthor.java index 9f5626d77..da5d4a908 100644 --- a/briar-api/src/org/briarproject/api/identity/LocalAuthor.java +++ b/briar-api/src/org/briarproject/api/identity/LocalAuthor.java @@ -3,14 +3,36 @@ package org.briarproject.api.identity; /** A pseudonym for the local user. */ public class LocalAuthor extends Author { + public enum Status { + + ADDING(0), ACTIVE(1), REMOVING(2); + + private final int value; + + Status(int value) { + this.value = value; + } + + public int getValue() { + return value; + } + + public static Status fromValue(int value) { + for (Status s : values()) if (s.value == value) return s; + throw new IllegalArgumentException(); + } + } + private final byte[] privateKey; private final long created; + private final Status status; public LocalAuthor(AuthorId id, String name, byte[] publicKey, - byte[] privateKey, long created) { + byte[] privateKey, long created, Status status) { super(id, name, publicKey); this.privateKey = privateKey; this.created = created; + this.status = status; } /** Returns the private key used to generate the pseudonym's signatures. */ @@ -18,7 +40,16 @@ public class LocalAuthor extends Author { return privateKey; } + /** + * Returns the time the pseudonym was created, in milliseconds since the + * Unix epoch. + */ public long getTimeCreated() { return created; } + + /** Returns the status of the pseudonym. */ + public Status getStatus() { + return status; + } } diff --git a/briar-core/src/org/briarproject/contact/ContactManagerImpl.java b/briar-core/src/org/briarproject/contact/ContactManagerImpl.java index 3eb37298d..92d802030 100644 --- a/briar-core/src/org/briarproject/contact/ContactManagerImpl.java +++ b/briar-core/src/org/briarproject/contact/ContactManagerImpl.java @@ -13,6 +13,7 @@ import org.briarproject.api.event.ContactRemovedEvent; import org.briarproject.api.event.EventBus; import org.briarproject.api.identity.Author; import org.briarproject.api.identity.AuthorId; +import org.briarproject.api.identity.IdentityManager.IdentityRemovedHook; import org.briarproject.api.lifecycle.Service; import java.util.ArrayList; @@ -27,7 +28,8 @@ import static org.briarproject.api.contact.Contact.Status.ACTIVE; import static org.briarproject.api.contact.Contact.Status.ADDING; import static org.briarproject.api.contact.Contact.Status.REMOVING; -class ContactManagerImpl implements ContactManager, Service { +class ContactManagerImpl implements ContactManager, Service, + IdentityRemovedHook { private static final Logger LOG = Logger.getLogger(ContactManagerImpl.class.getName()); @@ -118,4 +120,14 @@ class ContactManagerImpl implements ContactManager, Service { db.removeContact(c); eventBus.broadcast(new ContactRemovedEvent(c)); } + + @Override + public void identityRemoved(AuthorId a) { + // Remove any contacts of the local pseudonym that's being removed + try { + for (ContactId c : db.getContacts(a)) removeContact(c); + } catch (DbException e) { + if (LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e); + } + } } diff --git a/briar-core/src/org/briarproject/contact/ContactModule.java b/briar-core/src/org/briarproject/contact/ContactModule.java index 105aa9ef1..3a374ab5e 100644 --- a/briar-core/src/org/briarproject/contact/ContactModule.java +++ b/briar-core/src/org/briarproject/contact/ContactModule.java @@ -4,6 +4,7 @@ import com.google.inject.AbstractModule; import com.google.inject.Provides; import org.briarproject.api.contact.ContactManager; +import org.briarproject.api.identity.IdentityManager; import org.briarproject.api.lifecycle.LifecycleManager; import javax.inject.Singleton; @@ -15,8 +16,10 @@ public class ContactModule extends AbstractModule { @Provides @Singleton ContactManager getContactManager(LifecycleManager lifecycleManager, + IdentityManager identityManager, ContactManagerImpl contactManager) { lifecycleManager.register(contactManager); + identityManager.registerIdentityRemovedHook(contactManager); return contactManager; } } diff --git a/briar-core/src/org/briarproject/db/Database.java b/briar-core/src/org/briarproject/db/Database.java index 39790deed..65518cc5f 100644 --- a/briar-core/src/org/briarproject/db/Database.java +++ b/briar-core/src/org/briarproject/db/Database.java @@ -334,7 +334,7 @@ interface Database { * Locking: read */ Collection getMessageStatus(T txn, ContactId c, GroupId g) - throws DbException; + throws DbException; /** * Returns the status of the given message with respect to the given @@ -343,7 +343,7 @@ interface Database { * Locking: read */ MessageStatus getMessageStatus(T txn, ContactId c, MessageId m) - throws DbException; + throws DbException; /** * Returns the IDs of some messages received from the given contact that @@ -388,7 +388,7 @@ interface Database { * Locking: read. */ Collection getMessagesToValidate(T txn, ClientId c) - throws DbException; + throws DbException; /** * Returns the message with the given ID, in serialised form. @@ -639,6 +639,14 @@ interface Database { void setContactStatus(T txn, ContactId c, Contact.Status s) throws DbException; + /** + * Sets the status of the given local pseudonym. + *

+ * Locking: write. + */ + void setLocalAuthorStatus(T txn, AuthorId a, LocalAuthor.Status s) + throws DbException; + /** * Marks the given message as valid or invalid. *

diff --git a/briar-core/src/org/briarproject/db/DatabaseComponentImpl.java b/briar-core/src/org/briarproject/db/DatabaseComponentImpl.java index 546434ff5..13d650912 100644 --- a/briar-core/src/org/briarproject/db/DatabaseComponentImpl.java +++ b/briar-core/src/org/briarproject/db/DatabaseComponentImpl.java @@ -16,10 +16,7 @@ import org.briarproject.api.db.NoSuchLocalAuthorException; import org.briarproject.api.db.NoSuchMessageException; import org.briarproject.api.db.NoSuchSubscriptionException; import org.briarproject.api.db.NoSuchTransportException; -import org.briarproject.api.event.ContactRemovedEvent; import org.briarproject.api.event.EventBus; -import org.briarproject.api.event.LocalAuthorAddedEvent; -import org.briarproject.api.event.LocalAuthorRemovedEvent; import org.briarproject.api.event.LocalSubscriptionsUpdatedEvent; import org.briarproject.api.event.LocalTransportsUpdatedEvent; import org.briarproject.api.event.MessageAddedEvent; @@ -218,7 +215,6 @@ class DatabaseComponentImpl implements DatabaseComponent { } finally { lock.writeLock().unlock(); } - eventBus.broadcast(new LocalAuthorAddedEvent(a.getId())); } public void addLocalMessage(Message m, ClientId c, Metadata meta) @@ -572,6 +568,25 @@ class DatabaseComponentImpl implements DatabaseComponent { } } + public Collection getContacts(AuthorId a) throws DbException { + lock.readLock().lock(); + try { + T txn = db.startTransaction(); + try { + if (!db.containsLocalAuthor(txn, a)) + throw new NoSuchLocalAuthorException(); + Collection contacts = db.getContacts(txn, a); + db.commitTransaction(txn); + return contacts; + } catch (DbException e) { + db.abortTransaction(txn); + throw e; + } + } finally { + lock.readLock().unlock(); + } + } + public Group getGroup(GroupId g) throws DbException { lock.readLock().lock(); try { @@ -1242,14 +1257,12 @@ class DatabaseComponentImpl implements DatabaseComponent { } public void removeLocalAuthor(AuthorId a) throws DbException { - Collection affected; lock.writeLock().lock(); try { T txn = db.startTransaction(); try { if (!db.containsLocalAuthor(txn, a)) throw new NoSuchLocalAuthorException(); - affected = db.getContacts(txn, a); db.removeLocalAuthor(txn, a); db.commitTransaction(txn); } catch (DbException e) { @@ -1259,9 +1272,6 @@ class DatabaseComponentImpl implements DatabaseComponent { } finally { lock.writeLock().unlock(); } - for (ContactId c : affected) - eventBus.broadcast(new ContactRemovedEvent(c)); - eventBus.broadcast(new LocalAuthorRemovedEvent(a)); } public void removeTransport(TransportId t) throws DbException { @@ -1302,6 +1312,25 @@ class DatabaseComponentImpl implements DatabaseComponent { } } + public void setLocalAuthorStatus(AuthorId a, LocalAuthor.Status s) + throws DbException { + lock.writeLock().lock(); + try { + T txn = db.startTransaction(); + try { + if (!db.containsLocalAuthor(txn, a)) + throw new NoSuchLocalAuthorException(); + db.setLocalAuthorStatus(txn, a, s); + db.commitTransaction(txn); + } catch (DbException e) { + db.abortTransaction(txn); + throw e; + } + } finally { + lock.writeLock().unlock(); + } + } + public void setMessageValidity(Message m, ClientId c, boolean valid) throws DbException { lock.writeLock().lock(); diff --git a/briar-core/src/org/briarproject/db/JdbcDatabase.java b/briar-core/src/org/briarproject/db/JdbcDatabase.java index 1faf11bda..54be105b2 100644 --- a/briar-core/src/org/briarproject/db/JdbcDatabase.java +++ b/briar-core/src/org/briarproject/db/JdbcDatabase.java @@ -64,8 +64,8 @@ import static org.briarproject.db.ExponentialBackoff.calculateExpiry; */ abstract class JdbcDatabase implements Database { - private static final int SCHEMA_VERSION = 15; - private static final int MIN_SCHEMA_VERSION = 15; + private static final int SCHEMA_VERSION = 16; + private static final int MIN_SCHEMA_VERSION = 16; private static final String CREATE_SETTINGS = "CREATE TABLE settings" @@ -81,6 +81,7 @@ abstract class JdbcDatabase implements Database { + " publicKey BINARY NOT NULL," + " privateKey BINARY NOT NULL," + " created BIGINT NOT NULL," + + " status INT NOT NULL," + " PRIMARY KEY (authorId))"; private static final String CREATE_CONTACTS = @@ -719,15 +720,16 @@ abstract class JdbcDatabase implements Database { throws DbException { PreparedStatement ps = null; try { - String sql = "INSERT INTO localAuthors" - + " (authorId, name, publicKey, privateKey, created)" - + " VALUES (?, ?, ?, ?, ?)"; + String sql = "INSERT INTO localAuthors (authorId, name, publicKey," + + " privateKey, created, status)" + + " VALUES (?, ?, ?, ?, ?, ?)"; ps = txn.prepareStatement(sql); ps.setBytes(1, a.getId().getBytes()); ps.setString(2, a.getName()); ps.setBytes(3, a.getPublicKey()); ps.setBytes(4, a.getPrivateKey()); ps.setLong(5, a.getTimeCreated()); + ps.setInt(6, a.getStatus().getValue()); int affected = ps.executeUpdate(); if (affected != 1) throw new DbStateException(); ps.close(); @@ -1345,7 +1347,7 @@ abstract class JdbcDatabase implements Database { PreparedStatement ps = null; ResultSet rs = null; try { - String sql = "SELECT name, publicKey, privateKey, created" + String sql = "SELECT name, publicKey, privateKey, created, status" + " FROM localAuthors" + " WHERE authorId = ?"; ps = txn.prepareStatement(sql); @@ -1356,8 +1358,10 @@ abstract class JdbcDatabase implements Database { byte[] publicKey = rs.getBytes(2); byte[] privateKey = rs.getBytes(3); long created = rs.getLong(4); + LocalAuthor.Status status = LocalAuthor.Status.fromValue( + rs.getInt(5)); LocalAuthor localAuthor = new LocalAuthor(a, name, publicKey, - privateKey, created); + privateKey, created, status); if (rs.next()) throw new DbStateException(); rs.close(); ps.close(); @@ -1374,7 +1378,8 @@ abstract class JdbcDatabase implements Database { PreparedStatement ps = null; ResultSet rs = null; try { - String sql = "SELECT authorId, name, publicKey, privateKey, created" + String sql = "SELECT authorId, name, publicKey, privateKey," + + " created, status" + " FROM localAuthors"; ps = txn.prepareStatement(sql); rs = ps.executeQuery(); @@ -1385,8 +1390,10 @@ abstract class JdbcDatabase implements Database { byte[] publicKey = rs.getBytes(3); byte[] privateKey = rs.getBytes(4); long created = rs.getLong(5); + LocalAuthor.Status status = LocalAuthor.Status.fromValue( + rs.getInt(6)); authors.add(new LocalAuthor(authorId, name, publicKey, - privateKey, created)); + privateKey, created, status)); } rs.close(); ps.close(); @@ -2398,7 +2405,8 @@ abstract class JdbcDatabase implements Database { } } - public void mergeSettings(Connection txn, Settings s, String namespace) throws DbException { + public void mergeSettings(Connection txn, Settings s, String namespace) + throws DbException { PreparedStatement ps = null; try { // Update any settings that already exist @@ -2712,6 +2720,24 @@ abstract class JdbcDatabase implements Database { } } + public void setLocalAuthorStatus(Connection txn, AuthorId a, + LocalAuthor.Status s) throws DbException { + PreparedStatement ps = null; + try { + String sql = "UPDATE localAuthors SET status = ?" + + " WHERE authorId = ?"; + ps = txn.prepareStatement(sql); + ps.setInt(1, s.getValue()); + ps.setBytes(2, a.getBytes()); + int affected = ps.executeUpdate(); + if (affected < 0 || affected > 1) throw new DbStateException(); + ps.close(); + } catch (SQLException e) { + tryToClose(ps); + throw new DbException(e); + } + } + public void setMessageValidity(Connection txn, MessageId m, boolean valid) throws DbException { PreparedStatement ps = null; diff --git a/briar-core/src/org/briarproject/identity/IdentityManagerImpl.java b/briar-core/src/org/briarproject/identity/IdentityManagerImpl.java index 89e4f324d..2e6350cbe 100644 --- a/briar-core/src/org/briarproject/identity/IdentityManagerImpl.java +++ b/briar-core/src/org/briarproject/identity/IdentityManagerImpl.java @@ -4,38 +4,114 @@ import com.google.inject.Inject; import org.briarproject.api.db.DatabaseComponent; import org.briarproject.api.db.DbException; +import org.briarproject.api.db.NoSuchLocalAuthorException; +import org.briarproject.api.event.EventBus; +import org.briarproject.api.event.LocalAuthorAddedEvent; +import org.briarproject.api.event.LocalAuthorRemovedEvent; import org.briarproject.api.identity.AuthorId; import org.briarproject.api.identity.IdentityManager; import org.briarproject.api.identity.LocalAuthor; +import org.briarproject.api.lifecycle.Service; +import java.util.ArrayList; import java.util.Collection; +import java.util.Collections; +import java.util.List; +import java.util.concurrent.CopyOnWriteArrayList; +import java.util.logging.Logger; -class IdentityManagerImpl implements IdentityManager { +import static java.util.logging.Level.WARNING; +import static org.briarproject.api.identity.LocalAuthor.Status.ACTIVE; +import static org.briarproject.api.identity.LocalAuthor.Status.ADDING; +import static org.briarproject.api.identity.LocalAuthor.Status.REMOVING; + +class IdentityManagerImpl implements IdentityManager, Service { + + private static final Logger LOG = + Logger.getLogger(IdentityManagerImpl.class.getName()); private final DatabaseComponent db; + private final EventBus eventBus; + private final List addHooks; + private final List removeHooks; @Inject - IdentityManagerImpl(DatabaseComponent db) { + IdentityManagerImpl(DatabaseComponent db, EventBus eventBus) { this.db = db; + this.eventBus = eventBus; + addHooks = new CopyOnWriteArrayList(); + removeHooks = new CopyOnWriteArrayList(); + } + + @Override + public boolean start() { + // Finish adding/removing any partly added/removed pseudonyms + try { + for (LocalAuthor a : db.getLocalAuthors()) { + if (a.getStatus().equals(ADDING)) { + for (IdentityAddedHook hook : addHooks) + hook.identityAdded(a.getId()); + db.setLocalAuthorStatus(a.getId(), ACTIVE); + eventBus.broadcast(new LocalAuthorAddedEvent(a.getId())); + } else if (a.getStatus().equals(REMOVING)) { + for (IdentityRemovedHook hook : removeHooks) + hook.identityRemoved(a.getId()); + db.removeLocalAuthor(a.getId()); + eventBus.broadcast(new LocalAuthorRemovedEvent(a.getId())); + } + } + return true; + } catch (DbException e) { + if (LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e); + return false; + } + } + + @Override + public boolean stop() { + return false; + } + + @Override + public void registerIdentityAddedHook(IdentityAddedHook hook) { + addHooks.add(hook); + } + + @Override + public void registerIdentityRemovedHook(IdentityRemovedHook hook) { + removeHooks.add(hook); } @Override public void addLocalAuthor(LocalAuthor a) throws DbException { db.addLocalAuthor(a); + for (IdentityAddedHook hook : addHooks) hook.identityAdded(a.getId()); + db.setLocalAuthorStatus(a.getId(), ACTIVE); + eventBus.broadcast(new LocalAuthorAddedEvent(a.getId())); } @Override public LocalAuthor getLocalAuthor(AuthorId a) throws DbException { - return db.getLocalAuthor(a); + LocalAuthor author = db.getLocalAuthor(a); + if (author.getStatus().equals(ACTIVE)) return author; + throw new NoSuchLocalAuthorException(); } @Override public Collection getLocalAuthors() throws DbException { - return db.getLocalAuthors(); + Collection authors = db.getLocalAuthors(); + // Filter out any pseudonyms that are being added or removed + List active = new ArrayList(authors.size()); + for (LocalAuthor a : authors) + if (a.getStatus().equals(ACTIVE)) active.add(a); + return Collections.unmodifiableList(active); } @Override public void removeLocalAuthor(AuthorId a) throws DbException { + db.setLocalAuthorStatus(a, REMOVING); + for (IdentityRemovedHook hook : removeHooks) hook.identityRemoved(a); db.removeLocalAuthor(a); + eventBus.broadcast(new LocalAuthorRemovedEvent(a)); } } diff --git a/briar-core/src/org/briarproject/sync/AuthorFactoryImpl.java b/briar-core/src/org/briarproject/sync/AuthorFactoryImpl.java index bbe2674d0..102ea1ee8 100644 --- a/briar-core/src/org/briarproject/sync/AuthorFactoryImpl.java +++ b/briar-core/src/org/briarproject/sync/AuthorFactoryImpl.java @@ -14,6 +14,8 @@ import java.io.IOException; import javax.inject.Inject; +import static org.briarproject.api.identity.LocalAuthor.Status.ADDING; + class AuthorFactoryImpl implements AuthorFactory { private final CryptoComponent crypto; @@ -35,7 +37,7 @@ class AuthorFactoryImpl implements AuthorFactory { public LocalAuthor createLocalAuthor(String name, byte[] publicKey, byte[] privateKey) { return new LocalAuthor(getId(name, publicKey), name, publicKey, - privateKey, clock.currentTimeMillis()); + privateKey, clock.currentTimeMillis(), ADDING); } private AuthorId getId(String name, byte[] publicKey) { diff --git a/briar-tests/src/org/briarproject/db/DatabaseComponentImplTest.java b/briar-tests/src/org/briarproject/db/DatabaseComponentImplTest.java index b73ac3ee0..285da1aeb 100644 --- a/briar-tests/src/org/briarproject/db/DatabaseComponentImplTest.java +++ b/briar-tests/src/org/briarproject/db/DatabaseComponentImplTest.java @@ -2,7 +2,6 @@ package org.briarproject.db; import org.briarproject.BriarTestCase; import org.briarproject.TestUtils; -import org.briarproject.api.Settings; import org.briarproject.api.TransportId; import org.briarproject.api.TransportProperties; import org.briarproject.api.contact.Contact; @@ -17,8 +16,6 @@ import org.briarproject.api.db.NoSuchMessageException; import org.briarproject.api.db.NoSuchSubscriptionException; import org.briarproject.api.db.NoSuchTransportException; import org.briarproject.api.event.EventBus; -import org.briarproject.api.event.LocalAuthorAddedEvent; -import org.briarproject.api.event.LocalAuthorRemovedEvent; import org.briarproject.api.event.LocalSubscriptionsUpdatedEvent; import org.briarproject.api.event.LocalTransportsUpdatedEvent; import org.briarproject.api.event.MessageAddedEvent; @@ -57,7 +54,6 @@ import java.util.Arrays; import java.util.Collection; import java.util.Collections; -import static org.briarproject.api.contact.Contact.Status.ACTIVE; import static org.briarproject.api.identity.AuthorConstants.MAX_PUBLIC_KEY_LENGTH; import static org.briarproject.api.sync.SyncConstants.MAX_GROUP_DESCRIPTOR_LENGTH; import static org.briarproject.db.DatabaseConstants.MAX_OFFERED_MESSAGES; @@ -97,11 +93,12 @@ public class DatabaseComponentImplTest extends BriarTestCase { authorId = new AuthorId(TestUtils.getRandomId()); author = new Author(authorId, "Alice", new byte[MAX_PUBLIC_KEY_LENGTH]); localAuthorId = new AuthorId(TestUtils.getRandomId()); + long timestamp = System.currentTimeMillis(); localAuthor = new LocalAuthor(localAuthorId, "Bob", - new byte[MAX_PUBLIC_KEY_LENGTH], new byte[100], 1234); + new byte[MAX_PUBLIC_KEY_LENGTH], new byte[123], timestamp, + LocalAuthor.Status.ACTIVE); messageId = new MessageId(TestUtils.getRandomId()); messageId1 = new MessageId(TestUtils.getRandomId()); - long timestamp = System.currentTimeMillis(); size = 1234; raw = new byte[size]; message = new Message(messageId, groupId, timestamp, raw); @@ -112,7 +109,8 @@ public class DatabaseComponentImplTest extends BriarTestCase { "bar", "baz")); maxLatency = Integer.MAX_VALUE; contactId = new ContactId(234); - contact = new Contact(contactId, author, localAuthorId, ACTIVE); + contact = new Contact(contactId, author, localAuthorId, + Contact.Status.ACTIVE); } private DatabaseComponent createDatabaseComponent(Database database, @@ -141,7 +139,6 @@ public class DatabaseComponentImplTest extends BriarTestCase { oneOf(database).containsLocalAuthor(txn, localAuthorId); will(returnValue(false)); oneOf(database).addLocalAuthor(txn, localAuthor); - oneOf(eventBus).broadcast(with(any(LocalAuthorAddedEvent.class))); // addContact() oneOf(database).containsContact(txn, authorId); will(returnValue(false)); @@ -184,10 +181,7 @@ public class DatabaseComponentImplTest extends BriarTestCase { // removeLocalAuthor() oneOf(database).containsLocalAuthor(txn, localAuthorId); will(returnValue(true)); - oneOf(database).getContacts(txn, localAuthorId); - will(returnValue(Collections.emptyList())); oneOf(database).removeLocalAuthor(txn, localAuthorId); - oneOf(eventBus).broadcast(with(any(LocalAuthorRemovedEvent.class))); // close() oneOf(shutdown).removeShutdownHook(shutdownHandle); oneOf(database).close(); @@ -657,7 +651,6 @@ public class DatabaseComponentImplTest extends BriarTestCase { will(returnValue(false)); oneOf(database).addLocalAuthor(txn, localAuthor); oneOf(database).commitTransaction(txn); - oneOf(eventBus).broadcast(with(any(LocalAuthorAddedEvent.class))); // addContact() oneOf(database).startTransaction(); will(returnValue(txn)); diff --git a/briar-tests/src/org/briarproject/db/H2DatabaseTest.java b/briar-tests/src/org/briarproject/db/H2DatabaseTest.java index f034e3f4b..5f13ad463 100644 --- a/briar-tests/src/org/briarproject/db/H2DatabaseTest.java +++ b/briar-tests/src/org/briarproject/db/H2DatabaseTest.java @@ -3,6 +3,7 @@ package org.briarproject.db; import org.briarproject.BriarTestCase; import org.briarproject.TestDatabaseConfig; import org.briarproject.TestUtils; +import org.briarproject.api.Settings; import org.briarproject.api.TransportId; import org.briarproject.api.TransportProperties; import org.briarproject.api.contact.ContactId; @@ -21,7 +22,6 @@ import org.briarproject.api.sync.MessageStatus; import org.briarproject.api.transport.IncomingKeys; import org.briarproject.api.transport.OutgoingKeys; import org.briarproject.api.transport.TransportKeys; -import org.briarproject.api.Settings; import org.briarproject.system.SystemClock; import org.junit.After; import org.junit.Before; @@ -80,10 +80,11 @@ public class H2DatabaseTest extends BriarTestCase { AuthorId authorId = new AuthorId(TestUtils.getRandomId()); author = new Author(authorId, "Alice", new byte[MAX_PUBLIC_KEY_LENGTH]); localAuthorId = new AuthorId(TestUtils.getRandomId()); - localAuthor = new LocalAuthor(localAuthorId, "Bob", - new byte[MAX_PUBLIC_KEY_LENGTH], new byte[100], 1234); - messageId = new MessageId(TestUtils.getRandomId()); timestamp = System.currentTimeMillis(); + localAuthor = new LocalAuthor(localAuthorId, "Bob", + new byte[MAX_PUBLIC_KEY_LENGTH], new byte[123], timestamp, + LocalAuthor.Status.ACTIVE); + messageId = new MessageId(TestUtils.getRandomId()); size = 1234; raw = new byte[size]; random.nextBytes(raw); @@ -94,7 +95,7 @@ public class H2DatabaseTest extends BriarTestCase { @Before public void setUp() { - testDir.mkdirs(); + assertTrue(testDir.mkdirs()); } @Test diff --git a/briar-tests/src/org/briarproject/sync/ConstantsTest.java b/briar-tests/src/org/briarproject/sync/ConstantsTest.java index c2fa6bf5e..a67afd361 100644 --- a/briar-tests/src/org/briarproject/sync/ConstantsTest.java +++ b/briar-tests/src/org/briarproject/sync/ConstantsTest.java @@ -41,6 +41,7 @@ import org.briarproject.data.DataModule; import org.briarproject.db.DatabaseModule; import org.briarproject.event.EventModule; import org.briarproject.forum.ForumModule; +import org.briarproject.identity.IdentityModule; import org.briarproject.messaging.MessagingModule; import org.junit.Test; @@ -64,6 +65,8 @@ import static org.junit.Assert.assertTrue; public class ConstantsTest extends BriarTestCase { + // TODO: Break this up into tests that are relevant for each package + private final CryptoComponent crypto; private final GroupFactory groupFactory; private final AuthorFactory authorFactory; @@ -76,7 +79,7 @@ public class ConstantsTest extends BriarTestCase { new TestLifecycleModule(), new TestSystemModule(), new ContactModule(), new CryptoModule(), new DatabaseModule(), new DataModule(), new EventModule(), new ForumModule(), - new MessagingModule(), new SyncModule()); + new IdentityModule(), new MessagingModule(), new SyncModule()); crypto = i.getInstance(CryptoComponent.class); groupFactory = i.getInstance(GroupFactory.class); authorFactory = i.getInstance(AuthorFactory.class); diff --git a/briar-tests/src/org/briarproject/sync/SimplexMessagingIntegrationTest.java b/briar-tests/src/org/briarproject/sync/SimplexMessagingIntegrationTest.java index eb0db4854..dcb729281 100644 --- a/briar-tests/src/org/briarproject/sync/SimplexMessagingIntegrationTest.java +++ b/briar-tests/src/org/briarproject/sync/SimplexMessagingIntegrationTest.java @@ -123,7 +123,8 @@ public class SimplexMessagingIntegrationTest extends BriarTestCase { db.addTransport(transportId, MAX_LATENCY); // Add an identity for Alice LocalAuthor aliceAuthor = new LocalAuthor(aliceId, "Alice", - new byte[MAX_PUBLIC_KEY_LENGTH], new byte[100], timestamp); + new byte[MAX_PUBLIC_KEY_LENGTH], new byte[123], timestamp, + LocalAuthor.Status.ADDING); identityManager.addLocalAuthor(aliceAuthor); // Add Bob as a contact Author bobAuthor = new Author(bobId, "Bob", @@ -188,7 +189,8 @@ public class SimplexMessagingIntegrationTest extends BriarTestCase { db.addTransport(transportId, MAX_LATENCY); // Add an identity for Bob LocalAuthor bobAuthor = new LocalAuthor(bobId, "Bob", - new byte[MAX_PUBLIC_KEY_LENGTH], new byte[100], timestamp); + new byte[MAX_PUBLIC_KEY_LENGTH], new byte[123], timestamp, + LocalAuthor.Status.ADDING); identityManager.addLocalAuthor(bobAuthor); // Add Alice as a contact Author aliceAuthor = new Author(aliceId, "Alice", From 281ca734e311b20ead0d25a7d56bfd5ebd68aec3 Mon Sep 17 00:00:00 2001 From: akwizgran Date: Wed, 20 Jan 2016 14:07:24 +0000 Subject: [PATCH 4/4] Addressed issues from code review. --- .../org/briarproject/api/contact/Contact.java | 27 +++----------- .../api/contact/ContactManager.java | 12 +++---- .../api/db/DatabaseComponent.java | 4 +-- .../briarproject/api/db/StorageStatus.java | 21 +++++++++++ .../api/identity/IdentityManager.java | 12 +++---- .../api/identity/LocalAuthor.java | 28 +++------------ .../contact/ContactManagerImpl.java | 36 +++++++++---------- .../briarproject/contact/ContactModule.java | 2 +- .../src/org/briarproject/db/Database.java | 5 +-- .../db/DatabaseComponentImpl.java | 5 +-- .../src/org/briarproject/db/JdbcDatabase.java | 19 +++++----- .../identity/IdentityManagerImpl.java | 30 ++++++++-------- .../messaging/MessagingManagerImpl.java | 22 ++++++------ .../messaging/MessagingModule.java | 4 +-- .../briarproject/sync/AuthorFactoryImpl.java | 2 +- .../db/DatabaseComponentImplTest.java | 5 +-- .../org/briarproject/db/H2DatabaseTest.java | 3 +- .../sync/SimplexMessagingIntegrationTest.java | 5 +-- 18 files changed, 115 insertions(+), 127 deletions(-) create mode 100644 briar-api/src/org/briarproject/api/db/StorageStatus.java diff --git a/briar-api/src/org/briarproject/api/contact/Contact.java b/briar-api/src/org/briarproject/api/contact/Contact.java index 62d7efa64..18a2cb446 100644 --- a/briar-api/src/org/briarproject/api/contact/Contact.java +++ b/briar-api/src/org/briarproject/api/contact/Contact.java @@ -1,37 +1,18 @@ package org.briarproject.api.contact; +import org.briarproject.api.db.StorageStatus; import org.briarproject.api.identity.Author; import org.briarproject.api.identity.AuthorId; public class Contact { - public enum Status { - - ADDING(0), ACTIVE(1), REMOVING(2); - - private final int value; - - Status(int value) { - this.value = value; - } - - public int getValue() { - return value; - } - - public static Status fromValue(int value) { - for (Status s : values()) if (s.value == value) return s; - throw new IllegalArgumentException(); - } - } - private final ContactId id; private final Author author; private final AuthorId localAuthorId; - private final Status status; + private final StorageStatus status; public Contact(ContactId id, Author author, AuthorId localAuthorId, - Status status) { + StorageStatus status) { this.id = id; this.author = author; this.localAuthorId = localAuthorId; @@ -50,7 +31,7 @@ public class Contact { return localAuthorId; } - public Status getStatus() { + public StorageStatus getStatus() { return status; } diff --git a/briar-api/src/org/briarproject/api/contact/ContactManager.java b/briar-api/src/org/briarproject/api/contact/ContactManager.java index 9637d1491..08c9dc46a 100644 --- a/briar-api/src/org/briarproject/api/contact/ContactManager.java +++ b/briar-api/src/org/briarproject/api/contact/ContactManager.java @@ -9,10 +9,10 @@ import java.util.Collection; public interface ContactManager { /** Registers a hook to be called whenever a contact is added. */ - void registerContactAddedHook(ContactAddedHook hook); + void registerAddContactHook(AddContactHook hook); /** Registers a hook to be called whenever a contact is removed. */ - void registerContactRemovedHook(ContactRemovedHook hook); + void registerRemoveContactHook(RemoveContactHook hook); /** * Stores a contact associated with the given local and remote pseudonyms, @@ -29,11 +29,11 @@ public interface ContactManager { /** Removes a contact and all associated state. */ void removeContact(ContactId c) throws DbException; - interface ContactAddedHook { - void contactAdded(ContactId c); + interface AddContactHook { + void addingContact(ContactId c); } - interface ContactRemovedHook { - void contactRemoved(ContactId c); + interface RemoveContactHook { + void removingContact(ContactId c); } } diff --git a/briar-api/src/org/briarproject/api/db/DatabaseComponent.java b/briar-api/src/org/briarproject/api/db/DatabaseComponent.java index b2ee63bde..e67f0cb91 100644 --- a/briar-api/src/org/briarproject/api/db/DatabaseComponent.java +++ b/briar-api/src/org/briarproject/api/db/DatabaseComponent.java @@ -301,10 +301,10 @@ public interface DatabaseComponent { void removeTransport(TransportId t) throws DbException; /** Sets the status of the given contact. */ - void setContactStatus(ContactId c, Contact.Status s) throws DbException; + void setContactStatus(ContactId c, StorageStatus s) throws DbException; /** Sets the status of the given local pseudonym. */ - void setLocalAuthorStatus(AuthorId a, LocalAuthor.Status s) + void setLocalAuthorStatus(AuthorId a, StorageStatus s) throws DbException; /** Marks the given message as valid or invalid. */ diff --git a/briar-api/src/org/briarproject/api/db/StorageStatus.java b/briar-api/src/org/briarproject/api/db/StorageStatus.java new file mode 100644 index 000000000..dc2d554ff --- /dev/null +++ b/briar-api/src/org/briarproject/api/db/StorageStatus.java @@ -0,0 +1,21 @@ +package org.briarproject.api.db; + +public enum StorageStatus { + + ADDING(0), ACTIVE(1), REMOVING(2); + + private final int value; + + StorageStatus(int value) { + this.value = value; + } + + public int getValue() { + return value; + } + + public static StorageStatus fromValue(int value) { + for (StorageStatus s : values()) if (s.value == value) return s; + throw new IllegalArgumentException(); + } +} diff --git a/briar-api/src/org/briarproject/api/identity/IdentityManager.java b/briar-api/src/org/briarproject/api/identity/IdentityManager.java index ec2383371..9a5a030ab 100644 --- a/briar-api/src/org/briarproject/api/identity/IdentityManager.java +++ b/briar-api/src/org/briarproject/api/identity/IdentityManager.java @@ -7,10 +7,10 @@ import java.util.Collection; public interface IdentityManager { /** Registers a hook to be called whenever a local pseudonym is added. */ - void registerIdentityAddedHook(IdentityAddedHook hook); + void registerAddIdentityHook(AddIdentityHook hook); /** Registers a hook to be called whenever a local pseudonym is removed. */ - void registerIdentityRemovedHook(IdentityRemovedHook hook); + void registerRemoveIdentityHook(RemoveIdentityHook hook); /** Stores a local pseudonym. */ void addLocalAuthor(LocalAuthor a) throws DbException; @@ -24,11 +24,11 @@ public interface IdentityManager { /** Removes a local pseudonym and all associated state. */ void removeLocalAuthor(AuthorId a) throws DbException; - interface IdentityAddedHook { - void identityAdded(AuthorId a); + interface AddIdentityHook { + void addingIdentity(AuthorId a); } - interface IdentityRemovedHook { - void identityRemoved(AuthorId a); + interface RemoveIdentityHook { + void removingIdentity(AuthorId a); } } diff --git a/briar-api/src/org/briarproject/api/identity/LocalAuthor.java b/briar-api/src/org/briarproject/api/identity/LocalAuthor.java index da5d4a908..08ee121e5 100644 --- a/briar-api/src/org/briarproject/api/identity/LocalAuthor.java +++ b/briar-api/src/org/briarproject/api/identity/LocalAuthor.java @@ -1,34 +1,16 @@ package org.briarproject.api.identity; +import org.briarproject.api.db.StorageStatus; + /** A pseudonym for the local user. */ public class LocalAuthor extends Author { - public enum Status { - - ADDING(0), ACTIVE(1), REMOVING(2); - - private final int value; - - Status(int value) { - this.value = value; - } - - public int getValue() { - return value; - } - - public static Status fromValue(int value) { - for (Status s : values()) if (s.value == value) return s; - throw new IllegalArgumentException(); - } - } - private final byte[] privateKey; private final long created; - private final Status status; + private final StorageStatus status; public LocalAuthor(AuthorId id, String name, byte[] publicKey, - byte[] privateKey, long created, Status status) { + byte[] privateKey, long created, StorageStatus status) { super(id, name, publicKey); this.privateKey = privateKey; this.created = created; @@ -49,7 +31,7 @@ public class LocalAuthor extends Author { } /** Returns the status of the pseudonym. */ - public Status getStatus() { + public StorageStatus getStatus() { return status; } } diff --git a/briar-core/src/org/briarproject/contact/ContactManagerImpl.java b/briar-core/src/org/briarproject/contact/ContactManagerImpl.java index 92d802030..fa1c09621 100644 --- a/briar-core/src/org/briarproject/contact/ContactManagerImpl.java +++ b/briar-core/src/org/briarproject/contact/ContactManagerImpl.java @@ -13,7 +13,7 @@ import org.briarproject.api.event.ContactRemovedEvent; import org.briarproject.api.event.EventBus; import org.briarproject.api.identity.Author; import org.briarproject.api.identity.AuthorId; -import org.briarproject.api.identity.IdentityManager.IdentityRemovedHook; +import org.briarproject.api.identity.IdentityManager.RemoveIdentityHook; import org.briarproject.api.lifecycle.Service; import java.util.ArrayList; @@ -24,27 +24,27 @@ import java.util.concurrent.CopyOnWriteArrayList; import java.util.logging.Logger; import static java.util.logging.Level.WARNING; -import static org.briarproject.api.contact.Contact.Status.ACTIVE; -import static org.briarproject.api.contact.Contact.Status.ADDING; -import static org.briarproject.api.contact.Contact.Status.REMOVING; +import static org.briarproject.api.db.StorageStatus.ACTIVE; +import static org.briarproject.api.db.StorageStatus.ADDING; +import static org.briarproject.api.db.StorageStatus.REMOVING; class ContactManagerImpl implements ContactManager, Service, - IdentityRemovedHook { + RemoveIdentityHook { private static final Logger LOG = Logger.getLogger(ContactManagerImpl.class.getName()); private final DatabaseComponent db; private final EventBus eventBus; - private final List addHooks; - private final List removeHooks; + private final List addHooks; + private final List removeHooks; @Inject ContactManagerImpl(DatabaseComponent db, EventBus eventBus) { this.db = db; this.eventBus = eventBus; - addHooks = new CopyOnWriteArrayList(); - removeHooks = new CopyOnWriteArrayList(); + addHooks = new CopyOnWriteArrayList(); + removeHooks = new CopyOnWriteArrayList(); } @Override @@ -53,13 +53,13 @@ class ContactManagerImpl implements ContactManager, Service, try { for (Contact c : db.getContacts()) { if (c.getStatus().equals(ADDING)) { - for (ContactAddedHook hook : addHooks) - hook.contactAdded(c.getId()); + for (AddContactHook hook : addHooks) + hook.addingContact(c.getId()); db.setContactStatus(c.getId(), ACTIVE); eventBus.broadcast(new ContactAddedEvent(c.getId())); } else if (c.getStatus().equals(REMOVING)) { - for (ContactRemovedHook hook : removeHooks) - hook.contactRemoved(c.getId()); + for (RemoveContactHook hook : removeHooks) + hook.removingContact(c.getId()); db.removeContact(c.getId()); eventBus.broadcast(new ContactRemovedEvent(c.getId())); } @@ -77,12 +77,12 @@ class ContactManagerImpl implements ContactManager, Service, } @Override - public void registerContactAddedHook(ContactAddedHook hook) { + public void registerAddContactHook(AddContactHook hook) { addHooks.add(hook); } @Override - public void registerContactRemovedHook(ContactRemovedHook hook) { + public void registerRemoveContactHook(RemoveContactHook hook) { removeHooks.add(hook); } @@ -90,7 +90,7 @@ class ContactManagerImpl implements ContactManager, Service, public ContactId addContact(Author remote, AuthorId local) throws DbException { ContactId c = db.addContact(remote, local); - for (ContactAddedHook hook : addHooks) hook.contactAdded(c); + for (AddContactHook hook : addHooks) hook.addingContact(c); db.setContactStatus(c, ACTIVE); eventBus.broadcast(new ContactAddedEvent(c)); return c; @@ -116,13 +116,13 @@ class ContactManagerImpl implements ContactManager, Service, @Override public void removeContact(ContactId c) throws DbException { db.setContactStatus(c, REMOVING); - for (ContactRemovedHook hook : removeHooks) hook.contactRemoved(c); + for (RemoveContactHook hook : removeHooks) hook.removingContact(c); db.removeContact(c); eventBus.broadcast(new ContactRemovedEvent(c)); } @Override - public void identityRemoved(AuthorId a) { + public void removingIdentity(AuthorId a) { // Remove any contacts of the local pseudonym that's being removed try { for (ContactId c : db.getContacts(a)) removeContact(c); diff --git a/briar-core/src/org/briarproject/contact/ContactModule.java b/briar-core/src/org/briarproject/contact/ContactModule.java index 3a374ab5e..5bfb8e404 100644 --- a/briar-core/src/org/briarproject/contact/ContactModule.java +++ b/briar-core/src/org/briarproject/contact/ContactModule.java @@ -19,7 +19,7 @@ public class ContactModule extends AbstractModule { IdentityManager identityManager, ContactManagerImpl contactManager) { lifecycleManager.register(contactManager); - identityManager.registerIdentityRemovedHook(contactManager); + identityManager.registerRemoveIdentityHook(contactManager); return contactManager; } } diff --git a/briar-core/src/org/briarproject/db/Database.java b/briar-core/src/org/briarproject/db/Database.java index 65518cc5f..30e175453 100644 --- a/briar-core/src/org/briarproject/db/Database.java +++ b/briar-core/src/org/briarproject/db/Database.java @@ -7,6 +7,7 @@ import org.briarproject.api.contact.Contact; import org.briarproject.api.contact.ContactId; import org.briarproject.api.db.DbException; import org.briarproject.api.db.Metadata; +import org.briarproject.api.db.StorageStatus; import org.briarproject.api.identity.Author; import org.briarproject.api.identity.AuthorId; import org.briarproject.api.identity.LocalAuthor; @@ -636,7 +637,7 @@ interface Database { *

* Locking: write. */ - void setContactStatus(T txn, ContactId c, Contact.Status s) + void setContactStatus(T txn, ContactId c, StorageStatus s) throws DbException; /** @@ -644,7 +645,7 @@ interface Database { *

* Locking: write. */ - void setLocalAuthorStatus(T txn, AuthorId a, LocalAuthor.Status s) + void setLocalAuthorStatus(T txn, AuthorId a, StorageStatus s) throws DbException; /** diff --git a/briar-core/src/org/briarproject/db/DatabaseComponentImpl.java b/briar-core/src/org/briarproject/db/DatabaseComponentImpl.java index 13d650912..07aaee5cc 100644 --- a/briar-core/src/org/briarproject/db/DatabaseComponentImpl.java +++ b/briar-core/src/org/briarproject/db/DatabaseComponentImpl.java @@ -16,6 +16,7 @@ import org.briarproject.api.db.NoSuchLocalAuthorException; import org.briarproject.api.db.NoSuchMessageException; import org.briarproject.api.db.NoSuchSubscriptionException; import org.briarproject.api.db.NoSuchTransportException; +import org.briarproject.api.db.StorageStatus; import org.briarproject.api.event.EventBus; import org.briarproject.api.event.LocalSubscriptionsUpdatedEvent; import org.briarproject.api.event.LocalTransportsUpdatedEvent; @@ -1293,7 +1294,7 @@ class DatabaseComponentImpl implements DatabaseComponent { eventBus.broadcast(new TransportRemovedEvent(t)); } - public void setContactStatus(ContactId c, Contact.Status s) + public void setContactStatus(ContactId c, StorageStatus s) throws DbException { lock.writeLock().lock(); try { @@ -1312,7 +1313,7 @@ class DatabaseComponentImpl implements DatabaseComponent { } } - public void setLocalAuthorStatus(AuthorId a, LocalAuthor.Status s) + public void setLocalAuthorStatus(AuthorId a, StorageStatus s) throws DbException { lock.writeLock().lock(); try { diff --git a/briar-core/src/org/briarproject/db/JdbcDatabase.java b/briar-core/src/org/briarproject/db/JdbcDatabase.java index 54be105b2..c2ea25a9c 100644 --- a/briar-core/src/org/briarproject/db/JdbcDatabase.java +++ b/briar-core/src/org/briarproject/db/JdbcDatabase.java @@ -9,6 +9,7 @@ import org.briarproject.api.crypto.SecretKey; import org.briarproject.api.db.DbClosedException; import org.briarproject.api.db.DbException; import org.briarproject.api.db.Metadata; +import org.briarproject.api.db.StorageStatus; import org.briarproject.api.identity.Author; import org.briarproject.api.identity.AuthorId; import org.briarproject.api.identity.LocalAuthor; @@ -50,8 +51,8 @@ import java.util.concurrent.locks.ReentrantLock; import java.util.logging.Logger; import static java.util.logging.Level.WARNING; -import static org.briarproject.api.contact.Contact.Status.ADDING; import static org.briarproject.api.db.Metadata.REMOVE; +import static org.briarproject.api.db.StorageStatus.ADDING; import static org.briarproject.api.sync.SyncConstants.MAX_SUBSCRIPTIONS; import static org.briarproject.api.sync.ValidationManager.Status.INVALID; import static org.briarproject.api.sync.ValidationManager.Status.UNKNOWN; @@ -1208,7 +1209,7 @@ abstract class JdbcDatabase implements Database { String name = rs.getString(2); byte[] publicKey = rs.getBytes(3); AuthorId localAuthorId = new AuthorId(rs.getBytes(4)); - Contact.Status status = Contact.Status.fromValue(rs.getInt(5)); + StorageStatus status = StorageStatus.fromValue(rs.getInt(5)); rs.close(); ps.close(); Author author = new Author(authorId, name, publicKey); @@ -1258,7 +1259,7 @@ abstract class JdbcDatabase implements Database { byte[] publicKey = rs.getBytes(4); Author author = new Author(authorId, name, publicKey); AuthorId localAuthorId = new AuthorId(rs.getBytes(5)); - Contact.Status status = Contact.Status.fromValue(rs.getInt(6)); + StorageStatus status = StorageStatus.fromValue(rs.getInt(6)); contacts.add(new Contact(contactId, author, localAuthorId, status)); } @@ -1358,8 +1359,7 @@ abstract class JdbcDatabase implements Database { byte[] publicKey = rs.getBytes(2); byte[] privateKey = rs.getBytes(3); long created = rs.getLong(4); - LocalAuthor.Status status = LocalAuthor.Status.fromValue( - rs.getInt(5)); + StorageStatus status = StorageStatus.fromValue(rs.getInt(5)); LocalAuthor localAuthor = new LocalAuthor(a, name, publicKey, privateKey, created, status); if (rs.next()) throw new DbStateException(); @@ -1390,8 +1390,7 @@ abstract class JdbcDatabase implements Database { byte[] publicKey = rs.getBytes(3); byte[] privateKey = rs.getBytes(4); long created = rs.getLong(5); - LocalAuthor.Status status = LocalAuthor.Status.fromValue( - rs.getInt(6)); + StorageStatus status = StorageStatus.fromValue(rs.getInt(6)); authors.add(new LocalAuthor(authorId, name, publicKey, privateKey, created, status)); } @@ -1875,7 +1874,7 @@ abstract class JdbcDatabase implements Database { byte[] publicKey = rs.getBytes(4); Author author = new Author(authorId, name, publicKey); AuthorId localAuthorId = new AuthorId(rs.getBytes(5)); - Contact.Status status = Contact.Status.fromValue(rs.getInt(6)); + StorageStatus status = StorageStatus.fromValue(rs.getInt(6)); contacts.add(new Contact(contactId, author, localAuthorId, status)); } @@ -2703,7 +2702,7 @@ abstract class JdbcDatabase implements Database { } } - public void setContactStatus(Connection txn, ContactId c, Contact.Status s) + public void setContactStatus(Connection txn, ContactId c, StorageStatus s) throws DbException { PreparedStatement ps = null; try { @@ -2721,7 +2720,7 @@ abstract class JdbcDatabase implements Database { } public void setLocalAuthorStatus(Connection txn, AuthorId a, - LocalAuthor.Status s) throws DbException { + StorageStatus s) throws DbException { PreparedStatement ps = null; try { String sql = "UPDATE localAuthors SET status = ?" diff --git a/briar-core/src/org/briarproject/identity/IdentityManagerImpl.java b/briar-core/src/org/briarproject/identity/IdentityManagerImpl.java index 2e6350cbe..a51ce0238 100644 --- a/briar-core/src/org/briarproject/identity/IdentityManagerImpl.java +++ b/briar-core/src/org/briarproject/identity/IdentityManagerImpl.java @@ -21,9 +21,9 @@ import java.util.concurrent.CopyOnWriteArrayList; import java.util.logging.Logger; import static java.util.logging.Level.WARNING; -import static org.briarproject.api.identity.LocalAuthor.Status.ACTIVE; -import static org.briarproject.api.identity.LocalAuthor.Status.ADDING; -import static org.briarproject.api.identity.LocalAuthor.Status.REMOVING; +import static org.briarproject.api.db.StorageStatus.ACTIVE; +import static org.briarproject.api.db.StorageStatus.ADDING; +import static org.briarproject.api.db.StorageStatus.REMOVING; class IdentityManagerImpl implements IdentityManager, Service { @@ -32,15 +32,15 @@ class IdentityManagerImpl implements IdentityManager, Service { private final DatabaseComponent db; private final EventBus eventBus; - private final List addHooks; - private final List removeHooks; + private final List addHooks; + private final List removeHooks; @Inject IdentityManagerImpl(DatabaseComponent db, EventBus eventBus) { this.db = db; this.eventBus = eventBus; - addHooks = new CopyOnWriteArrayList(); - removeHooks = new CopyOnWriteArrayList(); + addHooks = new CopyOnWriteArrayList(); + removeHooks = new CopyOnWriteArrayList(); } @Override @@ -49,13 +49,13 @@ class IdentityManagerImpl implements IdentityManager, Service { try { for (LocalAuthor a : db.getLocalAuthors()) { if (a.getStatus().equals(ADDING)) { - for (IdentityAddedHook hook : addHooks) - hook.identityAdded(a.getId()); + for (AddIdentityHook hook : addHooks) + hook.addingIdentity(a.getId()); db.setLocalAuthorStatus(a.getId(), ACTIVE); eventBus.broadcast(new LocalAuthorAddedEvent(a.getId())); } else if (a.getStatus().equals(REMOVING)) { - for (IdentityRemovedHook hook : removeHooks) - hook.identityRemoved(a.getId()); + for (RemoveIdentityHook hook : removeHooks) + hook.removingIdentity(a.getId()); db.removeLocalAuthor(a.getId()); eventBus.broadcast(new LocalAuthorRemovedEvent(a.getId())); } @@ -73,19 +73,19 @@ class IdentityManagerImpl implements IdentityManager, Service { } @Override - public void registerIdentityAddedHook(IdentityAddedHook hook) { + public void registerAddIdentityHook(AddIdentityHook hook) { addHooks.add(hook); } @Override - public void registerIdentityRemovedHook(IdentityRemovedHook hook) { + public void registerRemoveIdentityHook(RemoveIdentityHook hook) { removeHooks.add(hook); } @Override public void addLocalAuthor(LocalAuthor a) throws DbException { db.addLocalAuthor(a); - for (IdentityAddedHook hook : addHooks) hook.identityAdded(a.getId()); + for (AddIdentityHook hook : addHooks) hook.addingIdentity(a.getId()); db.setLocalAuthorStatus(a.getId(), ACTIVE); eventBus.broadcast(new LocalAuthorAddedEvent(a.getId())); } @@ -110,7 +110,7 @@ class IdentityManagerImpl implements IdentityManager, Service { @Override public void removeLocalAuthor(AuthorId a) throws DbException { db.setLocalAuthorStatus(a, REMOVING); - for (IdentityRemovedHook hook : removeHooks) hook.identityRemoved(a); + for (RemoveIdentityHook hook : removeHooks) hook.removingIdentity(a); db.removeLocalAuthor(a); eventBus.broadcast(new LocalAuthorRemovedEvent(a)); } diff --git a/briar-core/src/org/briarproject/messaging/MessagingManagerImpl.java b/briar-core/src/org/briarproject/messaging/MessagingManagerImpl.java index 37f5acafe..49502aa04 100644 --- a/briar-core/src/org/briarproject/messaging/MessagingManagerImpl.java +++ b/briar-core/src/org/briarproject/messaging/MessagingManagerImpl.java @@ -6,8 +6,8 @@ import org.briarproject.api.FormatException; import org.briarproject.api.UniqueId; import org.briarproject.api.contact.Contact; import org.briarproject.api.contact.ContactId; -import org.briarproject.api.contact.ContactManager.ContactAddedHook; -import org.briarproject.api.contact.ContactManager.ContactRemovedHook; +import org.briarproject.api.contact.ContactManager.AddContactHook; +import org.briarproject.api.contact.ContactManager.RemoveContactHook; import org.briarproject.api.data.BdfDictionary; import org.briarproject.api.data.BdfReader; import org.briarproject.api.data.BdfReaderFactory; @@ -44,8 +44,8 @@ import static java.util.logging.Level.WARNING; import static org.briarproject.api.messaging.MessagingConstants.MAX_PRIVATE_MESSAGE_BODY_LENGTH; import static org.briarproject.api.sync.SyncConstants.MESSAGE_HEADER_LENGTH; -class MessagingManagerImpl implements MessagingManager, ContactAddedHook, - ContactRemovedHook { +class MessagingManagerImpl implements MessagingManager, AddContactHook, + RemoveContactHook { static final ClientId CLIENT_ID = new ClientId(StringUtils.fromHexString( "6bcdc006c0910b0f44e40644c3b31f1a" @@ -75,10 +75,10 @@ class MessagingManagerImpl implements MessagingManager, ContactAddedHook, } @Override - public void contactAdded(ContactId c) { + public void addingContact(ContactId c) { try { // Create the conversation group - Group g = createConversationGroup(db.getContact(c)); + Group g = getConversationGroup(db.getContact(c)); // Subscribe to the group and share it with the contact db.addGroup(g); db.addContactGroup(c, g); @@ -88,7 +88,7 @@ class MessagingManagerImpl implements MessagingManager, ContactAddedHook, } } - private Group createConversationGroup(Contact c) { + private Group getConversationGroup(Contact c) { AuthorId local = c.getLocalAuthorId(); AuthorId remote = c.getAuthor().getId(); byte[] descriptor = createGroupDescriptor(local, remote); @@ -116,9 +116,9 @@ class MessagingManagerImpl implements MessagingManager, ContactAddedHook, } @Override - public void contactRemoved(ContactId c) { + public void removingContact(ContactId c) { try { - db.removeGroup(createConversationGroup(db.getContact(c))); + db.removeGroup(getConversationGroup(db.getContact(c))); } catch (DbException e) { if (LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e); } @@ -149,7 +149,7 @@ class MessagingManagerImpl implements MessagingManager, ContactAddedHook, public ContactId getContactId(GroupId g) throws DbException { // TODO: Use metadata to attach the contact ID to the group for (Contact c : db.getContacts()) { - Group conversation = createConversationGroup(c); + Group conversation = getConversationGroup(c); if (conversation.getId().equals(g)) return c.getId(); } throw new NoSuchContactException(); @@ -157,7 +157,7 @@ class MessagingManagerImpl implements MessagingManager, ContactAddedHook, @Override public GroupId getConversationId(ContactId c) throws DbException { - return createConversationGroup(db.getContact(c)).getId(); + return getConversationGroup(db.getContact(c)).getId(); } @Override diff --git a/briar-core/src/org/briarproject/messaging/MessagingModule.java b/briar-core/src/org/briarproject/messaging/MessagingModule.java index 42827c2c0..1854afcc7 100644 --- a/briar-core/src/org/briarproject/messaging/MessagingModule.java +++ b/briar-core/src/org/briarproject/messaging/MessagingModule.java @@ -35,8 +35,8 @@ public class MessagingModule extends AbstractModule { @Provides @Singleton MessagingManager getMessagingManager(ContactManager contactManager, MessagingManagerImpl messagingManager) { - contactManager.registerContactAddedHook(messagingManager); - contactManager.registerContactRemovedHook(messagingManager); + contactManager.registerAddContactHook(messagingManager); + contactManager.registerRemoveContactHook(messagingManager); return messagingManager; } } diff --git a/briar-core/src/org/briarproject/sync/AuthorFactoryImpl.java b/briar-core/src/org/briarproject/sync/AuthorFactoryImpl.java index 102ea1ee8..da217728a 100644 --- a/briar-core/src/org/briarproject/sync/AuthorFactoryImpl.java +++ b/briar-core/src/org/briarproject/sync/AuthorFactoryImpl.java @@ -14,7 +14,7 @@ import java.io.IOException; import javax.inject.Inject; -import static org.briarproject.api.identity.LocalAuthor.Status.ADDING; +import static org.briarproject.api.db.StorageStatus.ADDING; class AuthorFactoryImpl implements AuthorFactory { diff --git a/briar-tests/src/org/briarproject/db/DatabaseComponentImplTest.java b/briar-tests/src/org/briarproject/db/DatabaseComponentImplTest.java index 285da1aeb..b4ff178a5 100644 --- a/briar-tests/src/org/briarproject/db/DatabaseComponentImplTest.java +++ b/briar-tests/src/org/briarproject/db/DatabaseComponentImplTest.java @@ -15,6 +15,7 @@ import org.briarproject.api.db.NoSuchLocalAuthorException; import org.briarproject.api.db.NoSuchMessageException; import org.briarproject.api.db.NoSuchSubscriptionException; import org.briarproject.api.db.NoSuchTransportException; +import org.briarproject.api.db.StorageStatus; import org.briarproject.api.event.EventBus; import org.briarproject.api.event.LocalSubscriptionsUpdatedEvent; import org.briarproject.api.event.LocalTransportsUpdatedEvent; @@ -96,7 +97,7 @@ public class DatabaseComponentImplTest extends BriarTestCase { long timestamp = System.currentTimeMillis(); localAuthor = new LocalAuthor(localAuthorId, "Bob", new byte[MAX_PUBLIC_KEY_LENGTH], new byte[123], timestamp, - LocalAuthor.Status.ACTIVE); + StorageStatus.ACTIVE); messageId = new MessageId(TestUtils.getRandomId()); messageId1 = new MessageId(TestUtils.getRandomId()); size = 1234; @@ -110,7 +111,7 @@ public class DatabaseComponentImplTest extends BriarTestCase { maxLatency = Integer.MAX_VALUE; contactId = new ContactId(234); contact = new Contact(contactId, author, localAuthorId, - Contact.Status.ACTIVE); + StorageStatus.ACTIVE); } private DatabaseComponent createDatabaseComponent(Database database, diff --git a/briar-tests/src/org/briarproject/db/H2DatabaseTest.java b/briar-tests/src/org/briarproject/db/H2DatabaseTest.java index 5f13ad463..c0cde9e40 100644 --- a/briar-tests/src/org/briarproject/db/H2DatabaseTest.java +++ b/briar-tests/src/org/briarproject/db/H2DatabaseTest.java @@ -10,6 +10,7 @@ import org.briarproject.api.contact.ContactId; import org.briarproject.api.crypto.SecretKey; import org.briarproject.api.db.DbException; import org.briarproject.api.db.Metadata; +import org.briarproject.api.db.StorageStatus; import org.briarproject.api.identity.Author; import org.briarproject.api.identity.AuthorId; import org.briarproject.api.identity.LocalAuthor; @@ -83,7 +84,7 @@ public class H2DatabaseTest extends BriarTestCase { timestamp = System.currentTimeMillis(); localAuthor = new LocalAuthor(localAuthorId, "Bob", new byte[MAX_PUBLIC_KEY_LENGTH], new byte[123], timestamp, - LocalAuthor.Status.ACTIVE); + StorageStatus.ACTIVE); messageId = new MessageId(TestUtils.getRandomId()); size = 1234; raw = new byte[size]; diff --git a/briar-tests/src/org/briarproject/sync/SimplexMessagingIntegrationTest.java b/briar-tests/src/org/briarproject/sync/SimplexMessagingIntegrationTest.java index dcb729281..198ac61a3 100644 --- a/briar-tests/src/org/briarproject/sync/SimplexMessagingIntegrationTest.java +++ b/briar-tests/src/org/briarproject/sync/SimplexMessagingIntegrationTest.java @@ -12,6 +12,7 @@ import org.briarproject.api.contact.ContactId; import org.briarproject.api.contact.ContactManager; import org.briarproject.api.crypto.SecretKey; import org.briarproject.api.db.DatabaseComponent; +import org.briarproject.api.db.StorageStatus; import org.briarproject.api.event.Event; import org.briarproject.api.event.EventBus; import org.briarproject.api.event.EventListener; @@ -124,7 +125,7 @@ public class SimplexMessagingIntegrationTest extends BriarTestCase { // Add an identity for Alice LocalAuthor aliceAuthor = new LocalAuthor(aliceId, "Alice", new byte[MAX_PUBLIC_KEY_LENGTH], new byte[123], timestamp, - LocalAuthor.Status.ADDING); + StorageStatus.ADDING); identityManager.addLocalAuthor(aliceAuthor); // Add Bob as a contact Author bobAuthor = new Author(bobId, "Bob", @@ -190,7 +191,7 @@ public class SimplexMessagingIntegrationTest extends BriarTestCase { // Add an identity for Bob LocalAuthor bobAuthor = new LocalAuthor(bobId, "Bob", new byte[MAX_PUBLIC_KEY_LENGTH], new byte[123], timestamp, - LocalAuthor.Status.ADDING); + StorageStatus.ADDING); identityManager.addLocalAuthor(bobAuthor); // Add Alice as a contact Author aliceAuthor = new Author(aliceId, "Alice",