Facades for private messaging. #173

This commit is contained in:
akwizgran
2015-12-17 15:29:20 +00:00
parent 4450ab171a
commit 87689855da
10 changed files with 144 additions and 62 deletions

View File

@@ -142,9 +142,9 @@ EventListener {
try {
ContactId id = c.getId();
GroupId inbox =
messagingManager.getInboxGroupId(id);
messagingManager.getConversationId(id);
Collection<MessageHeader> headers =
messagingManager.getInboxMessageHeaders(id);
messagingManager.getMessageHeaders(id);
displayContact(c, inbox, headers);
} catch (NoSuchContactException e) {
// Continue
@@ -294,7 +294,7 @@ EventListener {
try {
long now = System.currentTimeMillis();
Collection<MessageHeader> headers =
messagingManager.getInboxMessageHeaders(c);
messagingManager.getMessageHeaders(c);
long duration = System.currentTimeMillis() - now;
if (LOG.isLoggable(INFO))
LOG.info("Partial load took " + duration + " ms");

View File

@@ -39,11 +39,11 @@ import org.briarproject.api.event.MessagesAckedEvent;
import org.briarproject.api.event.MessagesSentEvent;
import org.briarproject.api.identity.AuthorId;
import org.briarproject.api.messaging.MessagingManager;
import org.briarproject.api.messaging.PrivateConversation;
import org.briarproject.api.messaging.PrivateMessageFactory;
import org.briarproject.api.plugins.ConnectionRegistry;
import org.briarproject.api.sync.Group;
import org.briarproject.api.sync.GroupId;
import org.briarproject.api.sync.Message;
import org.briarproject.api.sync.MessageFactory;
import org.briarproject.api.sync.MessageHeader;
import org.briarproject.api.sync.MessageHeader.State;
import org.briarproject.api.sync.MessageId;
@@ -93,11 +93,11 @@ implements EventListener, OnClickListener, OnItemClickListener {
@Inject private volatile ContactManager contactManager;
@Inject private volatile MessagingManager messagingManager;
@Inject private volatile EventBus eventBus;
@Inject private volatile MessageFactory messageFactory;
@Inject private volatile PrivateMessageFactory privateMessageFactory;
private volatile ContactId contactId = null;
private volatile String contactName = null;
private volatile GroupId groupId = null;
private volatile Group group = null;
private volatile PrivateConversation conversation = null;
private volatile AuthorId localAuthorId = null;
private volatile boolean connected;
@@ -147,7 +147,7 @@ implements EventListener, OnClickListener, OnItemClickListener {
content = (EditText) findViewById(R.id.contentView);
sendButton = (ImageButton) findViewById(R.id.sendButton);
sendButton.setEnabled(false); // Enabled after loading the group
sendButton.setEnabled(false); // Enabled after loading the conversation
sendButton.setOnClickListener(this);
}
@@ -167,12 +167,12 @@ implements EventListener, OnClickListener, OnItemClickListener {
Contact contact = contactManager.getContact(contactId);
contactName = contact.getAuthor().getName();
localAuthorId = contact.getLocalAuthorId();
groupId = messagingManager.getInboxGroupId(contactId);
group = messagingManager.getGroup(groupId);
groupId = messagingManager.getConversationId(contactId);
conversation = messagingManager.getConversation(groupId);
connected = connectionRegistry.isConnected(contactId);
long duration = System.currentTimeMillis() - now;
if (LOG.isLoggable(INFO)) {
LOG.info("Loading contact and group took "
LOG.info("Loading contact and conversation took "
+ duration + " ms");
}
displayContactDetails();
@@ -210,7 +210,7 @@ implements EventListener, OnClickListener, OnItemClickListener {
try {
long now = System.currentTimeMillis();
Collection<MessageHeader> headers =
messagingManager.getInboxMessageHeaders(contactId);
messagingManager.getMessageHeaders(contactId);
long duration = System.currentTimeMillis() - now;
if (LOG.isLoggable(INFO))
LOG.info("Loading headers took " + duration + " ms");
@@ -424,8 +424,8 @@ implements EventListener, OnClickListener, OnItemClickListener {
cryptoExecutor.execute(new Runnable() {
public void run() {
try {
Message m = messageFactory.createAnonymousMessage(null,
group, "text/plain", timestamp, body);
Message m = privateMessageFactory.createPrivateMessage(null,
conversation, "text/plain", timestamp, body);
storeMessage(m);
} catch (GeneralSecurityException e) {
throw new RuntimeException(e);

View File

@@ -24,10 +24,10 @@ import org.briarproject.api.identity.AuthorId;
import org.briarproject.api.identity.IdentityManager;
import org.briarproject.api.identity.LocalAuthor;
import org.briarproject.api.messaging.MessagingManager;
import org.briarproject.api.sync.Group;
import org.briarproject.api.messaging.PrivateConversation;
import org.briarproject.api.messaging.PrivateMessageFactory;
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.util.StringUtils;
@@ -65,21 +65,20 @@ implements OnClickListener {
// Fields that are accessed from background threads must be volatile
@Inject private volatile IdentityManager identityManager;
@Inject private volatile MessagingManager messagingManager;
@Inject private volatile MessageFactory messageFactory;
private volatile String contactName = null;
@Inject private volatile PrivateMessageFactory privateMessageFactory;
private volatile GroupId groupId = null;
private volatile AuthorId localAuthorId = null;
private volatile MessageId parentId = null;
private volatile long minTimestamp = -1;
private volatile LocalAuthor localAuthor = null;
private volatile Group group = null;
private volatile PrivateConversation conversation = null;
@Override
public void onCreate(Bundle state) {
super.onCreate(state);
Intent i = getIntent();
contactName = i.getStringExtra("briar.CONTACT_NAME");
String contactName = i.getStringExtra("briar.CONTACT_NAME");
if (contactName == null) throw new IllegalStateException();
setTitle(contactName);
byte[] b = i.getByteArrayExtra("briar.GROUP_ID");
@@ -118,7 +117,7 @@ implements OnClickListener {
sendButton.setId(2);
sendButton.setBackgroundResource(0);
sendButton.setImageResource(R.drawable.social_send_now);
sendButton.setEnabled(false); // Enabled after loading the group
sendButton.setEnabled(false); // Enabled after loading the conversation
sendButton.setOnClickListener(this);
RelativeLayout.LayoutParams right = CommonLayoutParams.relative();
right.addRule(ALIGN_PARENT_RIGHT);
@@ -140,16 +139,17 @@ implements OnClickListener {
@Override
public void onResume() {
super.onResume();
if (localAuthor == null || group == null) loadAuthorAndGroup();
if (localAuthor == null || conversation == null)
loadAuthorAndConversation();
}
private void loadAuthorAndGroup() {
private void loadAuthorAndConversation() {
runOnDbThread(new Runnable() {
public void run() {
try {
long now = System.currentTimeMillis();
localAuthor = identityManager.getLocalAuthor(localAuthorId);
group = messagingManager.getGroup(groupId);
conversation = messagingManager.getConversation(groupId);
long duration = System.currentTimeMillis() - now;
if (LOG.isLoggable(INFO))
LOG.info("Load took " + duration + " ms");
@@ -192,8 +192,9 @@ implements OnClickListener {
long timestamp = System.currentTimeMillis();
timestamp = Math.max(timestamp, minTimestamp);
try {
Message m = messageFactory.createAnonymousMessage(parentId,
group, "text/plain", timestamp, body);
Message m = privateMessageFactory.createPrivateMessage(
parentId, conversation, "text/plain", timestamp,
body);
storeMessage(m);
} catch (GeneralSecurityException e) {
throw new RuntimeException(e);

View File

@@ -2,7 +2,6 @@ package org.briarproject.api.messaging;
import org.briarproject.api.contact.ContactId;
import org.briarproject.api.db.DbException;
import org.briarproject.api.sync.Group;
import org.briarproject.api.sync.GroupId;
import org.briarproject.api.sync.Message;
import org.briarproject.api.sync.MessageHeader;
@@ -12,43 +11,35 @@ import java.util.Collection;
public interface MessagingManager {
/**
* Subscribes to a group, or returns false if the user already has the
* maximum number of public subscriptions.
*/
boolean addGroup(Group g) throws DbException;
/** Stores a local message. */
/** Stores a local private message. */
void addLocalMessage(Message m) throws DbException;
/** Returns the group with the given ID, if the user subscribes to it. */
Group getGroup(GroupId g) throws DbException;
/** Returns the private conversation with the given ID. */
PrivateConversation getConversation(GroupId g) throws DbException;
/**
* Returns the ID of the inbox group for the given contact, or null if no
* inbox group has been set.
* Returns the ID of the private conversation with the given contact, or
* null if no private conversation ID has been set.
*/
GroupId getInboxGroupId(ContactId c) throws DbException;
GroupId getConversationId(ContactId c) throws DbException;
/**
* Returns the headers of all messages in the inbox group for the given
* contact, or null if no inbox group has been set.
* Returns the headers of all messages in the private conversation with the
* given contact, or null if no private conversation ID has been set.
*/
Collection<MessageHeader> getInboxMessageHeaders(ContactId c)
Collection<MessageHeader> getMessageHeaders(ContactId c)
throws DbException;
/** Returns the body of the message with the given ID. */
/** Returns the body of the private message with the given ID. */
byte[] getMessageBody(MessageId m) throws DbException;
/**
* Makes a group visible to the given contact, adds it to the contact's
* subscriptions, and sets it as the inbox group for the contact.
* Makes a private conversation visible to the given contact, adds it to
* the contact's subscriptions, and sets it as the private conversation for
* the contact.
*/
void setInboxGroup(ContactId c, Group g) throws DbException;
void setConversation(ContactId c, PrivateConversation p) throws DbException;
/**
* Marks a message as read or unread.
*/
/** Marks a private message as read or unread. */
void setReadFlag(MessageId m, boolean read) throws DbException;
}

View File

@@ -0,0 +1,8 @@
package org.briarproject.api.messaging;
import org.briarproject.api.sync.GroupId;
public interface PrivateConversation {
GroupId getId();
}

View File

@@ -0,0 +1,15 @@
package org.briarproject.api.messaging;
import org.briarproject.api.sync.Message;
import org.briarproject.api.sync.MessageId;
import java.io.IOException;
import java.security.GeneralSecurityException;
public interface PrivateMessageFactory {
Message createPrivateMessage(MessageId parent,
PrivateConversation conversation, String contentType,
long timestamp, byte[] body) throws IOException,
GeneralSecurityException;
}

View File

@@ -6,7 +6,7 @@ import org.briarproject.api.contact.ContactId;
import org.briarproject.api.db.DatabaseComponent;
import org.briarproject.api.db.DbException;
import org.briarproject.api.messaging.MessagingManager;
import org.briarproject.api.sync.Group;
import org.briarproject.api.messaging.PrivateConversation;
import org.briarproject.api.sync.GroupId;
import org.briarproject.api.sync.Message;
import org.briarproject.api.sync.MessageHeader;
@@ -14,6 +14,7 @@ import org.briarproject.api.sync.MessageId;
import java.util.Collection;
// Temporary facade during sync protocol refactoring
class MessagingManagerImpl implements MessagingManager {
private final DatabaseComponent db;
@@ -23,28 +24,23 @@ class MessagingManagerImpl implements MessagingManager {
this.db = db;
}
@Override
public boolean addGroup(Group g) throws DbException {
return db.addGroup(g);
}
@Override
public void addLocalMessage(Message m) throws DbException {
db.addLocalMessage(m);
}
@Override
public Group getGroup(GroupId g) throws DbException {
return db.getGroup(g);
public PrivateConversation getConversation(GroupId g) throws DbException {
return new PrivateConversationImpl(db.getGroup(g));
}
@Override
public GroupId getInboxGroupId(ContactId c) throws DbException {
public GroupId getConversationId(ContactId c) throws DbException {
return db.getInboxGroupId(c);
}
@Override
public Collection<MessageHeader> getInboxMessageHeaders(ContactId c)
public Collection<MessageHeader> getMessageHeaders(ContactId c)
throws DbException {
return db.getInboxMessageHeaders(c);
}
@@ -55,8 +51,9 @@ class MessagingManagerImpl implements MessagingManager {
}
@Override
public void setInboxGroup(ContactId c, Group g) throws DbException {
db.setInboxGroup(c, g);
public void setConversation(ContactId c, PrivateConversation p)
throws DbException {
db.setInboxGroup(c, ((PrivateConversationImpl) p).getGroup());
}
@Override

View File

@@ -3,11 +3,13 @@ package org.briarproject.messaging;
import com.google.inject.AbstractModule;
import org.briarproject.api.messaging.MessagingManager;
import org.briarproject.api.messaging.PrivateMessageFactory;
public class MessagingModule extends AbstractModule {
@Override
protected void configure() {
bind(MessagingManager.class).to(MessagingManagerImpl.class);
bind(PrivateMessageFactory.class).to(PrivateMessageFactoryImpl.class);
}
}

View File

@@ -0,0 +1,35 @@
package org.briarproject.messaging;
import org.briarproject.api.messaging.PrivateConversation;
import org.briarproject.api.sync.Group;
import org.briarproject.api.sync.GroupId;
// Temporary facade during sync protocol refactoring
class PrivateConversationImpl implements PrivateConversation {
private final Group group;
PrivateConversationImpl(Group group) {
this.group = group;
}
@Override
public GroupId getId() {
return group.getId();
}
Group getGroup() {
return group;
}
@Override
public int hashCode() {
return group.hashCode();
}
@Override
public boolean equals(Object o) {
return o instanceof PrivateConversationImpl
&& group.equals(((PrivateConversationImpl) o).group);
}
}

View File

@@ -0,0 +1,33 @@
package org.briarproject.messaging;
import com.google.inject.Inject;
import org.briarproject.api.messaging.PrivateConversation;
import org.briarproject.api.messaging.PrivateMessageFactory;
import org.briarproject.api.sync.Message;
import org.briarproject.api.sync.MessageFactory;
import org.briarproject.api.sync.MessageId;
import java.io.IOException;
import java.security.GeneralSecurityException;
// Temporary facade during sync protocol refactoring
class PrivateMessageFactoryImpl implements PrivateMessageFactory {
private final MessageFactory messageFactory;
@Inject
PrivateMessageFactoryImpl(MessageFactory messageFactory) {
this.messageFactory = messageFactory;
}
@Override
public Message createPrivateMessage(MessageId parent,
PrivateConversation conversation, String contentType,
long timestamp, byte[] body)
throws IOException, GeneralSecurityException {
return messageFactory.createAnonymousMessage(parent,
((PrivateConversationImpl) conversation).getGroup(),
contentType, timestamp, body);
}
}