Do some PMD-driven cleanups
[fonbot.git] / src / ro / ieval / fonbot / PollResultCallback.java
1 package ro.ieval.fonbot;
2
3 import static ro.ieval.fonbot.Utils.toNonNull;
4
5 import java.io.InputStream;
6
7 import org.json.JSONArray;
8 import org.json.JSONObject;
9
10 import android.content.Context;
11 import android.os.Handler;
12 import android.os.Looper;
13 import android.util.Log;
14
15 import ro.ieval.fonbot.Address.Protocol;
16 import ro.ieval.fonbot.HttpCallExecutableRunnable.ResultCallback;
17
18 /*
19 * Copyright © 2013 Marius Gavrilescu
20 *
21 * This file is part of FonBot.
22 *
23 * FonBot is free software: you can redistribute it and/or modify
24 * it under the terms of the GNU General Public License as published by
25 * the Free Software Foundation, either version 3 of the License, or
26 * (at your option) any later version.
27 *
28 * FonBot is distributed in the hope that it will be useful,
29 * but WITHOUT ANY WARRANTY; without even the implied warranty of
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 * GNU General Public License for more details.
32 *
33 * You should have received a copy of the GNU General Public License
34 * along with FonBot. If not, see <http://www.gnu.org/licenses/>.
35 */
36
37 /**
38 * ResultCallback implementation that polls the server for pending commands.
39 *
40 * @author Marius Gavrilescu <marius@ieval.ro>
41 */
42 final class PollResultCallback implements ResultCallback {
43 /** Context instance */
44 private final Context context;
45
46 /**
47 * Construct a <code>PollResultCallback</code> with a Context.
48 *
49 * @param context Context instance
50 */
51 public PollResultCallback(final Context context) {
52 this.context=context;
53 }
54
55 @Override
56 public void onResult(final int responseCode, final String responseMessage, final InputStream inputStream) {
57 if(responseCode!=200)
58 return;
59
60 final Handler handler=new Handler(Looper.getMainLooper());
61
62 try{
63 final JSONArray array;
64 {
65 final byte[] buf=new byte[2048*1024];
66 final int length=inputStream.read(buf);
67 array=new JSONArray(new String(buf, 0, length));
68 }
69
70 for(int i=0;i<array.length();i++){
71 final JSONObject object=array.getJSONObject(i);
72
73 final JSONArray jsonargs=object.getJSONArray("args");
74 final String command=object.getString("command");
75 final Address replyTo=new Address(toNonNull(Protocol.HTTP), object.getString("replyto"));//NOPMD address depends on command
76 final String[] args=new String[jsonargs.length()];
77 for(int j=0;j<args.length;j++)
78 args[j]=jsonargs.getString(j);
79
80 Log.d(getClass().getName(), "Poll got command "+command+" with "+((args.length==0)?"no args":"args "+Utils.join(
81 " ",toNonNull(args))));
82
83 handler.post(new Runnable() {
84 @Override
85 public void run() {
86 Utils.processCommand(
87 toNonNull(context),
88 toNonNull(command),
89 args, replyTo);
90 }
91 });
92 }
93 }catch(Exception ex){
94 ex.printStackTrace();
95 }
96 }
97
98 @Override
99 public void onError(final String error) {
100 Log.e("PollResultCallback", "onError: "+error);
101 }
102
103 }
This page took 0.022748 seconds and 4 git commands to generate.