only try to send the first unsent message, producing only one de-serialized Intent at a time, thus re-sending one by one

Change-Id: I592518f35a83944d89a793e1f6456075805c3e72
This commit is contained in:
Axel Uhl committed 2016-07-24 19:15:04 +02:00
1 parent 42c1101efa
commit 65c2445929
2 files changed
+42 -41

No files matched your search

@@ -114,42 +114,34 @@ public class MessagePersistenceManager {
void restoreMessage(Context context, Intent messageIntent);
}
public List<Intent> restoreMessages() throws UnsupportedEncodingException {
List<Intent> delayedIntents = new ArrayList<Intent>();
for (String persistedMessage : persistedMessages) {
String[] lineParts = persistedMessage.split(";");
String url = lineParts[2];
String callbackPayload = lineParts[0];
String payload = URLDecoder.decode(lineParts[1], MessageSendingService.charsetName);
String callbackClassString = lineParts[3];
private Intent restorePersistedIntent(String persistedMessage) throws UnsupportedEncodingException {
String[] lineParts = persistedMessage.split(";");
String url = lineParts[2];
String callbackPayload = lineParts[0];
String payload = URLDecoder.decode(lineParts[1], MessageSendingService.charsetName);
String callbackClassString = lineParts[3];
Class<? extends ServerReplyCallback> callbackClass = null;
if (!"null".equals(callbackClassString)) {
try {
@SuppressWarnings("unchecked")
Class<? extends ServerReplyCallback> tmp =
(Class<? extends ServerReplyCallback>) Class.forName(callbackClassString);
callbackClass = tmp;
} catch (ClassNotFoundException e) {
ExLog.e(context, TAG, "Could not find class for callback name: " + callbackClassString);
}
}
// We are passing no message id, because we know it used to suppress message sending and
// we want this message to be sent.
Intent messageIntent = MessageSendingService.createMessageIntent(context, url, callbackPayload,
null, payload, callbackClass);
if (messageRestorer != null) {
messageRestorer.restoreMessage(context, messageIntent);
}
if (messageIntent != null) {
delayedIntents.add(messageIntent);
Class<? extends ServerReplyCallback> callbackClass = null;
if (!"null".equals(callbackClassString)) {
try {
@SuppressWarnings("unchecked")
Class<? extends ServerReplyCallback> tmp =
(Class<? extends ServerReplyCallback>) Class.forName(callbackClassString);
callbackClass = tmp;
} catch (ClassNotFoundException e) {
ExLog.e(context, TAG, "Could not find class for callback name: " + callbackClassString);
}
}
ExLog.i(context, TAG, "Restored " + delayedIntents.size() + " messages");
return delayedIntents;
// We are passing no message id, because we know it used to suppress message sending and
// we want this message to be sent.
Intent messageIntent = MessageSendingService.createMessageIntent(context, url, callbackPayload,
null, payload, callbackClass);
if (messageRestorer != null) {
messageRestorer.restoreMessage(context, messageIntent);
}
return messageIntent;
}
/**
@@ -172,8 +164,8 @@ public class MessagePersistenceManager {
private void saveMessage(String messageLine) {
persistedMessages.add(messageLine);
writePersistedMessagesToFile();
ExLog.i(context, TAG, "Wrote message to file: " + messageLine);
writeToFile(messageLine, Context.MODE_APPEND);
ExLog.i(context, TAG, "Appended message to file: " + messageLine);
}
private void writePersistedMessagesToFile() {
@@ -217,4 +209,8 @@ public class MessagePersistenceManager {
}
}
public Intent restoreFirstDelayedIntent() throws UnsupportedEncodingException {
return persistedMessages.isEmpty() ? null : restorePersistedIntent(persistedMessages.get(0));
}
}
@@ -72,6 +72,10 @@ public class MessageSendingService extends Service implements MessageSendingList
private MessagePersistenceManager persistenceManager;
private boolean isHandlerSet;
/**
* Can be used to keep messages from being sent; the race manager app, for example, uses this
* to avoid sending messages back to the server that were received from the server in the first place.
*/
private Set<Serializable> suppressedMessageIds = new HashSet<Serializable>();
private APIConnectivityListener apiConnectivityListener;
@@ -253,16 +257,16 @@ public class MessageSendingService extends Service implements MessageSendingList
ConnectivityChangedReceiver.enable(this);
serviceLogger.onMessageSentFailed();
} else {
sendDelayedMessages();
sendNextDelayedMessage();
}
}
private void sendDelayedMessages() {
private void sendNextDelayedMessage() {
try {
List<Intent> delayedIntents = persistenceManager.restoreMessages();
ExLog.i(this, TAG, String.format("Resending %d messages...", delayedIntents.size()));
for (Intent intent : delayedIntents) {
sendMessage(intent);
final Intent firstDelayedIntent = persistenceManager.restoreFirstDelayedIntent();
if (firstDelayedIntent != null) {
ExLog.i(this, TAG, String.format("Resending one message..."));
sendMessage(firstDelayedIntent);
}
} catch (UnsupportedEncodingException e) {
ExLog.e(this, TAG, "Could not restore messages (unsupported encoding)");
@@ -313,6 +317,7 @@ public class MessageSendingService extends Service implements MessageSendingList
if (persistenceManager.areIntentsDelayed()) {
try {
persistenceManager.removeIntent(result.getIntent());
sendNextDelayedMessage();
} catch (UnsupportedEncodingException e) {
ExLog.e(this, TAG, "Could not remove message (unsupported encoding)");
}