From: Marius Gavrilescu Date: Wed, 2 Oct 2013 09:00:45 +0000 (+0300) Subject: Make FonBot usable X-Git-Tag: 0.000_001~2 X-Git-Url: http://git.ieval.ro/?a=commitdiff_plain;h=82a2d0c2eef2659b6189a1bad2996843d7102094;p=fonbot.git Make FonBot usable 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 --- diff --git a/src/ro/ieval/fonbot/FonBotMainService.java b/src/ro/ieval/fonbot/FonBotMainService.java index 4da70cc..dd13aff 100644 --- a/src/ro/ieval/fonbot/FonBotMainService.java +++ b/src/ro/ieval/fonbot/FonBotMainService.java @@ -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; diff --git a/src/ro/ieval/fonbot/HttpCallExecutableRunnable.java b/src/ro/ieval/fonbot/HttpCallExecutableRunnable.java index 0eda24a..12b1014 100644 --- a/src/ro/ieval/fonbot/HttpCallExecutableRunnable.java +++ b/src/ro/ieval/fonbot/HttpCallExecutableRunnable.java @@ -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(); + } } diff --git a/src/ro/ieval/fonbot/PollResultCallback.java b/src/ro/ieval/fonbot/PollResultCallback.java index 46e3eda..19da5bb 100644 --- a/src/ro/ieval/fonbot/PollResultCallback.java +++ b/src/ro/ieval/fonbot/PollResultCallback.java @@ -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 } }