From: Marius Gavrilescu Date: Sat, 23 Mar 2013 11:56:06 +0000 (+0200) Subject: Add retries to message sending X-Git-Tag: 0.000_001~45 X-Git-Url: http://git.ieval.ro/?a=commitdiff_plain;h=b26f32d6098549c204dba44f353e10fe30a29781;p=fonbot.git Add retries to message sending HttpCallExecutableRunnable now takes a new parameter which determines whether it should be retried if it fails. The flag is currently set for calls that send messages to the user (since these are very important). --- diff --git a/src/ro/ieval/fonbot/FonBotMainActivity.java b/src/ro/ieval/fonbot/FonBotMainActivity.java index 342606a..d7581fa 100644 --- a/src/ro/ieval/fonbot/FonBotMainActivity.java +++ b/src/ro/ieval/fonbot/FonBotMainActivity.java @@ -218,7 +218,7 @@ public final class FonBotMainActivity extends ListActivity { super.onStart(); resultTextView.setText(logging_in); new HttpCallExecutableRunnable("/ok", null, - toNonNull(getApplicationContext()), toNonNull(updateResultCallback)).execute(); + toNonNull(getApplicationContext()), toNonNull(updateResultCallback), false).execute(); connection.refreshAdapter(); } diff --git a/src/ro/ieval/fonbot/HttpCallExecutableRunnable.java b/src/ro/ieval/fonbot/HttpCallExecutableRunnable.java index d41c7ef..3782acc 100644 --- a/src/ro/ieval/fonbot/HttpCallExecutableRunnable.java +++ b/src/ro/ieval/fonbot/HttpCallExecutableRunnable.java @@ -172,6 +172,8 @@ public final class HttpCallExecutableRunnable extends ExecutableRunnable{ * Callback to run after the request returns */ private final ResultCallback callback; + /** If true, the task should be retried if it fails */ + private final boolean mustRetryTask; /** * Constructs a SendHttpMessageAsyncTask which sends a binary message. @@ -180,13 +182,15 @@ public final class HttpCallExecutableRunnable extends ExecutableRunnable{ * @param headers the extra headers * @param context the context instance * @param resultCallback {@link ResultCallback} instance + * @param mustRetryTask true if this task should be retried if it fails * @param data the message to send */ - public HttpCallExecutableRunnable(final String path, final @Nullable Collection
headers, final Context context, final @Nullable ResultCallback resultCallback, final byte[] data){//NOPMD array is supposed to be immutable. + public HttpCallExecutableRunnable(final String path, final @Nullable Collection
headers, final Context context, final @Nullable ResultCallback resultCallback, final boolean mustRetryTask, final byte[] data){//NOPMD array is supposed to be immutable. this.path=path; this.headers=headers; this.context=context; this.callback=resultCallback; + this.mustRetryTask=mustRetryTask; this.data=data; } @@ -197,13 +201,15 @@ public final class HttpCallExecutableRunnable extends ExecutableRunnable{ * @param headers the extra headers * @param context the context instance * @param resultCallback {@link ResultCallback} instance + * @param mustRetryTask true if this task should be retried if it fails * @param message message to send */ - public HttpCallExecutableRunnable(final String path, final @Nullable Collection
headers, final Context context, final @Nullable ResultCallback resultCallback, final String... message){ + public HttpCallExecutableRunnable(final String path, final @Nullable Collection
headers, final Context context, final @Nullable ResultCallback resultCallback, final boolean mustRetryTask, final String... message){ this.path=path; this.headers=headers; this.context=context; this.callback=resultCallback; + this.mustRetryTask=mustRetryTask; if(message.length == 0) this.data=null;//NOPMD final field else @@ -255,6 +261,8 @@ public final class HttpCallExecutableRunnable extends ExecutableRunnable{ e.printStackTrace(); if(callback != null) callback.onError(toNonNull(context.getString(connection_error))); + if(mustRetryTask) + retry(); } } } diff --git a/src/ro/ieval/fonbot/RemoteCrashdumpHandler.java b/src/ro/ieval/fonbot/RemoteCrashdumpHandler.java index e09e31e..56911b6 100644 --- a/src/ro/ieval/fonbot/RemoteCrashdumpHandler.java +++ b/src/ro/ieval/fonbot/RemoteCrashdumpHandler.java @@ -60,7 +60,7 @@ final class RemoteCrashdumpHandler implements UncaughtExceptionHandler { pw.close(); new HttpCallExecutableRunnable("/crashdump", toNonNull(Collections.
emptyList()), - toNonNull(context), null, toNonNull(baos.toByteArray())).execute(); + toNonNull(context), null, false, toNonNull(baos.toByteArray())).execute(); try { baos.close(); diff --git a/src/ro/ieval/fonbot/Utils.java b/src/ro/ieval/fonbot/Utils.java index ddb506b..1262513 100644 --- a/src/ro/ieval/fonbot/Utils.java +++ b/src/ro/ieval/fonbot/Utils.java @@ -317,7 +317,7 @@ public final class Utils { switch(address.protocol){ case HTTP: new HttpCallExecutableRunnable("/send", toNonNull(Arrays.asList( - new Header("X-Destination", toNonNull(address.data)))), context, null, message).execute(); + new Header("X-Destination", toNonNull(address.data)))), context, null, true, message).execute(); break; case SMS: @@ -416,7 +416,7 @@ public final class Utils { * @param context Context instance */ public static void pollServer(final Context context){ - new HttpCallExecutableRunnable("/get", null, context, new PollResultCallback(context)).execute(); + new HttpCallExecutableRunnable("/get", null, context, new PollResultCallback(context), false).execute(); } /**