mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-16 12:49:55 +01:00
Use dedicated Enum for protocol Actions
This commit is contained in:
@@ -260,17 +260,15 @@ class ForumSharingManagerImpl extends BdfIncomingMessageHook
|
|||||||
SharerSessionState localState =
|
SharerSessionState localState =
|
||||||
initializeSharerState(txn, f, contactId);
|
initializeSharerState(txn, f, contactId);
|
||||||
|
|
||||||
// define action
|
// add invitation message to local state to be available for engine
|
||||||
BdfDictionary localAction = new BdfDictionary();
|
|
||||||
localAction.put(TYPE, SHARE_MSG_TYPE_INVITATION);
|
|
||||||
if (!StringUtils.isNullOrEmpty(msg)) {
|
if (!StringUtils.isNullOrEmpty(msg)) {
|
||||||
localAction.put(INVITATION_MSG, msg);
|
localState.setMessage(msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
// start engine and process its state update
|
// start engine and process its state update
|
||||||
SharerEngine engine = new SharerEngine();
|
SharerEngine engine = new SharerEngine();
|
||||||
processSharerStateUpdate(txn, null,
|
processSharerStateUpdate(txn, null,
|
||||||
engine.onLocalAction(localState, localAction));
|
engine.onLocalAction(localState, Action.LOCAL_INVITATION));
|
||||||
|
|
||||||
txn.setComplete();
|
txn.setComplete();
|
||||||
} catch (FormatException e) {
|
} catch (FormatException e) {
|
||||||
@@ -290,11 +288,11 @@ class ForumSharingManagerImpl extends BdfIncomingMessageHook
|
|||||||
InviteeSessionState localState = getSessionStateForResponse(txn, f);
|
InviteeSessionState localState = getSessionStateForResponse(txn, f);
|
||||||
|
|
||||||
// define action
|
// define action
|
||||||
BdfDictionary localAction = new BdfDictionary();
|
InviteeSessionState.Action localAction;
|
||||||
if (accept) {
|
if (accept) {
|
||||||
localAction.put(TYPE, SHARE_MSG_TYPE_ACCEPT);
|
localAction = InviteeSessionState.Action.LOCAL_ACCEPT;
|
||||||
} else {
|
} else {
|
||||||
localAction.put(TYPE, SHARE_MSG_TYPE_DECLINE);
|
localAction = InviteeSessionState.Action.LOCAL_DECLINE;
|
||||||
}
|
}
|
||||||
|
|
||||||
// start engine and process its state update
|
// start engine and process its state update
|
||||||
@@ -812,13 +810,14 @@ class ForumSharingManagerImpl extends BdfIncomingMessageHook
|
|||||||
throws DbException, FormatException {
|
throws DbException, FormatException {
|
||||||
|
|
||||||
ForumSharingSessionState state = getSessionStateForLeaving(txn, f, c);
|
ForumSharingSessionState state = getSessionStateForLeaving(txn, f, c);
|
||||||
BdfDictionary action = new BdfDictionary();
|
|
||||||
action.put(TYPE, SHARE_MSG_TYPE_LEAVE);
|
|
||||||
if (state instanceof SharerSessionState) {
|
if (state instanceof SharerSessionState) {
|
||||||
|
Action action = Action.LOCAL_LEAVE;
|
||||||
SharerEngine engine = new SharerEngine();
|
SharerEngine engine = new SharerEngine();
|
||||||
processSharerStateUpdate(txn, null,
|
processSharerStateUpdate(txn, null,
|
||||||
engine.onLocalAction((SharerSessionState) state, action));
|
engine.onLocalAction((SharerSessionState) state, action));
|
||||||
} else {
|
} else {
|
||||||
|
InviteeSessionState.Action action =
|
||||||
|
InviteeSessionState.Action.LOCAL_LEAVE;
|
||||||
InviteeEngine engine = new InviteeEngine(forumFactory);
|
InviteeEngine engine = new InviteeEngine(forumFactory);
|
||||||
processInviteeStateUpdate(txn, null,
|
processInviteeStateUpdate(txn, null,
|
||||||
engine.onLocalAction((InviteeSessionState) state, action));
|
engine.onLocalAction((InviteeSessionState) state, action));
|
||||||
|
|||||||
@@ -42,7 +42,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<BdfDictionary, InviteeSessionState, BdfDictionary> {
|
implements ProtocolEngine<Action, InviteeSessionState, BdfDictionary> {
|
||||||
|
|
||||||
private final ForumFactory forumFactory;
|
private final ForumFactory forumFactory;
|
||||||
private static final Logger LOG =
|
private static final Logger LOG =
|
||||||
@@ -54,12 +54,10 @@ public class InviteeEngine
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public StateUpdate<InviteeSessionState, BdfDictionary> onLocalAction(
|
public StateUpdate<InviteeSessionState, BdfDictionary> onLocalAction(
|
||||||
InviteeSessionState localState, BdfDictionary localAction) {
|
InviteeSessionState localState, Action action) {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
State currentState = localState.getState();
|
State currentState = localState.getState();
|
||||||
long type = localAction.getLong(TYPE);
|
|
||||||
Action action = Action.getLocal(type);
|
|
||||||
State nextState = currentState.next(action);
|
State nextState = currentState.next(action);
|
||||||
localState.setState(nextState);
|
localState.setState(nextState);
|
||||||
|
|
||||||
|
|||||||
@@ -8,8 +8,6 @@ import org.briarproject.api.sync.MessageId;
|
|||||||
|
|
||||||
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.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_DECLINE;
|
|
||||||
import static org.briarproject.api.forum.ForumConstants.SHARE_MSG_TYPE_INVITATION;
|
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.STATE;
|
import static org.briarproject.api.forum.ForumConstants.STATE;
|
||||||
@@ -111,14 +109,6 @@ public class InviteeSessionState extends ForumSharingSessionState {
|
|||||||
REMOTE_LEAVE,
|
REMOTE_LEAVE,
|
||||||
REMOTE_ABORT;
|
REMOTE_ABORT;
|
||||||
|
|
||||||
public static Action getLocal(long type) {
|
|
||||||
if (type == SHARE_MSG_TYPE_ACCEPT) return LOCAL_ACCEPT;
|
|
||||||
if (type == SHARE_MSG_TYPE_DECLINE) return LOCAL_DECLINE;
|
|
||||||
if (type == SHARE_MSG_TYPE_LEAVE) return LOCAL_LEAVE;
|
|
||||||
if (type == SHARE_MSG_TYPE_ABORT) return LOCAL_ABORT;
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static Action getRemote(long type) {
|
public static Action getRemote(long type) {
|
||||||
if (type == SHARE_MSG_TYPE_INVITATION) return REMOTE_INVITATION;
|
if (type == SHARE_MSG_TYPE_INVITATION) return REMOTE_INVITATION;
|
||||||
if (type == SHARE_MSG_TYPE_LEAVE) return REMOTE_LEAVE;
|
if (type == SHARE_MSG_TYPE_LEAVE) return REMOTE_LEAVE;
|
||||||
|
|||||||
@@ -42,19 +42,17 @@ 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<BdfDictionary, SharerSessionState, BdfDictionary> {
|
implements ProtocolEngine<Action, SharerSessionState, BdfDictionary> {
|
||||||
|
|
||||||
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, BdfDictionary> onLocalAction(
|
||||||
SharerSessionState localState, BdfDictionary localAction) {
|
SharerSessionState localState, Action action) {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
State currentState = localState.getState();
|
State currentState = localState.getState();
|
||||||
long type = localAction.getLong(TYPE);
|
|
||||||
Action action = Action.getLocal(type);
|
|
||||||
State nextState = currentState.next(action);
|
State nextState = currentState.next(action);
|
||||||
localState.setState(nextState);
|
localState.setState(nextState);
|
||||||
|
|
||||||
@@ -79,9 +77,8 @@ public class SharerEngine
|
|||||||
msg.put(GROUP_ID, localState.getGroupId());
|
msg.put(GROUP_ID, localState.getGroupId());
|
||||||
msg.put(FORUM_NAME, localState.getForumName());
|
msg.put(FORUM_NAME, localState.getForumName());
|
||||||
msg.put(FORUM_SALT, localState.getForumSalt());
|
msg.put(FORUM_SALT, localState.getForumSalt());
|
||||||
if (localAction.containsKey(INVITATION_MSG)) {
|
if (localState.getMessage() != null) {
|
||||||
msg.put(INVITATION_MSG,
|
msg.put(INVITATION_MSG, localState.getMessage());
|
||||||
localAction.getString(INVITATION_MSG));
|
|
||||||
}
|
}
|
||||||
messages = Collections.singletonList(msg);
|
messages = Collections.singletonList(msg);
|
||||||
logLocalAction(currentState, nextState, msg);
|
logLocalAction(currentState, nextState, msg);
|
||||||
|
|||||||
@@ -10,7 +10,6 @@ import static org.briarproject.api.forum.ForumConstants.IS_SHARER;
|
|||||||
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.STATE;
|
import static org.briarproject.api.forum.ForumConstants.STATE;
|
||||||
import static org.briarproject.forum.SharerSessionState.Action.LOCAL_INVITATION;
|
import static org.briarproject.forum.SharerSessionState.Action.LOCAL_INVITATION;
|
||||||
@@ -23,6 +22,7 @@ import static org.briarproject.forum.SharerSessionState.Action.REMOTE_LEAVE;
|
|||||||
public class SharerSessionState extends ForumSharingSessionState {
|
public class SharerSessionState extends ForumSharingSessionState {
|
||||||
|
|
||||||
private State state;
|
private State state;
|
||||||
|
private String msg = null;
|
||||||
|
|
||||||
public SharerSessionState(SessionId sessionId, MessageId storageId,
|
public SharerSessionState(SessionId sessionId, MessageId storageId,
|
||||||
GroupId groupId, State state, ContactId contactId, GroupId forumId,
|
GroupId groupId, State state, ContactId contactId, GroupId forumId,
|
||||||
@@ -48,6 +48,14 @@ public class SharerSessionState extends ForumSharingSessionState {
|
|||||||
return state;
|
return state;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setMessage(String msg) {
|
||||||
|
this.msg = msg;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getMessage() {
|
||||||
|
return this.msg;
|
||||||
|
}
|
||||||
|
|
||||||
public enum State {
|
public enum State {
|
||||||
ERROR(0),
|
ERROR(0),
|
||||||
PREPARE_INVITATION(1) {
|
PREPARE_INVITATION(1) {
|
||||||
@@ -111,13 +119,6 @@ public class SharerSessionState extends ForumSharingSessionState {
|
|||||||
REMOTE_LEAVE,
|
REMOTE_LEAVE,
|
||||||
REMOTE_ABORT;
|
REMOTE_ABORT;
|
||||||
|
|
||||||
public static Action getLocal(long type) {
|
|
||||||
if (type == SHARE_MSG_TYPE_INVITATION) return LOCAL_INVITATION;
|
|
||||||
if (type == SHARE_MSG_TYPE_LEAVE) return LOCAL_LEAVE;
|
|
||||||
if (type == SHARE_MSG_TYPE_ABORT) return LOCAL_ABORT;
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static Action getRemote(long type) {
|
public static Action getRemote(long type) {
|
||||||
if (type == SHARE_MSG_TYPE_ACCEPT) return REMOTE_ACCEPT;
|
if (type == SHARE_MSG_TYPE_ACCEPT) return REMOTE_ACCEPT;
|
||||||
if (type == SHARE_MSG_TYPE_DECLINE) return REMOTE_DECLINE;
|
if (type == SHARE_MSG_TYPE_DECLINE) return REMOTE_DECLINE;
|
||||||
|
|||||||
Reference in New Issue
Block a user