Built skeletons for server to client messages

pull/14/head
Sam Bosley 12 years ago
parent c64db963a1
commit d8da952cc7

@ -1,15 +1,20 @@
package com.todoroo.astrid.actfm.sync;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import org.json.JSONArray;
import org.json.JSONObject;
import com.todoroo.andlib.service.Autowired;
import com.todoroo.andlib.service.DependencyInjectionService;
import com.todoroo.andlib.utility.Pair;
import com.todoroo.astrid.actfm.sync.messages.BriefMe;
import com.todoroo.astrid.actfm.sync.messages.ChangesHappened;
import com.todoroo.astrid.actfm.sync.messages.ClientToServerMessage;
import com.todoroo.astrid.actfm.sync.messages.ServerToClientMessage;
import com.todoroo.astrid.dao.TagDataDao;
import com.todoroo.astrid.dao.TagOutstandingDao;
import com.todoroo.astrid.dao.TaskDao;
@ -38,6 +43,9 @@ public class ActFmSyncThread {
@Autowired
private TagOutstandingDao tagOutstandingDao;
@Autowired
private ActFmInvoker actFmInvoker;
public static enum ModelType {
TYPE_TASK,
TYPE_TAG
@ -61,6 +69,7 @@ public class ActFmSyncThread {
}
}
@SuppressWarnings("nls")
private void sync() {
try {
int batchSize = 1;
@ -92,10 +101,32 @@ public class ActFmSyncThread {
}
if (!messages.isEmpty()) {
// Get List<ServerToClientMessage> responses
// foreach response response.process
// if (responses.didntFinish) batchSize = Math.max(batchSize / 2, 1)
// else batchSize = min(batchSize, messages.size()) * 2
JSONArray payload = new JSONArray();
for (ClientToServerMessage<?> message : messages) {
JSONObject serialized = message.serializeToJSON();
if (serialized != null)
payload.put(serialized);
}
try {
JSONObject response = actFmInvoker.invoke("sync", "data", payload, "token", "");
// process responses
JSONArray serverMessagesJson = response.optJSONArray("messages");
if (serverMessagesJson != null) {
for (int i = 0; i < serverMessagesJson.length(); i++) {
JSONObject serverMessageJson = serverMessagesJson.optJSONObject(i);
if (serverMessageJson != null) {
ServerToClientMessage serverMessage = ServerToClientMessage.instantiateMessage(serverMessageJson);
if (serverMessage != null)
serverMessage.processMessage();
}
}
}
batchSize = Math.min(batchSize, messages.size()) * 2;
} catch (IOException e) {
batchSize = Math.max(batchSize / 2, 1);
}
messages = new LinkedList<ClientToServerMessage<?>>();
}
}

@ -1,6 +1,13 @@
package com.todoroo.astrid.actfm.sync.messages;
public class AcknowledgeChange implements ServerToClientMessage {
import org.json.JSONObject;
public class AcknowledgeChange extends ServerToClientMessage {
public AcknowledgeChange(JSONObject json) {
super(json);
throw new RuntimeException("No constructor for AcknowledgeChange implemented"); //$NON-NLS-1$
}
@Override
public void processMessage() {

@ -1,6 +1,13 @@
package com.todoroo.astrid.actfm.sync.messages;
public class Debug implements ServerToClientMessage {
import org.json.JSONObject;
public class Debug extends ServerToClientMessage {
public Debug(JSONObject json) {
super(json);
throw new RuntimeException("No constructor for Debug implemented"); //$NON-NLS-1$
}
@Override
public void processMessage() {

@ -1,6 +1,13 @@
package com.todoroo.astrid.actfm.sync.messages;
public class DoubleCheck implements ServerToClientMessage {
import org.json.JSONObject;
public class DoubleCheck extends ServerToClientMessage {
public DoubleCheck(JSONObject json) {
super(json);
throw new RuntimeException("No constructor for DoubleCheck implemented"); //$NON-NLS-1$
}
@Override
public void processMessage() {

@ -1,6 +1,13 @@
package com.todoroo.astrid.actfm.sync.messages;
public class MakeChanges implements ServerToClientMessage {
import org.json.JSONObject;
public class MakeChanges extends ServerToClientMessage {
public MakeChanges(JSONObject json) {
super(json);
throw new RuntimeException("No constructor for MakeChanges implemented"); //$NON-NLS-1$
}
@Override
public void processMessage() {

@ -1,7 +1,34 @@
package com.todoroo.astrid.actfm.sync.messages;
public interface ServerToClientMessage {
import org.json.JSONObject;
public void processMessage();
@SuppressWarnings("nls")
public abstract class ServerToClientMessage {
public abstract void processMessage();
private static final String TYPE_MAKE_CHANGES = "MakeChanges";
private static final String TYPE_ACKNOWLEDGE_CHANGE = "AcknowledgeChange";
private static final String TYPE_DOUBLE_CHECK = "DoubleCheck";
private static final String TYPE_DEBUG = "Debug";
@SuppressWarnings("unused")
public ServerToClientMessage(JSONObject json) {
//
}
public static ServerToClientMessage instantiateMessage(JSONObject json) {
String type = json.optString("type");
if (TYPE_MAKE_CHANGES.equals(type))
return new MakeChanges(json);
else if (TYPE_ACKNOWLEDGE_CHANGE.equals(type))
return new AcknowledgeChange(json);
else if (TYPE_DOUBLE_CHECK.equals(json))
return new DoubleCheck(json);
else if (TYPE_DEBUG.equals(json))
return new Debug(json);
return null;
}
}

Loading…
Cancel
Save