00001 package com.github.rosjava.android_remocons.common_tools.apps; 00002 00003 import android.util.Log; 00004 00005 import com.github.rosjava.android_remocons.common_tools.master.MasterDescription; 00006 00007 import org.ros.master.client.MasterStateClient; 00008 import org.ros.master.client.SystemState; 00009 import org.ros.master.client.TopicSystemState; 00010 import org.ros.namespace.GraphName; 00011 import org.ros.namespace.NameResolver; 00012 import org.ros.node.AbstractNodeMain; 00013 import org.ros.node.ConnectedNode; 00014 00015 public class MasterNameResolver extends AbstractNodeMain { 00016 00017 private MasterDescription currentMaster; 00018 private NameResolver masterNameResolver; 00019 private GraphName masterName; 00020 private ConnectedNode connectedNode; 00021 private boolean resolved = false; 00022 00023 public MasterNameResolver() { 00024 } 00025 00026 public void setMaster(MasterDescription currentMaster) { 00027 this.currentMaster = currentMaster; 00028 } 00029 00030 @Override 00031 public GraphName getDefaultNodeName() { 00032 return null; 00033 } 00034 00035 public void setMasterName(String name) { 00036 this.masterName = GraphName.of(name); 00037 } 00038 00045 public String getMasterName() { 00046 return this.masterName.toString(); 00047 } 00048 00049 public void resetMasterName(String name) { 00050 masterNameResolver = connectedNode.getResolver().newChild(name); 00051 } 00052 00063 public NameResolver getMasterNameSpace() { 00064 return masterNameResolver; 00065 } 00066 00074 public void waitForResolver() { 00075 while (!resolved) { 00076 try { 00077 Thread.sleep(100); 00078 } catch (Exception e) { 00079 Log.w("MasterRemocon", "Master name waitForResolver caught an arbitrary exception"); 00080 } 00081 } 00082 } 00083 00084 @Override 00094 public void onStart(final ConnectedNode connectedNode) { 00095 this.connectedNode = connectedNode; 00096 if (currentMaster != null) { 00097 masterName = GraphName.of(currentMaster.getAppsNameSpace()); 00098 } else { 00099 // This is duplicated in PlatformInfoServiceClient and could be better stored somewhere, but it's not much. 00100 MasterStateClient masterClient = new MasterStateClient(this.connectedNode, this.connectedNode.getMasterUri()); 00101 SystemState systemState = masterClient.getSystemState(); 00102 for (TopicSystemState topic : systemState.getTopics()) { 00103 String topicName = topic.getTopicName(); 00104 GraphName graph_name = GraphName.of(topicName); 00105 if ( graph_name.getBasename().toString().equals("app_list") ) { 00106 masterName = graph_name.getParent().toRelative(); 00107 Log.i("ApplicationManagement", "Configuring master namespace resolver [" + masterName + "]"); 00108 break; 00109 } 00110 } 00111 } 00112 masterNameResolver = connectedNode.getResolver().newChild(masterName); 00113 resolved = true; 00114 } 00115 }