Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 package com.github.rosjava.android_apps.application_management;
00035
00036 import android.util.Log;
00037 import java.io.BufferedReader;
00038 import java.io.InputStreamReader;
00039 import java.net.URI;
00040 import org.apache.http.HttpResponse;
00041 import org.apache.http.client.HttpClient;
00042 import org.apache.http.client.methods.HttpGet;
00043 import org.apache.http.impl.client.DefaultHttpClient;
00049 public class ControlChecker {
00050 public interface SuccessHandler {
00052 void handleSuccess();
00053 }
00054 public interface FailureHandler {
00059 void handleFailure(String reason);
00060 }
00061 public interface EvictionHandler {
00063 boolean doEviction(String user);
00064 }
00065 public interface StartHandler {
00067 void handleStarting();
00068 }
00069 private CheckerThread checkerThread;
00070 private SuccessHandler robotReadyCallback;
00071 private FailureHandler failureCallback;
00072 private EvictionHandler evictionCallback;
00073 private StartHandler startCallback;
00074 private boolean doStart;
00076 public ControlChecker(SuccessHandler robotReadyCallback, FailureHandler failureCallback) {
00077 this.robotReadyCallback = robotReadyCallback;
00078 this.failureCallback = failureCallback;
00079 this.evictionCallback = new EvictionHandler() {
00080 public boolean doEviction(String user) {
00081 return false;
00082 }};
00083 this.startCallback = null;
00084 this.doStart = false;
00085 }
00087 public ControlChecker(SuccessHandler robotReadyCallback, FailureHandler failureCallback, EvictionHandler evictionCallback) {
00088 this.robotReadyCallback = robotReadyCallback;
00089 this.failureCallback = failureCallback;
00090 this.evictionCallback = evictionCallback;
00091 this.startCallback = null;
00092 this.doStart = true;
00093 }
00095 public ControlChecker(SuccessHandler robotReadyCallback, FailureHandler failureCallback, EvictionHandler evictionCallback, StartHandler startCallback) {
00096 this.robotReadyCallback = robotReadyCallback;
00097 this.failureCallback = failureCallback;
00098 this.evictionCallback = evictionCallback;
00099 this.startCallback = startCallback;
00100 this.doStart = true;
00101 }
00106 public void beginChecking(RobotId robotId) {
00107 stopChecking();
00108
00109 if (robotId.getControlUri() == null) {
00110 robotReadyCallback.handleSuccess();
00111 return;
00112 }
00113 checkerThread = new CheckerThread(robotId);
00114 checkerThread.start();
00115 }
00117 public void stopChecking() {
00118 if (checkerThread != null && checkerThread.isAlive()) {
00119 checkerThread.interrupt();
00120 }
00121 }
00122 private class CheckerThread extends Thread {
00123 private RobotId robotId;
00124 public CheckerThread(RobotId robotId) {
00125 this.robotId = robotId;
00126 setDaemon(true);
00127
00128 setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
00129 @Override
00130 public void uncaughtException(Thread thread, Throwable ex) {
00131 failureCallback.handleFailure("exception: " + ex.getMessage());
00132 }
00133 });
00134 }
00135 private String getPage(String uri) {
00136 try {
00137 HttpClient client = new DefaultHttpClient();
00138 HttpGet request = new HttpGet();
00139 request.setURI(new URI(uri));
00140 HttpResponse response = client.execute(request);
00141 BufferedReader in = new BufferedReader
00142 (new InputStreamReader(response.getEntity().getContent()));
00143 StringBuffer sb = new StringBuffer("");
00144 String line = "";
00145 String NL = System.getProperty("line.separator");
00146 while ((line = in.readLine()) != null) {
00147 sb.append(line + NL);
00148 }
00149 in.close();
00150 String page = sb.toString();
00151 return page;
00152 } catch (java.io.IOException ex) {
00153 Log.e("ControlChecker", "IOError: " + uri, ex);
00154 } catch (java.net.URISyntaxException ex) {
00155 Log.e("ControlChecker", "URI Invalid: " + uri, ex);
00156 }
00157 return null;
00158 }
00159 final String USER_TAG = "ACTIVE_USER:";
00160 final String VALID_USER = "applications";
00161 final String NO_USER = "None";
00162 private String getActiveUser() {
00163 String page = getPage(robotId.getControlUri() + "?action=GET_STATE");
00164 if (page == null) {
00165 return null;
00166 }
00167 String[] pageLines = page.split("\n");
00168 String activeUser = NO_USER;
00169
00170 for (String i : pageLines) {
00171 if (i.trim().indexOf(USER_TAG) >= 0) {
00172 activeUser = i.trim().substring(USER_TAG.length() + 1).trim();
00173 }
00174 }
00175 return activeUser;
00176 }
00177 @Override
00178 public void run() {
00179 try {
00180 String activeUser = getActiveUser();
00181 Log.d("ControlChecker", "Active user: " + activeUser);
00182 if (activeUser == null) {
00183 failureCallback.handleFailure("Could not connect to the control page");
00184 return;
00185 }
00186 boolean goodState = false;
00187 boolean badUser = false;
00188 if (activeUser.equals(VALID_USER)) {
00189 goodState = true;
00190 } else if (!activeUser.equals(NO_USER)) {
00191 badUser = true;
00192 }
00193 if (goodState) {
00194 robotReadyCallback.handleSuccess();
00195 } else {
00196 if (badUser) {
00197 if (evictionCallback.doEviction(activeUser)) {
00198 Log.d("ControlChecker", "Stopping robot");
00199 getPage(robotId.getControlUri() + "?action=STOP_ROBOT");
00200 } else {
00201 failureCallback.handleFailure("Need to evict current user inorder to connect");
00202 return;
00203 }
00204 }
00205 if (doStart) {
00206 if (startCallback != null) {
00207 startCallback.handleStarting();
00208 }
00209 Log.d("ControlChecker", "Starting robot");
00210 getPage(robotId.getControlUri() + "?action=START_ROBOT");
00211
00212 int i = 0;
00213
00214 while (i < 30 && !VALID_USER.equals(getActiveUser())) {
00215 i++;
00216 Thread.sleep(1000L);
00217 }
00218
00219 if (VALID_USER.equals(getActiveUser())) {
00220 robotReadyCallback.handleSuccess();
00221 } else {
00222 failureCallback.handleFailure("Re-started the robot, but it is still not working");
00223 }
00224 } else {
00225
00226 failureCallback.handleFailure("Robot not started");
00227 }
00228 }
00229 } catch (Throwable ex) {
00230 Log.e("ControlChecker", "Exception while checking control URI "
00231 + robotId.getControlUri(), ex);
00232 failureCallback.handleFailure(ex.toString());
00233 }
00234 }
00235 }
00236 }