Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 package com.github.rosjava.android_apps.application_management;
00018
00019 import android.util.Log;
00020
00021 import org.ros.exception.RosRuntimeException;
00022 import org.ros.exception.ServiceNotFoundException;
00023 import org.ros.message.MessageListener;
00024 import org.ros.namespace.GraphName;
00025 import org.ros.namespace.NameResolver;
00026 import org.ros.node.AbstractNodeMain;
00027 import org.ros.node.ConnectedNode;
00028 import org.ros.node.service.ServiceClient;
00029 import org.ros.node.service.ServiceResponseListener;
00030 import org.ros.node.topic.Subscriber;
00031
00032 import rocon_app_manager_msgs.AppList;
00033 import rocon_app_manager_msgs.GetAppList;
00034 import rocon_app_manager_msgs.GetAppListRequest;
00035 import rocon_app_manager_msgs.GetAppListResponse;
00036 import rocon_app_manager_msgs.StartApp;
00037 import rocon_app_manager_msgs.StartAppRequest;
00038 import rocon_app_manager_msgs.StartAppResponse;
00039 import rocon_app_manager_msgs.StopApp;
00040 import rocon_app_manager_msgs.StopAppRequest;
00041 import rocon_app_manager_msgs.StopAppResponse;
00042
00064 public class AppManager extends AbstractNodeMain {
00065
00066
00067 static public final String PACKAGE = "com.github.rosjava.android_apps.application_management.AppManager";
00068 private static final String startTopic = "start_app";
00069 private static final String stopTopic = "stop_app";
00070 private static final String listService = "list_apps";
00071
00072 private String appName;
00073 private NameResolver resolver;
00074 private ServiceResponseListener<StartAppResponse> startServiceResponseListener;
00075 private ServiceResponseListener<StopAppResponse> stopServiceResponseListener;
00076 private ServiceResponseListener<GetAppListResponse> listServiceResponseListener;
00077 private MessageListener<AppList> appListListener;
00078 private Subscriber<AppList> subscriber;
00079
00080 private ConnectedNode connectedNode;
00081 private String function = null;
00082
00083 public AppManager(final String appName, NameResolver resolver) {
00084 this.appName = appName;
00085 this.resolver = resolver;
00086 }
00087
00088 public AppManager(final String appName) {
00089 this.appName = appName;
00090 }
00091
00092 public AppManager() {
00093
00094 }
00095
00096 public void setFunction(String function) {
00097 this.function = function;
00098 }
00099
00100 public void setAppName(String appName) {
00101 this.appName = appName;
00102 }
00103
00104 public void setAppListSubscriber(MessageListener<AppList> appListListener) {
00105 this.appListListener = appListListener;
00106 }
00107
00108 public void setStartService(
00109 ServiceResponseListener<StartAppResponse> startServiceResponseListener) {
00110 this.startServiceResponseListener = startServiceResponseListener;
00111 }
00112
00113 public void setStopService(
00114 ServiceResponseListener<StopAppResponse> stopServiceResponseListener) {
00115 this.stopServiceResponseListener = stopServiceResponseListener;
00116 }
00117
00118 public void setListService(
00119 ServiceResponseListener<GetAppListResponse> listServiceResponseListener) {
00120 this.listServiceResponseListener = listServiceResponseListener;
00121 }
00122
00123 public void continuouslyListApps() {
00124 subscriber = connectedNode.newSubscriber(resolver.resolve("app_list"),"rocon_app_manager_msgs/AppList");
00125 subscriber.addMessageListener(this.appListListener);
00126 }
00127
00128 public void startApp() {
00129 String startTopic = resolver.resolve(this.startTopic).toString();
00130
00131 ServiceClient<StartAppRequest, StartAppResponse> startAppClient;
00132 try {
00133 Log.d("ApplicationManagement", "start app service client created [" + startTopic + "]");
00134 startAppClient = connectedNode.newServiceClient(startTopic,
00135 StartApp._TYPE);
00136 } catch (ServiceNotFoundException e) {
00137 Log.w("ApplicationManagement", "start app service not found [" + startTopic + "]");
00138 throw new RosRuntimeException(e);
00139 }
00140 final StartAppRequest request = startAppClient.newMessage();
00141 request.setName(appName);
00142 startAppClient.call(request, startServiceResponseListener);
00143 Log.d("ApplicationManagement", "start app service call done [" + startTopic + "]");
00144 }
00145
00146 public void stopApp() {
00147 String stopTopic = resolver.resolve(this.stopTopic).toString();
00148
00149 ServiceClient<StopAppRequest, StopAppResponse> stopAppClient;
00150 try {
00151 Log.d("ApplicationManagement", "Stop app service client created");
00152 stopAppClient = connectedNode.newServiceClient(stopTopic,
00153 StopApp._TYPE);
00154 } catch (ServiceNotFoundException e) {
00155 Log.w("ApplicationManagement", "Stop app service not found");
00156 throw new RosRuntimeException(e);
00157 }
00158 final StopAppRequest request = stopAppClient.newMessage();
00159
00160 stopAppClient.call(request, stopServiceResponseListener);
00161 Log.d("ApplicationManagement", "Stop app service call done");
00162 }
00163
00164 public void listApps() {
00165 String listService = resolver.resolve(this.listService).toString();
00166
00167 ServiceClient<GetAppListRequest, GetAppListResponse> listAppsClient;
00168 try {
00169 Log.d("ApplicationManagement", "List app service client created [" + listService + "]");
00170 listAppsClient = connectedNode.newServiceClient(listService,
00171 GetAppList._TYPE);
00172 } catch (ServiceNotFoundException e) {
00173 Log.w("ApplicationManagement", "List app service not found [" + listService + "]");
00174 throw new RosRuntimeException(e);
00175 }
00176 final GetAppListRequest request = listAppsClient.newMessage();
00177 listAppsClient.call(request, listServiceResponseListener);
00178 Log.d("ApplicationManagement", "List apps service call done [" + listService + "]");
00179 }
00180
00181 @Override
00182 public GraphName getDefaultNodeName() {
00183 return null;
00184 }
00185
00194 @Override
00195 public void onStart(final ConnectedNode connectedNode) {
00196 if (this.connectedNode != null) {
00197 Log.e("ApplicationManagement", "app manager instances may only ever be executed once [" + function + "].");
00198 return;
00199 }
00200 this.connectedNode = connectedNode;
00201 if (function.equals("start")) {
00202 startApp();
00203 } else if (function.equals("stop")) {
00204 stopApp();
00205 } else if (function.equals("list")) {
00206 listApps();
00207 } else if (function.equals("list_apps")) {
00208 continuouslyListApps();
00209 }
00210 }
00211 }