Use dedicated Enum for protocol Actions

This commit is contained in:
Torsten Grote
2016-05-12 11:51:17 -03:00
parent d2722eed92
commit 9532a60f43
5 changed files with 24 additions and 39 deletions

View File

@@ -260,17 +260,15 @@ class ForumSharingManagerImpl extends BdfIncomingMessageHook
SharerSessionState localState =
initializeSharerState(txn, f, contactId);
// define action
BdfDictionary localAction = new BdfDictionary();
localAction.put(TYPE, SHARE_MSG_TYPE_INVITATION);
// add invitation message to local state to be available for engine
if (!StringUtils.isNullOrEmpty(msg)) {
localAction.put(INVITATION_MSG, msg);
localState.setMessage(msg);
}
// start engine and process its state update
SharerEngine engine = new SharerEngine();
processSharerStateUpdate(txn, null,
engine.onLocalAction(localState, localAction));
engine.onLocalAction(localState, Action.LOCAL_INVITATION));
txn.setComplete();
} catch (FormatException e) {
@@ -290,11 +288,11 @@ class ForumSharingManagerImpl extends BdfIncomingMessageHook
InviteeSessionState localState = getSessionStateForResponse(txn, f);
// define action
BdfDictionary localAction = new BdfDictionary();
InviteeSessionState.Action localAction;
if (accept) {
localAction.put(TYPE, SHARE_MSG_TYPE_ACCEPT);
localAction = InviteeSessionState.Action.LOCAL_ACCEPT;
} else {
localAction.put(TYPE, SHARE_MSG_TYPE_DECLINE);
localAction = InviteeSessionState.Action.LOCAL_DECLINE;
}
// start engine and process its state update
@@ -812,13 +810,14 @@ class ForumSharingManagerImpl extends BdfIncomingMessageHook
throws DbException, FormatException {
ForumSharingSessionState state = getSessionStateForLeaving(txn, f, c);
BdfDictionary action = new BdfDictionary();
action.put(TYPE, SHARE_MSG_TYPE_LEAVE);
if (state instanceof SharerSessionState) {
Action action = Action.LOCAL_LEAVE;
SharerEngine engine = new SharerEngine();
processSharerStateUpdate(txn, null,
engine.onLocalAction((SharerSessionState) state, action));
} else {
InviteeSessionState.Action action =
InviteeSessionState.Action.LOCAL_LEAVE;
InviteeEngine engine = new InviteeEngine(forumFactory);
processInviteeStateUpdate(txn, null,
engine.onLocalAction((InviteeSessionState) state, action));

View File

@@ -42,7 +42,7 @@ import static org.briarproject.forum.InviteeSessionState.State.FINISHED;
import static org.briarproject.forum.InviteeSessionState.State.LEFT;
public class InviteeEngine
implements ProtocolEngine<BdfDictionary, InviteeSessionState, BdfDictionary> {
implements ProtocolEngine<Action, InviteeSessionState, BdfDictionary> {
private final ForumFactory forumFactory;
private static final Logger LOG =
@@ -54,12 +54,10 @@ public class InviteeEngine
@Override
public StateUpdate<InviteeSessionState, BdfDictionary> onLocalAction(
InviteeSessionState localState, BdfDictionary localAction) {
InviteeSessionState localState, Action action) {
try {
State currentState = localState.getState();
long type = localAction.getLong(TYPE);
Action action = Action.getLocal(type);
State nextState = currentState.next(action);
localState.setState(nextState);

View File

@@ -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.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.STATE;
@@ -111,14 +109,6 @@ public class InviteeSessionState extends ForumSharingSessionState {
REMOTE_LEAVE,
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) {
if (type == SHARE_MSG_TYPE_INVITATION) return REMOTE_INVITATION;
if (type == SHARE_MSG_TYPE_LEAVE) return REMOTE_LEAVE;

View File

@@ -42,19 +42,17 @@ import static org.briarproject.forum.SharerSessionState.State.FINISHED;
import static org.briarproject.forum.SharerSessionState.State.LEFT;
public class SharerEngine
implements ProtocolEngine<BdfDictionary, SharerSessionState, BdfDictionary> {
implements ProtocolEngine<Action, SharerSessionState, BdfDictionary> {
private static final Logger LOG =
Logger.getLogger(SharerEngine.class.getName());
@Override
public StateUpdate<SharerSessionState, BdfDictionary> onLocalAction(
SharerSessionState localState, BdfDictionary localAction) {
SharerSessionState localState, Action action) {
try {
State currentState = localState.getState();
long type = localAction.getLong(TYPE);
Action action = Action.getLocal(type);
State nextState = currentState.next(action);
localState.setState(nextState);
@@ -79,9 +77,8 @@ public class SharerEngine
msg.put(GROUP_ID, localState.getGroupId());
msg.put(FORUM_NAME, localState.getForumName());
msg.put(FORUM_SALT, localState.getForumSalt());
if (localAction.containsKey(INVITATION_MSG)) {
msg.put(INVITATION_MSG,
localAction.getString(INVITATION_MSG));
if (localState.getMessage() != null) {
msg.put(INVITATION_MSG, localState.getMessage());
}
messages = Collections.singletonList(msg);
logLocalAction(currentState, nextState, msg);

View File

@@ -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_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.STATE;
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 {
private State state;
private String msg = null;
public SharerSessionState(SessionId sessionId, MessageId storageId,
GroupId groupId, State state, ContactId contactId, GroupId forumId,
@@ -48,6 +48,14 @@ public class SharerSessionState extends ForumSharingSessionState {
return state;
}
public void setMessage(String msg) {
this.msg = msg;
}
public String getMessage() {
return this.msg;
}
public enum State {
ERROR(0),
PREPARE_INVITATION(1) {
@@ -111,13 +119,6 @@ public class SharerSessionState extends ForumSharingSessionState {
REMOTE_LEAVE,
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) {
if (type == SHARE_MSG_TYPE_ACCEPT) return REMOTE_ACCEPT;
if (type == SHARE_MSG_TYPE_DECLINE) return REMOTE_DECLINE;