15bf9cc093e6a0932b1bb568d38b2a2c25d074b0
1 package ro
.ieval
.fonbot
;
3 import static ro
.ieval
.fonbot
.Utils
.toNonNull
;
5 import java
.io
.IOException
;
6 import java
.net
.HttpURLConnection
;
7 import java
.net
.MalformedURLException
;
9 import java
.util
.ArrayList
;
10 import java
.util
.List
;
12 import org
.eclipse
.jdt
.annotation
.Nullable
;
13 import org
.json
.JSONArray
;
14 import org
.json
.JSONException
;
15 import org
.json
.JSONObject
;
17 import ro
.ieval
.fonbot
.Address
.Protocol
;
18 import android
.content
.Context
;
19 import android
.os
.AsyncTask
;
20 import android
.preference
.PreferenceManager
;
21 import android
.util
.Base64
;
22 import android
.util
.Log
;
25 * Copyright © 2013 Marius Gavrilescu
27 * This file is part of FonBot.
29 * FonBot is free software: you can redistribute it and/or modify
30 * it under the terms of the GNU General Public License as published by
31 * the Free Software Foundation, either version 3 of the License, or
32 * (at your option) any later version.
34 * FonBot is distributed in the hope that it will be useful,
35 * but WITHOUT ANY WARRANTY; without even the implied warranty of
36 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
37 * GNU General Public License for more details.
39 * You should have received a copy of the GNU General Public License
40 * along with FonBot. If not, see <http://www.gnu.org/licenses/>.
44 * AsyncTask that polls the server for pending commands and executes any commands found.
46 * @author Marius Gavrilescu <marius@ieval.ro>
48 public final class PollServerAsyncTask
extends AsyncTask
<Void
, Void
, List
<PollServerAsyncTask
.Command
>> {
50 * POJO that represents a command in a basic form: a command name, a list of arguments and a reply address, all of them being strings
52 * @author Marius Gavrilescu <marius@ieval.ro>
58 final String command
;//NOPMD no confusion here
60 * The list of command arguments
64 * The reply address, excluding the protocol
69 * Construct a Command from its three parts
71 * @param command the command name
72 * @param args the command arguments
73 * @param replyto the reply address
75 Command(final String command
, final String
[] args
, final String replyto
) {//NOPMD array is immutable
82 /** Context instance */
83 private final Context context
;
86 * Constructs a PollServerAsyncTask.
88 * @param context Context instance
90 public PollServerAsyncTask(final Context context
) {
95 protected List
<Command
> doInBackground(final @Nullable Void
... params
) {
96 Log
.d(getClass().getName(), "Polling server");
97 final List
<Command
> commands
=new ArrayList
<Command
>(10);
99 final URL url
=Utils
.getServerURL(toNonNull(context
),"/get");
100 final HttpURLConnection conn
=(HttpURLConnection
) url
.openConnection();
101 conn
.setRequestProperty("X-ID", FonBotApplication
.instance
.regID
);
102 final String user
=PreferenceManager
.getDefaultSharedPreferences(context
).getString("username", null);
103 final String password
=PreferenceManager
.getDefaultSharedPreferences(context
).getString("password", null);
104 if(user
== null || password
== null)
107 conn
.setRequestProperty("Authorization", "Basic "+Base64
.encodeToString(
108 (user
+':'+password
).getBytes(), Base64
.NO_WRAP
));
110 final byte[] buf
=new byte[4096*1024];
111 Log
.d(getClass().getName(), "Server poll got response code "+conn
.getResponseCode()+" and message "+conn
.getResponseMessage());
112 if(conn
.getResponseCode()!=200)
114 conn
.getInputStream().read(buf
);
115 final JSONArray array
=new JSONArray(new String(buf
));
116 final List
<String
> list
=new ArrayList
<String
>();
117 for(int i
=0;i
<array
.length();i
++){
118 final JSONObject object
=array
.getJSONObject(i
);
120 for(int j
=0;object
.has("arg"+j
);j
++)
121 list
.add(object
.getString("arg"+j
));
123 commands
.add(new Command(//NOPMD command changes for each JSON object
124 toNonNull(object
.getString("command")),
125 toNonNull(list
.toArray(new String
[list
.size()])),
126 toNonNull(object
.getString("replyto"))));
129 } catch (MalformedURLException e
) {
131 } catch (IOException e
) {
133 } catch (JSONException e
) {
137 throw new AssertionError("commands is null in PollServerAsyncTask#doInBackground");//silence Eclipse bug
142 protected void onPostExecute(final @Nullable List
<Command
> commands
) {
145 for (Command command
: commands
) {
146 Log
.d(getClass().getName(), "Poll got command "+command
.command
+" with "+((command
.args
.length
==0)?
"no args":"args "+Utils
.join(
147 " ",toNonNull(command
.args
))));
148 Utils
.processCommand(
149 toNonNull(FonBotApplication
.instance
),
150 toNonNull(command
.command
),
151 toNonNull(command
.args
),
152 new Address(toNonNull(Protocol
.HTTP
), command
.replyto
));//NOPMD address depends on command
This page took 0.023892 seconds and 3 git commands to generate.