mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-13 19:29:06 +01:00
Also use dedicated classes to represent messages instead of BdfDictionary
This commit is contained in:
@@ -0,0 +1,166 @@
|
|||||||
|
package org.briarproject.api.forum;
|
||||||
|
|
||||||
|
import org.briarproject.api.FormatException;
|
||||||
|
import org.briarproject.api.clients.SessionId;
|
||||||
|
import org.briarproject.api.data.BdfDictionary;
|
||||||
|
import org.briarproject.api.data.BdfEntry;
|
||||||
|
import org.briarproject.api.data.BdfList;
|
||||||
|
import org.briarproject.api.sync.GroupId;
|
||||||
|
|
||||||
|
import static org.briarproject.api.forum.ForumConstants.FORUM_NAME;
|
||||||
|
import static org.briarproject.api.forum.ForumConstants.FORUM_SALT;
|
||||||
|
import static org.briarproject.api.forum.ForumConstants.GROUP_ID;
|
||||||
|
import static org.briarproject.api.forum.ForumConstants.INVITATION_MSG;
|
||||||
|
import static org.briarproject.api.forum.ForumConstants.SESSION_ID;
|
||||||
|
import static org.briarproject.api.forum.ForumConstants.SHARE_MSG_TYPE_ABORT;
|
||||||
|
import static org.briarproject.api.forum.ForumConstants.SHARE_MSG_TYPE_ACCEPT;
|
||||||
|
import static org.briarproject.api.forum.ForumConstants.SHARE_MSG_TYPE_DECLINE;
|
||||||
|
import static org.briarproject.api.forum.ForumConstants.SHARE_MSG_TYPE_INVITATION;
|
||||||
|
import static org.briarproject.api.forum.ForumConstants.SHARE_MSG_TYPE_LEAVE;
|
||||||
|
import static org.briarproject.api.forum.ForumConstants.TYPE;
|
||||||
|
|
||||||
|
public interface ForumSharingMessage {
|
||||||
|
|
||||||
|
abstract class BaseMessage {
|
||||||
|
private final GroupId groupId;
|
||||||
|
private final SessionId sessionId;
|
||||||
|
|
||||||
|
public BaseMessage(GroupId groupId, SessionId sessionId) {
|
||||||
|
|
||||||
|
this.groupId = groupId;
|
||||||
|
this.sessionId = sessionId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public BdfList toBdfList() {
|
||||||
|
return BdfList.of(getType(), getSessionId());
|
||||||
|
}
|
||||||
|
|
||||||
|
public abstract BdfDictionary toBdfDictionary();
|
||||||
|
|
||||||
|
protected BdfDictionary toBdfDictionaryHelper() {
|
||||||
|
return BdfDictionary.of(
|
||||||
|
new BdfEntry(TYPE, getType()),
|
||||||
|
new BdfEntry(GROUP_ID, groupId),
|
||||||
|
new BdfEntry(SESSION_ID, sessionId)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static BaseMessage from(GroupId groupId, BdfDictionary d)
|
||||||
|
throws FormatException {
|
||||||
|
|
||||||
|
long type = d.getLong(TYPE);
|
||||||
|
|
||||||
|
if (type == SHARE_MSG_TYPE_INVITATION)
|
||||||
|
return Invitation.from(groupId, d);
|
||||||
|
else
|
||||||
|
return SimpleMessage.from(type, groupId, d);
|
||||||
|
}
|
||||||
|
|
||||||
|
public abstract long getType();
|
||||||
|
|
||||||
|
public GroupId getGroupId() {
|
||||||
|
return groupId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public SessionId getSessionId() {
|
||||||
|
return sessionId;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class Invitation extends BaseMessage {
|
||||||
|
|
||||||
|
private final String forumName;
|
||||||
|
private final byte[] forumSalt;
|
||||||
|
private final String message;
|
||||||
|
|
||||||
|
public Invitation(GroupId groupId, SessionId sessionId,
|
||||||
|
String forumName, byte[] forumSalt, String message) {
|
||||||
|
|
||||||
|
super(groupId, sessionId);
|
||||||
|
|
||||||
|
this.forumName = forumName;
|
||||||
|
this.forumSalt = forumSalt;
|
||||||
|
this.message = message;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public long getType() {
|
||||||
|
return SHARE_MSG_TYPE_INVITATION;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BdfList toBdfList() {
|
||||||
|
BdfList list = super.toBdfList();
|
||||||
|
list.add(forumName);
|
||||||
|
list.add(forumSalt);
|
||||||
|
if (message != null) list.add(message);
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BdfDictionary toBdfDictionary() {
|
||||||
|
BdfDictionary d = toBdfDictionaryHelper();
|
||||||
|
d.put(FORUM_NAME, forumName);
|
||||||
|
d.put(FORUM_SALT, forumSalt);
|
||||||
|
if (message != null) d.put(INVITATION_MSG, message);
|
||||||
|
return d;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Invitation from(GroupId groupId, BdfDictionary d)
|
||||||
|
throws FormatException {
|
||||||
|
|
||||||
|
SessionId sessionId = new SessionId(d.getRaw(SESSION_ID));
|
||||||
|
String forumName = d.getString(FORUM_NAME);
|
||||||
|
byte[] forumSalt = d.getRaw(FORUM_SALT);
|
||||||
|
String message = d.getOptionalString(INVITATION_MSG);
|
||||||
|
|
||||||
|
return new Invitation(groupId, sessionId, forumName, forumSalt,
|
||||||
|
message);
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getForumName() {
|
||||||
|
return forumName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public byte[] getForumSalt() {
|
||||||
|
return forumSalt;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getMessage() {
|
||||||
|
return message;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class SimpleMessage extends BaseMessage {
|
||||||
|
|
||||||
|
private final long type;
|
||||||
|
|
||||||
|
public SimpleMessage(long type, GroupId groupId, SessionId sessionId) {
|
||||||
|
super(groupId, sessionId);
|
||||||
|
this.type = type;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public long getType() {
|
||||||
|
return type;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BdfDictionary toBdfDictionary() {
|
||||||
|
return toBdfDictionaryHelper();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static SimpleMessage from(long type, GroupId groupId,
|
||||||
|
BdfDictionary d) throws FormatException {
|
||||||
|
|
||||||
|
if (type != SHARE_MSG_TYPE_ACCEPT &&
|
||||||
|
type != SHARE_MSG_TYPE_DECLINE &&
|
||||||
|
type != SHARE_MSG_TYPE_LEAVE &&
|
||||||
|
type != SHARE_MSG_TYPE_ABORT) throw new FormatException();
|
||||||
|
|
||||||
|
SessionId sessionId = new SessionId(d.getRaw(SESSION_ID));
|
||||||
|
return new SimpleMessage(type, groupId, sessionId);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -55,11 +55,7 @@ import static java.util.logging.Level.INFO;
|
|||||||
import static java.util.logging.Level.WARNING;
|
import static java.util.logging.Level.WARNING;
|
||||||
import static org.briarproject.api.clients.ProtocolEngine.StateUpdate;
|
import static org.briarproject.api.clients.ProtocolEngine.StateUpdate;
|
||||||
import static org.briarproject.api.forum.ForumConstants.CONTACT_ID;
|
import static org.briarproject.api.forum.ForumConstants.CONTACT_ID;
|
||||||
import static org.briarproject.api.forum.ForumConstants.FORUM_NAME;
|
|
||||||
import static org.briarproject.api.forum.ForumConstants.FORUM_SALT;
|
|
||||||
import static org.briarproject.api.forum.ForumConstants.FORUM_SALT_LENGTH;
|
import static org.briarproject.api.forum.ForumConstants.FORUM_SALT_LENGTH;
|
||||||
import static org.briarproject.api.forum.ForumConstants.GROUP_ID;
|
|
||||||
import static org.briarproject.api.forum.ForumConstants.INVITATION_MSG;
|
|
||||||
import static org.briarproject.api.forum.ForumConstants.IS_SHARER;
|
import static org.briarproject.api.forum.ForumConstants.IS_SHARER;
|
||||||
import static org.briarproject.api.forum.ForumConstants.LOCAL;
|
import static org.briarproject.api.forum.ForumConstants.LOCAL;
|
||||||
import static org.briarproject.api.forum.ForumConstants.READ;
|
import static org.briarproject.api.forum.ForumConstants.READ;
|
||||||
@@ -83,6 +79,8 @@ import static org.briarproject.api.forum.ForumConstants.TIME;
|
|||||||
import static org.briarproject.api.forum.ForumConstants.TO_BE_SHARED_BY_US;
|
import static org.briarproject.api.forum.ForumConstants.TO_BE_SHARED_BY_US;
|
||||||
import static org.briarproject.api.forum.ForumConstants.TYPE;
|
import static org.briarproject.api.forum.ForumConstants.TYPE;
|
||||||
import static org.briarproject.api.forum.ForumManager.RemoveForumHook;
|
import static org.briarproject.api.forum.ForumManager.RemoveForumHook;
|
||||||
|
import static org.briarproject.api.forum.ForumSharingMessage.BaseMessage;
|
||||||
|
import static org.briarproject.api.forum.ForumSharingMessage.Invitation;
|
||||||
import static org.briarproject.forum.ForumSharingSessionState.fromBdfDictionary;
|
import static org.briarproject.forum.ForumSharingSessionState.fromBdfDictionary;
|
||||||
import static org.briarproject.forum.SharerSessionState.Action;
|
import static org.briarproject.forum.SharerSessionState.Action;
|
||||||
|
|
||||||
@@ -178,11 +176,12 @@ class ForumSharingManagerImpl extends BdfIncomingMessageHook
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void incomingMessage(Transaction txn, Message m, BdfList body,
|
protected void incomingMessage(Transaction txn, Message m, BdfList body,
|
||||||
BdfDictionary msg) throws DbException, FormatException {
|
BdfDictionary d) throws DbException, FormatException {
|
||||||
|
|
||||||
SessionId sessionId = new SessionId(msg.getRaw(SESSION_ID));
|
BaseMessage msg = BaseMessage.from(m.getGroupId(), d);
|
||||||
long type = msg.getLong(TYPE);
|
SessionId sessionId = msg.getSessionId();
|
||||||
if (type == SHARE_MSG_TYPE_INVITATION) {
|
|
||||||
|
if (msg.getType() == SHARE_MSG_TYPE_INVITATION) {
|
||||||
// we are an invitee who just received a new invitation
|
// we are an invitee who just received a new invitation
|
||||||
boolean stateExists = true;
|
boolean stateExists = true;
|
||||||
try {
|
try {
|
||||||
@@ -197,8 +196,9 @@ class ForumSharingManagerImpl extends BdfIncomingMessageHook
|
|||||||
if (stateExists) throw new FormatException();
|
if (stateExists) throw new FormatException();
|
||||||
|
|
||||||
// check if forum can be shared
|
// check if forum can be shared
|
||||||
Forum f = forumFactory.createForum(msg.getString(FORUM_NAME),
|
Invitation invitation = (Invitation) msg;
|
||||||
msg.getRaw(FORUM_SALT));
|
Forum f = forumFactory.createForum(invitation.getForumName(),
|
||||||
|
invitation.getForumSalt());
|
||||||
ContactId contactId = getContactId(txn, m.getGroupId());
|
ContactId contactId = getContactId(txn, m.getGroupId());
|
||||||
Contact contact = db.getContact(txn, contactId);
|
Contact contact = db.getContact(txn, contactId);
|
||||||
if (!canBeShared(txn, f.getId(), contact))
|
if (!canBeShared(txn, f.getId(), contact))
|
||||||
@@ -206,7 +206,7 @@ class ForumSharingManagerImpl extends BdfIncomingMessageHook
|
|||||||
|
|
||||||
// initialize state and process invitation
|
// initialize state and process invitation
|
||||||
InviteeSessionState state =
|
InviteeSessionState state =
|
||||||
initializeInviteeState(txn, contactId, msg);
|
initializeInviteeState(txn, contactId, invitation);
|
||||||
InviteeEngine engine = new InviteeEngine(forumFactory);
|
InviteeEngine engine = new InviteeEngine(forumFactory);
|
||||||
processInviteeStateUpdate(txn, m.getId(),
|
processInviteeStateUpdate(txn, m.getId(),
|
||||||
engine.onMessageReceived(state, msg));
|
engine.onMessageReceived(state, msg));
|
||||||
@@ -214,15 +214,15 @@ class ForumSharingManagerImpl extends BdfIncomingMessageHook
|
|||||||
if (LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
|
if (LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
|
||||||
deleteMessage(txn, m.getId());
|
deleteMessage(txn, m.getId());
|
||||||
}
|
}
|
||||||
} else if (type == SHARE_MSG_TYPE_ACCEPT ||
|
} else if (msg.getType() == SHARE_MSG_TYPE_ACCEPT ||
|
||||||
type == SHARE_MSG_TYPE_DECLINE) {
|
msg.getType() == SHARE_MSG_TYPE_DECLINE) {
|
||||||
// we are a sharer who just received a response
|
// we are a sharer who just received a response
|
||||||
SharerSessionState state = getSessionStateForSharer(txn, sessionId);
|
SharerSessionState state = getSessionStateForSharer(txn, sessionId);
|
||||||
SharerEngine engine = new SharerEngine();
|
SharerEngine engine = new SharerEngine();
|
||||||
processSharerStateUpdate(txn, m.getId(),
|
processSharerStateUpdate(txn, m.getId(),
|
||||||
engine.onMessageReceived(state, msg));
|
engine.onMessageReceived(state, msg));
|
||||||
} else if (type == SHARE_MSG_TYPE_LEAVE ||
|
} else if (msg.getType() == SHARE_MSG_TYPE_LEAVE ||
|
||||||
type == SHARE_MSG_TYPE_ABORT) {
|
msg.getType() == SHARE_MSG_TYPE_ABORT) {
|
||||||
// we don't know who we are, so figure it out
|
// we don't know who we are, so figure it out
|
||||||
ForumSharingSessionState s = getSessionState(txn, sessionId, true);
|
ForumSharingSessionState s = getSessionState(txn, sessionId, true);
|
||||||
if (s instanceof SharerSessionState) {
|
if (s instanceof SharerSessionState) {
|
||||||
@@ -322,34 +322,33 @@ class ForumSharingManagerImpl extends BdfIncomingMessageHook
|
|||||||
Map<MessageId, BdfDictionary> map = clientHelper
|
Map<MessageId, BdfDictionary> map = clientHelper
|
||||||
.getMessageMetadataAsDictionary(txn, group.getId());
|
.getMessageMetadataAsDictionary(txn, group.getId());
|
||||||
for (Map.Entry<MessageId, BdfDictionary> m : map.entrySet()) {
|
for (Map.Entry<MessageId, BdfDictionary> m : map.entrySet()) {
|
||||||
BdfDictionary msg = m.getValue();
|
BdfDictionary d = m.getValue();
|
||||||
try {
|
try {
|
||||||
if (msg.getLong(TYPE) != SHARE_MSG_TYPE_INVITATION)
|
if (d.getLong(TYPE) != SHARE_MSG_TYPE_INVITATION)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
|
Invitation msg = Invitation.from(group.getId(), d);
|
||||||
MessageStatus status =
|
MessageStatus status =
|
||||||
db.getMessageStatus(txn, contactId, m.getKey());
|
db.getMessageStatus(txn, contactId, m.getKey());
|
||||||
SessionId sessionId = new SessionId(msg.getRaw(SESSION_ID));
|
long time = d.getLong(TIME);
|
||||||
String name = msg.getString(FORUM_NAME);
|
boolean local = d.getBoolean(LOCAL);
|
||||||
String message = msg.getOptionalString(INVITATION_MSG);
|
boolean read = d.getBoolean(READ, false);
|
||||||
long time = msg.getLong(TIME);
|
|
||||||
boolean local = msg.getBoolean(LOCAL);
|
|
||||||
boolean read = msg.getBoolean(READ, false);
|
|
||||||
boolean available = false;
|
boolean available = false;
|
||||||
if (!local) {
|
if (!local) {
|
||||||
// figure out whether the forum is still available
|
// figure out whether the forum is still available
|
||||||
ForumSharingSessionState s =
|
ForumSharingSessionState s =
|
||||||
getSessionState(txn, sessionId, true);
|
getSessionState(txn, msg.getSessionId(), true);
|
||||||
if (!(s instanceof InviteeSessionState))
|
if (!(s instanceof InviteeSessionState))
|
||||||
continue;
|
continue;
|
||||||
available = ((InviteeSessionState) s).getState() ==
|
available = ((InviteeSessionState) s).getState() ==
|
||||||
InviteeSessionState.State.AWAIT_LOCAL_RESPONSE;
|
InviteeSessionState.State.AWAIT_LOCAL_RESPONSE;
|
||||||
}
|
}
|
||||||
ForumInvitationMessage im =
|
ForumInvitationMessage im =
|
||||||
new ForumInvitationMessage(m.getKey(), sessionId,
|
new ForumInvitationMessage(m.getKey(),
|
||||||
contactId, name, message, available, time,
|
msg.getSessionId(), contactId,
|
||||||
local, status.isSent(), status.isSeen(),
|
msg.getForumName(), msg.getMessage(),
|
||||||
read);
|
available, time, local, status.isSent(),
|
||||||
|
status.isSeen(), read);
|
||||||
list.add(im);
|
list.add(im);
|
||||||
} catch (FormatException e) {
|
} catch (FormatException e) {
|
||||||
if (LOG.isLoggable(WARNING))
|
if (LOG.isLoggable(WARNING))
|
||||||
@@ -508,13 +507,13 @@ class ForumSharingManagerImpl extends BdfIncomingMessageHook
|
|||||||
}
|
}
|
||||||
|
|
||||||
private InviteeSessionState initializeInviteeState(Transaction txn,
|
private InviteeSessionState initializeInviteeState(Transaction txn,
|
||||||
ContactId contactId, BdfDictionary msg)
|
ContactId contactId, Invitation msg)
|
||||||
throws FormatException, DbException {
|
throws FormatException, DbException {
|
||||||
|
|
||||||
Contact c = db.getContact(txn, contactId);
|
Contact c = db.getContact(txn, contactId);
|
||||||
Group group = getContactGroup(c);
|
Group group = getContactGroup(c);
|
||||||
String name = msg.getString(FORUM_NAME);
|
String name = msg.getForumName();
|
||||||
byte[] salt = msg.getRaw(FORUM_SALT);
|
byte[] salt = msg.getForumSalt();
|
||||||
Forum f = forumFactory.createForum(name, salt);
|
Forum f = forumFactory.createForum(name, salt);
|
||||||
|
|
||||||
// create local message to keep engine state
|
// create local message to keep engine state
|
||||||
@@ -524,11 +523,10 @@ class ForumSharingManagerImpl extends BdfIncomingMessageHook
|
|||||||
Message m = clientHelper.createMessage(localGroup.getId(), now,
|
Message m = clientHelper.createMessage(localGroup.getId(), now,
|
||||||
BdfList.of(mSalt));
|
BdfList.of(mSalt));
|
||||||
|
|
||||||
SessionId sessionId = new SessionId(msg.getRaw(SESSION_ID));
|
InviteeSessionState s = new InviteeSessionState(msg.getSessionId(),
|
||||||
|
m.getId(), group.getId(),
|
||||||
InviteeSessionState s = new InviteeSessionState(sessionId, m.getId(),
|
InviteeSessionState.State.AWAIT_INVITATION, contactId,
|
||||||
group.getId(), InviteeSessionState.State.AWAIT_INVITATION,
|
f.getId(), f.getName(), f.getSalt());
|
||||||
contactId, f.getId(), f.getName(), f.getSalt());
|
|
||||||
|
|
||||||
// save local state to database
|
// save local state to database
|
||||||
BdfDictionary d = s.toBdfDictionary();
|
BdfDictionary d = s.toBdfDictionary();
|
||||||
@@ -646,7 +644,7 @@ class ForumSharingManagerImpl extends BdfIncomingMessageHook
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void processStateUpdate(Transaction txn, MessageId messageId,
|
private void processStateUpdate(Transaction txn, MessageId messageId,
|
||||||
StateUpdate<ForumSharingSessionState, BdfDictionary> result)
|
StateUpdate<ForumSharingSessionState, BaseMessage> result)
|
||||||
throws DbException, FormatException {
|
throws DbException, FormatException {
|
||||||
|
|
||||||
// perform actions based on new local state
|
// perform actions based on new local state
|
||||||
@@ -658,8 +656,8 @@ class ForumSharingManagerImpl extends BdfIncomingMessageHook
|
|||||||
result.localState.toBdfDictionary());
|
result.localState.toBdfDictionary());
|
||||||
|
|
||||||
// send messages
|
// send messages
|
||||||
for (BdfDictionary d : result.toSend) {
|
for (BaseMessage msg : result.toSend) {
|
||||||
sendMessage(txn, d);
|
sendMessage(txn, msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
// broadcast events
|
// broadcast events
|
||||||
@@ -678,11 +676,11 @@ class ForumSharingManagerImpl extends BdfIncomingMessageHook
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void processSharerStateUpdate(Transaction txn, MessageId messageId,
|
private void processSharerStateUpdate(Transaction txn, MessageId messageId,
|
||||||
StateUpdate<SharerSessionState, BdfDictionary> result)
|
StateUpdate<SharerSessionState, BaseMessage> result)
|
||||||
throws DbException, FormatException {
|
throws DbException, FormatException {
|
||||||
|
|
||||||
StateUpdate<ForumSharingSessionState, BdfDictionary> r =
|
StateUpdate<ForumSharingSessionState, BaseMessage> r =
|
||||||
new StateUpdate<ForumSharingSessionState, BdfDictionary>(
|
new StateUpdate<ForumSharingSessionState, BaseMessage>(
|
||||||
result.deleteMessage, result.deleteState,
|
result.deleteMessage, result.deleteState,
|
||||||
result.localState, result.toSend, result.toBroadcast);
|
result.localState, result.toSend, result.toBroadcast);
|
||||||
|
|
||||||
@@ -690,11 +688,11 @@ class ForumSharingManagerImpl extends BdfIncomingMessageHook
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void processInviteeStateUpdate(Transaction txn, MessageId messageId,
|
private void processInviteeStateUpdate(Transaction txn, MessageId messageId,
|
||||||
StateUpdate<InviteeSessionState, BdfDictionary> result)
|
StateUpdate<InviteeSessionState, BaseMessage> result)
|
||||||
throws DbException, FormatException {
|
throws DbException, FormatException {
|
||||||
|
|
||||||
StateUpdate<ForumSharingSessionState, BdfDictionary> r =
|
StateUpdate<ForumSharingSessionState, BaseMessage> r =
|
||||||
new StateUpdate<ForumSharingSessionState, BdfDictionary>(
|
new StateUpdate<ForumSharingSessionState, BaseMessage>(
|
||||||
result.deleteMessage, result.deleteState,
|
result.deleteMessage, result.deleteState,
|
||||||
result.localState, result.toSend, result.toBroadcast);
|
result.localState, result.toSend, result.toBroadcast);
|
||||||
|
|
||||||
@@ -751,50 +749,23 @@ class ForumSharingManagerImpl extends BdfIncomingMessageHook
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void sendMessage(Transaction txn, BdfDictionary m)
|
private void sendMessage(Transaction txn, BaseMessage m)
|
||||||
throws FormatException, DbException {
|
throws FormatException, DbException {
|
||||||
|
|
||||||
BdfList list = encodeMessage(m);
|
byte[] body = clientHelper.toByteArray(m.toBdfList());
|
||||||
byte[] body = clientHelper.toByteArray(list);
|
Group group = db.getGroup(txn, m.getGroupId());
|
||||||
GroupId groupId = new GroupId(m.getRaw(GROUP_ID));
|
|
||||||
Group group = db.getGroup(txn, groupId);
|
|
||||||
long timestamp = clock.currentTimeMillis();
|
long timestamp = clock.currentTimeMillis();
|
||||||
|
|
||||||
// add message itself as metadata
|
// add message itself as metadata
|
||||||
m.put(LOCAL, true);
|
BdfDictionary d = m.toBdfDictionary();
|
||||||
m.put(TIME, timestamp);
|
d.put(LOCAL, true);
|
||||||
Metadata meta = metadataEncoder.encode(m);
|
d.put(TIME, timestamp);
|
||||||
|
Metadata meta = metadataEncoder.encode(d);
|
||||||
|
|
||||||
messageQueueManager
|
messageQueueManager
|
||||||
.sendMessage(txn, group, timestamp, body, meta);
|
.sendMessage(txn, group, timestamp, body, meta);
|
||||||
}
|
}
|
||||||
|
|
||||||
private BdfList encodeMessage(BdfDictionary m) throws FormatException {
|
|
||||||
long type = m.getLong(TYPE);
|
|
||||||
|
|
||||||
BdfList list;
|
|
||||||
if (type == SHARE_MSG_TYPE_INVITATION) {
|
|
||||||
list = BdfList.of(type,
|
|
||||||
m.getRaw(SESSION_ID),
|
|
||||||
m.getString(FORUM_NAME),
|
|
||||||
m.getRaw(FORUM_SALT)
|
|
||||||
);
|
|
||||||
String msg = m.getOptionalString(INVITATION_MSG);
|
|
||||||
if (msg != null) list.add(msg);
|
|
||||||
} else if (type == SHARE_MSG_TYPE_ACCEPT) {
|
|
||||||
list = BdfList.of(type, m.getRaw(SESSION_ID));
|
|
||||||
} else if (type == SHARE_MSG_TYPE_DECLINE) {
|
|
||||||
list = BdfList.of(type, m.getRaw(SESSION_ID));
|
|
||||||
} else if (type == SHARE_MSG_TYPE_LEAVE) {
|
|
||||||
list = BdfList.of(type, m.getRaw(SESSION_ID));
|
|
||||||
} else if (type == SHARE_MSG_TYPE_ABORT) {
|
|
||||||
list = BdfList.of(type, m.getRaw(SESSION_ID));
|
|
||||||
} else {
|
|
||||||
throw new FormatException();
|
|
||||||
}
|
|
||||||
return list;
|
|
||||||
}
|
|
||||||
|
|
||||||
private Group getContactGroup(Contact c) {
|
private Group getContactGroup(Contact c) {
|
||||||
return privateGroupFactory.createPrivateGroup(CLIENT_ID, c);
|
return privateGroupFactory.createPrivateGroup(CLIENT_ID, c);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -73,7 +73,6 @@ class ForumSharingValidator extends BdfMessageValidator {
|
|||||||
// Return the metadata
|
// Return the metadata
|
||||||
d.put(TYPE, type);
|
d.put(TYPE, type);
|
||||||
d.put(SESSION_ID, id);
|
d.put(SESSION_ID, id);
|
||||||
d.put(GROUP_ID, m.getGroupId());
|
|
||||||
d.put(LOCAL, false);
|
d.put(LOCAL, false);
|
||||||
d.put(TIME, m.getTimestamp());
|
d.put(TIME, m.getTimestamp());
|
||||||
return d;
|
return d;
|
||||||
|
|||||||
@@ -3,22 +3,17 @@ package org.briarproject.forum;
|
|||||||
import org.briarproject.api.FormatException;
|
import org.briarproject.api.FormatException;
|
||||||
import org.briarproject.api.clients.ProtocolEngine;
|
import org.briarproject.api.clients.ProtocolEngine;
|
||||||
import org.briarproject.api.contact.ContactId;
|
import org.briarproject.api.contact.ContactId;
|
||||||
import org.briarproject.api.data.BdfDictionary;
|
|
||||||
import org.briarproject.api.data.BdfEntry;
|
|
||||||
import org.briarproject.api.event.Event;
|
import org.briarproject.api.event.Event;
|
||||||
import org.briarproject.api.event.ForumInvitationReceivedEvent;
|
import org.briarproject.api.event.ForumInvitationReceivedEvent;
|
||||||
import org.briarproject.api.forum.Forum;
|
import org.briarproject.api.forum.Forum;
|
||||||
import org.briarproject.api.forum.ForumFactory;
|
import org.briarproject.api.forum.ForumFactory;
|
||||||
|
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
import static java.util.logging.Level.INFO;
|
import static java.util.logging.Level.INFO;
|
||||||
import static java.util.logging.Level.WARNING;
|
import static java.util.logging.Level.WARNING;
|
||||||
import static org.briarproject.api.forum.ForumConstants.GROUP_ID;
|
|
||||||
import static org.briarproject.api.forum.ForumConstants.SESSION_ID;
|
|
||||||
import static org.briarproject.api.forum.ForumConstants.SHARE_MSG_TYPE_ABORT;
|
import static org.briarproject.api.forum.ForumConstants.SHARE_MSG_TYPE_ABORT;
|
||||||
import static org.briarproject.api.forum.ForumConstants.SHARE_MSG_TYPE_ACCEPT;
|
import static org.briarproject.api.forum.ForumConstants.SHARE_MSG_TYPE_ACCEPT;
|
||||||
import static org.briarproject.api.forum.ForumConstants.SHARE_MSG_TYPE_DECLINE;
|
import static org.briarproject.api.forum.ForumConstants.SHARE_MSG_TYPE_DECLINE;
|
||||||
@@ -28,7 +23,8 @@ import static org.briarproject.api.forum.ForumConstants.TASK_ADD_FORUM_TO_LIST_S
|
|||||||
import static org.briarproject.api.forum.ForumConstants.TASK_ADD_SHARED_FORUM;
|
import static org.briarproject.api.forum.ForumConstants.TASK_ADD_SHARED_FORUM;
|
||||||
import static org.briarproject.api.forum.ForumConstants.TASK_REMOVE_FORUM_FROM_LIST_SHARED_WITH_US;
|
import static org.briarproject.api.forum.ForumConstants.TASK_REMOVE_FORUM_FROM_LIST_SHARED_WITH_US;
|
||||||
import static org.briarproject.api.forum.ForumConstants.TASK_UNSHARE_FORUM_SHARED_WITH_US;
|
import static org.briarproject.api.forum.ForumConstants.TASK_UNSHARE_FORUM_SHARED_WITH_US;
|
||||||
import static org.briarproject.api.forum.ForumConstants.TYPE;
|
import static org.briarproject.api.forum.ForumSharingMessage.SimpleMessage;
|
||||||
|
import static org.briarproject.api.forum.ForumSharingMessage.BaseMessage;
|
||||||
import static org.briarproject.forum.InviteeSessionState.Action;
|
import static org.briarproject.forum.InviteeSessionState.Action;
|
||||||
import static org.briarproject.forum.InviteeSessionState.Action.LOCAL_ABORT;
|
import static org.briarproject.forum.InviteeSessionState.Action.LOCAL_ABORT;
|
||||||
import static org.briarproject.forum.InviteeSessionState.Action.LOCAL_ACCEPT;
|
import static org.briarproject.forum.InviteeSessionState.Action.LOCAL_ACCEPT;
|
||||||
@@ -42,7 +38,7 @@ import static org.briarproject.forum.InviteeSessionState.State.FINISHED;
|
|||||||
import static org.briarproject.forum.InviteeSessionState.State.LEFT;
|
import static org.briarproject.forum.InviteeSessionState.State.LEFT;
|
||||||
|
|
||||||
public class InviteeEngine
|
public class InviteeEngine
|
||||||
implements ProtocolEngine<Action, InviteeSessionState, BdfDictionary> {
|
implements ProtocolEngine<Action, InviteeSessionState, BaseMessage> {
|
||||||
|
|
||||||
private final ForumFactory forumFactory;
|
private final ForumFactory forumFactory;
|
||||||
private static final Logger LOG =
|
private static final Logger LOG =
|
||||||
@@ -53,7 +49,7 @@ public class InviteeEngine
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public StateUpdate<InviteeSessionState, BdfDictionary> onLocalAction(
|
public StateUpdate<InviteeSessionState, BaseMessage> onLocalAction(
|
||||||
InviteeSessionState localState, Action action) {
|
InviteeSessionState localState, Action action) {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@@ -72,37 +68,34 @@ public class InviteeEngine
|
|||||||
}
|
}
|
||||||
return noUpdate(localState, true);
|
return noUpdate(localState, true);
|
||||||
}
|
}
|
||||||
List<BdfDictionary> messages;
|
List<BaseMessage> messages;
|
||||||
List<Event> events = Collections.emptyList();
|
List<Event> events = Collections.emptyList();
|
||||||
|
|
||||||
if (action == LOCAL_ACCEPT || action == LOCAL_DECLINE) {
|
if (action == LOCAL_ACCEPT || action == LOCAL_DECLINE) {
|
||||||
BdfDictionary msg = BdfDictionary.of(
|
BaseMessage msg;
|
||||||
new BdfEntry(SESSION_ID, localState.getSessionId()),
|
|
||||||
new BdfEntry(GROUP_ID, localState.getGroupId())
|
|
||||||
);
|
|
||||||
if (action == LOCAL_ACCEPT) {
|
if (action == LOCAL_ACCEPT) {
|
||||||
localState.setTask(TASK_ADD_SHARED_FORUM);
|
localState.setTask(TASK_ADD_SHARED_FORUM);
|
||||||
msg.put(TYPE, SHARE_MSG_TYPE_ACCEPT);
|
msg = new SimpleMessage(SHARE_MSG_TYPE_ACCEPT,
|
||||||
|
localState.getGroupId(), localState.getSessionId());
|
||||||
} else {
|
} else {
|
||||||
localState.setTask(
|
localState.setTask(
|
||||||
TASK_REMOVE_FORUM_FROM_LIST_SHARED_WITH_US);
|
TASK_REMOVE_FORUM_FROM_LIST_SHARED_WITH_US);
|
||||||
msg.put(TYPE, SHARE_MSG_TYPE_DECLINE);
|
msg = new SimpleMessage(SHARE_MSG_TYPE_DECLINE,
|
||||||
|
localState.getGroupId(), localState.getSessionId());
|
||||||
}
|
}
|
||||||
messages = Collections.singletonList(msg);
|
messages = Collections.singletonList(msg);
|
||||||
logLocalAction(currentState, localState, msg);
|
logLocalAction(currentState, localState, msg);
|
||||||
}
|
}
|
||||||
else if (action == LOCAL_LEAVE) {
|
else if (action == LOCAL_LEAVE) {
|
||||||
BdfDictionary msg = new BdfDictionary();
|
BaseMessage msg = new SimpleMessage(SHARE_MSG_TYPE_LEAVE,
|
||||||
msg.put(TYPE, SHARE_MSG_TYPE_LEAVE);
|
localState.getGroupId(), localState.getSessionId());
|
||||||
msg.put(SESSION_ID, localState.getSessionId());
|
|
||||||
msg.put(GROUP_ID, localState.getGroupId());
|
|
||||||
messages = Collections.singletonList(msg);
|
messages = Collections.singletonList(msg);
|
||||||
logLocalAction(currentState, localState, msg);
|
logLocalAction(currentState, localState, msg);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
throw new IllegalArgumentException("Unknown Local Action");
|
throw new IllegalArgumentException("Unknown Local Action");
|
||||||
}
|
}
|
||||||
return new StateUpdate<InviteeSessionState, BdfDictionary>(false,
|
return new StateUpdate<InviteeSessionState, BaseMessage>(false,
|
||||||
false, localState, messages, events);
|
false, localState, messages, events);
|
||||||
} catch (FormatException e) {
|
} catch (FormatException e) {
|
||||||
throw new IllegalArgumentException(e);
|
throw new IllegalArgumentException(e);
|
||||||
@@ -110,17 +103,16 @@ public class InviteeEngine
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public StateUpdate<InviteeSessionState, BdfDictionary> onMessageReceived(
|
public StateUpdate<InviteeSessionState, BaseMessage> onMessageReceived(
|
||||||
InviteeSessionState localState, BdfDictionary msg) {
|
InviteeSessionState localState, BaseMessage msg) {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
State currentState = localState.getState();
|
State currentState = localState.getState();
|
||||||
long type = msg.getLong(TYPE);
|
Action action = Action.getRemote(msg.getType());
|
||||||
Action action = Action.getRemote(type);
|
|
||||||
State nextState = currentState.next(action);
|
State nextState = currentState.next(action);
|
||||||
localState.setState(nextState);
|
localState.setState(nextState);
|
||||||
|
|
||||||
logMessageReceived(currentState, nextState, type, msg);
|
logMessageReceived(currentState, nextState, msg.getType(), msg);
|
||||||
|
|
||||||
if (nextState == ERROR) {
|
if (nextState == ERROR) {
|
||||||
if (currentState != ERROR) {
|
if (currentState != ERROR) {
|
||||||
@@ -130,7 +122,7 @@ public class InviteeEngine
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
List<BdfDictionary> messages = Collections.emptyList();
|
List<BaseMessage> messages = Collections.emptyList();
|
||||||
List<Event> events = Collections.emptyList();
|
List<Event> events = Collections.emptyList();
|
||||||
boolean deleteMsg = false;
|
boolean deleteMsg = false;
|
||||||
|
|
||||||
@@ -164,7 +156,7 @@ public class InviteeEngine
|
|||||||
else {
|
else {
|
||||||
throw new IllegalArgumentException("Bad state");
|
throw new IllegalArgumentException("Bad state");
|
||||||
}
|
}
|
||||||
return new StateUpdate<InviteeSessionState, BdfDictionary>(deleteMsg,
|
return new StateUpdate<InviteeSessionState, BaseMessage>(deleteMsg,
|
||||||
false, localState, messages, events);
|
false, localState, messages, events);
|
||||||
} catch (FormatException e) {
|
} catch (FormatException e) {
|
||||||
throw new IllegalArgumentException(e);
|
throw new IllegalArgumentException(e);
|
||||||
@@ -172,50 +164,42 @@ public class InviteeEngine
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void logLocalAction(State state,
|
private void logLocalAction(State state,
|
||||||
InviteeSessionState localState, BdfDictionary msg) {
|
InviteeSessionState localState, BaseMessage msg) {
|
||||||
|
|
||||||
if (!LOG.isLoggable(INFO)) return;
|
if (!LOG.isLoggable(INFO)) return;
|
||||||
|
|
||||||
String a = "response";
|
String a = "response";
|
||||||
if (msg.getLong(TYPE, -1L) == SHARE_MSG_TYPE_LEAVE) a = "leave";
|
if (msg.getType() == SHARE_MSG_TYPE_LEAVE) a = "leave";
|
||||||
|
|
||||||
try {
|
LOG.info("Sending " + a + " in state " + state.name() +
|
||||||
LOG.info("Sending " + a + " in state " + state.name() +
|
" with session ID " +
|
||||||
" with session ID " +
|
msg.getSessionId().hashCode() + " in group " +
|
||||||
Arrays.hashCode(msg.getRaw(SESSION_ID)) + " in group " +
|
msg.getGroupId().hashCode() + ". " +
|
||||||
Arrays.hashCode(msg.getRaw(GROUP_ID)) + ". " +
|
"Moving on to state " + localState.getState().name()
|
||||||
"Moving on to state " + localState.getState().name()
|
);
|
||||||
);
|
|
||||||
} catch (FormatException e) {
|
|
||||||
if (LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void logMessageReceived(State currentState, State nextState,
|
private void logMessageReceived(State currentState, State nextState,
|
||||||
long type, BdfDictionary msg) {
|
long type, BaseMessage msg) {
|
||||||
|
|
||||||
if (!LOG.isLoggable(INFO)) return;
|
if (!LOG.isLoggable(INFO)) return;
|
||||||
|
|
||||||
try {
|
String t = "unknown";
|
||||||
String t = "unknown";
|
if (type == SHARE_MSG_TYPE_INVITATION) t = "INVITE";
|
||||||
if (type == SHARE_MSG_TYPE_INVITATION) t = "INVITE";
|
else if (type == SHARE_MSG_TYPE_LEAVE) t = "LEAVE";
|
||||||
else if (type == SHARE_MSG_TYPE_LEAVE) t = "LEAVE";
|
else if (type == SHARE_MSG_TYPE_ABORT) t = "ABORT";
|
||||||
else if (type == SHARE_MSG_TYPE_ABORT) t = "ABORT";
|
|
||||||
|
|
||||||
LOG.info("Received " + t + " in state " + currentState.name() +
|
LOG.info("Received " + t + " in state " + currentState.name() +
|
||||||
" with session ID " +
|
" with session ID " +
|
||||||
Arrays.hashCode(msg.getRaw(SESSION_ID)) + " in group " +
|
msg.getSessionId().hashCode() + " in group " +
|
||||||
Arrays.hashCode(msg.getRaw(GROUP_ID)) + ". " +
|
msg.getGroupId().hashCode() + ". " +
|
||||||
"Moving on to state " + nextState.name()
|
"Moving on to state " + nextState.name()
|
||||||
);
|
);
|
||||||
} catch (FormatException e) {
|
|
||||||
if (LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public StateUpdate<InviteeSessionState, BdfDictionary> onMessageDelivered(
|
public StateUpdate<InviteeSessionState, BaseMessage> onMessageDelivered(
|
||||||
InviteeSessionState localState, BdfDictionary delivered) {
|
InviteeSessionState localState, BaseMessage delivered) {
|
||||||
try {
|
try {
|
||||||
return noUpdate(localState, false);
|
return noUpdate(localState, false);
|
||||||
} catch (FormatException e) {
|
} catch (FormatException e) {
|
||||||
@@ -224,7 +208,7 @@ public class InviteeEngine
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private StateUpdate<InviteeSessionState, BdfDictionary> abortSession(
|
private StateUpdate<InviteeSessionState, BaseMessage> abortSession(
|
||||||
State currentState, InviteeSessionState localState)
|
State currentState, InviteeSessionState localState)
|
||||||
throws FormatException {
|
throws FormatException {
|
||||||
|
|
||||||
@@ -233,25 +217,23 @@ public class InviteeEngine
|
|||||||
localState.getSessionId().hashCode() +
|
localState.getSessionId().hashCode() +
|
||||||
" in state " + currentState.name());
|
" in state " + currentState.name());
|
||||||
}
|
}
|
||||||
|
|
||||||
localState.setState(ERROR);
|
localState.setState(ERROR);
|
||||||
BdfDictionary msg = new BdfDictionary();
|
BaseMessage msg =
|
||||||
msg.put(TYPE, SHARE_MSG_TYPE_ABORT);
|
new SimpleMessage(SHARE_MSG_TYPE_ABORT, localState.getGroupId(),
|
||||||
msg.put(SESSION_ID, localState.getSessionId());
|
localState.getSessionId());
|
||||||
msg.put(GROUP_ID, localState.getGroupId());
|
List<BaseMessage> messages = Collections.singletonList(msg);
|
||||||
List<BdfDictionary> messages = Collections.singletonList(msg);
|
|
||||||
|
|
||||||
List<Event> events = Collections.emptyList();
|
List<Event> events = Collections.emptyList();
|
||||||
|
|
||||||
return new StateUpdate<InviteeSessionState, BdfDictionary>(false, false,
|
return new StateUpdate<InviteeSessionState, BaseMessage>(false, false,
|
||||||
localState, messages, events);
|
localState, messages, events);
|
||||||
}
|
}
|
||||||
|
|
||||||
private StateUpdate<InviteeSessionState, BdfDictionary> noUpdate(
|
private StateUpdate<InviteeSessionState, BaseMessage> noUpdate(
|
||||||
InviteeSessionState localState, boolean delete) throws FormatException {
|
InviteeSessionState localState, boolean delete) throws FormatException {
|
||||||
|
|
||||||
return new StateUpdate<InviteeSessionState, BdfDictionary>(delete, false,
|
return new StateUpdate<InviteeSessionState, BaseMessage>(delete, false,
|
||||||
localState, Collections.<BdfDictionary>emptyList(),
|
localState, Collections.<BaseMessage>emptyList(),
|
||||||
Collections.<Event>emptyList());
|
Collections.<Event>emptyList());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,33 +3,27 @@ package org.briarproject.forum;
|
|||||||
import org.briarproject.api.FormatException;
|
import org.briarproject.api.FormatException;
|
||||||
import org.briarproject.api.clients.ProtocolEngine;
|
import org.briarproject.api.clients.ProtocolEngine;
|
||||||
import org.briarproject.api.contact.ContactId;
|
import org.briarproject.api.contact.ContactId;
|
||||||
import org.briarproject.api.data.BdfDictionary;
|
|
||||||
import org.briarproject.api.event.Event;
|
import org.briarproject.api.event.Event;
|
||||||
import org.briarproject.api.event.ForumInvitationResponseReceivedEvent;
|
import org.briarproject.api.event.ForumInvitationResponseReceivedEvent;
|
||||||
import static org.briarproject.forum.SharerSessionState.Action;
|
|
||||||
|
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
import static java.util.logging.Level.INFO;
|
import static java.util.logging.Level.INFO;
|
||||||
import static java.util.logging.Level.WARNING;
|
import static java.util.logging.Level.WARNING;
|
||||||
import static org.briarproject.api.forum.ForumConstants.FORUM_NAME;
|
|
||||||
import static org.briarproject.api.forum.ForumConstants.FORUM_SALT;
|
|
||||||
import static org.briarproject.api.forum.ForumConstants.GROUP_ID;
|
|
||||||
import static org.briarproject.api.forum.ForumConstants.INVITATION_MSG;
|
|
||||||
import static org.briarproject.api.forum.ForumConstants.SESSION_ID;
|
|
||||||
import static org.briarproject.api.forum.ForumConstants.SHARE_MSG_TYPE_ABORT;
|
import static org.briarproject.api.forum.ForumConstants.SHARE_MSG_TYPE_ABORT;
|
||||||
import static org.briarproject.api.forum.ForumConstants.SHARE_MSG_TYPE_ACCEPT;
|
import static org.briarproject.api.forum.ForumConstants.SHARE_MSG_TYPE_ACCEPT;
|
||||||
import static org.briarproject.api.forum.ForumConstants.SHARE_MSG_TYPE_DECLINE;
|
import static org.briarproject.api.forum.ForumConstants.SHARE_MSG_TYPE_DECLINE;
|
||||||
import static org.briarproject.api.forum.ForumConstants.SHARE_MSG_TYPE_INVITATION;
|
|
||||||
import static org.briarproject.api.forum.ForumConstants.SHARE_MSG_TYPE_LEAVE;
|
import static org.briarproject.api.forum.ForumConstants.SHARE_MSG_TYPE_LEAVE;
|
||||||
import static org.briarproject.api.forum.ForumConstants.TASK_ADD_FORUM_TO_LIST_TO_BE_SHARED_BY_US;
|
import static org.briarproject.api.forum.ForumConstants.TASK_ADD_FORUM_TO_LIST_TO_BE_SHARED_BY_US;
|
||||||
import static org.briarproject.api.forum.ForumConstants.TASK_REMOVE_FORUM_FROM_LIST_TO_BE_SHARED_BY_US;
|
import static org.briarproject.api.forum.ForumConstants.TASK_REMOVE_FORUM_FROM_LIST_TO_BE_SHARED_BY_US;
|
||||||
import static org.briarproject.api.forum.ForumConstants.TASK_SHARE_FORUM;
|
import static org.briarproject.api.forum.ForumConstants.TASK_SHARE_FORUM;
|
||||||
import static org.briarproject.api.forum.ForumConstants.TASK_UNSHARE_FORUM_SHARED_BY_US;
|
import static org.briarproject.api.forum.ForumConstants.TASK_UNSHARE_FORUM_SHARED_BY_US;
|
||||||
import static org.briarproject.api.forum.ForumConstants.TYPE;
|
import static org.briarproject.api.forum.ForumSharingMessage.BaseMessage;
|
||||||
|
import static org.briarproject.api.forum.ForumSharingMessage.Invitation;
|
||||||
|
import static org.briarproject.api.forum.ForumSharingMessage.SimpleMessage;
|
||||||
|
import static org.briarproject.forum.SharerSessionState.Action;
|
||||||
import static org.briarproject.forum.SharerSessionState.Action.LOCAL_ABORT;
|
import static org.briarproject.forum.SharerSessionState.Action.LOCAL_ABORT;
|
||||||
import static org.briarproject.forum.SharerSessionState.Action.LOCAL_INVITATION;
|
import static org.briarproject.forum.SharerSessionState.Action.LOCAL_INVITATION;
|
||||||
import static org.briarproject.forum.SharerSessionState.Action.LOCAL_LEAVE;
|
import static org.briarproject.forum.SharerSessionState.Action.LOCAL_LEAVE;
|
||||||
@@ -42,13 +36,13 @@ import static org.briarproject.forum.SharerSessionState.State.FINISHED;
|
|||||||
import static org.briarproject.forum.SharerSessionState.State.LEFT;
|
import static org.briarproject.forum.SharerSessionState.State.LEFT;
|
||||||
|
|
||||||
public class SharerEngine
|
public class SharerEngine
|
||||||
implements ProtocolEngine<Action, SharerSessionState, BdfDictionary> {
|
implements ProtocolEngine<Action, SharerSessionState, BaseMessage> {
|
||||||
|
|
||||||
private static final Logger LOG =
|
private static final Logger LOG =
|
||||||
Logger.getLogger(SharerEngine.class.getName());
|
Logger.getLogger(SharerEngine.class.getName());
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public StateUpdate<SharerSessionState, BdfDictionary> onLocalAction(
|
public StateUpdate<SharerSessionState, BaseMessage> onLocalAction(
|
||||||
SharerSessionState localState, Action action) {
|
SharerSessionState localState, Action action) {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@@ -67,19 +61,13 @@ public class SharerEngine
|
|||||||
}
|
}
|
||||||
return noUpdate(localState, true);
|
return noUpdate(localState, true);
|
||||||
}
|
}
|
||||||
List<BdfDictionary> messages;
|
List<BaseMessage> messages;
|
||||||
List<Event> events = Collections.emptyList();
|
List<Event> events = Collections.emptyList();
|
||||||
|
|
||||||
if (action == LOCAL_INVITATION) {
|
if (action == LOCAL_INVITATION) {
|
||||||
BdfDictionary msg = new BdfDictionary();
|
BaseMessage msg = new Invitation(localState.getGroupId(),
|
||||||
msg.put(TYPE, SHARE_MSG_TYPE_INVITATION);
|
localState.getSessionId(), localState.getForumName(),
|
||||||
msg.put(SESSION_ID, localState.getSessionId());
|
localState.getForumSalt(), localState.getMessage());
|
||||||
msg.put(GROUP_ID, localState.getGroupId());
|
|
||||||
msg.put(FORUM_NAME, localState.getForumName());
|
|
||||||
msg.put(FORUM_SALT, localState.getForumSalt());
|
|
||||||
if (localState.getMessage() != null) {
|
|
||||||
msg.put(INVITATION_MSG, localState.getMessage());
|
|
||||||
}
|
|
||||||
messages = Collections.singletonList(msg);
|
messages = Collections.singletonList(msg);
|
||||||
logLocalAction(currentState, nextState, msg);
|
logLocalAction(currentState, nextState, msg);
|
||||||
|
|
||||||
@@ -87,17 +75,15 @@ public class SharerEngine
|
|||||||
localState.setTask(TASK_ADD_FORUM_TO_LIST_TO_BE_SHARED_BY_US);
|
localState.setTask(TASK_ADD_FORUM_TO_LIST_TO_BE_SHARED_BY_US);
|
||||||
}
|
}
|
||||||
else if (action == LOCAL_LEAVE) {
|
else if (action == LOCAL_LEAVE) {
|
||||||
BdfDictionary msg = new BdfDictionary();
|
BaseMessage msg = new SimpleMessage(SHARE_MSG_TYPE_LEAVE,
|
||||||
msg.put(TYPE, SHARE_MSG_TYPE_LEAVE);
|
localState.getGroupId(), localState.getSessionId());
|
||||||
msg.put(SESSION_ID, localState.getSessionId());
|
|
||||||
msg.put(GROUP_ID, localState.getGroupId());
|
|
||||||
messages = Collections.singletonList(msg);
|
messages = Collections.singletonList(msg);
|
||||||
logLocalAction(currentState, nextState, msg);
|
logLocalAction(currentState, nextState, msg);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
throw new IllegalArgumentException("Unknown Local Action");
|
throw new IllegalArgumentException("Unknown Local Action");
|
||||||
}
|
}
|
||||||
return new StateUpdate<SharerSessionState, BdfDictionary>(false,
|
return new StateUpdate<SharerSessionState, BaseMessage>(false,
|
||||||
false, localState, messages, events);
|
false, localState, messages, events);
|
||||||
} catch (FormatException e) {
|
} catch (FormatException e) {
|
||||||
throw new IllegalArgumentException(e);
|
throw new IllegalArgumentException(e);
|
||||||
@@ -105,17 +91,16 @@ public class SharerEngine
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public StateUpdate<SharerSessionState, BdfDictionary> onMessageReceived(
|
public StateUpdate<SharerSessionState, BaseMessage> onMessageReceived(
|
||||||
SharerSessionState localState, BdfDictionary msg) {
|
SharerSessionState localState, BaseMessage msg) {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
State currentState = localState.getState();
|
State currentState = localState.getState();
|
||||||
long type = msg.getLong(TYPE);
|
Action action = Action.getRemote(msg.getType());
|
||||||
Action action = Action.getRemote(type);
|
|
||||||
State nextState = currentState.next(action);
|
State nextState = currentState.next(action);
|
||||||
localState.setState(nextState);
|
localState.setState(nextState);
|
||||||
|
|
||||||
logMessageReceived(currentState, nextState, type, msg);
|
logMessageReceived(currentState, nextState, msg.getType(), msg);
|
||||||
|
|
||||||
if (nextState == ERROR) {
|
if (nextState == ERROR) {
|
||||||
if (currentState != ERROR) {
|
if (currentState != ERROR) {
|
||||||
@@ -124,7 +109,7 @@ public class SharerEngine
|
|||||||
return noUpdate(localState, true);
|
return noUpdate(localState, true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
List<BdfDictionary> messages = Collections.emptyList();
|
List<BaseMessage> messages = Collections.emptyList();
|
||||||
List<Event> events = Collections.emptyList();
|
List<Event> events = Collections.emptyList();
|
||||||
boolean deleteMsg = false;
|
boolean deleteMsg = false;
|
||||||
|
|
||||||
@@ -157,7 +142,7 @@ public class SharerEngine
|
|||||||
else {
|
else {
|
||||||
throw new IllegalArgumentException("Bad state");
|
throw new IllegalArgumentException("Bad state");
|
||||||
}
|
}
|
||||||
return new StateUpdate<SharerSessionState, BdfDictionary>(deleteMsg,
|
return new StateUpdate<SharerSessionState, BaseMessage>(deleteMsg,
|
||||||
false, localState, messages, events);
|
false, localState, messages, events);
|
||||||
} catch (FormatException e) {
|
} catch (FormatException e) {
|
||||||
throw new IllegalArgumentException(e);
|
throw new IllegalArgumentException(e);
|
||||||
@@ -165,51 +150,43 @@ public class SharerEngine
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void logLocalAction(State currentState, State nextState,
|
private void logLocalAction(State currentState, State nextState,
|
||||||
BdfDictionary msg) {
|
BaseMessage msg) {
|
||||||
|
|
||||||
if (!LOG.isLoggable(INFO)) return;
|
if (!LOG.isLoggable(INFO)) return;
|
||||||
|
|
||||||
String a = "invitation";
|
String a = "invitation";
|
||||||
if (msg.getLong(TYPE, -1L) == SHARE_MSG_TYPE_LEAVE) a = "leave";
|
if (msg.getType() == SHARE_MSG_TYPE_LEAVE) a = "leave";
|
||||||
|
|
||||||
try {
|
LOG.info("Sending " + a + " in state " + currentState.name() +
|
||||||
LOG.info("Sending " + a + " in state " + currentState.name() +
|
" with session ID " +
|
||||||
" with session ID " +
|
msg.getSessionId().hashCode() + " in group " +
|
||||||
Arrays.hashCode(msg.getRaw(SESSION_ID)) + " in group " +
|
msg.getGroupId().hashCode() + ". " +
|
||||||
Arrays.hashCode(msg.getRaw(GROUP_ID)) + ". " +
|
"Moving on to state " + nextState.name()
|
||||||
"Moving on to state " + nextState.name()
|
);
|
||||||
);
|
|
||||||
} catch (FormatException e) {
|
|
||||||
if (LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void logMessageReceived(State currentState, State nextState,
|
private void logMessageReceived(State currentState, State nextState,
|
||||||
long type, BdfDictionary msg) {
|
long type, BaseMessage msg) {
|
||||||
|
|
||||||
if (!LOG.isLoggable(INFO)) return;
|
if (!LOG.isLoggable(INFO)) return;
|
||||||
|
|
||||||
try {
|
String t = "unknown";
|
||||||
String t = "unknown";
|
if (type == SHARE_MSG_TYPE_ACCEPT) t = "ACCEPT";
|
||||||
if (type == SHARE_MSG_TYPE_ACCEPT) t = "ACCEPT";
|
else if (type == SHARE_MSG_TYPE_DECLINE) t = "DECLINE";
|
||||||
else if (type == SHARE_MSG_TYPE_DECLINE) t = "DECLINE";
|
else if (type == SHARE_MSG_TYPE_LEAVE) t = "LEAVE";
|
||||||
else if (type == SHARE_MSG_TYPE_LEAVE) t = "LEAVE";
|
else if (type == SHARE_MSG_TYPE_ABORT) t = "ABORT";
|
||||||
else if (type == SHARE_MSG_TYPE_ABORT) t = "ABORT";
|
|
||||||
|
|
||||||
LOG.info("Received " + t + " in state " + currentState.name() +
|
LOG.info("Received " + t + " in state " + currentState.name() +
|
||||||
" with session ID " +
|
" with session ID " +
|
||||||
Arrays.hashCode(msg.getRaw(SESSION_ID)) + " in group " +
|
msg.getSessionId().hashCode() + " in group " +
|
||||||
Arrays.hashCode(msg.getRaw(GROUP_ID)) + ". " +
|
msg.getGroupId().hashCode() + ". " +
|
||||||
"Moving on to state " + nextState.name()
|
"Moving on to state " + nextState.name()
|
||||||
);
|
);
|
||||||
} catch (FormatException e) {
|
|
||||||
if (LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public StateUpdate<SharerSessionState, BdfDictionary> onMessageDelivered(
|
public StateUpdate<SharerSessionState, BaseMessage> onMessageDelivered(
|
||||||
SharerSessionState localState, BdfDictionary delivered) {
|
SharerSessionState localState, BaseMessage delivered) {
|
||||||
try {
|
try {
|
||||||
return noUpdate(localState, false);
|
return noUpdate(localState, false);
|
||||||
} catch (FormatException e) {
|
} catch (FormatException e) {
|
||||||
@@ -218,7 +195,7 @@ public class SharerEngine
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private StateUpdate<SharerSessionState, BdfDictionary> abortSession(
|
private StateUpdate<SharerSessionState, BaseMessage> abortSession(
|
||||||
State currentState, SharerSessionState localState)
|
State currentState, SharerSessionState localState)
|
||||||
throws FormatException {
|
throws FormatException {
|
||||||
|
|
||||||
@@ -229,24 +206,22 @@ public class SharerEngine
|
|||||||
}
|
}
|
||||||
|
|
||||||
localState.setState(ERROR);
|
localState.setState(ERROR);
|
||||||
BdfDictionary msg = new BdfDictionary();
|
BaseMessage msg = new SimpleMessage(SHARE_MSG_TYPE_ABORT,
|
||||||
msg.put(TYPE, SHARE_MSG_TYPE_ABORT);
|
localState.getGroupId(), localState.getSessionId());
|
||||||
msg.put(SESSION_ID, localState.getSessionId());
|
List<BaseMessage> messages = Collections.singletonList(msg);
|
||||||
msg.put(GROUP_ID, localState.getGroupId());
|
|
||||||
List<BdfDictionary> messages = Collections.singletonList(msg);
|
|
||||||
|
|
||||||
List<Event> events = Collections.emptyList();
|
List<Event> events = Collections.emptyList();
|
||||||
|
|
||||||
return new StateUpdate<SharerSessionState, BdfDictionary>(false, false,
|
return new StateUpdate<SharerSessionState, BaseMessage>(false, false,
|
||||||
localState, messages, events);
|
localState, messages, events);
|
||||||
}
|
}
|
||||||
|
|
||||||
private StateUpdate<SharerSessionState, BdfDictionary> noUpdate(
|
private StateUpdate<SharerSessionState, BaseMessage> noUpdate(
|
||||||
SharerSessionState localState, boolean delete)
|
SharerSessionState localState, boolean delete)
|
||||||
throws FormatException {
|
throws FormatException {
|
||||||
|
|
||||||
return new StateUpdate<SharerSessionState, BdfDictionary>(delete, false,
|
return new StateUpdate<SharerSessionState, BaseMessage>(delete, false,
|
||||||
localState, Collections.<BdfDictionary>emptyList(),
|
localState, Collections.<BaseMessage>emptyList(),
|
||||||
Collections.<Event>emptyList());
|
Collections.<Event>emptyList());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user