Addressed issues from code review.

This commit is contained in:
akwizgran
2016-01-20 14:07:24 +00:00
parent c4692a7007
commit 281ca734e3
18 changed files with 115 additions and 127 deletions

View File

@@ -1,37 +1,18 @@
package org.briarproject.api.contact; package org.briarproject.api.contact;
import org.briarproject.api.db.StorageStatus;
import org.briarproject.api.identity.Author; import org.briarproject.api.identity.Author;
import org.briarproject.api.identity.AuthorId; import org.briarproject.api.identity.AuthorId;
public class Contact { public class Contact {
public enum Status {
ADDING(0), ACTIVE(1), REMOVING(2);
private final int value;
Status(int value) {
this.value = value;
}
public int getValue() {
return value;
}
public static Status fromValue(int value) {
for (Status s : values()) if (s.value == value) return s;
throw new IllegalArgumentException();
}
}
private final ContactId id; private final ContactId id;
private final Author author; private final Author author;
private final AuthorId localAuthorId; private final AuthorId localAuthorId;
private final Status status; private final StorageStatus status;
public Contact(ContactId id, Author author, AuthorId localAuthorId, public Contact(ContactId id, Author author, AuthorId localAuthorId,
Status status) { StorageStatus status) {
this.id = id; this.id = id;
this.author = author; this.author = author;
this.localAuthorId = localAuthorId; this.localAuthorId = localAuthorId;
@@ -50,7 +31,7 @@ public class Contact {
return localAuthorId; return localAuthorId;
} }
public Status getStatus() { public StorageStatus getStatus() {
return status; return status;
} }

View File

@@ -9,10 +9,10 @@ import java.util.Collection;
public interface ContactManager { public interface ContactManager {
/** Registers a hook to be called whenever a contact is added. */ /** Registers a hook to be called whenever a contact is added. */
void registerContactAddedHook(ContactAddedHook hook); void registerAddContactHook(AddContactHook hook);
/** Registers a hook to be called whenever a contact is removed. */ /** Registers a hook to be called whenever a contact is removed. */
void registerContactRemovedHook(ContactRemovedHook hook); void registerRemoveContactHook(RemoveContactHook hook);
/** /**
* Stores a contact associated with the given local and remote pseudonyms, * Stores a contact associated with the given local and remote pseudonyms,
@@ -29,11 +29,11 @@ public interface ContactManager {
/** Removes a contact and all associated state. */ /** Removes a contact and all associated state. */
void removeContact(ContactId c) throws DbException; void removeContact(ContactId c) throws DbException;
interface ContactAddedHook { interface AddContactHook {
void contactAdded(ContactId c); void addingContact(ContactId c);
} }
interface ContactRemovedHook { interface RemoveContactHook {
void contactRemoved(ContactId c); void removingContact(ContactId c);
} }
} }

View File

@@ -301,10 +301,10 @@ public interface DatabaseComponent {
void removeTransport(TransportId t) throws DbException; void removeTransport(TransportId t) throws DbException;
/** Sets the status of the given contact. */ /** Sets the status of the given contact. */
void setContactStatus(ContactId c, Contact.Status s) throws DbException; void setContactStatus(ContactId c, StorageStatus s) throws DbException;
/** Sets the status of the given local pseudonym. */ /** Sets the status of the given local pseudonym. */
void setLocalAuthorStatus(AuthorId a, LocalAuthor.Status s) void setLocalAuthorStatus(AuthorId a, StorageStatus s)
throws DbException; throws DbException;
/** Marks the given message as valid or invalid. */ /** Marks the given message as valid or invalid. */

View File

@@ -0,0 +1,21 @@
package org.briarproject.api.db;
public enum StorageStatus {
ADDING(0), ACTIVE(1), REMOVING(2);
private final int value;
StorageStatus(int value) {
this.value = value;
}
public int getValue() {
return value;
}
public static StorageStatus fromValue(int value) {
for (StorageStatus s : values()) if (s.value == value) return s;
throw new IllegalArgumentException();
}
}

View File

@@ -7,10 +7,10 @@ import java.util.Collection;
public interface IdentityManager { public interface IdentityManager {
/** Registers a hook to be called whenever a local pseudonym is added. */ /** Registers a hook to be called whenever a local pseudonym is added. */
void registerIdentityAddedHook(IdentityAddedHook hook); void registerAddIdentityHook(AddIdentityHook hook);
/** Registers a hook to be called whenever a local pseudonym is removed. */ /** Registers a hook to be called whenever a local pseudonym is removed. */
void registerIdentityRemovedHook(IdentityRemovedHook hook); void registerRemoveIdentityHook(RemoveIdentityHook hook);
/** Stores a local pseudonym. */ /** Stores a local pseudonym. */
void addLocalAuthor(LocalAuthor a) throws DbException; void addLocalAuthor(LocalAuthor a) throws DbException;
@@ -24,11 +24,11 @@ public interface IdentityManager {
/** Removes a local pseudonym and all associated state. */ /** Removes a local pseudonym and all associated state. */
void removeLocalAuthor(AuthorId a) throws DbException; void removeLocalAuthor(AuthorId a) throws DbException;
interface IdentityAddedHook { interface AddIdentityHook {
void identityAdded(AuthorId a); void addingIdentity(AuthorId a);
} }
interface IdentityRemovedHook { interface RemoveIdentityHook {
void identityRemoved(AuthorId a); void removingIdentity(AuthorId a);
} }
} }

View File

@@ -1,34 +1,16 @@
package org.briarproject.api.identity; package org.briarproject.api.identity;
import org.briarproject.api.db.StorageStatus;
/** A pseudonym for the local user. */ /** A pseudonym for the local user. */
public class LocalAuthor extends Author { public class LocalAuthor extends Author {
public enum Status {
ADDING(0), ACTIVE(1), REMOVING(2);
private final int value;
Status(int value) {
this.value = value;
}
public int getValue() {
return value;
}
public static Status fromValue(int value) {
for (Status s : values()) if (s.value == value) return s;
throw new IllegalArgumentException();
}
}
private final byte[] privateKey; private final byte[] privateKey;
private final long created; private final long created;
private final Status status; private final StorageStatus status;
public LocalAuthor(AuthorId id, String name, byte[] publicKey, public LocalAuthor(AuthorId id, String name, byte[] publicKey,
byte[] privateKey, long created, Status status) { byte[] privateKey, long created, StorageStatus status) {
super(id, name, publicKey); super(id, name, publicKey);
this.privateKey = privateKey; this.privateKey = privateKey;
this.created = created; this.created = created;
@@ -49,7 +31,7 @@ public class LocalAuthor extends Author {
} }
/** Returns the status of the pseudonym. */ /** Returns the status of the pseudonym. */
public Status getStatus() { public StorageStatus getStatus() {
return status; return status;
} }
} }

View File

@@ -13,7 +13,7 @@ import org.briarproject.api.event.ContactRemovedEvent;
import org.briarproject.api.event.EventBus; import org.briarproject.api.event.EventBus;
import org.briarproject.api.identity.Author; import org.briarproject.api.identity.Author;
import org.briarproject.api.identity.AuthorId; 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 org.briarproject.api.lifecycle.Service;
import java.util.ArrayList; import java.util.ArrayList;
@@ -24,27 +24,27 @@ import java.util.concurrent.CopyOnWriteArrayList;
import java.util.logging.Logger; import java.util.logging.Logger;
import static java.util.logging.Level.WARNING; import static java.util.logging.Level.WARNING;
import static org.briarproject.api.contact.Contact.Status.ACTIVE; import static org.briarproject.api.db.StorageStatus.ACTIVE;
import static org.briarproject.api.contact.Contact.Status.ADDING; import static org.briarproject.api.db.StorageStatus.ADDING;
import static org.briarproject.api.contact.Contact.Status.REMOVING; import static org.briarproject.api.db.StorageStatus.REMOVING;
class ContactManagerImpl implements ContactManager, Service, class ContactManagerImpl implements ContactManager, Service,
IdentityRemovedHook { RemoveIdentityHook {
private static final Logger LOG = private static final Logger LOG =
Logger.getLogger(ContactManagerImpl.class.getName()); Logger.getLogger(ContactManagerImpl.class.getName());
private final DatabaseComponent db; private final DatabaseComponent db;
private final EventBus eventBus; private final EventBus eventBus;
private final List<ContactAddedHook> addHooks; private final List<AddContactHook> addHooks;
private final List<ContactRemovedHook> removeHooks; private final List<RemoveContactHook> removeHooks;
@Inject @Inject
ContactManagerImpl(DatabaseComponent db, EventBus eventBus) { ContactManagerImpl(DatabaseComponent db, EventBus eventBus) {
this.db = db; this.db = db;
this.eventBus = eventBus; this.eventBus = eventBus;
addHooks = new CopyOnWriteArrayList<ContactAddedHook>(); addHooks = new CopyOnWriteArrayList<AddContactHook>();
removeHooks = new CopyOnWriteArrayList<ContactRemovedHook>(); removeHooks = new CopyOnWriteArrayList<RemoveContactHook>();
} }
@Override @Override
@@ -53,13 +53,13 @@ class ContactManagerImpl implements ContactManager, Service,
try { try {
for (Contact c : db.getContacts()) { for (Contact c : db.getContacts()) {
if (c.getStatus().equals(ADDING)) { if (c.getStatus().equals(ADDING)) {
for (ContactAddedHook hook : addHooks) for (AddContactHook hook : addHooks)
hook.contactAdded(c.getId()); hook.addingContact(c.getId());
db.setContactStatus(c.getId(), ACTIVE); db.setContactStatus(c.getId(), ACTIVE);
eventBus.broadcast(new ContactAddedEvent(c.getId())); eventBus.broadcast(new ContactAddedEvent(c.getId()));
} else if (c.getStatus().equals(REMOVING)) { } else if (c.getStatus().equals(REMOVING)) {
for (ContactRemovedHook hook : removeHooks) for (RemoveContactHook hook : removeHooks)
hook.contactRemoved(c.getId()); hook.removingContact(c.getId());
db.removeContact(c.getId()); db.removeContact(c.getId());
eventBus.broadcast(new ContactRemovedEvent(c.getId())); eventBus.broadcast(new ContactRemovedEvent(c.getId()));
} }
@@ -77,12 +77,12 @@ class ContactManagerImpl implements ContactManager, Service,
} }
@Override @Override
public void registerContactAddedHook(ContactAddedHook hook) { public void registerAddContactHook(AddContactHook hook) {
addHooks.add(hook); addHooks.add(hook);
} }
@Override @Override
public void registerContactRemovedHook(ContactRemovedHook hook) { public void registerRemoveContactHook(RemoveContactHook hook) {
removeHooks.add(hook); removeHooks.add(hook);
} }
@@ -90,7 +90,7 @@ class ContactManagerImpl implements ContactManager, Service,
public ContactId addContact(Author remote, AuthorId local) public ContactId addContact(Author remote, AuthorId local)
throws DbException { throws DbException {
ContactId c = db.addContact(remote, local); ContactId c = db.addContact(remote, local);
for (ContactAddedHook hook : addHooks) hook.contactAdded(c); for (AddContactHook hook : addHooks) hook.addingContact(c);
db.setContactStatus(c, ACTIVE); db.setContactStatus(c, ACTIVE);
eventBus.broadcast(new ContactAddedEvent(c)); eventBus.broadcast(new ContactAddedEvent(c));
return c; return c;
@@ -116,13 +116,13 @@ class ContactManagerImpl implements ContactManager, Service,
@Override @Override
public void removeContact(ContactId c) throws DbException { public void removeContact(ContactId c) throws DbException {
db.setContactStatus(c, REMOVING); db.setContactStatus(c, REMOVING);
for (ContactRemovedHook hook : removeHooks) hook.contactRemoved(c); for (RemoveContactHook hook : removeHooks) hook.removingContact(c);
db.removeContact(c); db.removeContact(c);
eventBus.broadcast(new ContactRemovedEvent(c)); eventBus.broadcast(new ContactRemovedEvent(c));
} }
@Override @Override
public void identityRemoved(AuthorId a) { public void removingIdentity(AuthorId a) {
// Remove any contacts of the local pseudonym that's being removed // Remove any contacts of the local pseudonym that's being removed
try { try {
for (ContactId c : db.getContacts(a)) removeContact(c); for (ContactId c : db.getContacts(a)) removeContact(c);

View File

@@ -19,7 +19,7 @@ public class ContactModule extends AbstractModule {
IdentityManager identityManager, IdentityManager identityManager,
ContactManagerImpl contactManager) { ContactManagerImpl contactManager) {
lifecycleManager.register(contactManager); lifecycleManager.register(contactManager);
identityManager.registerIdentityRemovedHook(contactManager); identityManager.registerRemoveIdentityHook(contactManager);
return contactManager; return contactManager;
} }
} }

View File

@@ -7,6 +7,7 @@ import org.briarproject.api.contact.Contact;
import org.briarproject.api.contact.ContactId; import org.briarproject.api.contact.ContactId;
import org.briarproject.api.db.DbException; import org.briarproject.api.db.DbException;
import org.briarproject.api.db.Metadata; import org.briarproject.api.db.Metadata;
import org.briarproject.api.db.StorageStatus;
import org.briarproject.api.identity.Author; import org.briarproject.api.identity.Author;
import org.briarproject.api.identity.AuthorId; import org.briarproject.api.identity.AuthorId;
import org.briarproject.api.identity.LocalAuthor; import org.briarproject.api.identity.LocalAuthor;
@@ -636,7 +637,7 @@ interface Database<T> {
* <p> * <p>
* Locking: write. * Locking: write.
*/ */
void setContactStatus(T txn, ContactId c, Contact.Status s) void setContactStatus(T txn, ContactId c, StorageStatus s)
throws DbException; throws DbException;
/** /**
@@ -644,7 +645,7 @@ interface Database<T> {
* <p> * <p>
* Locking: write. * Locking: write.
*/ */
void setLocalAuthorStatus(T txn, AuthorId a, LocalAuthor.Status s) void setLocalAuthorStatus(T txn, AuthorId a, StorageStatus s)
throws DbException; throws DbException;
/** /**

View File

@@ -16,6 +16,7 @@ import org.briarproject.api.db.NoSuchLocalAuthorException;
import org.briarproject.api.db.NoSuchMessageException; import org.briarproject.api.db.NoSuchMessageException;
import org.briarproject.api.db.NoSuchSubscriptionException; import org.briarproject.api.db.NoSuchSubscriptionException;
import org.briarproject.api.db.NoSuchTransportException; import org.briarproject.api.db.NoSuchTransportException;
import org.briarproject.api.db.StorageStatus;
import org.briarproject.api.event.EventBus; import org.briarproject.api.event.EventBus;
import org.briarproject.api.event.LocalSubscriptionsUpdatedEvent; import org.briarproject.api.event.LocalSubscriptionsUpdatedEvent;
import org.briarproject.api.event.LocalTransportsUpdatedEvent; import org.briarproject.api.event.LocalTransportsUpdatedEvent;
@@ -1293,7 +1294,7 @@ class DatabaseComponentImpl<T> implements DatabaseComponent {
eventBus.broadcast(new TransportRemovedEvent(t)); eventBus.broadcast(new TransportRemovedEvent(t));
} }
public void setContactStatus(ContactId c, Contact.Status s) public void setContactStatus(ContactId c, StorageStatus s)
throws DbException { throws DbException {
lock.writeLock().lock(); lock.writeLock().lock();
try { 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 { throws DbException {
lock.writeLock().lock(); lock.writeLock().lock();
try { try {

View File

@@ -9,6 +9,7 @@ import org.briarproject.api.crypto.SecretKey;
import org.briarproject.api.db.DbClosedException; import org.briarproject.api.db.DbClosedException;
import org.briarproject.api.db.DbException; import org.briarproject.api.db.DbException;
import org.briarproject.api.db.Metadata; import org.briarproject.api.db.Metadata;
import org.briarproject.api.db.StorageStatus;
import org.briarproject.api.identity.Author; import org.briarproject.api.identity.Author;
import org.briarproject.api.identity.AuthorId; import org.briarproject.api.identity.AuthorId;
import org.briarproject.api.identity.LocalAuthor; import org.briarproject.api.identity.LocalAuthor;
@@ -50,8 +51,8 @@ import java.util.concurrent.locks.ReentrantLock;
import java.util.logging.Logger; import java.util.logging.Logger;
import static java.util.logging.Level.WARNING; 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.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.SyncConstants.MAX_SUBSCRIPTIONS;
import static org.briarproject.api.sync.ValidationManager.Status.INVALID; import static org.briarproject.api.sync.ValidationManager.Status.INVALID;
import static org.briarproject.api.sync.ValidationManager.Status.UNKNOWN; import static org.briarproject.api.sync.ValidationManager.Status.UNKNOWN;
@@ -1208,7 +1209,7 @@ abstract class JdbcDatabase implements Database<Connection> {
String name = rs.getString(2); String name = rs.getString(2);
byte[] publicKey = rs.getBytes(3); byte[] publicKey = rs.getBytes(3);
AuthorId localAuthorId = new AuthorId(rs.getBytes(4)); 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(); rs.close();
ps.close(); ps.close();
Author author = new Author(authorId, name, publicKey); Author author = new Author(authorId, name, publicKey);
@@ -1258,7 +1259,7 @@ abstract class JdbcDatabase implements Database<Connection> {
byte[] publicKey = rs.getBytes(4); byte[] publicKey = rs.getBytes(4);
Author author = new Author(authorId, name, publicKey); Author author = new Author(authorId, name, publicKey);
AuthorId localAuthorId = new AuthorId(rs.getBytes(5)); 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, contacts.add(new Contact(contactId, author, localAuthorId,
status)); status));
} }
@@ -1358,8 +1359,7 @@ abstract class JdbcDatabase implements Database<Connection> {
byte[] publicKey = rs.getBytes(2); byte[] publicKey = rs.getBytes(2);
byte[] privateKey = rs.getBytes(3); byte[] privateKey = rs.getBytes(3);
long created = rs.getLong(4); long created = rs.getLong(4);
LocalAuthor.Status status = LocalAuthor.Status.fromValue( StorageStatus status = StorageStatus.fromValue(rs.getInt(5));
rs.getInt(5));
LocalAuthor localAuthor = new LocalAuthor(a, name, publicKey, LocalAuthor localAuthor = new LocalAuthor(a, name, publicKey,
privateKey, created, status); privateKey, created, status);
if (rs.next()) throw new DbStateException(); if (rs.next()) throw new DbStateException();
@@ -1390,8 +1390,7 @@ abstract class JdbcDatabase implements Database<Connection> {
byte[] publicKey = rs.getBytes(3); byte[] publicKey = rs.getBytes(3);
byte[] privateKey = rs.getBytes(4); byte[] privateKey = rs.getBytes(4);
long created = rs.getLong(5); long created = rs.getLong(5);
LocalAuthor.Status status = LocalAuthor.Status.fromValue( StorageStatus status = StorageStatus.fromValue(rs.getInt(6));
rs.getInt(6));
authors.add(new LocalAuthor(authorId, name, publicKey, authors.add(new LocalAuthor(authorId, name, publicKey,
privateKey, created, status)); privateKey, created, status));
} }
@@ -1875,7 +1874,7 @@ abstract class JdbcDatabase implements Database<Connection> {
byte[] publicKey = rs.getBytes(4); byte[] publicKey = rs.getBytes(4);
Author author = new Author(authorId, name, publicKey); Author author = new Author(authorId, name, publicKey);
AuthorId localAuthorId = new AuthorId(rs.getBytes(5)); 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, contacts.add(new Contact(contactId, author, localAuthorId,
status)); 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 { throws DbException {
PreparedStatement ps = null; PreparedStatement ps = null;
try { try {
@@ -2721,7 +2720,7 @@ abstract class JdbcDatabase implements Database<Connection> {
} }
public void setLocalAuthorStatus(Connection txn, AuthorId a, public void setLocalAuthorStatus(Connection txn, AuthorId a,
LocalAuthor.Status s) throws DbException { StorageStatus s) throws DbException {
PreparedStatement ps = null; PreparedStatement ps = null;
try { try {
String sql = "UPDATE localAuthors SET status = ?" String sql = "UPDATE localAuthors SET status = ?"

View File

@@ -21,9 +21,9 @@ import java.util.concurrent.CopyOnWriteArrayList;
import java.util.logging.Logger; import java.util.logging.Logger;
import static java.util.logging.Level.WARNING; import static java.util.logging.Level.WARNING;
import static org.briarproject.api.identity.LocalAuthor.Status.ACTIVE; import static org.briarproject.api.db.StorageStatus.ACTIVE;
import static org.briarproject.api.identity.LocalAuthor.Status.ADDING; import static org.briarproject.api.db.StorageStatus.ADDING;
import static org.briarproject.api.identity.LocalAuthor.Status.REMOVING; import static org.briarproject.api.db.StorageStatus.REMOVING;
class IdentityManagerImpl implements IdentityManager, Service { class IdentityManagerImpl implements IdentityManager, Service {
@@ -32,15 +32,15 @@ class IdentityManagerImpl implements IdentityManager, Service {
private final DatabaseComponent db; private final DatabaseComponent db;
private final EventBus eventBus; private final EventBus eventBus;
private final List<IdentityAddedHook> addHooks; private final List<AddIdentityHook> addHooks;
private final List<IdentityRemovedHook> removeHooks; private final List<RemoveIdentityHook> removeHooks;
@Inject @Inject
IdentityManagerImpl(DatabaseComponent db, EventBus eventBus) { IdentityManagerImpl(DatabaseComponent db, EventBus eventBus) {
this.db = db; this.db = db;
this.eventBus = eventBus; this.eventBus = eventBus;
addHooks = new CopyOnWriteArrayList<IdentityAddedHook>(); addHooks = new CopyOnWriteArrayList<AddIdentityHook>();
removeHooks = new CopyOnWriteArrayList<IdentityRemovedHook>(); removeHooks = new CopyOnWriteArrayList<RemoveIdentityHook>();
} }
@Override @Override
@@ -49,13 +49,13 @@ class IdentityManagerImpl implements IdentityManager, Service {
try { try {
for (LocalAuthor a : db.getLocalAuthors()) { for (LocalAuthor a : db.getLocalAuthors()) {
if (a.getStatus().equals(ADDING)) { if (a.getStatus().equals(ADDING)) {
for (IdentityAddedHook hook : addHooks) for (AddIdentityHook hook : addHooks)
hook.identityAdded(a.getId()); hook.addingIdentity(a.getId());
db.setLocalAuthorStatus(a.getId(), ACTIVE); db.setLocalAuthorStatus(a.getId(), ACTIVE);
eventBus.broadcast(new LocalAuthorAddedEvent(a.getId())); eventBus.broadcast(new LocalAuthorAddedEvent(a.getId()));
} else if (a.getStatus().equals(REMOVING)) { } else if (a.getStatus().equals(REMOVING)) {
for (IdentityRemovedHook hook : removeHooks) for (RemoveIdentityHook hook : removeHooks)
hook.identityRemoved(a.getId()); hook.removingIdentity(a.getId());
db.removeLocalAuthor(a.getId()); db.removeLocalAuthor(a.getId());
eventBus.broadcast(new LocalAuthorRemovedEvent(a.getId())); eventBus.broadcast(new LocalAuthorRemovedEvent(a.getId()));
} }
@@ -73,19 +73,19 @@ class IdentityManagerImpl implements IdentityManager, Service {
} }
@Override @Override
public void registerIdentityAddedHook(IdentityAddedHook hook) { public void registerAddIdentityHook(AddIdentityHook hook) {
addHooks.add(hook); addHooks.add(hook);
} }
@Override @Override
public void registerIdentityRemovedHook(IdentityRemovedHook hook) { public void registerRemoveIdentityHook(RemoveIdentityHook hook) {
removeHooks.add(hook); removeHooks.add(hook);
} }
@Override @Override
public void addLocalAuthor(LocalAuthor a) throws DbException { public void addLocalAuthor(LocalAuthor a) throws DbException {
db.addLocalAuthor(a); db.addLocalAuthor(a);
for (IdentityAddedHook hook : addHooks) hook.identityAdded(a.getId()); for (AddIdentityHook hook : addHooks) hook.addingIdentity(a.getId());
db.setLocalAuthorStatus(a.getId(), ACTIVE); db.setLocalAuthorStatus(a.getId(), ACTIVE);
eventBus.broadcast(new LocalAuthorAddedEvent(a.getId())); eventBus.broadcast(new LocalAuthorAddedEvent(a.getId()));
} }
@@ -110,7 +110,7 @@ class IdentityManagerImpl implements IdentityManager, Service {
@Override @Override
public void removeLocalAuthor(AuthorId a) throws DbException { public void removeLocalAuthor(AuthorId a) throws DbException {
db.setLocalAuthorStatus(a, REMOVING); db.setLocalAuthorStatus(a, REMOVING);
for (IdentityRemovedHook hook : removeHooks) hook.identityRemoved(a); for (RemoveIdentityHook hook : removeHooks) hook.removingIdentity(a);
db.removeLocalAuthor(a); db.removeLocalAuthor(a);
eventBus.broadcast(new LocalAuthorRemovedEvent(a)); eventBus.broadcast(new LocalAuthorRemovedEvent(a));
} }

View File

@@ -6,8 +6,8 @@ import org.briarproject.api.FormatException;
import org.briarproject.api.UniqueId; import org.briarproject.api.UniqueId;
import org.briarproject.api.contact.Contact; import org.briarproject.api.contact.Contact;
import org.briarproject.api.contact.ContactId; import org.briarproject.api.contact.ContactId;
import org.briarproject.api.contact.ContactManager.ContactAddedHook; import org.briarproject.api.contact.ContactManager.AddContactHook;
import org.briarproject.api.contact.ContactManager.ContactRemovedHook; import org.briarproject.api.contact.ContactManager.RemoveContactHook;
import org.briarproject.api.data.BdfDictionary; import org.briarproject.api.data.BdfDictionary;
import org.briarproject.api.data.BdfReader; import org.briarproject.api.data.BdfReader;
import org.briarproject.api.data.BdfReaderFactory; 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.messaging.MessagingConstants.MAX_PRIVATE_MESSAGE_BODY_LENGTH;
import static org.briarproject.api.sync.SyncConstants.MESSAGE_HEADER_LENGTH; import static org.briarproject.api.sync.SyncConstants.MESSAGE_HEADER_LENGTH;
class MessagingManagerImpl implements MessagingManager, ContactAddedHook, class MessagingManagerImpl implements MessagingManager, AddContactHook,
ContactRemovedHook { RemoveContactHook {
static final ClientId CLIENT_ID = new ClientId(StringUtils.fromHexString( static final ClientId CLIENT_ID = new ClientId(StringUtils.fromHexString(
"6bcdc006c0910b0f44e40644c3b31f1a" "6bcdc006c0910b0f44e40644c3b31f1a"
@@ -75,10 +75,10 @@ class MessagingManagerImpl implements MessagingManager, ContactAddedHook,
} }
@Override @Override
public void contactAdded(ContactId c) { public void addingContact(ContactId c) {
try { try {
// Create the conversation group // 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 // Subscribe to the group and share it with the contact
db.addGroup(g); db.addGroup(g);
db.addContactGroup(c, 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 local = c.getLocalAuthorId();
AuthorId remote = c.getAuthor().getId(); AuthorId remote = c.getAuthor().getId();
byte[] descriptor = createGroupDescriptor(local, remote); byte[] descriptor = createGroupDescriptor(local, remote);
@@ -116,9 +116,9 @@ class MessagingManagerImpl implements MessagingManager, ContactAddedHook,
} }
@Override @Override
public void contactRemoved(ContactId c) { public void removingContact(ContactId c) {
try { try {
db.removeGroup(createConversationGroup(db.getContact(c))); db.removeGroup(getConversationGroup(db.getContact(c)));
} catch (DbException e) { } catch (DbException e) {
if (LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), 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 { public ContactId getContactId(GroupId g) throws DbException {
// TODO: Use metadata to attach the contact ID to the group // TODO: Use metadata to attach the contact ID to the group
for (Contact c : db.getContacts()) { for (Contact c : db.getContacts()) {
Group conversation = createConversationGroup(c); Group conversation = getConversationGroup(c);
if (conversation.getId().equals(g)) return c.getId(); if (conversation.getId().equals(g)) return c.getId();
} }
throw new NoSuchContactException(); throw new NoSuchContactException();
@@ -157,7 +157,7 @@ class MessagingManagerImpl implements MessagingManager, ContactAddedHook,
@Override @Override
public GroupId getConversationId(ContactId c) throws DbException { public GroupId getConversationId(ContactId c) throws DbException {
return createConversationGroup(db.getContact(c)).getId(); return getConversationGroup(db.getContact(c)).getId();
} }
@Override @Override

View File

@@ -35,8 +35,8 @@ public class MessagingModule extends AbstractModule {
@Provides @Singleton @Provides @Singleton
MessagingManager getMessagingManager(ContactManager contactManager, MessagingManager getMessagingManager(ContactManager contactManager,
MessagingManagerImpl messagingManager) { MessagingManagerImpl messagingManager) {
contactManager.registerContactAddedHook(messagingManager); contactManager.registerAddContactHook(messagingManager);
contactManager.registerContactRemovedHook(messagingManager); contactManager.registerRemoveContactHook(messagingManager);
return messagingManager; return messagingManager;
} }
} }

View File

@@ -14,7 +14,7 @@ import java.io.IOException;
import javax.inject.Inject; 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 { class AuthorFactoryImpl implements AuthorFactory {

View File

@@ -15,6 +15,7 @@ import org.briarproject.api.db.NoSuchLocalAuthorException;
import org.briarproject.api.db.NoSuchMessageException; import org.briarproject.api.db.NoSuchMessageException;
import org.briarproject.api.db.NoSuchSubscriptionException; import org.briarproject.api.db.NoSuchSubscriptionException;
import org.briarproject.api.db.NoSuchTransportException; import org.briarproject.api.db.NoSuchTransportException;
import org.briarproject.api.db.StorageStatus;
import org.briarproject.api.event.EventBus; import org.briarproject.api.event.EventBus;
import org.briarproject.api.event.LocalSubscriptionsUpdatedEvent; import org.briarproject.api.event.LocalSubscriptionsUpdatedEvent;
import org.briarproject.api.event.LocalTransportsUpdatedEvent; import org.briarproject.api.event.LocalTransportsUpdatedEvent;
@@ -96,7 +97,7 @@ public class DatabaseComponentImplTest extends BriarTestCase {
long timestamp = System.currentTimeMillis(); long timestamp = System.currentTimeMillis();
localAuthor = new LocalAuthor(localAuthorId, "Bob", localAuthor = new LocalAuthor(localAuthorId, "Bob",
new byte[MAX_PUBLIC_KEY_LENGTH], new byte[123], timestamp, new byte[MAX_PUBLIC_KEY_LENGTH], new byte[123], timestamp,
LocalAuthor.Status.ACTIVE); StorageStatus.ACTIVE);
messageId = new MessageId(TestUtils.getRandomId()); messageId = new MessageId(TestUtils.getRandomId());
messageId1 = new MessageId(TestUtils.getRandomId()); messageId1 = new MessageId(TestUtils.getRandomId());
size = 1234; size = 1234;
@@ -110,7 +111,7 @@ public class DatabaseComponentImplTest extends BriarTestCase {
maxLatency = Integer.MAX_VALUE; maxLatency = Integer.MAX_VALUE;
contactId = new ContactId(234); contactId = new ContactId(234);
contact = new Contact(contactId, author, localAuthorId, contact = new Contact(contactId, author, localAuthorId,
Contact.Status.ACTIVE); StorageStatus.ACTIVE);
} }
private <T> DatabaseComponent createDatabaseComponent(Database<T> database, private <T> DatabaseComponent createDatabaseComponent(Database<T> database,

View File

@@ -10,6 +10,7 @@ import org.briarproject.api.contact.ContactId;
import org.briarproject.api.crypto.SecretKey; import org.briarproject.api.crypto.SecretKey;
import org.briarproject.api.db.DbException; import org.briarproject.api.db.DbException;
import org.briarproject.api.db.Metadata; import org.briarproject.api.db.Metadata;
import org.briarproject.api.db.StorageStatus;
import org.briarproject.api.identity.Author; import org.briarproject.api.identity.Author;
import org.briarproject.api.identity.AuthorId; import org.briarproject.api.identity.AuthorId;
import org.briarproject.api.identity.LocalAuthor; import org.briarproject.api.identity.LocalAuthor;
@@ -83,7 +84,7 @@ public class H2DatabaseTest extends BriarTestCase {
timestamp = System.currentTimeMillis(); timestamp = System.currentTimeMillis();
localAuthor = new LocalAuthor(localAuthorId, "Bob", localAuthor = new LocalAuthor(localAuthorId, "Bob",
new byte[MAX_PUBLIC_KEY_LENGTH], new byte[123], timestamp, new byte[MAX_PUBLIC_KEY_LENGTH], new byte[123], timestamp,
LocalAuthor.Status.ACTIVE); StorageStatus.ACTIVE);
messageId = new MessageId(TestUtils.getRandomId()); messageId = new MessageId(TestUtils.getRandomId());
size = 1234; size = 1234;
raw = new byte[size]; raw = new byte[size];

View File

@@ -12,6 +12,7 @@ import org.briarproject.api.contact.ContactId;
import org.briarproject.api.contact.ContactManager; import org.briarproject.api.contact.ContactManager;
import org.briarproject.api.crypto.SecretKey; import org.briarproject.api.crypto.SecretKey;
import org.briarproject.api.db.DatabaseComponent; import org.briarproject.api.db.DatabaseComponent;
import org.briarproject.api.db.StorageStatus;
import org.briarproject.api.event.Event; import org.briarproject.api.event.Event;
import org.briarproject.api.event.EventBus; import org.briarproject.api.event.EventBus;
import org.briarproject.api.event.EventListener; import org.briarproject.api.event.EventListener;
@@ -124,7 +125,7 @@ public class SimplexMessagingIntegrationTest extends BriarTestCase {
// Add an identity for Alice // Add an identity for Alice
LocalAuthor aliceAuthor = new LocalAuthor(aliceId, "Alice", LocalAuthor aliceAuthor = new LocalAuthor(aliceId, "Alice",
new byte[MAX_PUBLIC_KEY_LENGTH], new byte[123], timestamp, new byte[MAX_PUBLIC_KEY_LENGTH], new byte[123], timestamp,
LocalAuthor.Status.ADDING); StorageStatus.ADDING);
identityManager.addLocalAuthor(aliceAuthor); identityManager.addLocalAuthor(aliceAuthor);
// Add Bob as a contact // Add Bob as a contact
Author bobAuthor = new Author(bobId, "Bob", Author bobAuthor = new Author(bobId, "Bob",
@@ -190,7 +191,7 @@ public class SimplexMessagingIntegrationTest extends BriarTestCase {
// Add an identity for Bob // Add an identity for Bob
LocalAuthor bobAuthor = new LocalAuthor(bobId, "Bob", LocalAuthor bobAuthor = new LocalAuthor(bobId, "Bob",
new byte[MAX_PUBLIC_KEY_LENGTH], new byte[123], timestamp, new byte[MAX_PUBLIC_KEY_LENGTH], new byte[123], timestamp,
LocalAuthor.Status.ADDING); StorageStatus.ADDING);
identityManager.addLocalAuthor(bobAuthor); identityManager.addLocalAuthor(bobAuthor);
// Add Alice as a contact // Add Alice as a contact
Author aliceAuthor = new Author(aliceId, "Alice", Author aliceAuthor = new Author(aliceId, "Alice",