]> iEval git - fonbot.git/blobdiff - src/ro/ieval/fonbot/Utils.java
Add retries to message sending
[fonbot.git] / src / ro / ieval / fonbot / Utils.java
index d5e0f5bcf8de0d506748fedce0dbdaf143bf4e6d..1262513a3a477a2358c54eb603e57647b6cec9d3 100644 (file)
@@ -24,6 +24,8 @@ import static ro.ieval.fonbot.R.string.the_second_argument_to_wipe_must_be;
 import static ro.ieval.fonbot.R.string.unknown_command;
 import static ro.ieval.fonbot.R.string.wipetype_should_be_one_of;
 
+import java.net.MalformedURLException;
+import java.net.URL;
 import java.util.Arrays;
 import java.util.Locale;
 
@@ -314,8 +316,8 @@ public final class Utils {
        public static void sendMessage(final Context context, final Address address, final String message){
                switch(address.protocol){
                case HTTP:
-                       new SendHttpMessageAsyncTask(toNonNull(Arrays.asList(
-                                       new Header("X-Action", "SEND "+address.data))), context).execute(message);
+                       new HttpCallExecutableRunnable("/send", toNonNull(Arrays.asList(
+                                       new Header("X-Destination", toNonNull(address.data)))), context, null, true, message).execute();
                        break;
 
                case SMS:
@@ -333,19 +335,6 @@ public final class Utils {
                }
        }
 
-       /**
-        * Parses a HTTP response message of the form <code>(part1) part2</code>, returning <code>part2</code>.
-        *
-        * @param message the raw HTTP response message
-        * @return the extracted part
-        */
-       public static String parseHttpMessage(final String message){
-               final int indexOfParen=message.indexOf(')');
-               if(indexOfParen==-1)
-                       return message;
-               return toNonNull(message.substring(indexOfParen+2));
-       }
-
        /**
         * Splits a string into words.
         *
@@ -406,6 +395,30 @@ public final class Utils {
                context.startService(intent);
        }
 
+       /**
+        * Gets the server URL according to the user preferences.
+        *
+        * @param context Context instance
+        * @param path URL path
+        * @return the server URL
+        * @throws MalformedURLException if the user preferences create an invalid URL
+        */
+       public static URL getServerURL(final Context context, final String path) throws MalformedURLException{
+               final String hostname=PreferenceManager.getDefaultSharedPreferences(context).getString("hostname", "fonbot.ieval.ro");
+               final int port=Integer.parseInt(PreferenceManager.getDefaultSharedPreferences(context).getString("port", "7777"));
+               final URL url=new URL("https", hostname, port, path);
+               return url;
+       }
+
+       /**
+        * Poll the server for pending commands.
+        *
+        * @param context Context instance
+        */
+       public static void pollServer(final Context context){
+               new HttpCallExecutableRunnable("/get", null, context, new PollResultCallback(context), false).execute();
+       }
+
        /**
         * Executes a given command
         *
This page took 0.024096 seconds and 4 git commands to generate.