XActivity .java
publicclass XActivityextendsRosActivity
{
privateImuPublisher imu_pub;
privateSensorManager mSensorManager;
public XActivity()
{
super("Ros Android Sensors Driver","Ros Android Sensors Driver");
}
@Override
protectedvoid onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mSensorManager =(SensorManager)this.getSystemService(SENSOR_SERVICE);
}
@Override
protectedvoid onResume()
{
super.onResume();
}
@Override
protectedvoid init(NodeMainExecutor nodeMainExecutor)
{
NodeConfiguration nodeConfiguration3 =NodeConfiguration.newPublic(InetAddressFactory.newNonLoopback().getHostAddress());
nodeConfiguration3.setMasterUri(getMasterUri());
nodeConfiguration3.setNodeName("android_sensors_driver_imu");
this.imu_pub =newImuPublisher(mSensorManager);
nodeMainExecutor.execute(this.imu_pub, nodeConfiguration3);
}
}
其中的 nodeConfiguration3.setNodeName("android_sensors_driver_imu");
觉得很奇怪,在ImuPublisher中已经设置了节点名称,到底是谁在起作用呢?
ImuPublisher.java
publicGraphName getDefaultNodeName()
{
//节点名称
returnGraphName.of("android_sensors_driver/imuPublisher");
}
需要跟踪 nodeMainExecutor.execute(this.imu_pub, nodeConfiguration3);方法
XActivity .java
@Override
protectedvoid init(NodeMainExecutor nodeMainExecutor)
{
...
}
是从父类继承过来的,父类再启动中会startServic
RosActivity.java
protectedRosActivity(String notificationTicker,String notificationTitle, URI customMasterUri){
super();
this.notificationTicker = notificationTicker;
this.notificationTitle = notificationTitle;
nodeMainExecutorServiceConnection =newNodeMainExecutorServiceConnection(customMasterUri);
}
@Override
protectedvoid onStart(){
super.onStart();
bindNodeMainExecutorService();//启动service
}
//startService+bindService
//如果我们想保持和 Service 的通信,又不想让 Service 随着 Activity 退出而退出呢?你可以先 startService() 然后再 bindService() 。
//当你不需要绑定的时候就执行 unbindService() 方法,执行这个方法只会触发 Service 的 onUnbind() 而不会把这个 Service 销毁。
//这样就可以既保持和 Service 的通信,也不会随着 Activity 销毁而销毁了
protectedvoid bindNodeMainExecutorService(){
Intent intent =newIntent(this,NodeMainExecutorService.class);
intent.setAction(NodeMainExecutorService.ACTION_START);
intent.putExtra(NodeMainExecutorService.EXTRA_NOTIFICATION_TICKER, notificationTicker);
intent.putExtra(NodeMainExecutorService.EXTRA_NOTIFICATION_TITLE, notificationTitle);
startService(intent);//启动服务
Preconditions.checkState(
bindService(intent, nodeMainExecutorServiceConnection, BIND_AUTO_CREATE),//绑定连接状态回调
"Failed to bind NodeMainExecutorService.");
}
这个servic连接上的时候会回调,回调会取出binder,((NodeMainExecutorService.LocalBinder) binder).getService()得到nodeMainExecutorService 然后init();
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
RosActivity.java privatefinalclassNodeMainExecutorServiceConnectionimplementsServiceConnection{ private URI customMasterUri; publicNodeMainExecutorServiceConnection(URI customUri){ super (); customMasterUri = customUri; } @Override publicvoid onServiceConnected(ComponentName name,IBinder binder){ nodeMainExecutorService =((NodeMainExecutorService.LocalBinder) binder).getService(); //取出发布的操作类 if (customMasterUri != null ){ nodeMainExecutorService.setMasterUri(customMasterUri); nodeMainExecutorService.setRosHostname(getDefaultHostAddress()); } nodeMainExecutorService.addListener(newNodeMainExecutorServiceListener(){ @Override publicvoid onShutdown(NodeMainExecutorService nodeMainExecutorService){ // We may have added multiple shutdown listeners and we only want to // call finish() once. if (!RosActivity. this .isFinishing()){ RosActivity. this .finish(); } } }); if (getMasterUri()== null ){ startMasterChooser(); } else { init(); } } @Override publicvoid onServiceDisconnected(ComponentName name){ } }; |
init()里面就把nodeMainExecutorService传到了子类,子类(执行发布操作的类) 拿到 nodeMainExecutor并执行nodeMainExecutor.execute(this.imu_pub, nodeConfiguration3);
1
2
3
4
5
6
7
8
9
10
11
12
13
|
RosActivity.java protectedvoid init(){ // Run init() in a new thread as a convenience since it often requires // network access. newAsyncTask<Void,Void,Void>(){ @Override protectedVoid doInBackground(Void... params){ RosActivity. this .init(nodeMainExecutorService); //调子类的方法,传递发布的操作类 returnnull; } }.execute(); } |
所以要去查nodeMainExecutor的来源,就要找到启动的service :NodeMainExecutorService
1
2
3
4
5
6
7
8
9
10
11
12
13
|
nodeMainExecutor.execute( this .imu_pub, nodeConfiguration3);调用了如下代码 NodeMainExecutorService.java @Override publicvoid execute(NodeMain nodeMain,NodeConfiguration nodeConfiguration, Collection<NodeListener> nodeListeneners){ nodeMainExecutor.execute(nodeMain, nodeConfiguration, nodeListeneners); //主界面调用的execute到了这里 } @Override publicvoid execute(NodeMain nodeMain,NodeConfiguration nodeConfiguration){ execute(nodeMain, nodeConfiguration, null ); } |
其中的nodeMainExecutor在构造NodeMainExecutorService的时候就初始化了
1
2
3
4
5
6
7
8
9
10
11
|
NodeMainExecutorService.java publicNodeMainExecutorService(){ super (); rosHostname = null ; nodeMainExecutor =DefaultNodeMainExecutor.newDefault(); //初始化 binder =newLocalBinder(); listeners = newListenerGroup<NodeMainExecutorServiceListener>( nodeMainExecutor.getScheduledExecutorService()); } |
查看DefaultNodeMainExecutor.newDefault()是如何初始化的
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
publicstaticNodeMainExecutor newDefault(){ return newDefault(newDefaultScheduledExecutorService()); } publicstaticNodeMainExecutor newDefault(ScheduledExecutorService executorService){ returnnewDefaultNodeMainExecutor(newDefaultNodeFactory(executorService), executorService); } //真实的初始化执行发布的操作类 privateDefaultNodeMainExecutor(NodeFactory nodeFactory,ScheduledExecutorService scheduledExecutorService){ this .nodeFactory = nodeFactory; this .scheduledExecutorService = scheduledExecutorService; this .connectedNodes =Multimaps.synchronizedMultimap(HashMultimap.create()); this .nodeMains =Maps.synchronizedBiMap(HashBiMap.create()); Runtime.getRuntime().addShutdownHook(newThread(newRunnable(){ publicvoid run(){ DefaultNodeMainExecutor. this .shutdown(); } })); } |
实际的执行代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
publicvoid execute(finalNodeMain nodeMain,NodeConfiguration nodeConfiguration,finalCollection<NodeListener> nodeListeners){ finalNodeConfiguration nodeConfigurationCopy =NodeConfiguration.copyOf(nodeConfiguration); nodeConfigurationCopy.setDefaultNodeName(nodeMain.getDefaultNodeName()); Preconditions.checkNotNull(nodeConfigurationCopy.getNodeName(), "Node name not specified." ); this .scheduledExecutorService.execute(newRunnable(){ publicvoid run(){ ArrayList nodeListenersCopy =Lists.newArrayList(); nodeListenersCopy.add(DefaultNodeMainExecutor. this .newRegistrationListener( null )); nodeListenersCopy.add(nodeMain); if (nodeListeners != null ){ nodeListenersCopy.addAll(nodeListeners); } Node node =DefaultNodeMainExecutor. this .nodeFactory.newNode(nodeConfigurationCopy, nodeListenersCopy); DefaultNodeMainExecutor. this .nodeMains.put(node, nodeMain); } }); } |
nodeConfigurationCopy.setDefaultNodeName(nodeMain.getDefaultNodeName());
如果前台没配置节点名,就会用发布者默认的名称,
如果前台有,就使用前台的名字作为节点名字
1
2
3
4
5
6
7
8
|
NodeConfiguration.java publicNodeConfiguration setDefaultNodeName(GraphName nodeName){ if ( this .nodeName == null ){ this .setNodeName(nodeName); } returnthis; } |
实际上只执行一句DefaultNodeMainExecutor.this.nodeMains.put(node, nodeMain);
node是由前台的节点配置信息和两个监听实例(一个是本页面的,一个发布者的)构造的
-
1
Node node =DefaultNodeMainExecutor.
this
.nodeFactory.newNode(nodeConfigurationCopy, nodeListenersCopy);
构造里面做的事
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
DefaultNodeFactory.java publicclassDefaultNodeFactoryimplementsNodeFactory{ privatefinalScheduledExecutorService scheduledExecutorService; publicDefaultNodeFactory(ScheduledExecutorService scheduledExecutorService){ this .scheduledExecutorService =newSharedScheduledExecutorService(scheduledExecutorService); } publicNode newNode(NodeConfiguration nodeConfiguration,Collection<NodeListener> listeners){ returnnewDefaultNode(nodeConfiguration, listeners, this .scheduledExecutorService); } publicNode newNode(NodeConfiguration nodeConfiguration){ returnthis.newNode(nodeConfiguration,(Collection) null ); } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
DefaultNode.java publicDefaultNode(NodeConfiguration nodeConfiguration,Collection<NodeListener> nodeListeners,ScheduledExecutorService scheduledExecutorService){ this .nodeConfiguration =NodeConfiguration.copyOf(nodeConfiguration); this .nodeListeners =newListenerGroup(scheduledExecutorService); this .nodeListeners.addAll(nodeListeners); this .scheduledExecutorService = scheduledExecutorService; this .masterUri = nodeConfiguration.getMasterUri(); this .masterClient =newMasterClient( this .masterUri); this .topicParticipantManager =newTopicParticipantManager(); this .serviceManager =newServiceManager(); this .parameterManager =newParameterManager(scheduledExecutorService); GraphName basename = nodeConfiguration.getNodeName(); NameResolver parentResolver = nodeConfiguration.getParentResolver(); this .nodeName = parentResolver.getNamespace().join(basename); this .resolver =newNodeNameResolver( this .nodeName, parentResolver); this .slaveServer =newSlaveServer( this .nodeName, nodeConfiguration.getTcpRosBindAddress(), nodeConfiguration.getTcpRosAdvertiseAddress(), nodeConfiguration.getXmlRpcBindAddress(), nodeConfiguration.getXmlRpcAdvertiseAddress(), this .masterClient, this .topicParticipantManager, this .serviceManager, this .parameterManager, scheduledExecutorService); //nodeConfiguration.getTcpRosBindAddress()默认情况下端口为0 //nodeConfiguration.getTcpRosAdvertiseAddress()主界面组装时候配置的host(String)组成的TcpRosAdvertiseAddress //nodeConfiguration.getXmlRpcBindAddress() 默认情况下端口为0 //nodeConfiguration.getXmlRpcAdvertiseAddress() this .slaveServer.start(); NodeIdentifier nodeIdentifier = this .slaveServer.toNodeIdentifier(); this .parameterTree =DefaultParameterTree.newFromNodeIdentifier(nodeIdentifier, this .masterClient.getRemoteUri(), this .resolver, this .parameterManager); this .publisherFactory =newPublisherFactory(nodeIdentifier, this .topicParticipantManager, nodeConfiguration.getTopicMessageFactory(), scheduledExecutorService); this .subscriberFactory =newSubscriberFactory(nodeIdentifier, this .topicParticipantManager, scheduledExecutorService); this .serviceFactory =newServiceFactory( this .nodeName, this .slaveServer, this .serviceManager, scheduledExecutorService); this .registrar =newRegistrar( this .masterClient, scheduledExecutorService); this .topicParticipantManager.setListener( this .registrar); this .serviceManager.setListener( this .registrar); scheduledExecutorService.execute(newRunnable(){ publicvoid run(){ DefaultNode. this .start(); } }); } |
其中this.masterClient =newMasterClient(this.masterUri);配置服务端的地址
1
2
3
4
5
|
publicClient(URI uri,Class<T> interfaceClass){ this .uri = uri; XmlRpcClientConfigImpl config =newXmlRpcClientConfigImpl(); try { config.setServerURL(uri.toURL()); |
前台的节点配置信息
1
2
3
|
NodeConfiguration nodeConfiguration3 =NodeConfiguration.newPublic(InetAddressFactory.newNonLoopback().getHostAddress()); nodeConfiguration3.setMasterUri(getMasterUri()); //如果没有设置默认值为 "http://localhost:11311/" nodeConfiguration3.setNodeName( "android_sensors_driver_imu" ); |
DefaultNodeMainExecutor.java的监听器
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
privateclassRegistrationListenerimplementsNodeListener{ privateRegistrationListener(){ } publicvoid onStart(ConnectedNode connectedNode){ DefaultNodeMainExecutor. this .registerNode(connectedNode); } publicvoid onShutdown(Node node){ } publicvoid onShutdownComplete(Node node){ DefaultNodeMainExecutor. this .unregisterNode(node); } publicvoid onError(Node node,Throwable throwable){ DefaultNodeMainExecutor.log.error( "Node error." , throwable); DefaultNodeMainExecutor. this .unregisterNode(node); } } } |
发布者的监听器
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
//@Override publicvoid onStart(ConnectedNode node) { try { this .publisher = node.newPublisher( "android/fix" , "sensor_msgs/NavSatFix" ); this .navSatFixListener =newNavSatListener(publisher); this .navSatThread =newNavSatThread( this .locationManager, this .navSatFixListener); this .navSatThread.start(); } catch (Exception e) { if (node != null ) { node.getLog().fatal(e); } else { e.printStackTrace(); } } } //@Override publicvoid onShutdown(Node arg0){ this .navSatThread.shutdown(); try { this .navSatThread.join(); } catch (InterruptedException e){ e.printStackTrace(); } } //@Override publicvoid onShutdownComplete(Node arg0){ } publicvoid onError(Node node,Throwable throwable) { } |
评论(0)
您还未登录,请登录后发表或查看评论