Address review comments

This commit is contained in:
Torsten Grote
2016-10-06 12:03:04 -03:00
parent 784561144a
commit 7f2db71160
41 changed files with 171 additions and 159 deletions

View File

@@ -81,8 +81,8 @@ public abstract class BdfIncomingMessageHook implements IncomingMessageHook,
protected void trackMessage(Transaction txn, GroupId g, long time,
boolean read) throws DbException {
GroupCount c = getGroupCount(txn, g);
long msgCount = c.getMsgCount() + 1;
long unreadCount = c.getUnreadCount() + (read ? 0 : 1);
int msgCount = c.getMsgCount() + 1;
int unreadCount = c.getUnreadCount() + (read ? 0 : 1);
long latestTime =
time > c.getLatestMsgTime() ? time : c.getLatestMsgTime();
storeGroupCount(txn, g,
@@ -109,8 +109,8 @@ public abstract class BdfIncomingMessageHook implements IncomingMessageHook,
try {
BdfDictionary d = clientHelper.getGroupMetadataAsDictionary(txn, g);
count = new GroupCount(
d.getLong(GROUP_KEY_MSG_COUNT, 0L),
d.getLong(GROUP_KEY_UNREAD_COUNT, 0L),
d.getLong(GROUP_KEY_MSG_COUNT, 0L).intValue(),
d.getLong(GROUP_KEY_UNREAD_COUNT, 0L).intValue(),
d.getLong(GROUP_KEY_LATEST_MSG, 0L)
);
} catch (FormatException e) {

View File

@@ -8,19 +8,18 @@ import org.briarproject.api.db.DatabaseComponent;
import org.briarproject.api.db.DbException;
import org.briarproject.api.db.Transaction;
import org.briarproject.api.messaging.ConversationManager;
import org.briarproject.api.messaging.ConversationManager.ConversationClient;
import org.briarproject.api.sync.Group;
import org.briarproject.api.sync.GroupId;
public abstract class ConversationClient extends BdfIncomingMessageHook
implements ConversationManager.ConversationClient {
public abstract class ConversationClientImpl extends BdfIncomingMessageHook
implements ConversationClient {
protected ConversationClient(DatabaseComponent db,
protected ConversationClientImpl(DatabaseComponent db,
ClientHelper clientHelper, MetadataParser metadataParser) {
super(db, clientHelper, metadataParser);
}
// TODO overwrite super methods to store GroupCount data in a single group
protected abstract Group getContactGroup(Contact contact);
@Override

View File

@@ -29,7 +29,7 @@ import org.briarproject.api.sync.GroupId;
import org.briarproject.api.sync.Message;
import org.briarproject.api.sync.MessageId;
import org.briarproject.api.sync.MessageStatus;
import org.briarproject.clients.ConversationClient;
import org.briarproject.clients.ConversationClientImpl;
import org.briarproject.util.StringUtils;
import java.io.IOException;
@@ -76,7 +76,7 @@ import static org.briarproject.api.introduction.IntroductionConstants.TYPE_REQUE
import static org.briarproject.api.introduction.IntroductionConstants.TYPE_RESPONSE;
import static org.briarproject.clients.BdfConstants.MSG_KEY_READ;
class IntroductionManagerImpl extends ConversationClient
class IntroductionManagerImpl extends ConversationClientImpl
implements IntroductionManager, Client, AddContactHook,
RemoveContactHook {

View File

@@ -32,7 +32,10 @@ class ConversationManagerImpl implements ConversationManager {
@Override
public void registerConversationClient(ConversationClient client) {
clients.add(client);
if (!clients.add(client)) {
throw new IllegalStateException(
"This client is already registered");
}
}
@Override
@@ -56,7 +59,8 @@ class ConversationManagerImpl implements ConversationManager {
public GroupCount getGroupCount(ContactId contactId)
throws DbException {
long msgCount = 0, unreadCount = 0, latestTime = 0;
int msgCount = 0, unreadCount = 0;
long latestTime = 0;
Transaction txn = db.startTransaction(true);
try {
for (ConversationClient client : clients) {

View File

@@ -24,7 +24,7 @@ import org.briarproject.api.sync.GroupId;
import org.briarproject.api.sync.Message;
import org.briarproject.api.sync.MessageId;
import org.briarproject.api.sync.MessageStatus;
import org.briarproject.clients.ConversationClient;
import org.briarproject.clients.ConversationClientImpl;
import org.briarproject.util.StringUtils;
import java.util.ArrayList;
@@ -35,7 +35,7 @@ import javax.inject.Inject;
import static org.briarproject.clients.BdfConstants.MSG_KEY_READ;
class MessagingManagerImpl extends ConversationClient
class MessagingManagerImpl extends ConversationClientImpl
implements MessagingManager, Client, AddContactHook, RemoveContactHook {
static final ClientId CLIENT_ID = new ClientId(StringUtils.fromHexString(

View File

@@ -5,6 +5,7 @@ import org.briarproject.api.contact.ContactId;
import org.briarproject.api.data.BdfDictionary;
import org.briarproject.api.sync.GroupId;
import org.briarproject.api.sync.MessageId;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import static org.briarproject.api.blogs.BlogConstants.BLOG_AUTHOR_NAME;
@@ -22,7 +23,7 @@ public class BlogInviteeSessionState extends InviteeSessionState {
public BlogInviteeSessionState(SessionId sessionId, MessageId storageId,
GroupId groupId, State state, ContactId contactId, GroupId blogId,
String blogTitle, String blogDesc, String blogAuthorName,
byte[] blogPublicKey, @Nullable MessageId invitationId) {
byte[] blogPublicKey, @NotNull MessageId invitationId) {
super(sessionId, storageId, groupId, state, contactId, blogId,
invitationId);

View File

@@ -44,6 +44,7 @@ import static org.briarproject.api.blogs.BlogConstants.BLOG_DESC;
import static org.briarproject.api.blogs.BlogConstants.BLOG_PUBLIC_KEY;
import static org.briarproject.api.blogs.BlogConstants.BLOG_TITLE;
import static org.briarproject.api.sharing.SharingConstants.INVITATION_ID;
import static org.briarproject.api.sharing.SharingConstants.RESPONSE_ID;
class BlogSharingManagerImpl extends
SharingManagerImpl<Blog, BlogInvitation, BlogInviteeSessionState, BlogSharerSessionState, BlogInvitationReceivedEvent, BlogInvitationResponseReceivedEvent>
@@ -253,10 +254,7 @@ class BlogSharingManagerImpl extends
String blogDesc = d.getString(BLOG_DESC);
String blogAuthorName = d.getString(BLOG_AUTHOR_NAME);
byte[] blogPublicKey = d.getRaw(BLOG_PUBLIC_KEY);
MessageId invitationId = null;
byte[] invitationIdBytes = d.getOptionalRaw(INVITATION_ID);
if (invitationIdBytes != null)
invitationId = new MessageId(invitationIdBytes);
MessageId invitationId = new MessageId(d.getRaw(INVITATION_ID));
return new BlogInviteeSessionState(sessionId, storageId,
groupId, state, contactId, blogId, blogTitle, blogDesc,
blogAuthorName, blogPublicKey, invitationId);
@@ -286,7 +284,7 @@ class BlogSharingManagerImpl extends
String blogAuthorName = d.getString(BLOG_AUTHOR_NAME);
byte[] blogPublicKey = d.getRaw(BLOG_PUBLIC_KEY);
MessageId responseId = null;
byte[] responseIdBytes = d.getOptionalRaw(INVITATION_ID);
byte[] responseIdBytes = d.getOptionalRaw(RESPONSE_ID);
if (responseIdBytes != null)
responseId = new MessageId(responseIdBytes);
return new BlogSharerSessionState(sessionId, storageId,
@@ -336,8 +334,11 @@ class BlogSharingManagerImpl extends
BlogSharerSessionState localState, boolean accept, long time) {
String title = localState.getBlogTitle();
ContactId c = localState.getContactId();
MessageId responseId = localState.getResponseId();
if (responseId == null)
throw new IllegalStateException("No responseId");
BlogInvitationResponse response =
new BlogInvitationResponse(localState.getResponseId(),
new BlogInvitationResponse(responseId,
localState.getSessionId(), localState.getGroupId(),
localState.getContactId(), accept, time, false,
false, false, false);

View File

@@ -5,7 +5,7 @@ import org.briarproject.api.contact.ContactId;
import org.briarproject.api.data.BdfDictionary;
import org.briarproject.api.sync.GroupId;
import org.briarproject.api.sync.MessageId;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.NotNull;
import static org.briarproject.api.forum.ForumConstants.FORUM_NAME;
import static org.briarproject.api.forum.ForumConstants.FORUM_SALT;
@@ -18,7 +18,7 @@ public class ForumInviteeSessionState extends InviteeSessionState {
public ForumInviteeSessionState(SessionId sessionId, MessageId storageId,
GroupId groupId, State state, ContactId contactId, GroupId forumId,
String forumName, byte[] forumSalt,
@Nullable MessageId invitationId) {
@NotNull MessageId invitationId) {
super(sessionId, storageId, groupId, state, contactId, forumId,
invitationId);

View File

@@ -36,6 +36,7 @@ import javax.inject.Inject;
import static org.briarproject.api.forum.ForumConstants.FORUM_NAME;
import static org.briarproject.api.forum.ForumConstants.FORUM_SALT;
import static org.briarproject.api.sharing.SharingConstants.INVITATION_ID;
import static org.briarproject.api.sharing.SharingConstants.RESPONSE_ID;
class ForumSharingManagerImpl extends
SharingManagerImpl<Forum, ForumInvitation, ForumInviteeSessionState, ForumSharerSessionState, ForumInvitationReceivedEvent, ForumInvitationResponseReceivedEvent>
@@ -204,10 +205,7 @@ class ForumSharingManagerImpl extends
GroupId forumId, BdfDictionary d) throws FormatException {
String forumName = d.getString(FORUM_NAME);
byte[] forumSalt = d.getRaw(FORUM_SALT);
MessageId invitationId = null;
byte[] invitationIdBytes = d.getOptionalRaw(INVITATION_ID);
if (invitationIdBytes != null)
invitationId = new MessageId(invitationIdBytes);
MessageId invitationId = new MessageId(d.getRaw(INVITATION_ID));
return new ForumInviteeSessionState(sessionId, storageId,
groupId, state, contactId, forumId, forumName, forumSalt,
invitationId);
@@ -234,7 +232,7 @@ class ForumSharingManagerImpl extends
String forumName = d.getString(FORUM_NAME);
byte[] forumSalt = d.getRaw(FORUM_SALT);
MessageId responseId = null;
byte[] responseIdBytes = d.getOptionalRaw(INVITATION_ID);
byte[] responseIdBytes = d.getOptionalRaw(RESPONSE_ID);
if (responseIdBytes != null)
responseId = new MessageId(responseIdBytes);
return new ForumSharerSessionState(sessionId, storageId,
@@ -282,8 +280,11 @@ class ForumSharingManagerImpl extends
ForumSharerSessionState localState, boolean accept, long time) {
String name = localState.getForumName();
ContactId c = localState.getContactId();
MessageId responseId = localState.getResponseId();
if (responseId == null)
throw new IllegalStateException("No responseId");
ForumInvitationResponse response = new ForumInvitationResponse(
localState.getResponseId(), localState.getSessionId(),
responseId, localState.getSessionId(),
localState.getGroupId(), localState.getContactId(), accept,
time, false, false, false, false);
return new ForumInvitationResponseReceivedEvent(name, c, response);

View File

@@ -1,8 +1,8 @@
package org.briarproject.sharing;
import org.briarproject.api.event.InvitationReceivedEvent;
import org.briarproject.api.event.InvitationRequestReceivedEvent;
public interface InvitationReceivedEventFactory<IS extends InviteeSessionState, IR extends InvitationReceivedEvent> {
public interface InvitationReceivedEventFactory<IS extends InviteeSessionState, IR extends InvitationRequestReceivedEvent> {
IR build(IS localState, long time, String msg);
}

View File

@@ -3,7 +3,7 @@ package org.briarproject.sharing;
import org.briarproject.api.FormatException;
import org.briarproject.api.clients.ProtocolEngine;
import org.briarproject.api.event.Event;
import org.briarproject.api.event.InvitationReceivedEvent;
import org.briarproject.api.event.InvitationRequestReceivedEvent;
import org.briarproject.api.sharing.SharingMessage.Invitation;
import org.briarproject.api.system.Clock;
@@ -25,7 +25,7 @@ import static org.briarproject.api.sharing.SharingConstants.TASK_UNSHARE_SHAREAB
import static org.briarproject.api.sharing.SharingMessage.BaseMessage;
import static org.briarproject.api.sharing.SharingMessage.SimpleMessage;
class InviteeEngine<IS extends InviteeSessionState, IR extends InvitationReceivedEvent>
class InviteeEngine<IS extends InviteeSessionState, IR extends InvitationRequestReceivedEvent>
implements ProtocolEngine<InviteeSessionState.Action, IS, BaseMessage> {
private static final Logger LOG =

View File

@@ -5,7 +5,7 @@ import org.briarproject.api.contact.ContactId;
import org.briarproject.api.data.BdfDictionary;
import org.briarproject.api.sync.GroupId;
import org.briarproject.api.sync.MessageId;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.NotNull;
import static org.briarproject.api.sharing.SharingConstants.INVITATION_ID;
import static org.briarproject.api.sharing.SharingConstants.IS_SHARER;
@@ -23,11 +23,12 @@ import static org.briarproject.sharing.InviteeSessionState.Action.REMOTE_LEAVE;
public abstract class InviteeSessionState extends SharingSessionState {
private State state;
@NotNull
private final MessageId invitationId;
public InviteeSessionState(SessionId sessionId, MessageId storageId,
GroupId groupId, State state, ContactId contactId,
GroupId shareableId, MessageId invitationId) {
GroupId shareableId, @NotNull MessageId invitationId) {
super(sessionId, storageId, groupId, contactId, shareableId);
this.state = state;
@@ -38,7 +39,7 @@ public abstract class InviteeSessionState extends SharingSessionState {
BdfDictionary d = super.toBdfDictionary();
d.put(STATE, getState().getValue());
d.put(IS_SHARER, false);
if (invitationId != null) d.put(INVITATION_ID, invitationId);
d.put(INVITATION_ID, invitationId);
return d;
}
@@ -50,7 +51,7 @@ public abstract class InviteeSessionState extends SharingSessionState {
return state;
}
@Nullable
@NotNull
public MessageId getInvitationId() {
return invitationId;
}

View File

@@ -22,7 +22,7 @@ import org.briarproject.api.db.Metadata;
import org.briarproject.api.db.NoSuchMessageException;
import org.briarproject.api.db.Transaction;
import org.briarproject.api.event.Event;
import org.briarproject.api.event.InvitationReceivedEvent;
import org.briarproject.api.event.InvitationRequestReceivedEvent;
import org.briarproject.api.event.InvitationResponseReceivedEvent;
import org.briarproject.api.identity.LocalAuthor;
import org.briarproject.api.sharing.InvitationItem;
@@ -36,7 +36,7 @@ import org.briarproject.api.sync.Message;
import org.briarproject.api.sync.MessageId;
import org.briarproject.api.sync.MessageStatus;
import org.briarproject.api.system.Clock;
import org.briarproject.clients.ConversationClient;
import org.briarproject.clients.ConversationClientImpl;
import org.briarproject.util.StringUtils;
import java.io.IOException;
@@ -85,8 +85,8 @@ import static org.briarproject.api.sharing.SharingMessage.Invitation;
import static org.briarproject.clients.BdfConstants.MSG_KEY_READ;
import static org.briarproject.sharing.InviteeSessionState.State.AWAIT_LOCAL_RESPONSE;
abstract class SharingManagerImpl<S extends Shareable, I extends Invitation, IS extends InviteeSessionState, SS extends SharerSessionState, IR extends InvitationReceivedEvent, IRR extends InvitationResponseReceivedEvent>
extends ConversationClient
abstract class SharingManagerImpl<S extends Shareable, I extends Invitation, IS extends InviteeSessionState, SS extends SharerSessionState, IR extends InvitationRequestReceivedEvent, IRR extends InvitationResponseReceivedEvent>
extends ConversationClientImpl
implements SharingManager<S>, Client, AddContactHook,
RemoveContactHook {
@@ -212,7 +212,6 @@ abstract class SharingManagerImpl<S extends Shareable, I extends Invitation, IS
if (stateExists) throw new FormatException();
// check if shareable can be shared
@SuppressWarnings("unchecked")
I invitation = (I) msg;
S f = getSFactory().parse(invitation);
ContactId contactId = getContactId(txn, m.getGroupId());
@@ -249,7 +248,6 @@ abstract class SharingManagerImpl<S extends Shareable, I extends Invitation, IS
SharingSessionState s = getSessionState(txn, sessionId, true);
if (s instanceof SharerSessionState) {
// we are a sharer and the invitee wants to leave or abort
@SuppressWarnings("unchecked")
SS state = (SS) s;
SharerEngine<I, SS, IRR> engine =
new SharerEngine<I, SS, IRR>(getIFactory(),
@@ -258,7 +256,6 @@ abstract class SharingManagerImpl<S extends Shareable, I extends Invitation, IS
engine.onMessageReceived(state, msg));
} else {
// we are an invitee and the sharer wants to leave or abort
@SuppressWarnings("unchecked")
IS state = (IS) s;
InviteeEngine<IS, IR> engine =
new InviteeEngine<IS, IR>(getIRFactory(), clock);
@@ -298,6 +295,7 @@ abstract class SharingManagerImpl<S extends Shareable, I extends Invitation, IS
processSharerStateUpdate(txn, null, update);
// track message
// TODO handle this properly without engine hacks (#376)
long time = update.toSend.get(0).getTime();
trackMessage(txn, localState.getGroupId(), time, true);
@@ -334,6 +332,7 @@ abstract class SharingManagerImpl<S extends Shareable, I extends Invitation, IS
processInviteeStateUpdate(txn, null, update);
// track message
// TODO handle this properly without engine hacks (#376)
long time = update.toSend.get(0).getTime();
trackMessage(txn, localState.getGroupId(), time, true);
@@ -475,7 +474,6 @@ abstract class SharingManagerImpl<S extends Shareable, I extends Invitation, IS
BdfDictionary d = m.getValue();
try {
I msg = getIFactory().build(group.getId(), d);
@SuppressWarnings("unchecked")
IS iss = (IS) getSessionState(txn, msg.getSessionId(), true);
// get and add the shareable if the invitation is unanswered
if (iss.getState().equals(AWAIT_LOCAL_RESPONSE)) {
@@ -733,7 +731,6 @@ abstract class SharingManagerImpl<S extends Shareable, I extends Invitation, IS
if (!d.getBoolean(IS_SHARER)) throw new FormatException();
//noinspection unchecked
return (SS) SharingSessionState
.fromBdfDictionary(getISFactory(), getSSFactory(), d);
}
@@ -768,7 +765,6 @@ abstract class SharingManagerImpl<S extends Shareable, I extends Invitation, IS
}
throw new DbException();
}
//noinspection unchecked
return (IS) SharingSessionState
.fromBdfDictionary(getISFactory(), getSSFactory(),
map.values().iterator().next());
@@ -954,7 +950,6 @@ abstract class SharingManagerImpl<S extends Shareable, I extends Invitation, IS
SharerEngine<I, SS, IRR> engine =
new SharerEngine<I, SS, IRR>(getIFactory(),
getIRRFactory(), clock);
//noinspection unchecked
processSharerStateUpdate(txn, null,
engine.onLocalAction((SS) state, action));
} else {
@@ -962,7 +957,6 @@ abstract class SharingManagerImpl<S extends Shareable, I extends Invitation, IS
InviteeSessionState.Action.LOCAL_LEAVE;
InviteeEngine<IS, IR> engine =
new InviteeEngine<IS, IR>(getIRFactory(), clock);
//noinspection unchecked
processInviteeStateUpdate(txn, null,
engine.onLocalAction((IS) state, action));
}