Contact manager hooks. #209

This commit is contained in:
akwizgran
2016-01-19 19:16:35 +00:00
parent 33ef09a6bf
commit 82cf12040f
29 changed files with 333 additions and 195 deletions

View File

@@ -6,6 +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.data.BdfDictionary;
import org.briarproject.api.data.BdfReader;
import org.briarproject.api.data.BdfReaderFactory;
@@ -42,7 +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 {
class MessagingManagerImpl implements MessagingManager, ContactAddedHook,
ContactRemovedHook {
static final ClientId CLIENT_ID = new ClientId(StringUtils.fromHexString(
"6bcdc006c0910b0f44e40644c3b31f1a"
@@ -72,18 +75,17 @@ class MessagingManagerImpl implements MessagingManager {
}
@Override
public ClientId getClientId() {
return CLIENT_ID;
}
@Override
public void addContact(ContactId c) throws DbException {
// Create the conversation group
Group conversation = createConversationGroup(db.getContact(c));
// Subscribe to the group and share it with the contact
db.addGroup(conversation);
db.addContactGroup(c, conversation);
db.setVisibility(conversation.getId(), Collections.singletonList(c));
public void contactAdded(ContactId c) {
try {
// Create the conversation group
Group g = createConversationGroup(db.getContact(c));
// Subscribe to the group and share it with the contact
db.addGroup(g);
db.addContactGroup(c, g);
db.setVisibility(g.getId(), Collections.singletonList(c));
} catch (DbException e) {
if (LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
}
}
private Group createConversationGroup(Contact c) {
@@ -113,6 +115,20 @@ class MessagingManagerImpl implements MessagingManager {
return out.toByteArray();
}
@Override
public void contactRemoved(ContactId c) {
try {
db.removeGroup(createConversationGroup(db.getContact(c)));
} catch (DbException e) {
if (LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
}
}
@Override
public ClientId getClientId() {
return CLIENT_ID;
}
@Override
public void addLocalMessage(PrivateMessage m) throws DbException {
BdfDictionary d = new BdfDictionary();
@@ -131,7 +147,7 @@ class MessagingManagerImpl implements MessagingManager {
@Override
public ContactId getContactId(GroupId g) throws DbException {
// TODO: Make this more efficient
// TODO: Use metadata to attach the contact ID to the group
for (Contact c : db.getContacts()) {
Group conversation = createConversationGroup(c);
if (conversation.getId().equals(g)) return c.getId();

View File

@@ -3,9 +3,9 @@ package org.briarproject.messaging;
import com.google.inject.AbstractModule;
import com.google.inject.Provides;
import org.briarproject.api.contact.ContactManager;
import org.briarproject.api.data.BdfReaderFactory;
import org.briarproject.api.data.MetadataEncoder;
import org.briarproject.api.lifecycle.LifecycleManager;
import org.briarproject.api.messaging.MessagingManager;
import org.briarproject.api.messaging.PrivateMessageFactory;
import org.briarproject.api.sync.ValidationManager;
@@ -17,18 +17,26 @@ public class MessagingModule extends AbstractModule {
@Override
protected void configure() {
bind(MessagingManager.class).to(MessagingManagerImpl.class);
bind(PrivateMessageFactory.class).to(PrivateMessageFactoryImpl.class);
}
@Provides @Singleton
PrivateMessageValidator getValidator(LifecycleManager lifecycleManager,
ValidationManager validationManager,
PrivateMessageValidator getValidator(ValidationManager validationManager,
MessagingManager messagingManager,
BdfReaderFactory bdfReaderFactory, MetadataEncoder metadataEncoder,
Clock clock) {
PrivateMessageValidator validator = new PrivateMessageValidator(
validationManager, bdfReaderFactory, metadataEncoder, clock);
lifecycleManager.register(validator);
bdfReaderFactory, metadataEncoder, clock);
validationManager.setMessageValidator(messagingManager.getClientId(),
validator);
return validator;
}
@Provides @Singleton
MessagingManager getMessagingManager(ContactManager contactManager,
MessagingManagerImpl messagingManager) {
contactManager.registerContactAddedHook(messagingManager);
contactManager.registerContactRemovedHook(messagingManager);
return messagingManager;
}
}

View File

@@ -7,11 +7,9 @@ import org.briarproject.api.data.BdfReader;
import org.briarproject.api.data.BdfReaderFactory;
import org.briarproject.api.data.MetadataEncoder;
import org.briarproject.api.db.Metadata;
import org.briarproject.api.lifecycle.Service;
import org.briarproject.api.sync.Message;
import org.briarproject.api.sync.MessageId;
import org.briarproject.api.sync.MessageValidator;
import org.briarproject.api.sync.ValidationManager;
import org.briarproject.api.system.Clock;
import java.io.ByteArrayInputStream;
@@ -24,39 +22,24 @@ import static org.briarproject.api.messaging.MessagingConstants.MAX_CONTENT_TYPE
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.transport.TransportConstants.MAX_CLOCK_DIFFERENCE;
import static org.briarproject.messaging.MessagingManagerImpl.CLIENT_ID;
class PrivateMessageValidator implements MessageValidator, Service {
class PrivateMessageValidator implements MessageValidator {
private static final Logger LOG =
Logger.getLogger(PrivateMessageValidator.class.getName());
private final ValidationManager validationManager;
private final BdfReaderFactory bdfReaderFactory;
private final MetadataEncoder metadataEncoder;
private final Clock clock;
@Inject
PrivateMessageValidator(ValidationManager validationManager,
BdfReaderFactory bdfReaderFactory, MetadataEncoder metadataEncoder,
Clock clock) {
this.validationManager = validationManager;
PrivateMessageValidator(BdfReaderFactory bdfReaderFactory,
MetadataEncoder metadataEncoder, Clock clock) {
this.bdfReaderFactory = bdfReaderFactory;
this.metadataEncoder = metadataEncoder;
this.clock = clock;
}
@Override
public boolean start() {
validationManager.setMessageValidator(CLIENT_ID, this);
return true;
}
@Override
public boolean stop() {
return true;
}
@Override
public Metadata validateMessage(Message m) {
// Reject the message if it's too far in the future