Skip to content

Commit

Permalink
Merge pull request #105 from megawac/unsub-topic
Browse files Browse the repository at this point in the history
Unsubscribe from topic
  • Loading branch information
rctoris committed Sep 21, 2014
2 parents 3b642f6 + 6c230e0 commit 98d3425
Showing 1 changed file with 39 additions and 53 deletions.
92 changes: 39 additions & 53 deletions src/core/Topic.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ ROSLIB.Topic = function(options) {
this.queue_size = options.queue_size || 100;

// Check for valid compression types
if (this.compression && this.compression !== 'png' && this.compression !== 'none') {
if (this.compression && this.compression !== 'png' && this.compression !==
'none') {
this.emit('warning', this.compression +
' compression is not supported. No compression will be used.');
}
Expand All @@ -51,44 +52,34 @@ ROSLIB.Topic.prototype.__proto__ = EventEmitter2.prototype;
*/
ROSLIB.Topic.prototype.subscribe = function(callback) {
var that = this;

this.on('message', function(message) {
callback(message);
});

this.on('message', callback);
this.ros.on(this.name, function(data) {
var message = new ROSLIB.Message(data);
that.emit('message', message);
});

this.ros.idCounter++;
var subscribeId = 'subscribe:' + this.name + ':' + this.ros.idCounter;
var call = {
op : 'subscribe',
id : subscribeId,
type : this.messageType,
topic : this.name,
compression : this.compression,
throttle_rate : this.throttle_rate
};

this.ros.callOnConnection(call);
this.id = 'subscribe:' + this.name + ':' + (++this.ros.idCounter);
this.ros.callOnConnection({
op: 'subscribe',
id: this.id,
type: this.messageType,
topic: this.name,
compression: this.compression,
throttle_rate: this.throttle_rate
});
};

/**
* Unregisters as a subscriber for the topic. Unsubscribing will remove
* all subscribe callbacks.
*/
ROSLIB.Topic.prototype.unsubscribe = function() {
this.ros.removeAllListeners([ this.name ]);
this.ros.idCounter++;
var unsubscribeId = 'unsubscribe:' + this.name + ':' + this.ros.idCounter;
var call = {
op : 'unsubscribe',
id : unsubscribeId,
topic : this.name
};
this.ros.callOnConnection(call);
this.ros.removeAllListeners([this.name]);
this.ros.callOnConnection({
op: 'unsubscribe',
id: this.subscribeId,
topic: this.name
});
};

/**
Expand All @@ -98,17 +89,15 @@ ROSLIB.Topic.prototype.advertise = function() {
if (this.isAdvertised) {
return;
}
this.ros.idCounter++;
this.advertiseId = 'advertise:' + this.name + ':' + this.ros.idCounter;
var call = {
op : 'advertise',
id : this.advertiseId,
type : this.messageType,
topic : this.name,
latch : this.latch,
queue_size : this.queue_size
};
this.ros.callOnConnection(call);
this.advertiseId = 'advertise:' + this.name + ':' + (++this.ros.idCounter);
this.ros.callOnConnection({
op: 'advertise',
id: this.advertiseId,
type: this.messageType,
topic: this.name,
latch: this.latch,
queue_size: this.queue_size
});
this.isAdvertised = true;
};

Expand All @@ -119,13 +108,11 @@ ROSLIB.Topic.prototype.unadvertise = function() {
if (!this.isAdvertised) {
return;
}
var unadvertiseId = this.advertiseId;
var call = {
op : 'unadvertise',
id : unadvertiseId,
topic : this.name
};
this.ros.callOnConnection(call);
this.ros.callOnConnection({
op: 'unadvertise',
id: this.advertiseId,
topic: this.name
});
this.isAdvertised = false;
};

Expand All @@ -136,17 +123,16 @@ ROSLIB.Topic.prototype.unadvertise = function() {
*/
ROSLIB.Topic.prototype.publish = function(message) {
if (!this.isAdvertised) {
this.advertise();
this.advertise();
}

this.ros.idCounter++;
var publishId = 'publish:' + this.name + ':' + this.ros.idCounter;
var call = {
op : 'publish',
id : publishId,
topic : this.name,
msg : message,
latch : this.latch
op: 'publish',
id: 'publish:' + this.name + ':' + this.ros.idCounter,
topic: this.name,
msg: message,
latch: this.latch
};
this.ros.callOnConnection(call);
};
};

0 comments on commit 98d3425

Please sign in to comment.