mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-14 03:39:05 +01:00
Addressed issues from code review.
This commit is contained in:
@@ -13,7 +13,7 @@ import org.briarproject.api.event.ContactRemovedEvent;
|
||||
import org.briarproject.api.event.EventBus;
|
||||
import org.briarproject.api.identity.Author;
|
||||
import org.briarproject.api.identity.AuthorId;
|
||||
import org.briarproject.api.identity.IdentityManager.IdentityRemovedHook;
|
||||
import org.briarproject.api.identity.IdentityManager.RemoveIdentityHook;
|
||||
import org.briarproject.api.lifecycle.Service;
|
||||
|
||||
import java.util.ArrayList;
|
||||
@@ -24,27 +24,27 @@ import java.util.concurrent.CopyOnWriteArrayList;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import static java.util.logging.Level.WARNING;
|
||||
import static org.briarproject.api.contact.Contact.Status.ACTIVE;
|
||||
import static org.briarproject.api.contact.Contact.Status.ADDING;
|
||||
import static org.briarproject.api.contact.Contact.Status.REMOVING;
|
||||
import static org.briarproject.api.db.StorageStatus.ACTIVE;
|
||||
import static org.briarproject.api.db.StorageStatus.ADDING;
|
||||
import static org.briarproject.api.db.StorageStatus.REMOVING;
|
||||
|
||||
class ContactManagerImpl implements ContactManager, Service,
|
||||
IdentityRemovedHook {
|
||||
RemoveIdentityHook {
|
||||
|
||||
private static final Logger LOG =
|
||||
Logger.getLogger(ContactManagerImpl.class.getName());
|
||||
|
||||
private final DatabaseComponent db;
|
||||
private final EventBus eventBus;
|
||||
private final List<ContactAddedHook> addHooks;
|
||||
private final List<ContactRemovedHook> removeHooks;
|
||||
private final List<AddContactHook> addHooks;
|
||||
private final List<RemoveContactHook> removeHooks;
|
||||
|
||||
@Inject
|
||||
ContactManagerImpl(DatabaseComponent db, EventBus eventBus) {
|
||||
this.db = db;
|
||||
this.eventBus = eventBus;
|
||||
addHooks = new CopyOnWriteArrayList<ContactAddedHook>();
|
||||
removeHooks = new CopyOnWriteArrayList<ContactRemovedHook>();
|
||||
addHooks = new CopyOnWriteArrayList<AddContactHook>();
|
||||
removeHooks = new CopyOnWriteArrayList<RemoveContactHook>();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -53,13 +53,13 @@ class ContactManagerImpl implements ContactManager, Service,
|
||||
try {
|
||||
for (Contact c : db.getContacts()) {
|
||||
if (c.getStatus().equals(ADDING)) {
|
||||
for (ContactAddedHook hook : addHooks)
|
||||
hook.contactAdded(c.getId());
|
||||
for (AddContactHook hook : addHooks)
|
||||
hook.addingContact(c.getId());
|
||||
db.setContactStatus(c.getId(), ACTIVE);
|
||||
eventBus.broadcast(new ContactAddedEvent(c.getId()));
|
||||
} else if (c.getStatus().equals(REMOVING)) {
|
||||
for (ContactRemovedHook hook : removeHooks)
|
||||
hook.contactRemoved(c.getId());
|
||||
for (RemoveContactHook hook : removeHooks)
|
||||
hook.removingContact(c.getId());
|
||||
db.removeContact(c.getId());
|
||||
eventBus.broadcast(new ContactRemovedEvent(c.getId()));
|
||||
}
|
||||
@@ -77,12 +77,12 @@ class ContactManagerImpl implements ContactManager, Service,
|
||||
}
|
||||
|
||||
@Override
|
||||
public void registerContactAddedHook(ContactAddedHook hook) {
|
||||
public void registerAddContactHook(AddContactHook hook) {
|
||||
addHooks.add(hook);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void registerContactRemovedHook(ContactRemovedHook hook) {
|
||||
public void registerRemoveContactHook(RemoveContactHook hook) {
|
||||
removeHooks.add(hook);
|
||||
}
|
||||
|
||||
@@ -90,7 +90,7 @@ class ContactManagerImpl implements ContactManager, Service,
|
||||
public ContactId addContact(Author remote, AuthorId local)
|
||||
throws DbException {
|
||||
ContactId c = db.addContact(remote, local);
|
||||
for (ContactAddedHook hook : addHooks) hook.contactAdded(c);
|
||||
for (AddContactHook hook : addHooks) hook.addingContact(c);
|
||||
db.setContactStatus(c, ACTIVE);
|
||||
eventBus.broadcast(new ContactAddedEvent(c));
|
||||
return c;
|
||||
@@ -116,13 +116,13 @@ class ContactManagerImpl implements ContactManager, Service,
|
||||
@Override
|
||||
public void removeContact(ContactId c) throws DbException {
|
||||
db.setContactStatus(c, REMOVING);
|
||||
for (ContactRemovedHook hook : removeHooks) hook.contactRemoved(c);
|
||||
for (RemoveContactHook hook : removeHooks) hook.removingContact(c);
|
||||
db.removeContact(c);
|
||||
eventBus.broadcast(new ContactRemovedEvent(c));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void identityRemoved(AuthorId a) {
|
||||
public void removingIdentity(AuthorId a) {
|
||||
// Remove any contacts of the local pseudonym that's being removed
|
||||
try {
|
||||
for (ContactId c : db.getContacts(a)) removeContact(c);
|
||||
|
||||
@@ -19,7 +19,7 @@ public class ContactModule extends AbstractModule {
|
||||
IdentityManager identityManager,
|
||||
ContactManagerImpl contactManager) {
|
||||
lifecycleManager.register(contactManager);
|
||||
identityManager.registerIdentityRemovedHook(contactManager);
|
||||
identityManager.registerRemoveIdentityHook(contactManager);
|
||||
return contactManager;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ import org.briarproject.api.contact.Contact;
|
||||
import org.briarproject.api.contact.ContactId;
|
||||
import org.briarproject.api.db.DbException;
|
||||
import org.briarproject.api.db.Metadata;
|
||||
import org.briarproject.api.db.StorageStatus;
|
||||
import org.briarproject.api.identity.Author;
|
||||
import org.briarproject.api.identity.AuthorId;
|
||||
import org.briarproject.api.identity.LocalAuthor;
|
||||
@@ -636,7 +637,7 @@ interface Database<T> {
|
||||
* <p>
|
||||
* Locking: write.
|
||||
*/
|
||||
void setContactStatus(T txn, ContactId c, Contact.Status s)
|
||||
void setContactStatus(T txn, ContactId c, StorageStatus s)
|
||||
throws DbException;
|
||||
|
||||
/**
|
||||
@@ -644,7 +645,7 @@ interface Database<T> {
|
||||
* <p>
|
||||
* Locking: write.
|
||||
*/
|
||||
void setLocalAuthorStatus(T txn, AuthorId a, LocalAuthor.Status s)
|
||||
void setLocalAuthorStatus(T txn, AuthorId a, StorageStatus s)
|
||||
throws DbException;
|
||||
|
||||
/**
|
||||
|
||||
@@ -16,6 +16,7 @@ import org.briarproject.api.db.NoSuchLocalAuthorException;
|
||||
import org.briarproject.api.db.NoSuchMessageException;
|
||||
import org.briarproject.api.db.NoSuchSubscriptionException;
|
||||
import org.briarproject.api.db.NoSuchTransportException;
|
||||
import org.briarproject.api.db.StorageStatus;
|
||||
import org.briarproject.api.event.EventBus;
|
||||
import org.briarproject.api.event.LocalSubscriptionsUpdatedEvent;
|
||||
import org.briarproject.api.event.LocalTransportsUpdatedEvent;
|
||||
@@ -1293,7 +1294,7 @@ class DatabaseComponentImpl<T> implements DatabaseComponent {
|
||||
eventBus.broadcast(new TransportRemovedEvent(t));
|
||||
}
|
||||
|
||||
public void setContactStatus(ContactId c, Contact.Status s)
|
||||
public void setContactStatus(ContactId c, StorageStatus s)
|
||||
throws DbException {
|
||||
lock.writeLock().lock();
|
||||
try {
|
||||
@@ -1312,7 +1313,7 @@ class DatabaseComponentImpl<T> implements DatabaseComponent {
|
||||
}
|
||||
}
|
||||
|
||||
public void setLocalAuthorStatus(AuthorId a, LocalAuthor.Status s)
|
||||
public void setLocalAuthorStatus(AuthorId a, StorageStatus s)
|
||||
throws DbException {
|
||||
lock.writeLock().lock();
|
||||
try {
|
||||
|
||||
@@ -9,6 +9,7 @@ import org.briarproject.api.crypto.SecretKey;
|
||||
import org.briarproject.api.db.DbClosedException;
|
||||
import org.briarproject.api.db.DbException;
|
||||
import org.briarproject.api.db.Metadata;
|
||||
import org.briarproject.api.db.StorageStatus;
|
||||
import org.briarproject.api.identity.Author;
|
||||
import org.briarproject.api.identity.AuthorId;
|
||||
import org.briarproject.api.identity.LocalAuthor;
|
||||
@@ -50,8 +51,8 @@ import java.util.concurrent.locks.ReentrantLock;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import static java.util.logging.Level.WARNING;
|
||||
import static org.briarproject.api.contact.Contact.Status.ADDING;
|
||||
import static org.briarproject.api.db.Metadata.REMOVE;
|
||||
import static org.briarproject.api.db.StorageStatus.ADDING;
|
||||
import static org.briarproject.api.sync.SyncConstants.MAX_SUBSCRIPTIONS;
|
||||
import static org.briarproject.api.sync.ValidationManager.Status.INVALID;
|
||||
import static org.briarproject.api.sync.ValidationManager.Status.UNKNOWN;
|
||||
@@ -1208,7 +1209,7 @@ abstract class JdbcDatabase implements Database<Connection> {
|
||||
String name = rs.getString(2);
|
||||
byte[] publicKey = rs.getBytes(3);
|
||||
AuthorId localAuthorId = new AuthorId(rs.getBytes(4));
|
||||
Contact.Status status = Contact.Status.fromValue(rs.getInt(5));
|
||||
StorageStatus status = StorageStatus.fromValue(rs.getInt(5));
|
||||
rs.close();
|
||||
ps.close();
|
||||
Author author = new Author(authorId, name, publicKey);
|
||||
@@ -1258,7 +1259,7 @@ abstract class JdbcDatabase implements Database<Connection> {
|
||||
byte[] publicKey = rs.getBytes(4);
|
||||
Author author = new Author(authorId, name, publicKey);
|
||||
AuthorId localAuthorId = new AuthorId(rs.getBytes(5));
|
||||
Contact.Status status = Contact.Status.fromValue(rs.getInt(6));
|
||||
StorageStatus status = StorageStatus.fromValue(rs.getInt(6));
|
||||
contacts.add(new Contact(contactId, author, localAuthorId,
|
||||
status));
|
||||
}
|
||||
@@ -1358,8 +1359,7 @@ abstract class JdbcDatabase implements Database<Connection> {
|
||||
byte[] publicKey = rs.getBytes(2);
|
||||
byte[] privateKey = rs.getBytes(3);
|
||||
long created = rs.getLong(4);
|
||||
LocalAuthor.Status status = LocalAuthor.Status.fromValue(
|
||||
rs.getInt(5));
|
||||
StorageStatus status = StorageStatus.fromValue(rs.getInt(5));
|
||||
LocalAuthor localAuthor = new LocalAuthor(a, name, publicKey,
|
||||
privateKey, created, status);
|
||||
if (rs.next()) throw new DbStateException();
|
||||
@@ -1390,8 +1390,7 @@ abstract class JdbcDatabase implements Database<Connection> {
|
||||
byte[] publicKey = rs.getBytes(3);
|
||||
byte[] privateKey = rs.getBytes(4);
|
||||
long created = rs.getLong(5);
|
||||
LocalAuthor.Status status = LocalAuthor.Status.fromValue(
|
||||
rs.getInt(6));
|
||||
StorageStatus status = StorageStatus.fromValue(rs.getInt(6));
|
||||
authors.add(new LocalAuthor(authorId, name, publicKey,
|
||||
privateKey, created, status));
|
||||
}
|
||||
@@ -1875,7 +1874,7 @@ abstract class JdbcDatabase implements Database<Connection> {
|
||||
byte[] publicKey = rs.getBytes(4);
|
||||
Author author = new Author(authorId, name, publicKey);
|
||||
AuthorId localAuthorId = new AuthorId(rs.getBytes(5));
|
||||
Contact.Status status = Contact.Status.fromValue(rs.getInt(6));
|
||||
StorageStatus status = StorageStatus.fromValue(rs.getInt(6));
|
||||
contacts.add(new Contact(contactId, author, localAuthorId,
|
||||
status));
|
||||
}
|
||||
@@ -2703,7 +2702,7 @@ abstract class JdbcDatabase implements Database<Connection> {
|
||||
}
|
||||
}
|
||||
|
||||
public void setContactStatus(Connection txn, ContactId c, Contact.Status s)
|
||||
public void setContactStatus(Connection txn, ContactId c, StorageStatus s)
|
||||
throws DbException {
|
||||
PreparedStatement ps = null;
|
||||
try {
|
||||
@@ -2721,7 +2720,7 @@ abstract class JdbcDatabase implements Database<Connection> {
|
||||
}
|
||||
|
||||
public void setLocalAuthorStatus(Connection txn, AuthorId a,
|
||||
LocalAuthor.Status s) throws DbException {
|
||||
StorageStatus s) throws DbException {
|
||||
PreparedStatement ps = null;
|
||||
try {
|
||||
String sql = "UPDATE localAuthors SET status = ?"
|
||||
|
||||
@@ -21,9 +21,9 @@ import java.util.concurrent.CopyOnWriteArrayList;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import static java.util.logging.Level.WARNING;
|
||||
import static org.briarproject.api.identity.LocalAuthor.Status.ACTIVE;
|
||||
import static org.briarproject.api.identity.LocalAuthor.Status.ADDING;
|
||||
import static org.briarproject.api.identity.LocalAuthor.Status.REMOVING;
|
||||
import static org.briarproject.api.db.StorageStatus.ACTIVE;
|
||||
import static org.briarproject.api.db.StorageStatus.ADDING;
|
||||
import static org.briarproject.api.db.StorageStatus.REMOVING;
|
||||
|
||||
class IdentityManagerImpl implements IdentityManager, Service {
|
||||
|
||||
@@ -32,15 +32,15 @@ class IdentityManagerImpl implements IdentityManager, Service {
|
||||
|
||||
private final DatabaseComponent db;
|
||||
private final EventBus eventBus;
|
||||
private final List<IdentityAddedHook> addHooks;
|
||||
private final List<IdentityRemovedHook> removeHooks;
|
||||
private final List<AddIdentityHook> addHooks;
|
||||
private final List<RemoveIdentityHook> removeHooks;
|
||||
|
||||
@Inject
|
||||
IdentityManagerImpl(DatabaseComponent db, EventBus eventBus) {
|
||||
this.db = db;
|
||||
this.eventBus = eventBus;
|
||||
addHooks = new CopyOnWriteArrayList<IdentityAddedHook>();
|
||||
removeHooks = new CopyOnWriteArrayList<IdentityRemovedHook>();
|
||||
addHooks = new CopyOnWriteArrayList<AddIdentityHook>();
|
||||
removeHooks = new CopyOnWriteArrayList<RemoveIdentityHook>();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -49,13 +49,13 @@ class IdentityManagerImpl implements IdentityManager, Service {
|
||||
try {
|
||||
for (LocalAuthor a : db.getLocalAuthors()) {
|
||||
if (a.getStatus().equals(ADDING)) {
|
||||
for (IdentityAddedHook hook : addHooks)
|
||||
hook.identityAdded(a.getId());
|
||||
for (AddIdentityHook hook : addHooks)
|
||||
hook.addingIdentity(a.getId());
|
||||
db.setLocalAuthorStatus(a.getId(), ACTIVE);
|
||||
eventBus.broadcast(new LocalAuthorAddedEvent(a.getId()));
|
||||
} else if (a.getStatus().equals(REMOVING)) {
|
||||
for (IdentityRemovedHook hook : removeHooks)
|
||||
hook.identityRemoved(a.getId());
|
||||
for (RemoveIdentityHook hook : removeHooks)
|
||||
hook.removingIdentity(a.getId());
|
||||
db.removeLocalAuthor(a.getId());
|
||||
eventBus.broadcast(new LocalAuthorRemovedEvent(a.getId()));
|
||||
}
|
||||
@@ -73,19 +73,19 @@ class IdentityManagerImpl implements IdentityManager, Service {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void registerIdentityAddedHook(IdentityAddedHook hook) {
|
||||
public void registerAddIdentityHook(AddIdentityHook hook) {
|
||||
addHooks.add(hook);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void registerIdentityRemovedHook(IdentityRemovedHook hook) {
|
||||
public void registerRemoveIdentityHook(RemoveIdentityHook hook) {
|
||||
removeHooks.add(hook);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addLocalAuthor(LocalAuthor a) throws DbException {
|
||||
db.addLocalAuthor(a);
|
||||
for (IdentityAddedHook hook : addHooks) hook.identityAdded(a.getId());
|
||||
for (AddIdentityHook hook : addHooks) hook.addingIdentity(a.getId());
|
||||
db.setLocalAuthorStatus(a.getId(), ACTIVE);
|
||||
eventBus.broadcast(new LocalAuthorAddedEvent(a.getId()));
|
||||
}
|
||||
@@ -110,7 +110,7 @@ class IdentityManagerImpl implements IdentityManager, Service {
|
||||
@Override
|
||||
public void removeLocalAuthor(AuthorId a) throws DbException {
|
||||
db.setLocalAuthorStatus(a, REMOVING);
|
||||
for (IdentityRemovedHook hook : removeHooks) hook.identityRemoved(a);
|
||||
for (RemoveIdentityHook hook : removeHooks) hook.removingIdentity(a);
|
||||
db.removeLocalAuthor(a);
|
||||
eventBus.broadcast(new LocalAuthorRemovedEvent(a));
|
||||
}
|
||||
|
||||
@@ -6,8 +6,8 @@ import org.briarproject.api.FormatException;
|
||||
import org.briarproject.api.UniqueId;
|
||||
import org.briarproject.api.contact.Contact;
|
||||
import org.briarproject.api.contact.ContactId;
|
||||
import org.briarproject.api.contact.ContactManager.ContactAddedHook;
|
||||
import org.briarproject.api.contact.ContactManager.ContactRemovedHook;
|
||||
import org.briarproject.api.contact.ContactManager.AddContactHook;
|
||||
import org.briarproject.api.contact.ContactManager.RemoveContactHook;
|
||||
import org.briarproject.api.data.BdfDictionary;
|
||||
import org.briarproject.api.data.BdfReader;
|
||||
import org.briarproject.api.data.BdfReaderFactory;
|
||||
@@ -44,8 +44,8 @@ import static java.util.logging.Level.WARNING;
|
||||
import static org.briarproject.api.messaging.MessagingConstants.MAX_PRIVATE_MESSAGE_BODY_LENGTH;
|
||||
import static org.briarproject.api.sync.SyncConstants.MESSAGE_HEADER_LENGTH;
|
||||
|
||||
class MessagingManagerImpl implements MessagingManager, ContactAddedHook,
|
||||
ContactRemovedHook {
|
||||
class MessagingManagerImpl implements MessagingManager, AddContactHook,
|
||||
RemoveContactHook {
|
||||
|
||||
static final ClientId CLIENT_ID = new ClientId(StringUtils.fromHexString(
|
||||
"6bcdc006c0910b0f44e40644c3b31f1a"
|
||||
@@ -75,10 +75,10 @@ class MessagingManagerImpl implements MessagingManager, ContactAddedHook,
|
||||
}
|
||||
|
||||
@Override
|
||||
public void contactAdded(ContactId c) {
|
||||
public void addingContact(ContactId c) {
|
||||
try {
|
||||
// Create the conversation group
|
||||
Group g = createConversationGroup(db.getContact(c));
|
||||
Group g = getConversationGroup(db.getContact(c));
|
||||
// Subscribe to the group and share it with the contact
|
||||
db.addGroup(g);
|
||||
db.addContactGroup(c, g);
|
||||
@@ -88,7 +88,7 @@ class MessagingManagerImpl implements MessagingManager, ContactAddedHook,
|
||||
}
|
||||
}
|
||||
|
||||
private Group createConversationGroup(Contact c) {
|
||||
private Group getConversationGroup(Contact c) {
|
||||
AuthorId local = c.getLocalAuthorId();
|
||||
AuthorId remote = c.getAuthor().getId();
|
||||
byte[] descriptor = createGroupDescriptor(local, remote);
|
||||
@@ -116,9 +116,9 @@ class MessagingManagerImpl implements MessagingManager, ContactAddedHook,
|
||||
}
|
||||
|
||||
@Override
|
||||
public void contactRemoved(ContactId c) {
|
||||
public void removingContact(ContactId c) {
|
||||
try {
|
||||
db.removeGroup(createConversationGroup(db.getContact(c)));
|
||||
db.removeGroup(getConversationGroup(db.getContact(c)));
|
||||
} catch (DbException e) {
|
||||
if (LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
|
||||
}
|
||||
@@ -149,7 +149,7 @@ class MessagingManagerImpl implements MessagingManager, ContactAddedHook,
|
||||
public ContactId getContactId(GroupId g) throws DbException {
|
||||
// TODO: Use metadata to attach the contact ID to the group
|
||||
for (Contact c : db.getContacts()) {
|
||||
Group conversation = createConversationGroup(c);
|
||||
Group conversation = getConversationGroup(c);
|
||||
if (conversation.getId().equals(g)) return c.getId();
|
||||
}
|
||||
throw new NoSuchContactException();
|
||||
@@ -157,7 +157,7 @@ class MessagingManagerImpl implements MessagingManager, ContactAddedHook,
|
||||
|
||||
@Override
|
||||
public GroupId getConversationId(ContactId c) throws DbException {
|
||||
return createConversationGroup(db.getContact(c)).getId();
|
||||
return getConversationGroup(db.getContact(c)).getId();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -35,8 +35,8 @@ public class MessagingModule extends AbstractModule {
|
||||
@Provides @Singleton
|
||||
MessagingManager getMessagingManager(ContactManager contactManager,
|
||||
MessagingManagerImpl messagingManager) {
|
||||
contactManager.registerContactAddedHook(messagingManager);
|
||||
contactManager.registerContactRemovedHook(messagingManager);
|
||||
contactManager.registerAddContactHook(messagingManager);
|
||||
contactManager.registerRemoveContactHook(messagingManager);
|
||||
return messagingManager;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@ import java.io.IOException;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
import static org.briarproject.api.identity.LocalAuthor.Status.ADDING;
|
||||
import static org.briarproject.api.db.StorageStatus.ADDING;
|
||||
|
||||
class AuthorFactoryImpl implements AuthorFactory {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user