Go to the documentation of this file.00001 package com.github.rosjava.android_apps.application_management.rapp_manager;
00002
00003 import android.util.Log;
00004
00005 import org.ros.exception.ServiceNotFoundException;
00006 import org.ros.exception.RemoteException;
00007 import org.ros.exception.RosRuntimeException;
00008 import org.ros.master.client.TopicSystemState;
00009 import org.ros.master.client.SystemState;
00010 import org.ros.master.client.MasterStateClient;
00011 import org.ros.namespace.GraphName;
00012 import org.ros.namespace.NameResolver;
00013 import org.ros.node.AbstractNodeMain;
00014 import org.ros.node.ConnectedNode;
00015 import org.ros.node.service.ServiceClient;
00016 import org.ros.node.service.ServiceResponseListener;
00017
00018 import rocon_app_manager_msgs.Icon;
00019 import rocon_app_manager_msgs.PlatformInfo;
00020 import rocon_app_manager_msgs.GetPlatformInfo;
00021 import rocon_app_manager_msgs.GetPlatformInfoRequest;
00022 import rocon_app_manager_msgs.GetPlatformInfoResponse;
00023
00031 public class PlatformInfoServiceClient extends AbstractNodeMain {
00032 private String namespace;
00033 private String robotUniqueName;
00034 private ServiceResponseListener<GetPlatformInfoResponse> platformInfoListener;
00035 private PlatformInfo platformInfo;
00036 private ConnectedNode connectedNode;
00037 private String errorMessage = "";
00038
00044 public PlatformInfoServiceClient(String namespace) {
00045 this.namespace = namespace;
00046 this._createListeners();
00047 }
00048
00049 public PlatformInfoServiceClient() { this._createListeners(); }
00050
00051 private void _createListeners() {
00052 this.platformInfoListener = new ServiceResponseListener<GetPlatformInfoResponse>() {
00053 @Override
00054 public void onSuccess(GetPlatformInfoResponse message) {
00055 Log.i("ApplicationManagement", "platform info retrieved successfully");
00056 platformInfo = message.getPlatformInfo();
00057 }
00058
00059 @Override
00060 public void onFailure(RemoteException e) {
00061 Log.e("ApplicationManagement", "failed to get platform information!");
00062 }
00063 };
00064 }
00065
00071 public void waitForResponse() throws ServiceNotFoundException {
00072 int count = 0;
00073 while ( platformInfo == null ) {
00074 if ( errorMessage != "" ) {
00075 throw new ServiceNotFoundException(errorMessage);
00076 }
00077 try {
00078 Thread.sleep(100);
00079 } catch (Exception e) {
00080 throw new RosRuntimeException(e);
00081 }
00082 if ( count == 20 ) {
00083 throw new ServiceNotFoundException("timed out waiting for a platform_info service response");
00084 }
00085 count = count + 1;
00086 }
00087 }
00088
00089 public PlatformInfo getPlatformInfo() {
00090 return platformInfo;
00091 }
00092
00093 public String getRobotAppManagerNamespace() {
00094 return this.namespace;
00095 }
00096
00101 public String getRobotUniqueName() {
00102 return this.robotUniqueName;
00103 }
00104
00105 public String getRobotType() {
00106 return this.platformInfo.getRobot();
00107 }
00108
00109 public Icon getRobotIcon() {
00110 return this.platformInfo.getIcon();
00111 }
00112
00119 @Override
00120 public void onStart(final ConnectedNode connectedNode) {
00121 if (this.connectedNode != null) {
00122 errorMessage = "service client instances may only ever be executed once";
00123 Log.e("ApplicationManagement", errorMessage + ".");
00124 return;
00125 }
00126 this.connectedNode = connectedNode;
00127
00128
00129 int count = 0;
00130 MasterStateClient masterClient = new MasterStateClient(this.connectedNode, this.connectedNode.getMasterUri());
00131 while ( this.namespace == null ) {
00132 SystemState systemState = masterClient.getSystemState();
00133 for (TopicSystemState topic : systemState.getTopics()) {
00134 String name = topic.getTopicName();
00135 GraphName graph_name = GraphName.of(name);
00136 if ( graph_name.getBasename().toString().equals("app_list") ) {
00137 this.namespace = graph_name.getParent().toString();
00138 this.robotUniqueName = graph_name.getParent().toRelative().toString();
00139 Log.i("ApplicationManagement", "found the namespace for the robot app manager [" + this.namespace + "]");
00140 break;
00141 }
00142 }
00143 try {
00144 Thread.sleep(200);
00145 } catch (Exception e) {
00146 errorMessage = "interrupted while looking for the robot app manager.";
00147 Log.w("ApplicationManagement", errorMessage);
00148 return;
00149 }
00150 if ( count == 10 ) {
00151 errorMessage = "Timed out waiting for the robot app manager to appear.";
00152 Log.w("ApplicationManagement", errorMessage);
00153 return;
00154 }
00155 count = count + 1;
00156 }
00157
00158
00159 NameResolver resolver = this.connectedNode.getResolver().newChild(this.namespace);
00160 String serviceName = resolver.resolve("platform_info").toString();
00161 ServiceClient<GetPlatformInfoRequest, GetPlatformInfoResponse> client;
00162 try {
00163 client = connectedNode.newServiceClient(serviceName,
00164 GetPlatformInfo._TYPE);
00165 Log.d("ApplicationManagement", "service client created [" + serviceName + "]");
00166 } catch (ServiceNotFoundException e) {
00167 errorMessage = "Service not found [" + serviceName + "]";
00168 Log.w("ApplicationManagement", errorMessage);
00169 return;
00170 } catch (RosRuntimeException e) {
00171 errorMessage = "Couldn't connect to the platform_info service [is ROS_IP set?][" + e.getMessage() + "]";
00172 Log.e("ApplicationManagement", errorMessage);
00173 return;
00174 }
00175 final GetPlatformInfoRequest request = client.newMessage();
00176 client.call(request, platformInfoListener);
00177 Log.d("ApplicationManagement", "service call done [" + serviceName + "]");
00178 }
00179
00184 @Override
00185 public GraphName getDefaultNodeName() {
00186 return null;
00187 }
00188 }