diff --git a/briar-android/src/org/briarproject/android/AndroidNotificationManagerImpl.java b/briar-android/src/org/briarproject/android/AndroidNotificationManagerImpl.java index 8a5bcba15e5acf951ffe05faed623fa53803d873..ca3ed6a4cef08dbe8b4a2b8e2a7f09a35c14f93f 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 ed3223c4b7652ec44f31e6674399efaa6de7bb97..3aad189dc32c3b404fa8cf1ba695c8e55e2acebd 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 efc336cf020c17686bd1441e50202828b437fecc..35248962d65da3a6e6d2de481a21e4c08f874f7b 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 d318421a472c16b5a78f597028c8786901477fd7..d3d7ef520a2d0f29554f74dcb160417a28f86e19 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 729e96ddccd6a47a06a100d4e1f2ac7af01c286b..ee1d7e967208b8ee32aaab897fa09e2f3b68be79 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 e2f76f081b89d057eed77a873add5149e85afa41..32800d7af410448fff4e9b8679349f091d217a4e 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 205b290f478ca8589f0c683f9e7b56e7bb0c74eb..5f321d33dcf55d895de118cbc0eb86af0ba7426f 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 a3b4919380799809513fa8ee5b6e0d4572facbbd..c97c66b6af2d37af84368a9d1ef53c6991508424 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 abdf1657800d0b496084a3baffb7013fced02aa8..0625b2d2461c69635a7b347d577cad0b76cac0d9 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<DuplexPlugin>(); } + @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 68040be941b37f2aa0edf7ccef1a59f5ec91fd78..d3e8612661ad52afda70a0c1be32dd090c1dabc2 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 c36e844d2efddcc5cb25fd42c7e3afced68321a5..aa50d8e5f60bb544301e63e8c9792fb6af963c03 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<TransportId, TransportKeyManager>(); } + @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 3a7be3b74b4254f3299a1bbb75a2cd878f3fdf02..07eb61d57ac582cc8a0b6d3fa1bef64e2eb909a0 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