InvitationServiceClient.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.Invite;
00016 import rocon_app_manager_msgs.InviteRequest;
00017 import rocon_app_manager_msgs.InviteResponse;
00018 
00023 public class InvitationServiceClient extends AbstractNodeMain {
00024     private String namespace; // this is the namespace under which all rapp manager services reside.
00025     private String gatewayName; // this is the name of our gateway controller
00026     private ServiceResponseListener<InviteResponse> listener;
00027     private Boolean invitationAccepted = null;
00028     private ConnectedNode connectedNode;
00029     private Boolean cancel;
00030 
00036     public InvitationServiceClient(String gatewayName, String namespace) {
00037         init(gatewayName, namespace, Boolean.FALSE);
00038     }
00039 
00040     public InvitationServiceClient(String gatewayName, String namespace, Boolean cancel) {
00041         init(gatewayName, namespace, cancel);
00042     }
00043 
00044     private void init(String gatewayName, String namespace, Boolean cancel) {
00045         this.gatewayName = gatewayName;
00046         this.namespace = namespace;
00047         this.cancel = cancel;
00048         this.listener = new ServiceResponseListener<InviteResponse>() {
00049             @Override
00050             public void onSuccess(InviteResponse message) {
00051                 if ( invitationAccepted = message.getResult() ) {
00052                     Log.i("ApplicationManagement", "invitation accepted");
00053                 } else {
00054                     Log.w("ApplicationManagement", "invitation rejected");
00055                 }
00056             }
00057             @Override
00058             public void onFailure(RemoteException e) {
00059                 Log.e("ApplicationManagement", "failed to send invitation!");
00060             }
00061         };
00062     }
00063 
00069     public Boolean waitForResponse() {
00070         int count = 0;
00071         while ( (invitationAccepted == null) && (count < 20) ) {
00072             try {
00073                 Thread.sleep(100);
00074             } catch (Exception e) {
00075             }
00076             count = count + 1;
00077         }
00078         if ( count < 20 ) {
00079             return Boolean.TRUE;
00080         } else {
00081             return Boolean.FALSE;
00082         }
00083     }
00084 
00085     public Boolean getInvitationResult() {
00086         return invitationAccepted;
00087     }
00088 
00089     @Override
00090     public void onStart(final ConnectedNode connectedNode) {
00091         if (this.connectedNode != null) {
00092             Log.e("ApplicationManagement", "service client instances may only ever be executed once.");
00093             return;
00094         }
00095         this.connectedNode = connectedNode;
00096         NameResolver resolver = this.connectedNode.getResolver().newChild(this.namespace);
00097         //NameResolver resolver = this.connectedNode.getResolver().newChild("pairing_master");
00098         String serviceName = resolver.resolve("invite").toString();
00099         ServiceClient<InviteRequest, InviteResponse> client;
00100         try {
00101             Log.d("ApplicationManagement", "service client created [" + serviceName + "]");
00102             client = connectedNode.newServiceClient(serviceName,
00103                     Invite._TYPE);
00104         } catch (ServiceNotFoundException e) {
00105             Log.w("ApplicationManagement", "service not found [" + serviceName + "]");
00106             throw new RosRuntimeException(e);
00107         } catch (RosRuntimeException e) {
00108             Log.e("ApplicationManagement", "failed to create connection to the rapp manager's invite service [" + e.getMessage() + "]");
00109             throw e;
00110         }
00111         final InviteRequest request = client.newMessage();
00112         request.setRemoteTargetName(this.gatewayName);
00113         request.setApplicationNamespace(this.namespace);
00114         request.setCancel(this.cancel);
00115         client.call(request, listener);
00116         Log.d("ApplicationManagement", "service call done [" + serviceName + "]");
00117     }
00118 
00123     @Override
00124     public GraphName getDefaultNodeName() {
00125         return null;
00126     }
00127 }


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