Skip to content

Commit 66040f6

Browse files
committed
some clean up. Implement getBusInfo.
1 parent e318a1c commit 66040f6

6 files changed

Lines changed: 317 additions & 90 deletions

File tree

src/lib/Publisher.js

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,35 +20,48 @@
2020
const EventEmitter = require('events');
2121
const {rebroadcast} = require('../utils/event_utils.js');
2222

23+
/**
24+
* @class Publisher
25+
* Public facing publishers class. Allows users to send messages to subscribers
26+
* on a given topic.
27+
*/
2328
class Publisher extends EventEmitter {
2429
constructor(impl) {
2530
super();
2631

2732
++impl.count;
2833
this._impl = impl;
2934

35+
this._topic = impl.getTopic();
36+
this._type = impl.getType();
37+
3038
rebroadcast('registered', this._impl, this);
3139
rebroadcast('connection', this._impl, this);
3240
rebroadcast('disconnect', this._impl, this);
3341
rebroadcast('error', this._impl, this);
3442
}
3543

44+
/**
45+
* Get the topic this publisher is publishing on
46+
* @returns {string}
47+
*/
3648
getTopic() {
37-
if (this._impl) {
38-
return this._impl.getTopic();
39-
}
40-
// else
41-
return null;
49+
return this._topic;
4250
}
4351

52+
/**
53+
* Get the type of message this publisher is sending
54+
* (e.g. std_msgs/String)
55+
* @returns {string}
56+
*/
4457
getType() {
45-
if (this._impl) {
46-
return this._impl.getType();
47-
}
48-
// else
49-
return null;
58+
return this._type;
5059
}
5160

61+
/**
62+
* Check if this publisher is latching
63+
* @returns {boolean}
64+
*/
5265
getLatching() {
5366
if (this._impl) {
5467
return this._impl.getLatching();
@@ -57,6 +70,10 @@ class Publisher extends EventEmitter {
5770
return false;
5871
}
5972

73+
/**
74+
* Get the numbber of subscribers currently connected to this publisher
75+
* @returns {number}
76+
*/
6077
getNumSubscribers() {
6178
if (this._impl) {
6279
return this._impl.getNumSubscribers();
@@ -65,22 +82,32 @@ class Publisher extends EventEmitter {
6582
return 0;
6683
}
6784

85+
/**
86+
* Shuts down this publisher. If this is the last publisher on this topic
87+
* for this node, closes the publisher and unregisters the topic from Master
88+
* @returns {Promise}
89+
*/
6890
shutdown() {
6991
const topic= this.getTopic();
7092
if (this._impl) {
7193
const impl = this._impl
7294
this._impl = null;
73-
this.removeAllListeners();
7495

7596
--impl.count;
7697
if (impl.count <= 0) {
77-
return impl.shutdown();
98+
return impl.getNode().unadvertise(impl.getTopic());
7899
}
100+
101+
this.removeAllListeners();
79102
}
80103
// else
81104
return Promise.resolve();
82105
}
83106

107+
/**
108+
* Check if this publisher has been shutdown
109+
* @returns {boolean}
110+
*/
84111
isShutdown() {
85112
return !!this._impl;
86113
}

src/lib/RosNode.js

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ class RosNode extends EventEmitter {
139139
if (sub) {
140140
this._debugLog.info('Unsubscribing from topic %s', topic);
141141
delete this._subscribers[topic];
142-
sub._shutdown();
142+
sub.shutdown();
143143
return this.unregisterSubscriber(topic);
144144
}
145145
}
@@ -149,7 +149,7 @@ class RosNode extends EventEmitter {
149149
if (pub) {
150150
this._debugLog.info('Unadvertising topic %s', topic);
151151
delete this._publishers[topic];
152-
pub._shutdown();
152+
pub.shutdown();
153153
return this.unregisterPublisher(topic);
154154
}
155155
}
@@ -537,7 +537,42 @@ class RosNode extends EventEmitter {
537537
}
538538

539539
_handleGetBusInfo(err, params, callback) {
540-
this._log.error('Not implemented');
540+
const busInfo = [];
541+
let count = 0;
542+
Object.keys(this._subscribers).forEach((topic) => {
543+
const sub = this._subscribers[topic];
544+
sub.getClientUris().forEach((clientUri) => {
545+
busInfo.push([
546+
++count,
547+
clientUri,
548+
'i',
549+
'TCPROS',
550+
sub.getTopic(),
551+
true
552+
]);
553+
});
554+
});
555+
556+
Object.keys(this._publishers).forEach((topic) => {
557+
const pub = this._publishers[topic];
558+
pub.getClientUris().forEach((clientUri) => {
559+
busInfo.push([
560+
++count,
561+
clientUri,
562+
'o',
563+
'TCPROS',
564+
pub.getTopic(),
565+
true
566+
]);
567+
});
568+
});
569+
570+
const resp = [
571+
1,
572+
this.getNodeName(),
573+
busInfo
574+
];
575+
callback(null, resp);
541576
}
542577

543578
_handleGetBusStats(err, params, callback) {

src/lib/ServiceServer.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ class ServiceServer extends EventEmitter {
7474
return NetworkUtils.formatServiceUri(this._port);
7575
}
7676

77+
getClientUris() {
78+
return Object.keys(this._clients);
79+
}
80+
7781
/**
7882
* The ROS client shutdown code is a little noodly. Users can close a client through
7983
* the ROS node or the client itself and both are correct. Either through a node.unadvertise()

src/lib/Subscriber.js

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,36 +22,49 @@ const {rebroadcast} = require('../utils/event_utils.js');
2222

2323
//-----------------------------------------------------------------------
2424

25+
/**
26+
* @class Subscriber
27+
* Public facing subscriber class. Allows users to listen to messages from
28+
* publishers on a given topic.
29+
*/
2530
class Subscriber extends EventEmitter {
2631
constructor(impl) {
2732
super();
2833

2934
++impl.count;
3035
this._impl = impl;
3136

37+
this._topic = impl.getTopic();
38+
this._type = impl.getType();
39+
3240
rebroadcast('registered', this._impl, this);
3341
rebroadcast('connection', this._impl, this);
3442
rebroadcast('disconnect', this._impl, this);
3543
rebroadcast('error', this._impl, this);
3644
rebroadcast('message', this._impl, this);
3745
}
3846

47+
/**
48+
* Get the topic this publisher is publishing on
49+
* @returns {string}
50+
*/
3951
getTopic() {
40-
if (this._impl) {
41-
return this._impl.getTopic();
42-
}
43-
// else
44-
return null;
52+
return this._topic;
4553
}
4654

55+
/**
56+
* Get the type of message this publisher is sending
57+
* (e.g. std_msgs/String)
58+
* @returns {string}
59+
*/
4760
getType() {
48-
if (this._impl) {
49-
return this._impl.getType();
50-
}
51-
// else
52-
return null;
61+
return this._type;
5362
}
5463

64+
/**
65+
* Get the numbber of publishers currently connected to this subscriber
66+
* @returns {number}
67+
*/
5568
getNumPublishers() {
5669
if (this._impl) {
5770
return this._impl.getNumPublishers();
@@ -60,21 +73,31 @@ class Subscriber extends EventEmitter {
6073
return 0;
6174
}
6275

76+
/**
77+
* Shuts down this subscriber. If this is the last subscriber on this topic
78+
* for this node, closes the subscriber and unregisters the topic from Master
79+
* @returns {Promise}
80+
*/
6381
shutdown() {
6482
if (this._impl) {
6583
const impl = this._impl
6684
this._impl = null;
67-
this.removeAllListeners();
6885

6986
--impl.count;
7087
if (impl.count <= 0) {
71-
return impl.shutdown();
88+
return impl.getNode().unsubscribe(impl.getTopic());
7289
}
90+
91+
this.removeAllListeners();
7392
}
7493
// else
7594
return Promise.resolve();
7695
}
7796

97+
/**
98+
* Check if this publisher has been shutdown
99+
* @returns {boolean}
100+
*/
78101
isShutdown() {
79102
return !!this._impl;
80103
}

0 commit comments

Comments
 (0)