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
00035
00036
00037 package com.github.rosjava.android_remocons.common_tools;
00038
00039 import android.util.Log;
00040
00041 import com.github.rosjava.android_apps.application_management.ConcertDescription;
00042 import com.github.rosjava.android_apps.application_management.MasterId;
00043
00044 import org.ros.address.InetAddressFactory;
00045 import org.ros.android.NodeMainExecutorService;
00046 import org.ros.internal.node.client.ParameterClient;
00047 import org.ros.internal.node.server.NodeIdentifier;
00048 import org.ros.namespace.GraphName;
00049 import org.ros.node.NodeConfiguration;
00050
00051 import java.net.URI;
00052 import java.net.URISyntaxException;
00053 import java.util.Date;
00054
00055 import static com.github.rosjava.android_remocons.common_tools.RoconConstants.*;
00056
00066 public class ConcertChecker {
00067 public interface ConcertDescriptionReceiver {
00071 void receive(ConcertDescription concertDescription);
00072 }
00073
00074 public interface FailureHandler {
00079 void handleFailure(String reason);
00080 }
00081
00082 private CheckerThread checkerThread;
00083 private ConcertDescriptionReceiver foundConcertCallback;
00084 private FailureHandler failureCallback;
00085
00089 public ConcertChecker(ConcertDescriptionReceiver foundConcertCallback, FailureHandler failureCallback) {
00090 this.foundConcertCallback = foundConcertCallback;
00091 this.failureCallback = failureCallback;
00092 }
00093
00098 public void beginChecking(MasterId masterId) {
00099 stopChecking();
00100 if (masterId.getMasterUri() == null) {
00101 failureCallback.handleFailure("empty concert URI");
00102 return;
00103 }
00104 URI uri;
00105 try {
00106 uri = new URI(masterId.getMasterUri());
00107 } catch (URISyntaxException e) {
00108 failureCallback.handleFailure("invalid concert URI");
00109 return;
00110 }
00111 checkerThread = new CheckerThread(masterId, uri);
00112 checkerThread.start();
00113 }
00114
00118 public void stopChecking() {
00119 if (checkerThread != null && checkerThread.isAlive()) {
00120 checkerThread.interrupt();
00121 }
00122 }
00123
00124 private class CheckerThread extends Thread {
00125 private URI concertUri;
00126 private MasterId masterId;
00127
00128 public CheckerThread(MasterId masterId, URI concertUri) {
00129 this.concertUri = concertUri;
00130 this.masterId = masterId;
00131 setDaemon(true);
00132
00133 setUncaughtExceptionHandler(new UncaughtExceptionHandler() {
00134 @Override
00135 public void uncaughtException(Thread thread, Throwable ex) {
00136 failureCallback.handleFailure("exception: " + ex.getMessage());
00137 }
00138 });
00139 }
00140
00141 @Override
00142 public void run() {
00143 try {
00144
00145
00146 ParameterClient paramClient = new ParameterClient(
00147 NodeIdentifier.forNameAndUri("/concert_checker", concertUri.toString()), concertUri);
00148 String name = (String) paramClient.getParam(GraphName.of(CONCERT_NAME_PARAM)).getResult();
00149 Log.i("ConcertRemocon", "Concert " + name + " found; retrieve additional information");
00150
00151 NodeMainExecutorService nodeMainExecutorService = new NodeMainExecutorService();
00152 NodeConfiguration nodeConfiguration = NodeConfiguration.newPublic(
00153 InetAddressFactory.newNonLoopback().getHostAddress(), concertUri);
00154
00155
00156 ListenerNode<concert_msgs.ConcertInfo> readInfoTopic =
00157 new ListenerNode(CONCERT_INFO_TOPIC, concert_msgs.ConcertInfo._TYPE);
00158 nodeMainExecutorService.execute(readInfoTopic, nodeConfiguration.setNodeName("read_info_node"));
00159 readInfoTopic.waitForResponse();
00160
00161
00162 concert_msgs.ConcertInfo concertInfo = readInfoTopic.getLastMessage();
00163
00164 String concertName = concertInfo.getName();
00165 String concertDesc = concertInfo.getDescription();
00166 rocon_std_msgs.Icon concertIcon = concertInfo.getIcon();
00167
00168 if (name.equals(concertName) == false)
00169 Log.w("ConcertRemocon", "Concert names from parameter and topic differs; we use the later");
00170
00171
00172 ListenerNode<concert_msgs.Roles> readRolesTopic =
00173 new ListenerNode(CONCERT_ROLES_TOPIC, concert_msgs.Roles._TYPE);
00174 nodeMainExecutorService.execute(readRolesTopic, nodeConfiguration.setNodeName("concert_roles_node"));
00175 readRolesTopic.waitForResponse();
00176
00177 nodeMainExecutorService.shutdownNodeMain(readInfoTopic);
00178 nodeMainExecutorService.shutdownNodeMain(readRolesTopic);
00179
00180
00181 Date timeLastSeen = new Date();
00182 ConcertDescription description = new ConcertDescription(masterId, concertName, concertDesc, concertIcon, timeLastSeen);
00183 Log.i("ConcertRemocon", "Concert is available");
00184 description.setConnectionStatus(ConcertDescription.OK);
00185 description.setUserRoles(readRolesTopic.getLastMessage());
00186 foundConcertCallback.receive(description);
00187 return;
00188 } catch (RuntimeException e) {
00189
00190 Log.w("ConcertRemocon", "could not find concert [" + concertUri + "][" + e.toString() + "]");
00191 failureCallback.handleFailure(e.toString());
00192 } catch (Throwable e) {
00193 Log.w("ConcertRemocon", "exception while creating node in concert checker for URI " + concertUri, e);
00194 failureCallback.handleFailure(e.toString());
00195 }
00196 }
00197 }
00198 }