StatusServiceClient.java
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.RemoteException;
00006 import org.ros.exception.RosRuntimeException;
00007 import org.ros.exception.ServiceNotFoundException;
00008 import org.ros.namespace.GraphName;
00009 import org.ros.namespace.NameResolver;
00010 import org.ros.node.AbstractNodeMain;
00011 import org.ros.node.ConnectedNode;
00012 import org.ros.node.service.ServiceClient;
00013 import org.ros.node.service.ServiceResponseListener;
00014 
00015 import rocon_app_manager_msgs.Constants;
00016 import rocon_app_manager_msgs.Status;
00017 import rocon_app_manager_msgs.StatusRequest;
00018 import rocon_app_manager_msgs.StatusResponse;
00019 
00027 public class StatusServiceClient extends AbstractNodeMain {
00028     private String namespace; // this is the namespace under which all rapp manager services reside.
00029     private ServiceResponseListener<StatusResponse> listener;
00030     private ConnectedNode connectedNode;
00031     // private String applicationNamespace; // namespace under which the rapp manager launches apps.
00032     private String remoteController = "";
00033     private Boolean isAvailable = null; // set this to true if it is free OR we are already controlling it.
00034 
00041     public StatusServiceClient(String namespace, final String ourControllerName) {
00042         this.namespace = namespace;
00043         this.listener = new ServiceResponseListener<StatusResponse>() {
00044             @Override
00045             public void onSuccess(StatusResponse message) {
00046                 //isAvailable = ( message.getRemoteController() == Constants.NO_REMOTE_CONNECTION) ? Boolean.TRUE : Boolean.FALSE;
00047                 if ( message.getRemoteController().equals(Constants.NO_REMOTE_CONNECTION)) {
00048                     Log.i("ApplicationManagement", "rapp manager is available");
00049                     isAvailable = Boolean.TRUE;
00050                 } else {
00051                     if ( ourControllerName.equals(message.getRemoteController())) {
00052                         Log.i("ApplicationManagement", "rapp manager is already being remote controlled by us");
00053                         isAvailable = Boolean.TRUE;
00054                     } else {
00055                         Log.i("ApplicationManagement", "rapp manager is being remote controlled [" + message.getRemoteController() + "]");
00056                         isAvailable = Boolean.FALSE;
00057                     }
00058                 }
00059             }
00060             @Override
00061             public void onFailure(RemoteException e) {
00062                 Log.e("ApplicationManagement", "failed to send status request!");
00063             }
00064         };
00065     }
00066 
00067     public String getRemoteControllerName() { return remoteController; }
00068 
00072     public void waitForResponse() {
00073         while (isAvailable == null) {
00074             try {
00075                 Thread.sleep(100);
00076             } catch (Exception e) {
00077             }
00078         }
00079     }
00080 
00081     public Boolean isAvailable() {
00082         return isAvailable;
00083     }
00084 
00085     @Override
00086     public void onStart(final ConnectedNode connectedNode) {
00087         if (this.connectedNode != null) {
00088             Log.e("ApplicationManagement", "service client instances may only ever be executed once.");
00089             return;
00090         }
00091         this.connectedNode = connectedNode;
00092         NameResolver resolver = this.connectedNode.getResolver().newChild(this.namespace);
00093         String serviceName = resolver.resolve("status").toString();
00094         ServiceClient<StatusRequest, StatusResponse> client;
00095         try {
00096             Log.d("ApplicationManagement", "service client created [" + serviceName + "]");
00097             client = connectedNode.newServiceClient(serviceName,
00098                     Status._TYPE);
00099         } catch (ServiceNotFoundException e) {
00100             Log.w("ApplicationManagement", "service not found [" + serviceName + "]");
00101             throw new RosRuntimeException(e);
00102         } catch (RosRuntimeException e) {
00103             Log.e("ApplicationManagement", "failed to create connection to the rapp manager's status service [" + e.getMessage() + "]");
00104             throw e;
00105         }
00106         final StatusRequest request = client.newMessage();
00107         client.call(request, listener);
00108         Log.d("ApplicationManagement", "service call done [" + serviceName + "]");
00109     }
00110 
00115     @Override
00116     public GraphName getDefaultNodeName() {
00117         return null;
00118     }
00119 }


android_apps
Author(s): Daniel Stonier , Kazuto Murase
autogenerated on Fri Aug 28 2015 10:04:40