Skip to content

Commit

Permalink
Adds unpublish function to publisher.
Browse files Browse the repository at this point in the history
Unregisters publisher as a publisher of a given topic with master. Unpublish may
be an ambiguous name though.

See Issue #11.
  • Loading branch information
baalexander committed Apr 25, 2012
1 parent 35b3add commit 1ac8491
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 33 deletions.
76 changes: 44 additions & 32 deletions example/how_to.js
Original file line number Diff line number Diff line change
@@ -1,58 +1,69 @@
var exec = require('child_process').exec
, should = require('should')
, ros = require('../lib/ros')
;

describe('How to use rosnodejs', function() {

// it('to publish messages', function(done) {
// this.timeout(5000);

// ros.types([
// 'std_msgs/String'
// ], function(String) {
// // Uses the ROS command line tool rostopic to echo messages published
// // over the 'publish_example' topic.
// var subscribecommand = 'rostopic'
// + ' echo'
// + ' /publish_example';
// var child = exec(subscribecommand, function(error, stdout, stderr) {
// should.not.exist(error);
// });

// // Creates the topic 'publish_example'
// var publishExample = new ros.topic({
// node : 'talker'
// , topic : 'publish_example'
// , messageType : String
// });

// // Sends a std_msgs/String message over the 'publish_example' topic.
// var message = new String({ data: 'howdy' });
// publishExample.publish(message);
// setTimeout(done, 1500);
// });
// });
it('to publish messages', function(done) {
this.timeout(5000);

ros.types([
'std_msgs/String'
], function(String) {

// Uses the ROS command line tool rostopic to echo messages published
// over the 'publish_example' topic.
var subscribeCommand = 'rostopic'
+ ' echo'
+ ' /publish_example'
+ ' -n 1'
;
var child = exec(subscribeCommand, function(error, stdout, stderr) {
should.not.exist(error);
});

// Creates the topic 'publish_example'
var publisher = new ros.topic({
node : 'talker'
, topic : 'publish_example'
, messageType : String
});

publisher.on('unregistered_publisher', done);

// Sends a std_msgs/String message over the 'publish_example' topic.
var message = new String({ data: 'howdy' });
publisher.publish(message);

// Unregister as a publisher for test clean up
setTimeout(function() {
publisher.unpublish();
}, 1000);
});
});

it('to subscribe to messages', function(done) {
this.timeout(5000);

ros.types([
'std_msgs/String'
], function(String) {

// Creates the topic 'subscribe_example'
var subscriber = new ros.topic({
node : 'listener'
, topic : 'subscribe_example'
, messageType : String
});

subscriber.on('unregistered_subscriber', function() {
done();
});
subscriber.on('unregistered_subscriber', done);

// Subscribes to the 'subscribe_example' topic
subscriber.subscribe(function(message) {
message.data.should.equal('howdy');

// Unregister as a subscriber for test cleanup
subscriber.unsubscribe();
});

Expand All @@ -62,7 +73,8 @@ describe('How to use rosnodejs', function() {
+ ' /subscribe_example'
+ ' std_msgs/String'
+ ' howdy'
+ ' --once';
+ ' --once'
;
var child = exec(publishCommand, function(error, stdout, stderr) {
should.not.exist(error);
});
Expand Down
15 changes: 14 additions & 1 deletion lib/master.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ var xmlrpc = require('xmlrpc')
, environment = require('./environment')
;


var master = exports

master.registerPublisher = function(publisher, callback) {
Expand All @@ -20,6 +19,20 @@ master.registerPublisher = function(publisher, callback) {
});
};

master.unregisterPublisher = function(publisher, callback) {
var masterUri = environment.getRosMasterUri()
, callerUri = publisher.callerUri
, callerId = getGraphResourceName(publisher.callerId)
, topicName = getGraphResourceName(publisher.topic)
, params = [callerId, topicName, callerUri]
, client = xmlrpc.createClient(masterUri)
;

client.methodCall('unregisterPublisher', params, function(error, value) {
parseResponse(error, value, callback);
});
};

master.registerSubscriber = function(subscriber, callback) {
var masterUri = environment.getRosMasterUri()
, callerUri = subscriber.callerUri
Expand Down
22 changes: 22 additions & 0 deletions lib/topic.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,28 @@ Topic.prototype.publish = function(message) {
}
};

Topic.prototype.unpublish = function(message) {
var that = this;

if (this.protocol) {
this.getUri(function(uri) {
var masterParams = {
callerId : that.node
, callerUri : uri
, topic : that.topic
};
master.unregisterPublisher(masterParams, function(error) {
if (error) {
that.emit('error', error);
}
else {
that.emit('unregistered_publisher');
}
});
});
}
};

Topic.prototype.subscribe = function(callback) {
var that = this;

Expand Down

0 comments on commit 1ac8491

Please sign in to comment.