GatewayInfoSubscriber.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.RosRuntimeException;
00006 import org.ros.message.MessageListener;
00007 import org.ros.namespace.GraphName;
00008 import org.ros.namespace.NameResolver;
00009 import org.ros.node.AbstractNodeMain;
00010 import org.ros.node.ConnectedNode;
00011 import org.ros.node.topic.Subscriber;
00012 import gateway_msgs.GatewayInfo;
00013 
00017 public class GatewayInfoSubscriber extends AbstractNodeMain {
00018     private MessageListener<GatewayInfo> listener;
00019     private ConnectedNode connectedNode;
00020     private Subscriber<GatewayInfo> subscriber;
00021     private NameResolver resolver;
00022     private String gatewayName = "";
00023     private String errorMessage = "";
00024 
00025     public GatewayInfoSubscriber() {
00026     }
00027 
00028     @Override
00029     public GraphName getDefaultNodeName() {
00030         return null;
00031     }
00032 
00033     public String getGatewayName() { return gatewayName; }
00034 
00040     public void waitForResponse() throws RosRuntimeException {
00041         int count = 0;
00042         while ( this.gatewayName == "" ) {
00043             if ( errorMessage != "" ) {  // errorMessage gets set by an exception in the run method
00044                 throw new RosRuntimeException(errorMessage);
00045             }
00046             try {
00047                 Thread.sleep(100);
00048             } catch (Exception e) {
00049                 throw new RosRuntimeException(e);
00050             }
00051             if ( count == 20 ) {  // timeout.
00052                 Log.e("ApplicationManagement", "timed out waiting for a gateway_info publication");
00053                 throw new RosRuntimeException("timed out waiting for a gateway_info publication");
00054             }
00055             count = count + 1;
00056         }
00057     }
00058 
00067     @Override
00068     public void onStart(final ConnectedNode connectedNode) {
00069         if (this.connectedNode != null) {
00070             Log.e("ApplicationManagement", "gateway info subscribers may only ever be executed once.");
00071             return;
00072         }
00073         this.connectedNode = connectedNode;
00074         NameResolver resolver = this.connectedNode.getResolver().newChild("gateway");
00075         String topicName = resolver.resolve("gateway_info").toString();
00076         subscriber = connectedNode.newSubscriber(topicName, "gateway_msgs/GatewayInfo");
00077         this.listener = new MessageListener<GatewayInfo>() {
00078             @Override
00079             public void onNewMessage(GatewayInfo message) {
00080                 gatewayName = message.getName();
00081                 Log.i("ApplicationManagement", "gateway info retrieved successfully [" + gatewayName + "]");
00082             }
00083         };
00084         subscriber.addMessageListener(this.listener);
00085         Log.d("ApplicationManagement", "latched gateway info subscriber created [" + topicName + "]");
00086     }
00087 }


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