mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-12 10:49:06 +01:00
Implement Service interface rather than extending it.
Whether or not a class needs to run as a service is an implementation decision.
This commit is contained in:
@@ -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();
|
||||
|
||||
@@ -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);
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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());
|
||||
|
||||
@@ -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());
|
||||
|
||||
@@ -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");
|
||||
|
||||
@@ -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());
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user