Update sync protocol

pull/286/head
Alinson S. Xavier 9 years ago
parent 442c3ed78d
commit e6e80b9841

@ -77,7 +77,7 @@ public class ArchiveHabitsCommand extends Command
{ {
JSONObject root = super.toJSON(); JSONObject root = super.toJSON();
JSONObject data = root.getJSONObject("data"); JSONObject data = root.getJSONObject("data");
root.put("command", "ArchiveHabits"); root.put("event", "ArchiveHabits");
data.put("ids", CommandParser.habitListToJSON(habits)); data.put("ids", CommandParser.habitListToJSON(habits));
return root; return root;
} }

@ -98,7 +98,7 @@ public class ChangeHabitColorCommand extends Command
{ {
JSONObject root = super.toJSON(); JSONObject root = super.toJSON();
JSONObject data = root.getJSONObject("data"); JSONObject data = root.getJSONObject("data");
root.put("command", "ChangeHabitColor"); root.put("event", "ChangeHabitColor");
data.put("ids", CommandParser.habitListToJSON(habits)); data.put("ids", CommandParser.habitListToJSON(habits));
data.put("color", newColor); data.put("color", newColor);
return root; return root;

@ -33,7 +33,7 @@ public class CommandParser
{ {
public static Command fromJSON(JSONObject json) throws JSONException public static Command fromJSON(JSONObject json) throws JSONException
{ {
switch(json.getString("command")) switch(json.getString("event"))
{ {
case "ToggleRepetition": case "ToggleRepetition":
return ToggleRepetitionCommand.fromJSON(json); return ToggleRepetitionCommand.fromJSON(json);

@ -86,7 +86,7 @@ public class CreateHabitCommand extends Command
{ {
JSONObject root = super.toJSON(); JSONObject root = super.toJSON();
JSONObject data = root.getJSONObject("data"); JSONObject data = root.getJSONObject("data");
root.put("command", "CreateHabit"); root.put("event", "CreateHabit");
data.put("habit", model.toJSON()); data.put("habit", model.toJSON());
data.put("id", savedId); data.put("id", savedId);
return root; return root;

@ -75,7 +75,7 @@ public class DeleteHabitsCommand extends Command
{ {
JSONObject root = super.toJSON(); JSONObject root = super.toJSON();
JSONObject data = root.getJSONObject("data"); JSONObject data = root.getJSONObject("data");
root.put("command", "DeleteHabits"); root.put("event", "DeleteHabits");
data.put("ids", CommandParser.habitListToJSON(habits)); data.put("ids", CommandParser.habitListToJSON(habits));
return root; return root;
} }

@ -104,7 +104,7 @@ public class EditHabitCommand extends Command
{ {
JSONObject root = super.toJSON(); JSONObject root = super.toJSON();
JSONObject data = root.getJSONObject("data"); JSONObject data = root.getJSONObject("data");
root.put("command", "EditHabit"); root.put("event", "EditHabit");
data.put("id", savedId); data.put("id", savedId);
data.put("params", modified.toJSON()); data.put("params", modified.toJSON());
return root; return root;

@ -63,7 +63,7 @@ public class ToggleRepetitionCommand extends Command
{ {
JSONObject root = super.toJSON(); JSONObject root = super.toJSON();
JSONObject data = root.getJSONObject("data"); JSONObject data = root.getJSONObject("data");
root.put("command", "ToggleRepetition"); root.put("event", "ToggleRepetition");
data.put("habit", habit.getId()); data.put("habit", habit.getId());
data.put("timestamp", timestamp); data.put("timestamp", timestamp);
return root; return root;

@ -73,7 +73,7 @@ public class UnarchiveHabitsCommand extends Command
{ {
JSONObject root = super.toJSON(); JSONObject root = super.toJSON();
JSONObject data = root.getJSONObject("data"); JSONObject data = root.getJSONObject("data");
root.put("command", "UnarchiveHabits"); root.put("event", "UnarchiveHabits");
data.put("ids", CommandParser.habitListToJSON(habits)); data.put("ids", CommandParser.habitListToJSON(habits));
return root; return root;
} }

@ -76,7 +76,7 @@ public class DatabaseHelper
public static String getRandomId() public static String getRandomId()
{ {
return new BigInteger(130, new Random()).toString(32); return new BigInteger(260, new Random()).toString(32).substring(0, 32);
} }
public interface Command public interface Command

@ -52,8 +52,8 @@ public class SyncManager
{ {
public static final String EVENT_AUTH = "auth"; public static final String EVENT_AUTH = "auth";
public static final String EVENT_AUTH_OK = "authOK"; public static final String EVENT_AUTH_OK = "authOK";
public static final String EVENT_EXECUTE_COMMAND = "execute"; public static final String EVENT_EXECUTE_EVENT = "execute";
public static final String EVENT_POST_COMMAND = "post"; public static final String EVENT_POST_EVENT = "postEvent";
public static final String EVENT_FETCH = "fetch"; public static final String EVENT_FETCH = "fetch";
public static final String EVENT_FETCH_OK = "fetchOK"; public static final String EVENT_FETCH_OK = "fetchOK";
@ -80,6 +80,8 @@ public class SyncManager
GROUP_KEY = prefs.getString("syncKey", DatabaseHelper.getRandomId()); GROUP_KEY = prefs.getString("syncKey", DatabaseHelper.getRandomId());
CLIENT_ID = DatabaseHelper.getRandomId(); CLIENT_ID = DatabaseHelper.getRandomId();
Log.d("SyncManager", DatabaseHelper.getRandomId());
try try
{ {
IO.setDefaultSSLContext(getCACertSSLContext()); IO.setDefaultSSLContext(getCACertSSLContext());
@ -101,7 +103,7 @@ public class SyncManager
socket.on(Socket.EVENT_CONNECT, new OnConnectListener()); socket.on(Socket.EVENT_CONNECT, new OnConnectListener());
socket.on(Socket.EVENT_DISCONNECT, new OnDisconnectListener()); socket.on(Socket.EVENT_DISCONNECT, new OnDisconnectListener());
socket.on(EVENT_EXECUTE_COMMAND, new OnExecuteCommandListener()); socket.on(EVENT_EXECUTE_EVENT, new OnExecuteCommandListener());
socket.on(EVENT_AUTH_OK, new OnAuthOKListener()); socket.on(EVENT_AUTH_OK, new OnAuthOKListener());
socket.on(EVENT_FETCH_OK, new OnFetchOKListener()); socket.on(EVENT_FETCH_OK, new OnFetchOKListener());
@ -174,7 +176,7 @@ public class SyncManager
for (Event e : pendingEmit) for (Event e : pendingEmit)
{ {
Log.i("SyncManager", "Emitting: " + e.message); Log.i("SyncManager", "Emitting: " + e.message);
socket.emit(EVENT_POST_COMMAND, new JSONObject(e.message)); socket.emit(EVENT_POST_EVENT, new JSONObject(e.message));
pendingConfirmation.add(e); pendingConfirmation.add(e);
} }
@ -293,7 +295,7 @@ public class SyncManager
{ {
Log.i("SyncManager", "Fetch OK"); Log.i("SyncManager", "Fetch OK");
JSONObject json = new JSONObject((String) args[0]); JSONObject json = (JSONObject) args[0];
updateLastSync(json.getLong("timestamp")); updateLastSync(json.getLong("timestamp"));
emitPending(); emitPending();
@ -317,6 +319,6 @@ public class SyncManager
private void updateLastSync(Long timestamp) private void updateLastSync(Long timestamp)
{ {
prefs.edit().putLong("lastSync", timestamp).apply(); prefs.edit().putLong("lastSync", timestamp + 1).apply();
} }
} }

Loading…
Cancel
Save