source: other-projects/diy-streetview-pano-capture/trunk/Client/src/com/theboboon/diy/streetview/remote/control/networking.java@ 26882

Last change on this file since 26882 was 26882, checked in by davidb, 11 years ago

Joshua Hollands software for operation with the diy camera built for Smoke and Mirrors 2012

File size: 9.3 KB
Line 
1package com.theboboon.diy.streetview.remote.control;
2
3import java.io.BufferedReader;
4import java.io.BufferedWriter;
5import java.io.IOException;
6import java.io.InputStreamReader;
7import java.io.OutputStreamWriter;
8import java.lang.ref.WeakReference;
9import java.net.Socket;
10import java.net.UnknownHostException;
11
12import org.json.JSONException;
13import org.json.JSONObject;
14
15import android.os.Bundle;
16import android.os.Handler;
17import android.os.Looper;
18import android.os.Message;
19import android.util.Base64;
20import android.util.Log;
21
22public class networking extends Thread {
23 Handler me;
24 Handler main_coms;
25 Socket server;
26 BufferedReader downstream;
27 BufferedWriter upstream;
28 int count_cameras = 0;
29
30 public networking (Handler _main_coms)
31 {
32 main_coms = _main_coms;
33 }
34
35 @Override
36 public void run() {
37 try {
38 Looper.prepare();
39 me = new NetworkingHandler(this);
40 Looper.loop();
41 } catch (Throwable t) {
42 Log.e(getName(), "halted due to an error", t);
43 }
44 }
45
46 public void processmessage(Message msg) {
47 Bundle humble = msg.getData();
48 if (humble != null && humble.containsKey("command"))
49 {
50 String command = humble.getString("command");
51 if (command.equals("connect"))
52 {
53 connect(humble.getString("address"), humble.getInt("port"));
54 }
55 if (command.equals("disconnect"))
56 {
57 disconnect();
58 }
59 if (command.equals("detectcameras"))
60 {
61 detectcameras();
62 }
63 if (command.equals("capture"))
64 {
65 capture();
66 }
67 if (command.equals("shutdown"))
68 {
69 shutdown();
70 }
71 if (command.equals("montage"))
72 {
73 montage(humble.getString("id"));
74 }
75 }
76 }
77
78 public Handler gethandler()
79 {
80 while (me == null)
81 {
82
83 }
84 return me;
85 }
86
87 public void sethandler(Handler _main)
88 {
89 main_coms = _main;
90 }
91
92 private void connect(String address, int port)
93 {
94 try {
95 server = new Socket(address,port);
96 if (server.isConnected() && !server.isInputShutdown())
97 {
98 server.setSoTimeout(15000);
99 downstream = new BufferedReader(new InputStreamReader(server.getInputStream()));
100 upstream = new BufferedWriter(new OutputStreamWriter(server.getOutputStream()));
101 String comms_check = sendToServer("COMMS CHECK");
102 if (comms_check.equals("HELO"))
103 {
104 Bundle mail = new Bundle();
105 mail.putString("response","connection_status");
106 mail.putBoolean("status", server.isConnected());
107 sendToMain(mail);
108 }
109 else
110 {
111 if (!(comms_check.trim().length() == 0))
112 {
113 error("Server didn't respond to the Connection request.");
114 }
115 else if (comms_check.length() > 0)
116 {
117 error("Server didn't respond correctly. Please check that the correct port was used.");
118 }
119 disconnect();
120 }
121 }
122 else
123 {
124 error("Server is already in use by another client.");
125 disconnect();
126 }
127 } catch (UnknownHostException e) {
128 error(e.getMessage());
129 Bundle mail = new Bundle();
130 mail.putString("response","connection_status");
131 mail.putBoolean("status", false);
132 sendToMain(mail);
133 } catch (IOException e) {
134 error(e.getMessage());
135 Bundle mail = new Bundle();
136 mail.putString("response","connection_status");
137 mail.putBoolean("status", false);
138 sendToMain(mail);
139 }
140 }
141
142 private void disconnect()
143 {
144 try {
145 if (server != null && server.isConnected() && !server.isClosed())
146 {
147 server.close();
148 }
149 } catch (IOException e) {
150 error(e.getMessage());
151 }
152 Bundle mail = new Bundle();
153 mail.putString("response","connection_status");
154 mail.putBoolean("status", false);
155 sendToMain(mail);
156 }
157
158 private void detectcameras()
159 {
160 if (server.isClosed())
161 {
162 error("Not connected to a Server. Aborting camera detection.");
163 return;
164 }
165 try {
166 String response = sendToServer("Detect Cameras");
167 if (response.length() != 0)
168 {
169 JSONObject data = new JSONObject(response);
170 int cameras = data.getInt("count");
171 Bundle mail = new Bundle();
172 mail.putString("response","detected cameras");
173 mail.putInt("count", cameras);
174 sendToMain(mail);
175 count_cameras = cameras;
176 }
177 } catch (JSONException e) {
178 error(e.getMessage());
179 } catch (NullPointerException e) {
180 error(e.getMessage());
181 disconnect();
182 }
183 }
184 private void capture()
185 {
186 if (server.isClosed())
187 {
188 error("Not connected to a Server. Aborting Capture.");
189 return;
190 }
191 notify("Attempting to capture " + count_cameras + " images... This may take some time...");
192 try {
193 upstream.write("Capture\n");
194 upstream.flush();
195 } catch (IOException e) {
196 error("Comunication error: " + e.getMessage());
197 disconnect();
198 }
199
200 try {
201 int count = 0;
202 while (count < (count_cameras + 1))
203 {
204 String response = "";
205 response = downstream.readLine();
206 JSONObject data = new JSONObject(response);
207 if (data.has("success"))
208 {
209 boolean success = data.getBoolean("success");
210 Bundle mail = new Bundle();
211 mail.putString("response","capture");
212 mail.putBoolean("success", success);
213 if (success)
214 {
215 mail.putString("set",data.getString("set"));
216 }
217 sendToMain(mail);
218 }
219 else if (data.has("captured"))
220 {
221 Bundle mail = new Bundle();
222 mail.putString("pending","captured");
223 mail.putString("camera", "" + data.getInt("captured"));
224 sendToMain(mail);
225 }
226 else if (data.has("count"))
227 {
228 count_cameras = data.getInt("count");
229 count = count_cameras + 2;
230 Bundle mail = new Bundle();
231 mail.putString("response","capture");
232 mail.putBoolean("success", false);
233 mail.putBoolean("known", true);
234 error("Camera Capture Aborted: Number of cameras connected has changed.");
235 sendToMain(mail);
236 mail = new Bundle();
237 mail.putString("response","detected cameras");
238 mail.putInt("count", data.getInt("count"));
239 sendToMain(mail);
240 }
241 else
242 {
243 error("Camera Capture Aborted: An Unspecified error has occoured.");
244 count = count_cameras + 2;
245 Bundle mail = new Bundle();
246 mail.putString("response","capture");
247 mail.putBoolean("success", false);
248 sendToMain(mail);
249 }
250 count++;
251 }
252 } catch (IOException e) {
253 error("Connection to Server Timed out.");
254 disconnect();
255 } catch (JSONException e) {
256 error(e.getMessage());
257 } catch (NullPointerException e) {
258 error("Unexpected loss of connection to server.");
259 disconnect();
260 }
261 }
262
263 private void montage(String id)
264 {
265 if (server.isClosed())
266 {
267 error("Not connected to a Server. Aborting retreval of Montage.");
268 return;
269 }
270
271 try {
272 String response = sendToServer("Montage:" + id);
273 if (response.length() != 0)
274 {
275 JSONObject data = new JSONObject(response);
276 byte[] image = Base64.decode(data.getString("image_data"), Base64.DEFAULT);
277 Bundle mail = new Bundle();
278 mail.putString("response", "montage");
279 mail.putByteArray("image", image);
280 sendToMain(mail);
281 }
282 } catch (JSONException e) {
283 error(e.getMessage());
284 } catch (NullPointerException e) {
285 error("Unexpected loss of connection to server.");
286 disconnect();
287 }
288 }
289
290 private void shutdown()
291 {
292 if (server.isClosed())
293 {
294 error("Not connected to a Server. Aborting Server Shutdown.");
295 return;
296 }
297 sendToServerNR("exit");
298 warning("Server shutting down... Automaticly disconnecting from server.");
299 disconnect();
300 }
301
302 private void error(String message)
303 {
304 Bundle mail = new Bundle();
305 mail.putString("response","error");
306 mail.putString("message", message);
307 sendToMain(mail);
308 }
309
310 private void warning(String message)
311 {
312 Bundle mail = new Bundle();
313 mail.putString("response","warning");
314 mail.putString("message", message);
315 sendToMain(mail);
316 }
317
318 private void notify(String message)
319 {
320 Bundle mail = new Bundle();
321 mail.putString("response","notify");
322 mail.putString("message", message);
323 sendToMain(mail);
324 }
325
326 private void sendToMain(Bundle message)
327 {
328 Message msg = main_coms.obtainMessage();
329 msg.setData(message);
330 msg.sendToTarget();
331 }
332
333 private String sendToServer(String command)
334 {
335 String response = "";
336 try {
337 upstream.write(command + "\n");
338 upstream.flush();
339 } catch (IOException e) {
340 error("Comunication error: " + e.getMessage());
341 disconnect();
342 }
343 try {
344 response = downstream.readLine();
345 } catch (IOException e) {
346 error("Connection to Server Timed out.");
347 disconnect();
348 }
349 return response;
350 }
351
352 private void sendToServerNR(String command)
353 {
354 try {
355 upstream.write(command + "\n");
356 upstream.flush();
357 } catch (IOException e) {
358 error("Comunication error: " + e.getMessage());
359 disconnect();
360 }
361 }
362
363 private static class NetworkingHandler extends Handler
364 {
365 private final WeakReference<networking> mActivity;
366
367 public NetworkingHandler(networking activity) {
368 mActivity = new WeakReference<networking>(activity);
369 }
370
371 public void handleMessage(Message msg)
372 {
373 super.handleMessage(msg);
374 mActivity.get().processmessage(msg);
375 }
376 }
377}
Note: See TracBrowser for help on using the repository browser.