Switched Roboguice/Guice out for Dagger 2

This commit is contained in:
Ernir Erlingsson
2016-03-03 10:24:40 +01:00
parent e5d7038195
commit 1be400eb84
89 changed files with 1640 additions and 802 deletions

View File

@@ -1,7 +1,5 @@
package org.briarproject.contact;
import com.google.inject.Inject;
import org.briarproject.api.contact.Contact;
import org.briarproject.api.contact.ContactId;
import org.briarproject.api.contact.ContactManager;
@@ -21,7 +19,22 @@ import java.util.Collections;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
<<<<<<< 08099714bab27d1ed48a8bee431a35a38098ecec
class ContactManagerImpl implements ContactManager, RemoveIdentityHook {
=======
import javax.inject.Inject;
import static java.util.logging.Level.WARNING;
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,
RemoveIdentityHook {
private static final Logger LOG =
Logger.getLogger(ContactManagerImpl.class.getName());
>>>>>>> Switched Roboguice/Guice out for Dagger 2
private final DatabaseComponent db;
private final KeyManager keyManager;

View File

@@ -1,21 +1,23 @@
package org.briarproject.contact;
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;
public class ContactModule extends AbstractModule {
import dagger.Module;
import dagger.Provides;
@Override
protected void configure() {}
@Module
public class ContactModule {
@Provides @Singleton
ContactManager getContactManager(IdentityManager identityManager,
@Provides
@Singleton
ContactManager getContactManager(LifecycleManager lifecycleManager,
IdentityManager identityManager,
ContactManagerImpl contactManager) {
lifecycleManager.register(contactManager);
identityManager.registerRemoveIdentityHook(contactManager);
return contactManager;
}

View File

@@ -1,14 +1,12 @@
package org.briarproject.crypto;
import com.google.inject.AbstractModule;
import com.google.inject.Provides;
import org.briarproject.api.crypto.CryptoComponent;
import org.briarproject.api.crypto.CryptoExecutor;
import org.briarproject.api.crypto.PasswordStrengthEstimator;
import org.briarproject.api.crypto.StreamDecrypterFactory;
import org.briarproject.api.crypto.StreamEncrypterFactory;
import org.briarproject.api.lifecycle.LifecycleManager;
import org.briarproject.api.system.SeedProvider;
import java.security.SecureRandom;
import java.util.concurrent.BlockingQueue;
@@ -18,13 +16,20 @@ import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadPoolExecutor;
import javax.inject.Provider;
import javax.inject.Singleton;
import dagger.Module;
import dagger.Provides;
import static java.util.concurrent.TimeUnit.SECONDS;
public class CryptoModule extends AbstractModule {
@Module
public class CryptoModule {
/** The maximum number of executor threads. */
/**
* The maximum number of executor threads.
*/
private static final int MAX_EXECUTOR_THREADS =
Runtime.getRuntime().availableProcessors();
@@ -41,19 +46,37 @@ public class CryptoModule extends AbstractModule {
60, SECONDS, queue, policy);
}
@Override
protected void configure() {
bind(AuthenticatedCipher.class).to(
XSalsa20Poly1305AuthenticatedCipher.class);
bind(CryptoComponent.class).to(
CryptoComponentImpl.class).in(Singleton.class);
bind(PasswordStrengthEstimator.class).to(
PasswordStrengthEstimatorImpl.class);
bind(StreamDecrypterFactory.class).to(StreamDecrypterFactoryImpl.class);
bind(StreamEncrypterFactory.class).to(StreamEncrypterFactoryImpl.class);
@Provides
AuthenticatedCipher provideAuthenticatedCipher() {
return new XSalsa20Poly1305AuthenticatedCipher();
}
@Provides @Singleton @CryptoExecutor
@Provides
@Singleton
CryptoComponent provideCryptoComponent(SeedProvider seedProvider) {
return new CryptoComponentImpl(seedProvider);
}
@Provides
PasswordStrengthEstimator providePasswordStrengthEstimator() {
return new PasswordStrengthEstimatorImpl();
}
@Provides
StreamDecrypterFactory provideStreamDecrypterFactory(
Provider<AuthenticatedCipher> cipherProvider) {
return new StreamDecrypterFactoryImpl(cipherProvider);
}
@Provides
StreamEncrypterFactory provideStreamEncrypterFactory(CryptoComponent crypto,
Provider<AuthenticatedCipher> cipherProvider) {
return new StreamEncrypterFactoryImpl(crypto, cipherProvider);
}
@Provides
@Singleton
@CryptoExecutor
Executor getCryptoExecutor(LifecycleManager lifecycleManager) {
lifecycleManager.registerForShutdown(cryptoExecutor);
return cryptoExecutor;
@@ -63,4 +86,5 @@ public class CryptoModule extends AbstractModule {
SecureRandom getSecureRandom(CryptoComponent crypto) {
return crypto.getSecureRandom();
}
}

View File

@@ -1,19 +1,35 @@
package org.briarproject.data;
import com.google.inject.AbstractModule;
import org.briarproject.api.data.BdfReaderFactory;
import org.briarproject.api.data.BdfWriterFactory;
import org.briarproject.api.data.MetadataEncoder;
import org.briarproject.api.data.MetadataParser;
public class DataModule extends AbstractModule {
import dagger.Module;
import dagger.Provides;
@Override
protected void configure() {
bind(BdfReaderFactory.class).to(BdfReaderFactoryImpl.class);
bind(BdfWriterFactory.class).to(BdfWriterFactoryImpl.class);
bind(MetadataParser.class).to(MetadataParserImpl.class);
bind(MetadataEncoder.class).to(MetadataEncoderImpl.class);
@Module
public class DataModule {
@Provides
BdfReaderFactory provideBdfReaderFactory() {
return new BdfReaderFactoryImpl();
}
@Provides
BdfWriterFactory provideBdfWriterFactory() {
return new BdfWriterFactoryImpl();
}
@Provides
MetadataParser provideMetaDataParser() {
return new MetadataParserImpl();
}
@Provides
MetadataEncoder provideMetaDataEncoider() {
return new MetadataEncoderImpl();
}
}

View File

@@ -1,8 +1,5 @@
package org.briarproject.db;
import com.google.inject.AbstractModule;
import com.google.inject.Provides;
import org.briarproject.api.db.DatabaseComponent;
import org.briarproject.api.db.DatabaseConfig;
import org.briarproject.api.db.DatabaseExecutor;
@@ -20,11 +17,16 @@ import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadPoolExecutor;
import javax.inject.Named;
import javax.inject.Singleton;
import dagger.Module;
import dagger.Provides;
import static java.util.concurrent.TimeUnit.SECONDS;
public class DatabaseModule extends AbstractModule {
@Module
public class DatabaseModule {
private final ExecutorService databaseExecutor;
@@ -39,30 +41,28 @@ public class DatabaseModule extends AbstractModule {
policy);
}
@Override
protected void configure() {}
@Provides @Singleton
Database<Connection> getDatabase(DatabaseConfig config,
@Provides
@Singleton
Database<Connection> provideDatabase(DatabaseConfig config,
SecureRandom random, Clock clock) {
return new H2Database(config, random, clock);
}
@Provides @Singleton
DatabaseComponent getDatabaseComponent(Database<Connection> db,
DatabaseComponent provideDatabaseComponent(Database<Connection> db,
EventBus eventBus, ShutdownManager shutdown) {
return new DatabaseComponentImpl<Connection>(db, Connection.class,
eventBus, shutdown);
}
@Provides @Singleton @DatabaseExecutor
ExecutorService getDatabaseExecutor(LifecycleManager lifecycleManager) {
ExecutorService provideDatabaseExecutorService(LifecycleManager lifecycleManager) {
lifecycleManager.registerForShutdown(databaseExecutor);
return databaseExecutor;
}
@Provides @Singleton @DatabaseExecutor
Executor getDatabaseExecutor(@DatabaseExecutor ExecutorService dbExecutor) {
Executor provideDatabaseExecutor(@DatabaseExecutor ExecutorService dbExecutor) {
return dbExecutor;
}
}

View File

@@ -2,13 +2,17 @@ package org.briarproject.event;
import org.briarproject.api.event.EventBus;
import com.google.inject.AbstractModule;
import com.google.inject.Singleton;
import javax.inject.Singleton;
public class EventModule extends AbstractModule {
import dagger.Module;
import dagger.Provides;
@Override
protected void configure() {
bind(EventBus.class).to(EventBusImpl.class).in(Singleton.class);
@Module
public class EventModule {
@Provides
@Singleton
EventBus provideEventBus() {
return new EventBusImpl();
}
}

View File

@@ -1,14 +1,15 @@
package org.briarproject.forum;
import com.google.inject.Inject;
import org.briarproject.api.FormatException;
import org.briarproject.api.clients.ClientHelper;
import org.briarproject.api.contact.Contact;
import org.briarproject.api.data.BdfDictionary;
import org.briarproject.api.data.BdfList;
import org.briarproject.api.data.BdfReader;
import org.briarproject.api.data.BdfReaderFactory;
import org.briarproject.api.data.MetadataEncoder;
import org.briarproject.api.data.MetadataParser;
import org.briarproject.api.db.DatabaseComponent;
import org.briarproject.api.db.DbException;
import org.briarproject.api.db.Metadata;
import org.briarproject.api.db.Transaction;
import org.briarproject.api.forum.Forum;
import org.briarproject.api.forum.ForumManager;
@@ -23,6 +24,8 @@ import org.briarproject.api.sync.GroupId;
import org.briarproject.api.sync.MessageId;
import org.briarproject.util.StringUtils;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
@@ -31,10 +34,18 @@ import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.logging.Logger;
import javax.inject.Inject;
import static java.util.logging.Level.WARNING;
import static org.briarproject.api.forum.ForumConstants.FORUM_SALT_LENGTH;
import static org.briarproject.api.forum.ForumConstants.MAX_FORUM_NAME_LENGTH;
import static org.briarproject.api.forum.ForumConstants.MAX_FORUM_POST_BODY_LENGTH;
import static org.briarproject.api.identity.Author.Status.ANONYMOUS;
import static org.briarproject.api.identity.Author.Status.UNKNOWN;
import static org.briarproject.api.identity.Author.Status.VERIFIED;
import static org.briarproject.api.sync.SyncConstants.MESSAGE_HEADER_LENGTH;
class ForumManagerImpl implements ForumManager {
@@ -42,13 +53,21 @@ class ForumManagerImpl implements ForumManager {
"859a7be50dca035b64bd6902fb797097"
+ "795af837abbf8c16d750b3c2ccc186ea"));
private static final Logger LOG =
Logger.getLogger(ForumManagerImpl.class.getName());
private final DatabaseComponent db;
private final ClientHelper clientHelper;
private final BdfReaderFactory bdfReaderFactory;
private final MetadataEncoder metadataEncoder;
private final MetadataParser metadataParser;
@Inject
ForumManagerImpl(DatabaseComponent db, ClientHelper clientHelper) {
ForumManagerImpl(DatabaseComponent db, BdfReaderFactory bdfReaderFactory,
MetadataEncoder metadataEncoder, MetadataParser metadataParser) {
this.db = db;
this.clientHelper = clientHelper;
this.bdfReaderFactory = bdfReaderFactory;
this.metadataEncoder = metadataEncoder;
this.metadataParser = metadataParser;
}
@Override
@@ -59,21 +78,29 @@ class ForumManagerImpl implements ForumManager {
@Override
public void addLocalPost(ForumPost p) throws DbException {
try {
BdfDictionary meta = new BdfDictionary();
meta.put("timestamp", p.getMessage().getTimestamp());
if (p.getParent() != null) meta.put("parent", p.getParent());
BdfDictionary d = new BdfDictionary();
d.put("timestamp", p.getMessage().getTimestamp());
if (p.getParent() != null)
d.put("parent", p.getParent().getBytes());
if (p.getAuthor() != null) {
Author a = p.getAuthor();
BdfDictionary authorMeta = new BdfDictionary();
authorMeta.put("id", a.getId());
authorMeta.put("name", a.getName());
authorMeta.put("publicKey", a.getPublicKey());
meta.put("author", authorMeta);
BdfDictionary d1 = new BdfDictionary();
d1.put("id", a.getId().getBytes());
d1.put("name", a.getName());
d1.put("publicKey", a.getPublicKey());
d.put("author", d1);
}
d.put("contentType", p.getContentType());
d.put("local", true);
d.put("read", true);
Metadata meta = metadataEncoder.encode(d);
Transaction txn = db.startTransaction();
try {
db.addLocalMessage(txn, p.getMessage(), CLIENT_ID, meta, true);
txn.setComplete();
} finally {
db.endTransaction(txn);
}
meta.put("contentType", p.getContentType());
meta.put("local", true);
meta.put("read", true);
clientHelper.addLocalMessage(p.getMessage(), CLIENT_ID, meta, true);
} catch (FormatException e) {
throw new RuntimeException(e);
}
@@ -118,11 +145,34 @@ class ForumManagerImpl implements ForumManager {
@Override
public byte[] getPostBody(MessageId m) throws DbException {
try {
// Parent ID, author, content type, forum post body, signature
BdfList message = clientHelper.getMessageAsList(m);
return message.getRaw(3);
byte[] raw;
Transaction txn = db.startTransaction();
try {
raw = db.getRawMessage(txn, m);
txn.setComplete();
} finally {
db.endTransaction(txn);
}
ByteArrayInputStream in = new ByteArrayInputStream(raw,
MESSAGE_HEADER_LENGTH, raw.length - MESSAGE_HEADER_LENGTH);
BdfReader r = bdfReaderFactory.createReader(in);
r.readListStart();
if (r.hasRaw()) r.skipRaw(); // Parent ID
else r.skipNull(); // No parent
if (r.hasList()) r.skipList(); // Author
else r.skipNull(); // No author
r.skipString(); // Content type
byte[] postBody = r.readRaw(MAX_FORUM_POST_BODY_LENGTH);
if (r.hasRaw()) r.skipRaw(); // Signature
else r.skipNull();
r.readListEnd();
if (!r.eof()) throw new FormatException();
return postBody;
} catch (FormatException e) {
throw new DbException(e);
} catch (IOException e) {
// Shouldn't happen with ByteArrayInputStream
throw new RuntimeException(e);
}
}
@@ -131,7 +181,7 @@ class ForumManagerImpl implements ForumManager {
throws DbException {
Set<AuthorId> localAuthorIds = new HashSet<AuthorId>();
Set<AuthorId> contactAuthorIds = new HashSet<AuthorId>();
Map<MessageId, BdfDictionary> metadata;
Map<MessageId, Metadata> metadata;
Transaction txn = db.startTransaction();
try {
// Load the IDs of the user's identities
@@ -141,22 +191,20 @@ class ForumManagerImpl implements ForumManager {
for (Contact c : db.getContacts(txn))
contactAuthorIds.add(c.getAuthor().getId());
// Load the metadata
metadata = clientHelper.getMessageMetadataAsDictionary(txn, g);
metadata = db.getMessageMetadata(txn, g);
txn.setComplete();
} catch (FormatException e) {
throw new DbException(e);
} finally {
db.endTransaction(txn);
}
// Parse the metadata
Collection<ForumPostHeader> headers = new ArrayList<ForumPostHeader>();
for (Entry<MessageId, BdfDictionary> entry : metadata.entrySet()) {
for (Entry<MessageId, Metadata> e : metadata.entrySet()) {
try {
BdfDictionary meta = entry.getValue();
long timestamp = meta.getLong("timestamp");
BdfDictionary d = metadataParser.parse(e.getValue());
long timestamp = d.getLong("timestamp");
Author author = null;
Author.Status authorStatus = ANONYMOUS;
BdfDictionary d1 = meta.getDictionary("author", null);
BdfDictionary d1 = d.getDictionary("author", null);
if (d1 != null) {
AuthorId authorId = new AuthorId(d1.getRaw("id"));
String name = d1.getString("name");
@@ -168,12 +216,13 @@ class ForumManagerImpl implements ForumManager {
authorStatus = VERIFIED;
else authorStatus = UNKNOWN;
}
String contentType = meta.getString("contentType");
boolean read = meta.getBoolean("read");
headers.add(new ForumPostHeader(entry.getKey(), timestamp,
author, authorStatus, contentType, read));
} catch (FormatException e) {
throw new DbException(e);
String contentType = d.getString("contentType");
boolean read = d.getBoolean("read");
headers.add(new ForumPostHeader(e.getKey(), timestamp, author,
authorStatus, contentType, read));
} catch (FormatException ex) {
if (LOG.isLoggable(WARNING))
LOG.log(WARNING, ex.toString(), ex);
}
}
return headers;
@@ -182,18 +231,36 @@ class ForumManagerImpl implements ForumManager {
@Override
public void setReadFlag(MessageId m, boolean read) throws DbException {
try {
BdfDictionary meta = new BdfDictionary();
meta.put("read", read);
clientHelper.mergeMessageMetadata(m, meta);
BdfDictionary d = new BdfDictionary();
d.put("read", read);
Metadata meta = metadataEncoder.encode(d);
Transaction txn = db.startTransaction();
try {
db.mergeMessageMetadata(txn, m, meta);
txn.setComplete();
} finally {
db.endTransaction(txn);
}
} catch (FormatException e) {
throw new RuntimeException(e);
}
}
private Forum parseForum(Group g) throws FormatException {
byte[] descriptor = g.getDescriptor();
// Name, salt
BdfList forum = clientHelper.toList(descriptor, 0, descriptor.length);
return new Forum(g, forum.getString(0), forum.getRaw(1));
ByteArrayInputStream in = new ByteArrayInputStream(g.getDescriptor());
BdfReader r = bdfReaderFactory.createReader(in);
try {
r.readListStart();
String name = r.readString(MAX_FORUM_NAME_LENGTH);
byte[] salt = r.readRaw(FORUM_SALT_LENGTH);
r.readListEnd();
if (!r.eof()) throw new FormatException();
return new Forum(g, name, salt);
} catch (FormatException e) {
throw e;
} catch (IOException e) {
// Shouldn't happen with ByteArrayInputStream
throw new RuntimeException(e);
}
}
}

View File

@@ -1,54 +1,80 @@
package org.briarproject.forum;
import com.google.inject.AbstractModule;
import com.google.inject.Provides;
import org.briarproject.api.clients.ClientHelper;
import org.briarproject.api.contact.ContactManager;
import org.briarproject.api.crypto.CryptoComponent;
import org.briarproject.api.data.BdfReaderFactory;
import org.briarproject.api.data.BdfWriterFactory;
import org.briarproject.api.data.MetadataEncoder;
import org.briarproject.api.data.MetadataParser;
import org.briarproject.api.data.ObjectReader;
import org.briarproject.api.db.DatabaseComponent;
import org.briarproject.api.forum.ForumManager;
import org.briarproject.api.forum.ForumPostFactory;
import org.briarproject.api.forum.ForumSharingManager;
import org.briarproject.api.identity.AuthorFactory;
import org.briarproject.api.identity.Author;
import org.briarproject.api.sync.MessageFactory;
import org.briarproject.api.sync.ValidationManager;
import org.briarproject.api.system.Clock;
import javax.inject.Singleton;
public class ForumModule extends AbstractModule {
import dagger.Module;
import dagger.Provides;
@Override
protected void configure() {
bind(ForumManager.class).to(ForumManagerImpl.class).in(Singleton.class);
bind(ForumPostFactory.class).to(ForumPostFactoryImpl.class);
@Module
public class ForumModule {
@Provides
@Singleton
ForumManager provideForumManager(DatabaseComponent db,
ContactManager contactManager,
BdfReaderFactory bdfReaderFactory, MetadataEncoder metadataEncoder,
MetadataParser metadataParser) {
return new ForumManagerImpl(db, contactManager, bdfReaderFactory,
metadataEncoder, metadataParser);
}
@Provides @Singleton
ForumPostValidator getForumPostValidator(
@Provides
ForumPostFactory provideForumPostFactory(CryptoComponent crypto,
MessageFactory messageFactory,
BdfWriterFactory bdfWriterFactory) {
return new ForumPostFactoryImpl(crypto, messageFactory,
bdfWriterFactory);
}
@Provides
@Singleton
ForumPostValidator provideForumPostValidator(
ValidationManager validationManager, CryptoComponent crypto,
AuthorFactory authorFactory, ClientHelper clientHelper,
MetadataEncoder metadataEncoder, Clock clock) {
BdfReaderFactory bdfReaderFactory,
BdfWriterFactory bdfWriterFactory,
ObjectReader<Author> authorReader, MetadataEncoder metadataEncoder,
Clock clock) {
ForumPostValidator validator = new ForumPostValidator(crypto,
authorFactory, clientHelper, metadataEncoder, clock);
bdfReaderFactory, bdfWriterFactory, authorReader,
metadataEncoder, clock);
validationManager.registerMessageValidator(
ForumManagerImpl.CLIENT_ID, validator);
return validator;
}
@Provides @Singleton
ForumListValidator getForumListValidator(
ValidationManager validationManager, ClientHelper clientHelper,
MetadataEncoder metadataEncoder, Clock clock) {
ForumListValidator validator = new ForumListValidator(clientHelper,
metadataEncoder, clock);
@Provides
@Singleton
ForumListValidator provideForumListValidator(
ValidationManager validationManager,
BdfReaderFactory bdfReaderFactory,
MetadataEncoder metadataEncoder) {
ForumListValidator validator = new ForumListValidator(bdfReaderFactory,
metadataEncoder);
validationManager.registerMessageValidator(
ForumSharingManagerImpl.CLIENT_ID, validator);
return validator;
}
@Provides @Singleton
ForumSharingManager getForumSharingManager(ContactManager contactManager,
@Provides
@Singleton
ForumSharingManager provideForumSharingManager(
ContactManager contactManager,
ValidationManager validationManager,
ForumSharingManagerImpl forumSharingManager) {
contactManager.registerAddContactHook(forumSharingManager);

View File

@@ -1,7 +1,5 @@
package org.briarproject.forum;
import com.google.inject.Inject;
import org.briarproject.api.FormatException;
import org.briarproject.api.clients.ClientHelper;
import org.briarproject.api.clients.PrivateGroupFactory;
@@ -39,6 +37,12 @@ import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
<<<<<<< 08099714bab27d1ed48a8bee431a35a38098ecec
=======
import javax.inject.Inject;
import static java.util.logging.Level.WARNING;
>>>>>>> Switched Roboguice/Guice out for Dagger 2
import static org.briarproject.api.forum.ForumConstants.FORUM_SALT_LENGTH;
import static org.briarproject.api.forum.ForumConstants.MAX_FORUM_NAME_LENGTH;
import static org.briarproject.api.sync.SyncConstants.MESSAGE_HEADER_LENGTH;

View File

@@ -1,7 +1,5 @@
package org.briarproject.identity;
import com.google.inject.Inject;
import org.briarproject.api.db.DatabaseComponent;
import org.briarproject.api.db.DbException;
import org.briarproject.api.db.Transaction;
@@ -13,7 +11,21 @@ import java.util.Collection;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
<<<<<<< 08099714bab27d1ed48a8bee431a35a38098ecec
class IdentityManagerImpl implements IdentityManager {
=======
import javax.inject.Inject;
import static java.util.logging.Level.WARNING;
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 {
private static final Logger LOG =
Logger.getLogger(IdentityManagerImpl.class.getName());
>>>>>>> Switched Roboguice/Guice out for Dagger 2
private final DatabaseComponent db;
private final List<AddIdentityHook> addHooks;

View File

@@ -1,23 +1,20 @@
package org.briarproject.identity;
import com.google.inject.AbstractModule;
import com.google.inject.Provides;
import org.briarproject.api.data.ObjectReader;
import org.briarproject.api.identity.Author;
import org.briarproject.api.identity.AuthorFactory;
import org.briarproject.api.db.DatabaseComponent;
import org.briarproject.api.event.EventBus;
import org.briarproject.api.identity.IdentityManager;
public class IdentityModule extends AbstractModule {
import javax.inject.Singleton;
@Override
protected void configure() {
bind(AuthorFactory.class).to(AuthorFactoryImpl.class);
bind(IdentityManager.class).to(IdentityManagerImpl.class);
}
import dagger.Module;
import dagger.Provides;
@Module
public class IdentityModule {
@Provides
ObjectReader<Author> getAuthorReader(AuthorFactory authorFactory) {
return new AuthorReader(authorFactory);
@Singleton
IdentityManager provideIdendityModule(DatabaseComponent db, EventBus eventBus) {
return new IdentityManagerImpl(db, eventBus);
}
}

View File

@@ -2,14 +2,41 @@ package org.briarproject.invitation;
import javax.inject.Singleton;
import org.briarproject.api.contact.ContactManager;
import org.briarproject.api.crypto.CryptoComponent;
import org.briarproject.api.data.BdfReaderFactory;
import org.briarproject.api.data.BdfWriterFactory;
import org.briarproject.api.identity.AuthorFactory;
import org.briarproject.api.identity.IdentityManager;
import org.briarproject.api.invitation.InvitationTaskFactory;
import org.briarproject.api.plugins.ConnectionManager;
import org.briarproject.api.plugins.PluginManager;
import org.briarproject.api.sync.GroupFactory;
import org.briarproject.api.system.Clock;
import org.briarproject.api.transport.KeyManager;
import org.briarproject.api.transport.StreamReaderFactory;
import org.briarproject.api.transport.StreamWriterFactory;
import com.google.inject.AbstractModule;
import dagger.Module;
import dagger.Provides;
public class InvitationModule extends AbstractModule {
@Module
public class InvitationModule {
protected void configure() {
bind(InvitationTaskFactory.class).to(
InvitationTaskFactoryImpl.class).in(Singleton.class);
@Provides
@Singleton
InvitationTaskFactory provideInvitationTaskFactory(CryptoComponent crypto,
BdfReaderFactory bdfReaderFactory,
BdfWriterFactory bdfWriterFactory,
StreamReaderFactory streamReaderFactory,
StreamWriterFactory streamWriterFactory,
AuthorFactory authorFactory, GroupFactory groupFactory,
KeyManager keyManager, ConnectionManager connectionManager,
IdentityManager identityManager, ContactManager contactManager,
Clock clock, PluginManager pluginManager) {
return new InvitationTaskFactoryImpl(crypto, bdfReaderFactory,
bdfWriterFactory, streamReaderFactory, streamWriterFactory,
authorFactory, groupFactory, keyManager, connectionManager,
identityManager, contactManager, clock, pluginManager);
}
}

View File

@@ -11,14 +11,18 @@ import java.util.concurrent.ThreadPoolExecutor;
import javax.inject.Singleton;
import org.briarproject.api.db.DatabaseComponent;
import org.briarproject.api.event.EventBus;
import org.briarproject.api.lifecycle.IoExecutor;
import org.briarproject.api.lifecycle.LifecycleManager;
import org.briarproject.api.lifecycle.ShutdownManager;
import org.briarproject.api.system.Clock;
import com.google.inject.AbstractModule;
import com.google.inject.Provides;
import dagger.Module;
import dagger.Provides;
public class LifecycleModule extends AbstractModule {
@Module
public class LifecycleModule {
private final ExecutorService ioExecutor;
@@ -33,17 +37,25 @@ public class LifecycleModule extends AbstractModule {
60, SECONDS, queue, policy);
}
@Override
protected void configure() {
bind(LifecycleManager.class).to(
LifecycleManagerImpl.class).in(Singleton.class);
bind(ShutdownManager.class).to(
ShutdownManagerImpl.class).in(Singleton.class);
@Provides
@Singleton
ShutdownManager provideShutdownManager() {
return new ShutdownManagerImpl();
}
@Provides @Singleton @IoExecutor
@Provides
@Singleton
LifecycleManager provideLifeCycleManager(Clock clock, DatabaseComponent db,
EventBus eventBus) {
return new LifecycleManagerImpl(clock, db, eventBus);
}
@Provides
@Singleton
@IoExecutor
Executor getIoExecutor(LifecycleManager lifecycleManager) {
lifecycleManager.registerForShutdown(ioExecutor);
return ioExecutor;
}
}

View File

@@ -1,18 +1,19 @@
package org.briarproject.messaging;
import com.google.inject.Inject;
import org.briarproject.api.FormatException;
import org.briarproject.api.clients.ClientHelper;
import org.briarproject.api.clients.PrivateGroupFactory;
import org.briarproject.api.contact.Contact;
import org.briarproject.api.contact.ContactId;
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.BdfList;
import org.briarproject.api.data.BdfReader;
import org.briarproject.api.data.BdfReaderFactory;
import org.briarproject.api.data.MetadataEncoder;
import org.briarproject.api.data.MetadataParser;
import org.briarproject.api.db.DatabaseComponent;
import org.briarproject.api.db.DbException;
import org.briarproject.api.db.Metadata;
import org.briarproject.api.db.Transaction;
import org.briarproject.api.messaging.MessagingManager;
import org.briarproject.api.messaging.PrivateMessage;
@@ -24,9 +25,18 @@ import org.briarproject.api.sync.MessageId;
import org.briarproject.api.sync.MessageStatus;
import org.briarproject.util.StringUtils;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Map;
import java.util.logging.Logger;
import javax.inject.Inject;
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, AddContactHook,
RemoveContactHook {
@@ -35,16 +45,25 @@ class MessagingManagerImpl implements MessagingManager, AddContactHook,
"6bcdc006c0910b0f44e40644c3b31f1a"
+ "8bf9a6d6021d40d219c86b731b903070"));
private static final Logger LOG =
Logger.getLogger(MessagingManagerImpl.class.getName());
private final DatabaseComponent db;
private final ClientHelper clientHelper;
private final PrivateGroupFactory privateGroupFactory;
private final BdfReaderFactory bdfReaderFactory;
private final MetadataEncoder metadataEncoder;
private final MetadataParser metadataParser;
@Inject
MessagingManagerImpl(DatabaseComponent db, ClientHelper clientHelper,
PrivateGroupFactory privateGroupFactory) {
MessagingManagerImpl(DatabaseComponent db,
PrivateGroupFactory privateGroupFactory,
BdfReaderFactory bdfReaderFactory, MetadataEncoder metadataEncoder,
MetadataParser metadataParser) {
this.db = db;
this.clientHelper = clientHelper;
this.privateGroupFactory = privateGroupFactory;
this.bdfReaderFactory = bdfReaderFactory;
this.metadataEncoder = metadataEncoder;
this.metadataParser = metadataParser;
}
@Override
@@ -58,7 +77,7 @@ class MessagingManagerImpl implements MessagingManager, AddContactHook,
// Attach the contact ID to the group
BdfDictionary d = new BdfDictionary();
d.put("contactId", c.getId().getInt());
clientHelper.mergeGroupMetadata(txn, g.getId(), d);
db.mergeGroupMetadata(txn, g.getId(), metadataEncoder.encode(d));
} catch (FormatException e) {
throw new RuntimeException(e);
}
@@ -81,13 +100,21 @@ class MessagingManagerImpl implements MessagingManager, AddContactHook,
@Override
public void addLocalMessage(PrivateMessage m) throws DbException {
try {
BdfDictionary meta = new BdfDictionary();
meta.put("timestamp", m.getMessage().getTimestamp());
if (m.getParent() != null) meta.put("parent", m.getParent());
meta.put("contentType", m.getContentType());
meta.put("local", true);
meta.put("read", true);
clientHelper.addLocalMessage(m.getMessage(), CLIENT_ID, meta, true);
BdfDictionary d = new BdfDictionary();
d.put("timestamp", m.getMessage().getTimestamp());
if (m.getParent() != null)
d.put("parent", m.getParent().getBytes());
d.put("contentType", m.getContentType());
d.put("local", true);
d.put("read", true);
Metadata meta = metadataEncoder.encode(d);
Transaction txn = db.startTransaction();
try {
db.addLocalMessage(txn, m.getMessage(), CLIENT_ID, meta, true);
txn.setComplete();
} finally {
db.endTransaction(txn);
}
} catch (FormatException e) {
throw new RuntimeException(e);
}
@@ -96,8 +123,16 @@ class MessagingManagerImpl implements MessagingManager, AddContactHook,
@Override
public ContactId getContactId(GroupId g) throws DbException {
try {
BdfDictionary meta = clientHelper.getGroupMetadataAsDictionary(g);
return new ContactId(meta.getLong("contactId").intValue());
Metadata meta;
Transaction txn = db.startTransaction();
try {
meta = db.getGroupMetadata(txn, g);
txn.setComplete();
} finally {
db.endTransaction(txn);
}
BdfDictionary d = metadataParser.parse(meta);
return new ContactId(d.getLong("contactId").intValue());
} catch (FormatException e) {
throw new DbException(e);
}
@@ -119,16 +154,14 @@ class MessagingManagerImpl implements MessagingManager, AddContactHook,
@Override
public Collection<PrivateMessageHeader> getMessageHeaders(ContactId c)
throws DbException {
Map<MessageId, BdfDictionary> metadata;
Map<MessageId, Metadata> metadata;
Collection<MessageStatus> statuses;
Transaction txn = db.startTransaction();
try {
GroupId g = getContactGroup(db.getContact(txn, c)).getId();
metadata = clientHelper.getMessageMetadataAsDictionary(txn, g);
metadata = db.getMessageMetadata(txn, g);
statuses = db.getMessageStatus(txn, c, g);
txn.setComplete();
} catch (FormatException e) {
throw new DbException(e);
} finally {
db.endTransaction(txn);
}
@@ -136,17 +169,18 @@ class MessagingManagerImpl implements MessagingManager, AddContactHook,
new ArrayList<PrivateMessageHeader>();
for (MessageStatus s : statuses) {
MessageId id = s.getMessageId();
BdfDictionary meta = metadata.get(id);
if (meta == null) continue;
Metadata m = metadata.get(id);
if (m == null) continue;
try {
long timestamp = meta.getLong("timestamp");
String contentType = meta.getString("contentType");
boolean local = meta.getBoolean("local");
boolean read = meta.getBoolean("read");
BdfDictionary d = metadataParser.parse(m);
long timestamp = d.getLong("timestamp");
String contentType = d.getString("contentType");
boolean local = d.getBoolean("local");
boolean read = d.getBoolean("read");
headers.add(new PrivateMessageHeader(id, timestamp, contentType,
local, read, s.isSent(), s.isSeen()));
} catch (FormatException e) {
throw new DbException(e);
if (LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
}
}
return headers;
@@ -154,21 +188,47 @@ class MessagingManagerImpl implements MessagingManager, AddContactHook,
@Override
public byte[] getMessageBody(MessageId m) throws DbException {
byte[] raw;
Transaction txn = db.startTransaction();
try {
// Parent ID, content type, private message body
BdfList message = clientHelper.getMessageAsList(m);
return message.getRaw(2);
raw = db.getRawMessage(txn, m);
txn.setComplete();
} finally {
db.endTransaction(txn);
}
ByteArrayInputStream in = new ByteArrayInputStream(raw,
MESSAGE_HEADER_LENGTH, raw.length - MESSAGE_HEADER_LENGTH);
BdfReader r = bdfReaderFactory.createReader(in);
try {
r.readListStart();
if (r.hasRaw()) r.skipRaw(); // Parent ID
else r.skipNull(); // No parent
r.skipString(); // Content type
byte[] messageBody = r.readRaw(MAX_PRIVATE_MESSAGE_BODY_LENGTH);
r.readListEnd();
if (!r.eof()) throw new FormatException();
return messageBody;
} catch (FormatException e) {
throw new DbException(e);
} catch (IOException e) {
// Shouldn't happen with ByteArrayInputStream
throw new RuntimeException(e);
}
}
@Override
public void setReadFlag(MessageId m, boolean read) throws DbException {
try {
BdfDictionary meta = new BdfDictionary();
meta.put("read", read);
clientHelper.mergeMessageMetadata(m, meta);
BdfDictionary d = new BdfDictionary();
d.put("read", read);
Metadata meta = metadataEncoder.encode(d);
Transaction txn = db.startTransaction();
try {
db.mergeMessageMetadata(txn, m, meta);
txn.setComplete();
} finally {
db.endTransaction(txn);
}
} catch (FormatException e) {
throw new RuntimeException(e);
}

View File

@@ -1,38 +1,46 @@
package org.briarproject.messaging;
import com.google.inject.AbstractModule;
import com.google.inject.Provides;
import org.briarproject.api.clients.ClientHelper;
import org.briarproject.api.contact.ContactManager;
import org.briarproject.api.data.BdfReaderFactory;
import org.briarproject.api.data.BdfWriterFactory;
import org.briarproject.api.data.MetadataEncoder;
import org.briarproject.api.messaging.MessagingManager;
import org.briarproject.api.messaging.PrivateMessageFactory;
import org.briarproject.api.sync.MessageFactory;
import org.briarproject.api.sync.ValidationManager;
import org.briarproject.api.system.Clock;
import javax.inject.Singleton;
import dagger.Module;
import dagger.Provides;
import static org.briarproject.messaging.MessagingManagerImpl.CLIENT_ID;
public class MessagingModule extends AbstractModule {
@Module
public class MessagingModule {
@Override
protected void configure() {
bind(PrivateMessageFactory.class).to(PrivateMessageFactoryImpl.class);
@Provides
PrivateMessageFactory providePrivateMessageFactory(
MessageFactory messageFactory,
BdfWriterFactory bdfWriterFactory) {
return new PrivateMessageFactoryImpl(messageFactory, bdfWriterFactory);
}
@Provides @Singleton
@Provides
@Singleton
PrivateMessageValidator getValidator(ValidationManager validationManager,
ClientHelper clientHelper, MetadataEncoder metadataEncoder,
BdfReaderFactory bdfReaderFactory, MetadataEncoder metadataEncoder,
Clock clock) {
PrivateMessageValidator validator = new PrivateMessageValidator(
clientHelper, metadataEncoder, clock);
bdfReaderFactory, metadataEncoder, clock);
validationManager.registerMessageValidator(CLIENT_ID, validator);
return validator;
}
@Provides @Singleton
@Provides
@Singleton
MessagingManager getMessagingManager(ContactManager contactManager,
MessagingManagerImpl messagingManager) {
contactManager.registerAddContactHook(messagingManager);

View File

@@ -1,7 +1,5 @@
package org.briarproject.plugins;
import com.google.inject.Inject;
import org.briarproject.api.TransportId;
import org.briarproject.api.contact.ContactId;
import org.briarproject.api.event.ContactConnectedEvent;
@@ -19,6 +17,8 @@ import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.logging.Logger;
import javax.inject.Inject;
import static java.util.logging.Level.INFO;
class ConnectionRegistryImpl implements ConnectionRegistry {

View File

@@ -1,28 +1,54 @@
package org.briarproject.plugins;
import com.google.inject.AbstractModule;
import com.google.inject.Provides;
import org.briarproject.api.lifecycle.LifecycleManager;
import org.briarproject.api.plugins.BackoffFactory;
import org.briarproject.api.plugins.ConnectionManager;
import org.briarproject.api.plugins.ConnectionRegistry;
import org.briarproject.api.plugins.PluginManager;
import javax.inject.Singleton;
public class PluginsModule extends AbstractModule {
import org.briarproject.api.event.EventBus;
import org.briarproject.api.lifecycle.IoExecutor;
import org.briarproject.api.lifecycle.LifecycleManager;
import org.briarproject.api.plugins.ConnectionManager;
import org.briarproject.api.plugins.ConnectionRegistry;
import org.briarproject.api.plugins.PluginManager;
import org.briarproject.api.sync.SyncSessionFactory;
import org.briarproject.api.system.Timer;
import org.briarproject.api.transport.KeyManager;
import org.briarproject.api.transport.StreamReaderFactory;
import org.briarproject.api.transport.StreamWriterFactory;
@Override
protected void configure() {
bind(BackoffFactory.class).to(BackoffFactoryImpl.class);
bind(Poller.class).to(PollerImpl.class);
bind(ConnectionManager.class).to(ConnectionManagerImpl.class);
bind(ConnectionRegistry.class).to(
ConnectionRegistryImpl.class).in(Singleton.class);
import java.util.concurrent.Executor;
import dagger.Module;
import dagger.Provides;
@Module
public class PluginsModule {
@Provides
Poller providePoller(@IoExecutor Executor ioExecutor,
ConnectionRegistry connectionRegistry, Timer timer) {
return new PollerImpl(ioExecutor, connectionRegistry, timer);
}
@Provides @Singleton
@Provides
ConnectionManager provideConnectionManager(
@IoExecutor Executor ioExecutor,
KeyManager keyManager, StreamReaderFactory streamReaderFactory,
StreamWriterFactory streamWriterFactory,
SyncSessionFactory syncSessionFactory,
ConnectionRegistry connectionRegistry) {
return new ConnectionManagerImpl(ioExecutor, keyManager,
streamReaderFactory, streamWriterFactory, syncSessionFactory,
connectionRegistry);
}
@Provides
@Singleton
ConnectionRegistry provideConnectionRegistry(EventBus eventBus) {
return new ConnectionRegistryImpl(eventBus);
}
@Provides
@Singleton
PluginManager getPluginManager(LifecycleManager lifecycleManager,
PluginManagerImpl pluginManager) {
lifecycleManager.register(pluginManager);

View File

@@ -1,10 +1,7 @@
package org.briarproject.properties;
import com.google.inject.AbstractModule;
import com.google.inject.Provides;
import org.briarproject.api.clients.ClientHelper;
import org.briarproject.api.contact.ContactManager;
import org.briarproject.api.data.BdfReaderFactory;
import org.briarproject.api.data.MetadataEncoder;
import org.briarproject.api.properties.TransportPropertyManager;
import org.briarproject.api.sync.ValidationManager;
@@ -12,19 +9,21 @@ import org.briarproject.api.system.Clock;
import javax.inject.Singleton;
import dagger.Module;
import dagger.Provides;
import static org.briarproject.properties.TransportPropertyManagerImpl.CLIENT_ID;
public class PropertiesModule extends AbstractModule {
@Module
public class PropertiesModule {
@Override
protected void configure() {}
@Provides @Singleton
@Provides
@Singleton
TransportPropertyValidator getValidator(ValidationManager validationManager,
ClientHelper clientHelper, MetadataEncoder metadataEncoder,
BdfReaderFactory bdfReaderFactory, MetadataEncoder metadataEncoder,
Clock clock) {
TransportPropertyValidator validator = new TransportPropertyValidator(
clientHelper, metadataEncoder, clock);
bdfReaderFactory, metadataEncoder, clock);
validationManager.registerMessageValidator(CLIENT_ID, validator);
return validator;
}

View File

@@ -1,20 +1,23 @@
package org.briarproject.properties;
import com.google.inject.Inject;
import org.briarproject.api.DeviceId;
import org.briarproject.api.FormatException;
import org.briarproject.api.TransportId;
import org.briarproject.api.clients.ClientHelper;
import org.briarproject.api.clients.PrivateGroupFactory;
import org.briarproject.api.contact.Contact;
import org.briarproject.api.contact.ContactId;
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.BdfList;
import org.briarproject.api.data.BdfReader;
import org.briarproject.api.data.BdfReaderFactory;
import org.briarproject.api.data.BdfWriter;
import org.briarproject.api.data.BdfWriterFactory;
import org.briarproject.api.data.MetadataEncoder;
import org.briarproject.api.data.MetadataParser;
import org.briarproject.api.db.DatabaseComponent;
import org.briarproject.api.db.DbException;
import org.briarproject.api.db.Metadata;
import org.briarproject.api.db.NoSuchGroupException;
import org.briarproject.api.db.Transaction;
import org.briarproject.api.properties.TransportProperties;
@@ -24,15 +27,24 @@ import org.briarproject.api.sync.Group;
import org.briarproject.api.sync.GroupFactory;
import org.briarproject.api.sync.GroupId;
import org.briarproject.api.sync.Message;
import org.briarproject.api.sync.MessageFactory;
import org.briarproject.api.sync.MessageId;
import org.briarproject.api.system.Clock;
import org.briarproject.util.StringUtils;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import javax.inject.Inject;
import static org.briarproject.api.properties.TransportPropertyConstants.MAX_PROPERTY_LENGTH;
import static org.briarproject.api.sync.SyncConstants.MESSAGE_HEADER_LENGTH;
class TransportPropertyManagerImpl implements TransportPropertyManager,
AddContactHook, RemoveContactHook {
@@ -43,18 +55,28 @@ class TransportPropertyManagerImpl implements TransportPropertyManager,
private static final byte[] LOCAL_GROUP_DESCRIPTOR = new byte[0];
private final DatabaseComponent db;
private final ClientHelper clientHelper;
private final PrivateGroupFactory privateGroupFactory;
private final MessageFactory messageFactory;
private final BdfReaderFactory bdfReaderFactory;
private final BdfWriterFactory bdfWriterFactory;
private final MetadataEncoder metadataEncoder;
private final MetadataParser metadataParser;
private final Clock clock;
private final Group localGroup;
@Inject
TransportPropertyManagerImpl(DatabaseComponent db,
ClientHelper clientHelper, GroupFactory groupFactory,
PrivateGroupFactory privateGroupFactory, Clock clock) {
GroupFactory groupFactory, PrivateGroupFactory privateGroupFactory,
MessageFactory messageFactory, BdfReaderFactory bdfReaderFactory,
BdfWriterFactory bdfWriterFactory, MetadataEncoder metadataEncoder,
MetadataParser metadataParser, Clock clock) {
this.db = db;
this.clientHelper = clientHelper;
this.privateGroupFactory = privateGroupFactory;
this.messageFactory = messageFactory;
this.bdfReaderFactory = bdfReaderFactory;
this.bdfWriterFactory = bdfWriterFactory;
this.metadataEncoder = metadataEncoder;
this.metadataParser = metadataParser;
this.clock = clock;
localGroup = groupFactory.createGroup(CLIENT_ID,
LOCAL_GROUP_DESCRIPTOR);
@@ -123,9 +145,8 @@ class TransportPropertyManagerImpl implements TransportPropertyManager,
true);
if (latest != null) {
// Retrieve and parse the latest local properties
BdfList message = clientHelper.getMessageAsList(txn,
latest.messageId);
p = parseProperties(message);
byte[] raw = db.getRawMessage(txn, latest.messageId);
p = parseProperties(raw);
}
txn.setComplete();
} finally {
@@ -154,9 +175,8 @@ class TransportPropertyManagerImpl implements TransportPropertyManager,
LatestUpdate latest = findLatest(txn, g.getId(), t, false);
if (latest != null) {
// Retrieve and parse the latest remote properties
BdfList message = clientHelper.getMessageAsList(txn,
latest.messageId);
remote.put(c.getId(), parseProperties(message));
byte[] raw = db.getRawMessage(txn, latest.messageId);
remote.put(c.getId(), parseProperties(raw));
}
}
txn.setComplete();
@@ -186,9 +206,8 @@ class TransportPropertyManagerImpl implements TransportPropertyManager,
merged = p;
changed = true;
} else {
BdfList message = clientHelper.getMessageAsList(txn,
latest.messageId);
TransportProperties old = parseProperties(message);
byte[] raw = db.getRawMessage(txn, latest.messageId);
TransportProperties old = parseProperties(raw);
merged = new TransportProperties(old);
merged.putAll(p);
changed = !merged.equals(old);
@@ -231,9 +250,8 @@ class TransportPropertyManagerImpl implements TransportPropertyManager,
localGroup.getId(), true);
// Retrieve and parse the latest local properties
for (Entry<TransportId, LatestUpdate> e : latest.entrySet()) {
BdfList message = clientHelper.getMessageAsList(txn,
e.getValue().messageId);
local.put(e.getKey(), parseProperties(message));
byte[] raw = db.getRawMessage(txn, e.getValue().messageId);
local.put(e.getKey(), parseProperties(raw));
}
return local;
} catch (NoSuchGroupException e) {
@@ -248,35 +266,48 @@ class TransportPropertyManagerImpl implements TransportPropertyManager,
TransportId t, TransportProperties p, long version, boolean local,
boolean shared) throws DbException {
try {
BdfList body = encodeProperties(dev, t, p, version);
byte[] body = encodeProperties(dev, t, p, version);
long now = clock.currentTimeMillis();
Message m = clientHelper.createMessage(g, now, body);
BdfDictionary meta = new BdfDictionary();
meta.put("transportId", t.getString());
meta.put("version", version);
meta.put("local", local);
clientHelper.addLocalMessage(txn, m, CLIENT_ID, meta, shared);
Message m = messageFactory.createMessage(g, now, body);
BdfDictionary d = new BdfDictionary();
d.put("transportId", t.getString());
d.put("version", version);
d.put("local", local);
Metadata meta = metadataEncoder.encode(d);
db.addLocalMessage(txn, m, CLIENT_ID, meta, shared);
} catch (FormatException e) {
throw new RuntimeException(e);
}
}
private BdfList encodeProperties(DeviceId dev, TransportId t,
private byte[] encodeProperties(DeviceId dev, TransportId t,
TransportProperties p, long version) {
return BdfList.of(dev, t.getString(), version, p);
ByteArrayOutputStream out = new ByteArrayOutputStream();
BdfWriter w = bdfWriterFactory.createWriter(out);
try {
w.writeListStart();
w.writeRaw(dev.getBytes());
w.writeString(t.getString());
w.writeLong(version);
w.writeDictionary(p);
w.writeListEnd();
} catch (IOException e) {
// Shouldn't happen with ByteArrayOutputStream
throw new RuntimeException(e);
}
return out.toByteArray();
}
private Map<TransportId, LatestUpdate> findLatest(Transaction txn,
GroupId g, boolean local) throws DbException, FormatException {
Map<TransportId, LatestUpdate> latestUpdates =
new HashMap<TransportId, LatestUpdate>();
Map<MessageId, BdfDictionary> metadata =
clientHelper.getMessageMetadataAsDictionary(txn, g);
for (Entry<MessageId, BdfDictionary> e : metadata.entrySet()) {
BdfDictionary meta = e.getValue();
if (meta.getBoolean("local") == local) {
TransportId t = new TransportId(meta.getString("transportId"));
long version = meta.getLong("version");
Map<MessageId, Metadata> metadata = db.getMessageMetadata(txn, g);
for (Entry<MessageId, Metadata> e : metadata.entrySet()) {
BdfDictionary d = metadataParser.parse(e.getValue());
if (d.getBoolean("local") == local) {
TransportId t = new TransportId(d.getString("transportId"));
long version = d.getLong("version");
LatestUpdate latest = latestUpdates.get(t);
if (latest == null || version > latest.version)
latestUpdates.put(t, new LatestUpdate(e.getKey(), version));
@@ -288,13 +319,12 @@ class TransportPropertyManagerImpl implements TransportPropertyManager,
private LatestUpdate findLatest(Transaction txn, GroupId g, TransportId t,
boolean local) throws DbException, FormatException {
LatestUpdate latest = null;
Map<MessageId, BdfDictionary> metadata =
clientHelper.getMessageMetadataAsDictionary(txn, g);
for (Entry<MessageId, BdfDictionary> e : metadata.entrySet()) {
BdfDictionary meta = e.getValue();
if (meta.getString("transportId").equals(t.getString())
&& meta.getBoolean("local") == local) {
long version = meta.getLong("version");
Map<MessageId, Metadata> metadata = db.getMessageMetadata(txn, g);
for (Entry<MessageId, Metadata> e : metadata.entrySet()) {
BdfDictionary d = metadataParser.parse(e.getValue());
if (d.getString("transportId").equals(t.getString())
&& d.getBoolean("local") == local) {
long version = d.getLong("version");
if (latest == null || version > latest.version)
latest = new LatestUpdate(e.getKey(), version);
}
@@ -302,14 +332,33 @@ class TransportPropertyManagerImpl implements TransportPropertyManager,
return latest;
}
private TransportProperties parseProperties(BdfList message)
private TransportProperties parseProperties(byte[] raw)
throws FormatException {
// Device ID, transport ID, version, properties
BdfDictionary dictionary = message.getDictionary(3);
TransportProperties p = new TransportProperties();
for (String key : dictionary.keySet())
p.put(key, dictionary.getString(key));
return p;
ByteArrayInputStream in = new ByteArrayInputStream(raw,
MESSAGE_HEADER_LENGTH, raw.length - MESSAGE_HEADER_LENGTH);
BdfReader r = bdfReaderFactory.createReader(in);
try {
r.readListStart();
r.skipRaw(); // Device ID
r.skipString(); // Transport ID
r.skipLong(); // Version
r.readDictionaryStart();
while (!r.hasDictionaryEnd()) {
String key = r.readString(MAX_PROPERTY_LENGTH);
String value = r.readString(MAX_PROPERTY_LENGTH);
p.put(key, value);
}
r.readDictionaryEnd();
r.readListEnd();
if (!r.eof()) throw new FormatException();
return p;
} catch (FormatException e) {
throw e;
} catch (IOException e) {
// Shouldn't happen with ByteArrayInputStream
throw new RuntimeException(e);
}
}
private static class LatestUpdate {

View File

@@ -1,14 +1,30 @@
package org.briarproject.reliability;
import org.briarproject.api.lifecycle.IoExecutor;
import org.briarproject.api.reliability.ReliabilityLayerFactory;
import com.google.inject.AbstractModule;
import java.util.concurrent.Executor;
public class ReliabilityModule extends AbstractModule {
import dagger.Module;
import dagger.Provides;
@Override
protected void configure() {
bind(ReliabilityLayerFactory.class).to(
ReliabilityLayerFactoryImpl.class);
@Module
public class ReliabilityModule {
/*
@Provides
ReliabilityLayerFactory provideReliabilityFactory(@IoExecutor
Executor ioExecutor) {
return new ReliabilityLayerFactoryImpl(ioExecutor);
}
*/
@Provides
ReliabilityLayerFactory provideReliabilityFactory(
ReliabilityLayerFactoryImpl reliabilityLayerFactory) {
return reliabilityLayerFactory;
}
}

View File

@@ -1,6 +1,5 @@
package org.briarproject.settings;
import com.google.inject.Inject;
import org.briarproject.api.db.DatabaseComponent;
import org.briarproject.api.db.DbException;
@@ -8,6 +7,8 @@ import org.briarproject.api.db.Transaction;
import org.briarproject.api.settings.Settings;
import org.briarproject.api.settings.SettingsManager;
import javax.inject.Inject;
class SettingsManagerImpl implements SettingsManager {
private final DatabaseComponent db;

View File

@@ -1,13 +1,17 @@
package org.briarproject.settings;
import com.google.inject.AbstractModule;
import org.briarproject.api.db.DatabaseComponent;
import org.briarproject.api.settings.SettingsManager;
public class SettingsModule extends AbstractModule {
import dagger.Module;
import dagger.Provides;
@Override
protected void configure() {
bind(SettingsManager.class).to(SettingsManagerImpl.class);
@Module
public class SettingsModule {
@Provides
SettingsManager provideSettingsManager(DatabaseComponent db) {
return new SettingsManagerImpl(db);
}
}

View File

@@ -1,6 +1,5 @@
package org.briarproject.sync;
import com.google.inject.Inject;
import org.briarproject.api.UniqueId;
import org.briarproject.api.crypto.CryptoComponent;
@@ -10,6 +9,8 @@ import org.briarproject.api.sync.MessageFactory;
import org.briarproject.api.sync.MessageId;
import org.briarproject.util.ByteUtils;
import javax.inject.Inject;
import static org.briarproject.api.sync.SyncConstants.MAX_MESSAGE_BODY_LENGTH;
import static org.briarproject.api.sync.SyncConstants.MESSAGE_HEADER_LENGTH;

View File

@@ -1,32 +1,84 @@
package org.briarproject.sync;
import com.google.inject.AbstractModule;
import com.google.inject.Provides;
import org.briarproject.api.crypto.CryptoComponent;
import org.briarproject.api.data.BdfWriterFactory;
import org.briarproject.api.data.ObjectReader;
import org.briarproject.api.db.DatabaseComponent;
import org.briarproject.api.db.DatabaseExecutor;
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;
import org.briarproject.api.sync.PacketReaderFactory;
import org.briarproject.api.sync.PacketWriterFactory;
import org.briarproject.api.sync.PrivateGroupFactory;
import org.briarproject.api.sync.SyncSessionFactory;
import org.briarproject.api.sync.ValidationManager;
import org.briarproject.api.system.Clock;
import java.util.concurrent.Executor;
import javax.inject.Singleton;
public class SyncModule extends AbstractModule {
import dagger.Module;
import dagger.Provides;
@Override
protected void configure() {
bind(GroupFactory.class).to(GroupFactoryImpl.class);
bind(MessageFactory.class).to(MessageFactoryImpl.class);
bind(PacketReaderFactory.class).to(PacketReaderFactoryImpl.class);
bind(PacketWriterFactory.class).to(PacketWriterFactoryImpl.class);
bind(SyncSessionFactory.class).to(
SyncSessionFactoryImpl.class).in(Singleton.class);
@Module
public class SyncModule {
@Provides
AuthorFactory provideAuthFactory(CryptoComponent crypto,
BdfWriterFactory bdfWriterFactory, Clock clock) {
return new AuthorFactoryImpl(crypto, bdfWriterFactory, clock);
}
@Provides @Singleton
@Provides
GroupFactory provideGroupFactory(CryptoComponent crypto) {
return new GroupFactoryImpl(crypto);
}
@Provides
MessageFactory provideMessageFactory(CryptoComponent crypto) {
return new MessageFactoryImpl(crypto);
}
@Provides
PacketReaderFactory providePacketReaderFactory(CryptoComponent crypto) {
return new PacketReaderFactoryImpl(crypto);
}
@Provides
PacketWriterFactory providePacketWriterFactory() {
return new PacketWriterFactoryImpl();
}
@Provides
PrivateGroupFactory providePrivateGroupFactory(GroupFactory groupFactory,
BdfWriterFactory bdfWriterFactory) {
return new PrivateGroupFactoryImpl(groupFactory, bdfWriterFactory);
}
@Provides
@Singleton
SyncSessionFactory provideSyncSessionFactory(DatabaseComponent db,
@DatabaseExecutor Executor dbExecutor, EventBus eventBus,
Clock clock, PacketReaderFactory packetReaderFactory,
PacketWriterFactory packetWriterFactory) {
return new SyncSessionFactoryImpl(db, dbExecutor, eventBus, clock,
packetReaderFactory, packetWriterFactory);
}
@Provides
ObjectReader<Author> getAuthorReader(AuthorFactory authorFactory) {
return new AuthorReader(authorFactory);
}
@Provides
@Singleton
ValidationManager getValidationManager(LifecycleManager lifecycleManager,
EventBus eventBus, ValidationManagerImpl validationManager) {
lifecycleManager.register(validationManager);

View File

@@ -1,7 +1,5 @@
package org.briarproject.sync;
import com.google.inject.Inject;
import org.briarproject.api.UniqueId;
import org.briarproject.api.crypto.CryptoExecutor;
import org.briarproject.api.db.DatabaseComponent;
@@ -30,6 +28,8 @@ import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executor;
import java.util.logging.Logger;
import javax.inject.Inject;
import static java.util.logging.Level.WARNING;
import static org.briarproject.api.sync.SyncConstants.MESSAGE_HEADER_LENGTH;

View File

@@ -1,8 +1,7 @@
package org.briarproject.transport;
import com.google.inject.AbstractModule;
import com.google.inject.Provides;
import org.briarproject.api.crypto.StreamDecrypterFactory;
import org.briarproject.api.crypto.StreamEncrypterFactory;
import org.briarproject.api.event.EventBus;
import org.briarproject.api.lifecycle.LifecycleManager;
import org.briarproject.api.transport.KeyManager;
@@ -11,15 +10,26 @@ import org.briarproject.api.transport.StreamWriterFactory;
import javax.inject.Singleton;
public class TransportModule extends AbstractModule {
import dagger.Module;
import dagger.Provides;
@Override
protected void configure() {
bind(StreamReaderFactory.class).to(StreamReaderFactoryImpl.class);
bind(StreamWriterFactory.class).to(StreamWriterFactoryImpl.class);
@Module
public class TransportModule {
@Provides
StreamReaderFactory provideStreamReaderFactory(
StreamDecrypterFactory streamDecrypterFactory) {
return new StreamReaderFactoryImpl(streamDecrypterFactory);
}
@Provides @Singleton
@Provides
StreamWriterFactory provideStreamWriterFactory(
StreamEncrypterFactory streamEncrypterFactory) {
return new StreamWriterFactoryImpl(streamEncrypterFactory);
}
@Provides
@Singleton
KeyManager getKeyManager(LifecycleManager lifecycleManager,
EventBus eventBus, KeyManagerImpl keyManager) {
lifecycleManager.register(keyManager);