Make FonBot usable
authorMarius Gavrilescu <marius@ieval.ro>
Wed, 2 Oct 2013 09:00:45 +0000 (12:00 +0300)
committerMarius Gavrilescu <marius@ieval.ro>
Wed, 2 Oct 2013 09:00:45 +0000 (12:00 +0300)
This commit fixes the worst bug in FonBot. Before this, the long polling
service stopped when the request returned with HTTP 504 Gateway Timeout.

This no longer happens, so FonBot is finally usable.

The first version will be released soon

src/ro/ieval/fonbot/FonBotMainService.java
src/ro/ieval/fonbot/HttpCallExecutableRunnable.java
src/ro/ieval/fonbot/PollResultCallback.java

index 4da70cca45455e011d1d4d126509d41150ee3c48..dd13affe715aef6f403d1fba71fc6c87e00bf8ac 100644 (file)
@@ -3,6 +3,7 @@ package ro.ieval.fonbot;
 import static ro.ieval.fonbot.R.string.*;
 import static ro.ieval.fonbot.Utils.toNonNull;
 
+import java.net.SocketTimeoutException;
 import java.util.Collections;
 import java.util.EnumSet;
 import java.util.Set;
@@ -77,7 +78,9 @@ public final class FonBotMainService extends Service {
                        Log.d("LongPollRunnable", "Long polling started");
                        while(man.getActiveNetworkInfo() != null && man.getActiveNetworkInfo().isConnected())
                                try {
-                                       runnable.run();
+                                       runnable.doRun();
+                               } catch (final SocketTimeoutException e){
+                                       Log.d("LongPollRunnable", "Socket timeout, refreshing connection");
                                } catch (final Exception ex){
                                        ex.printStackTrace();
                                        break;
index 0eda24af98154dcc56f8864ab75b331691f588eb..12b1014c672cf4f6e1b3a760e89227471313a58b 100644 (file)
@@ -129,46 +129,9 @@ public final class HttpCallExecutableRunnable extends ExecutableRunnable{
        }
 
        @Override
-       public void run() {
+       public void run(){
                try {
-                       final URL url=Utils.getServerURL(toNonNull(context),toNonNull(path));
-                       final HttpURLConnection conn=(HttpURLConnection) url.openConnection();
-                       conn.setReadTimeout(24*60*1000);//24 minutes
-                       if(data!=null){
-                               conn.setDoOutput(true);
-                               conn.setFixedLengthStreamingMode(data.length);
-                       }
-                       final String user=PreferenceManager.getDefaultSharedPreferences(context).getString("username", null);
-                       final String password=PreferenceManager.getDefaultSharedPreferences(context).getString("password", null);
-                       if(user == null || password == null || user.length() == 0 || password.length() == 0){
-                               if(callback!=null)
-                                       callback.onError(toNonNull(context.getString(user_or_password_not_set)));
-                               return;
-                       }
-
-                       conn.setRequestProperty("Authorization", "Basic "+Base64.encodeToString(
-                                       (user+':'+password).getBytes(), Base64.NO_WRAP));
-                       if(headers != null)
-                               for (final Header header : headers)
-                                       conn.setRequestProperty(header.name, header.value);
-                       conn.connect();
-                       if(data!=null){
-                               final OutputStream stream=conn.getOutputStream();
-                               stream.write(data);
-                               stream.close();
-                       }
-                       Log.d(getClass().getName(),"HTTP Response: "+conn.getResponseCode()+" "+conn.getResponseMessage());
-                       String message=conn.getResponseMessage();
-                       if(message==null && callback != null)
-                               callback.onError(toNonNull(context.getString(no_response_returned_from_server)));
-                       else if(message != null && callback != null){
-                               if(message.charAt(message.length()-1) == ')')//message is (something)
-                                       message=message.substring(1, message.length()-1);
-                               else if(message.charAt(0) == '(')//message is (something) something else
-                                       message=message.substring(message.indexOf(')')+2);
-                               callback.onResult(conn.getResponseCode(), message, conn.getResponseCode() == 200 ? conn.getInputStream() : null);
-                       }
-                       conn.disconnect();
+                       doRun();
                } catch (Exception e) {
                        e.printStackTrace();
                        if(callback != null)
@@ -177,4 +140,44 @@ public final class HttpCallExecutableRunnable extends ExecutableRunnable{
                                retry();
                }
        }
+
+       public void doRun() throws Exception{
+               final URL url=Utils.getServerURL(toNonNull(context),toNonNull(path));
+               final HttpURLConnection conn=(HttpURLConnection) url.openConnection();
+               conn.setReadTimeout(24*60*1000);//24 minutes
+               if(data!=null){
+                       conn.setDoOutput(true);
+                       conn.setFixedLengthStreamingMode(data.length);
+               }
+               final String user=PreferenceManager.getDefaultSharedPreferences(context).getString("username", null);
+               final String password=PreferenceManager.getDefaultSharedPreferences(context).getString("password", null);
+               if(user == null || password == null || user.length() == 0 || password.length() == 0){
+                       if(callback!=null)
+                               callback.onError(toNonNull(context.getString(user_or_password_not_set)));
+                       return;
+               }
+
+               conn.setRequestProperty("Authorization", "Basic "+Base64.encodeToString((user+':'+password).getBytes(), Base64.NO_WRAP));
+               if(headers != null)
+                       for (final Header header : headers)
+                               conn.setRequestProperty(header.name, header.value);
+               conn.connect();
+               if(data!=null){
+                       final OutputStream stream=conn.getOutputStream();
+                       stream.write(data);
+                       stream.close();
+               }
+               Log.d(getClass().getName(),"HTTP Response: "+conn.getResponseCode()+" "+conn.getResponseMessage());
+               String message=conn.getResponseMessage();
+               if(message==null && callback != null)
+                       callback.onError(toNonNull(context.getString(no_response_returned_from_server)));
+               else if(message != null && callback != null){
+                       if(message.charAt(message.length()-1) == ')')//message is (something)
+                               message=message.substring(1, message.length()-1);
+                       else if(message.charAt(0) == '(')//message is (something) something else
+                               message=message.substring(message.indexOf(')')+2);
+                       callback.onResult(conn.getResponseCode(), message, conn.getResponseCode() == 200 ? conn.getInputStream() : null);
+               }
+               conn.disconnect();
+       }
 }
index 46e3eda83e39cc4ed15455ea0fc64f05f6c4f787..19da5bbabbdbb321177d15c2647501fdc11f251f 100644 (file)
@@ -56,7 +56,7 @@ final class PollResultCallback implements ResultCallback {
        @Override
        public void onResult(final int responseCode, final String responseMessage, final @Nullable InputStream inputStream) {
                if(responseCode != 200 || inputStream==null){
-                       if(responseCode != 204)
+                       if(responseCode != 204 && responseCode != 504)
                                throw new RuntimeException("Bad HTTP response code: "+responseCode);
                        return;
                }
@@ -101,8 +101,7 @@ final class PollResultCallback implements ResultCallback {
 
        @Override
        public void onError(final String error) {
-               Log.e("PollResultCallback", "onError: "+error);
-               throw new RuntimeException(error);
+               //error handling is done by the LongPollRunnable thread
        }
 
 }
This page took 0.014121 seconds and 4 git commands to generate.