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