]>
Commit | Line | Data |
---|---|---|
8dfb76c9 MG |
1 | package ro.ieval.fonbot; |
2 | ||
3 | import static ro.ieval.fonbot.Utils.toNonNull; | |
4 | ||
5 | import java.io.IOException; | |
6 | import java.net.HttpURLConnection; | |
7 | import java.net.MalformedURLException; | |
8 | import java.net.URL; | |
9 | import java.util.ArrayList; | |
10 | import java.util.List; | |
11 | ||
12 | import org.eclipse.jdt.annotation.Nullable; | |
13 | import org.json.JSONArray; | |
14 | import org.json.JSONException; | |
15 | import org.json.JSONObject; | |
16 | ||
17 | import ro.ieval.fonbot.Address.Protocol; | |
18 | import android.os.AsyncTask; | |
19 | import android.util.Log; | |
20 | ||
21 | /* | |
22 | * Copyright © 2013 Marius Gavrilescu | |
23 | * | |
24 | * This file is part of FonBot. | |
25 | * | |
26 | * FonBot is free software: you can redistribute it and/or modify | |
27 | * it under the terms of the GNU General Public License as published by | |
28 | * the Free Software Foundation, either version 3 of the License, or | |
29 | * (at your option) any later version. | |
30 | * | |
31 | * FonBot is distributed in the hope that it will be useful, | |
32 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
33 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
34 | * GNU General Public License for more details. | |
35 | * | |
36 | * You should have received a copy of the GNU General Public License | |
37 | * along with FonBot. If not, see <http://www.gnu.org/licenses/>. | |
38 | */ | |
39 | ||
40 | /** | |
41 | * AsyncTask that polls the server for pending commands and executes any commands found. | |
42 | * | |
43 | * @author Marius Gavrilescu <marius@ieval.ro> | |
44 | */ | |
45 | public final class PollServerAsyncTask extends AsyncTask<Void, Void, List<PollServerAsyncTask.Command>> { | |
46 | /** | |
47 | * 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 | |
48 | * | |
49 | * @author Marius Gavrilescu <marius@ieval.ro> | |
50 | */ | |
51 | final class Command{ | |
52 | /** | |
53 | * The command name | |
54 | */ | |
55 | final String command;//NOPMD no confusion here | |
56 | /** | |
57 | * The list of command arguments | |
58 | */ | |
59 | final String[] args; | |
60 | /** | |
61 | * The reply address, excluding the protocol | |
62 | */ | |
63 | final String replyto; | |
64 | ||
65 | /** | |
66 | * Construct a Command from its three parts | |
67 | * | |
68 | * @param command the command name | |
69 | * @param args the command arguments | |
70 | * @param replyto the reply address | |
71 | */ | |
72 | Command(final String command, final String[] args, final String replyto) {//NOPMD array is immutable | |
73 | this.command=command; | |
74 | this.args=args; | |
75 | this.replyto=replyto; | |
76 | } | |
77 | } | |
78 | ||
79 | @Override | |
80 | protected List<Command> doInBackground(final @Nullable Void... params) { | |
81 | Log.d(getClass().getName(), "Polling server"); | |
82 | final List<Command> commands=new ArrayList<Command>(10); | |
83 | try { | |
84 | final URL url=new URL("http://ieval.ro:7777/get"); | |
85 | final HttpURLConnection conn=(HttpURLConnection) url.openConnection(); | |
86 | conn.setRequestProperty("X-ID", FonBotApplication.instance.regID); | |
87 | conn.connect(); | |
88 | final byte[] buf=new byte[4096*1024]; | |
89 | Log.d(getClass().getName(), "Server poll got response code "+conn.getResponseCode()+" and message "+conn.getResponseMessage()); | |
90 | if(conn.getResponseCode()!=200) | |
91 | return commands; | |
92 | conn.getInputStream().read(buf); | |
93 | final JSONArray array=new JSONArray(new String(buf)); | |
94 | final List<String> list=new ArrayList<String>(); | |
95 | for(int i=0;i<array.length();i++){ | |
96 | final JSONObject object=array.getJSONObject(i); | |
97 | ||
98 | for(int j=0;object.has("arg"+j);j++) | |
99 | list.add(object.getString("arg"+j)); | |
100 | ||
101 | commands.add(new Command(//NOPMD command changes for each JSON object | |
102 | toNonNull(object.getString("command")), | |
103 | toNonNull(list.toArray(new String[list.size()])), | |
104 | toNonNull(object.getString("replyto")))); | |
105 | list.clear(); | |
106 | } | |
107 | } catch (MalformedURLException e) { | |
108 | e.printStackTrace(); | |
109 | } catch (IOException e) { | |
110 | e.printStackTrace(); | |
111 | } catch (JSONException e) { | |
112 | e.printStackTrace(); | |
113 | } | |
114 | if(commands==null) | |
115 | throw new AssertionError("commands is null in PollServerAsyncTask#doInBackground");//silence Eclipse bug | |
116 | return commands; | |
117 | } | |
118 | ||
119 | @Override | |
120 | protected void onPostExecute(final @Nullable List<Command> commands) { | |
121 | if(commands==null) | |
122 | return; | |
123 | for (Command command : commands) { | |
124 | Log.d(getClass().getName(), "Poll got command "+command.command+" with "+((command.args.length==0)?"no args":"args "+Utils.join( | |
125 | " ",toNonNull(command.args)))); | |
126 | Utils.processCommand( | |
127 | toNonNull(FonBotApplication.instance), | |
128 | toNonNull(command.command), | |
129 | toNonNull(command.args), | |
130 | new Address(toNonNull(Protocol.HTTP), command.replyto));//NOPMD address depends on command | |
131 | } | |
132 | } | |
133 | } |