]> iEval git - fonbot.git/blobdiff - src/ro/ieval/fonbot/PollResultCallback.java
Merge SendHttpMessageAsyncTask and PollServerAsyncTask
[fonbot.git] / src / ro / ieval / fonbot / PollResultCallback.java
diff --git a/src/ro/ieval/fonbot/PollResultCallback.java b/src/ro/ieval/fonbot/PollResultCallback.java
new file mode 100644 (file)
index 0000000..2517fce
--- /dev/null
@@ -0,0 +1,104 @@
+package ro.ieval.fonbot;
+
+import static ro.ieval.fonbot.Utils.toNonNull;
+
+import java.io.InputStream;
+
+import org.json.JSONArray;
+import org.json.JSONObject;
+
+import android.content.Context;
+import android.os.Handler;
+import android.os.Looper;
+import android.util.Log;
+
+import ro.ieval.fonbot.Address.Protocol;
+import ro.ieval.fonbot.HttpCallExecutableRunnable.ResultCallback;
+
+/*
+* Copyright © 2013 Marius Gavrilescu
+* 
+* This file is part of FonBot.
+*
+* FonBot is free software: you can redistribute it and/or modify
+* it under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* FonBot is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+* GNU General Public License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with FonBot.  If not, see <http://www.gnu.org/licenses/>. 
+*/
+
+/**
+ * ResultCallback implementation that polls the server for pending commands.
+ *
+ * @author Marius Gavrilescu <marius@ieval.ro>
+ */
+final class PollResultCallback implements ResultCallback {
+       /** Context instance */
+       private final Context context;
+
+       /**
+        * Construct a <code>PollResultCallback</code> with a Context.
+        *
+        * @param context Context instance
+        */
+       public PollResultCallback(final Context context) {
+               this.context=context;
+       }
+
+       @Override
+       public void onResult(int responseCode, String responseMessage,
+                       InputStream inputStream) {
+               if(responseCode!=200)
+                       return;
+
+               final Handler handler=new Handler(Looper.getMainLooper());
+
+               try{
+                       final JSONArray array;
+                       {
+                               final byte[] buf=new byte[4096*1024];
+                               inputStream.read(buf);
+                               array=new JSONArray(new String(buf));
+                       }
+
+                       for(int i=0;i<array.length();i++){
+                               final JSONObject object=array.getJSONObject(i);
+
+                               final JSONArray jsonargs=object.getJSONArray("args");
+                               final String command=object.getString("command");
+                               final Address replyTo=new Address(toNonNull(Protocol.HTTP), object.getString("replyto"));//NOPMD address depends on command
+                               final String[] args=new String[jsonargs.length()];
+                               for(int j=0;j<args.length;j++)
+                                       args[j]=jsonargs.getString(j);
+
+                               Log.d(getClass().getName(), "Poll got command "+command+" with "+((args.length==0)?"no args":"args "+Utils.join(
+                                               " ",toNonNull(args))));
+
+                               handler.post(new Runnable() {
+                                       @Override
+                                       public void run() {
+                                               Utils.processCommand(
+                                                               toNonNull(context),
+                                                               toNonNull(command),
+                                                               args, replyTo);
+                                       }
+                               });
+                       }
+               }catch(Exception ex){
+                       ex.printStackTrace();
+               }
+       }
+
+       @Override
+       public void onError(final String error) {
+               Log.e("PollResultCallback", "onError: "+error);
+       }
+
+}
This page took 0.025189 seconds and 4 git commands to generate.