mirror of
https://github.com/cwinfo/envayasms.git
synced 2024-12-04 20:45:32 +00:00
enable long polling - increase socket timeout, start only one PollerTask at a time, and add 5 sec poll interval; prevent sending duplicate outgoing SMS if server does not receive send_status request
This commit is contained in:
parent
22f8c26da1
commit
c18a28b1be
@ -2,6 +2,7 @@
|
||||
<resources>
|
||||
|
||||
<string-array name="check_intervals">
|
||||
<item>5 sec</item>
|
||||
<item>15 sec</item>
|
||||
<item>30 sec</item>
|
||||
<item>1 minute</item>
|
||||
@ -14,6 +15,7 @@
|
||||
</string-array>
|
||||
|
||||
<string-array name="check_intervals_values">
|
||||
<item>5</item>
|
||||
<item>15</item>
|
||||
<item>30</item>
|
||||
<item>60</item>
|
||||
|
@ -99,6 +99,7 @@ public final class App extends Application {
|
||||
public static final int LOG_TIMESTAMP_INTERVAL = 60000; // ms
|
||||
|
||||
public static final int HTTP_CONNECTION_TIMEOUT = 10000; // ms
|
||||
public static final int HTTP_SOCKET_TIMEOUT = 60000; // ms
|
||||
|
||||
// Each QueuedMessage is identified within our internal Map by its Uri.
|
||||
// Currently QueuedMessage instances are only available within EnvayaSMS,
|
||||
@ -353,16 +354,39 @@ public final class App extends Application {
|
||||
extras);
|
||||
}
|
||||
|
||||
public void checkOutgoingMessages()
|
||||
private boolean pollActive = false;
|
||||
private boolean requestedPoll = false;
|
||||
|
||||
public synchronized void markPollComplete()
|
||||
{
|
||||
pollActive = false;
|
||||
|
||||
if (requestedPoll)
|
||||
{
|
||||
requestedPoll = false;
|
||||
checkOutgoingMessages();
|
||||
}
|
||||
}
|
||||
|
||||
public synchronized void checkOutgoingMessages()
|
||||
{
|
||||
if (!pollActive)
|
||||
{
|
||||
String serverUrl = getServerUrl();
|
||||
if (serverUrl.length() > 0) {
|
||||
log("Checking for outgoing messages");
|
||||
pollActive = true;
|
||||
new PollerTask(this).execute();
|
||||
} else {
|
||||
log("Can't check outgoing messages; server URL not set");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
debug("Waiting for server response...");
|
||||
requestedPoll = true;
|
||||
}
|
||||
}
|
||||
|
||||
public void setOutgoingMessageAlarm() {
|
||||
AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
|
||||
@ -696,7 +720,7 @@ public final class App extends Application {
|
||||
{
|
||||
HttpParams httpParams = new BasicHttpParams();
|
||||
HttpConnectionParams.setConnectionTimeout(httpParams, HTTP_CONNECTION_TIMEOUT);
|
||||
HttpConnectionParams.setSoTimeout(httpParams, HTTP_CONNECTION_TIMEOUT);
|
||||
HttpConnectionParams.setSoTimeout(httpParams, HTTP_SOCKET_TIMEOUT);
|
||||
HttpProtocolParams.setContentCharset(httpParams, "UTF-8");
|
||||
return httpParams;
|
||||
}
|
||||
|
@ -11,8 +11,12 @@ import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Comparator;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.LinkedList;
|
||||
import java.util.Map;
|
||||
import java.util.PriorityQueue;
|
||||
import java.util.Queue;
|
||||
import java.util.Set;
|
||||
import org.apache.http.message.BasicNameValuePair;
|
||||
import org.envaya.sms.receiver.DequeueOutgoingMessageReceiver;
|
||||
import org.envaya.sms.task.HttpTask;
|
||||
@ -22,6 +26,12 @@ public class Outbox {
|
||||
|
||||
private App app;
|
||||
|
||||
// keep track of some recent message URIs to avoid sending duplicate messages
|
||||
// (e.g. if send_status HTTP request fails for some reason)
|
||||
public static final int MAX_RECENT_URIS = 100;
|
||||
private Set<Uri> recentSentMessageUris = new HashSet<Uri>();
|
||||
private Queue<Uri> recentSentMessageUriOrder = new LinkedList<Uri>();
|
||||
|
||||
// number of outgoing messages that are currently being sent and waiting for
|
||||
// messageSent or messageFailed to be called
|
||||
private int numSendingOutgoingMessages = 0;
|
||||
@ -108,7 +118,11 @@ public class Outbox {
|
||||
|
||||
notifyMessageStatus(sms, App.STATUS_SENT, "");
|
||||
|
||||
outgoingMessages.remove(sms.getUri());
|
||||
Uri uri = sms.getUri();
|
||||
|
||||
outgoingMessages.remove(uri);
|
||||
|
||||
addRecentSentMessage(sms);
|
||||
|
||||
notifyChanged();
|
||||
|
||||
@ -116,6 +130,23 @@ public class Outbox {
|
||||
maybeDequeueMessage();
|
||||
}
|
||||
|
||||
private synchronized void addRecentSentMessage(OutgoingMessage sms)
|
||||
{
|
||||
if (sms.getServerId() != null)
|
||||
{
|
||||
Uri uri = sms.getUri();
|
||||
|
||||
recentSentMessageUris.add(uri);
|
||||
recentSentMessageUriOrder.add(uri);
|
||||
|
||||
if (recentSentMessageUriOrder.size() > MAX_RECENT_URIS)
|
||||
{
|
||||
Uri oldUri = recentSentMessageUriOrder.remove();
|
||||
recentSentMessageUris.remove(oldUri);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public synchronized void messageFailed(OutgoingMessage sms, String error)
|
||||
{
|
||||
if (sms.scheduleRetry())
|
||||
@ -167,6 +198,13 @@ public class Outbox {
|
||||
return;
|
||||
}
|
||||
|
||||
if (recentSentMessageUris.contains(uri))
|
||||
{
|
||||
app.debug("Outgoing " + sms.getLogName() + " already sent, re-notifying server");
|
||||
notifyMessageStatus(sms, App.STATUS_SENT, "");
|
||||
return;
|
||||
}
|
||||
|
||||
outgoingMessages.put(uri, sms);
|
||||
enqueueMessage(sms);
|
||||
}
|
||||
|
@ -12,6 +12,12 @@ public class PollerTask extends HttpTask {
|
||||
super(app, new BasicNameValuePair("action", App.ACTION_OUTGOING));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPostExecute(HttpResponse response) {
|
||||
super.onPostExecute(response);
|
||||
app.markPollComplete();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void handleResponse(HttpResponse response) throws Exception {
|
||||
for (OutgoingMessage reply : parseResponseXML(response)) {
|
||||
|
Loading…
Reference in New Issue
Block a user