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