Skip to content

Commit

Permalink
Simplify core/Topic
Browse files Browse the repository at this point in the history
  • Loading branch information
megawac committed Sep 21, 2014
1 parent 30adfeb commit 6c230e0
Showing 1 changed file with 39 additions and 50 deletions.
89 changes: 39 additions & 50 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,42 +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.subscribeId = 'subscribe:' + this.name + ':' + (++this.ros.idCounter);
var call = {
op : 'subscribe',
id : this.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 ]);

var call = {
op : 'unsubscribe',
id : this.subscribeId,
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 @@ -96,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 @@ -117,12 +108,11 @@ ROSLIB.Topic.prototype.unadvertise = function() {
if (!this.isAdvertised) {
return;
}
var call = {
op : 'unadvertise',
id : this.advertiseId,
topic : this.name
};
this.ros.callOnConnection(call);
this.ros.callOnConnection({
op: 'unadvertise',
id: this.advertiseId,
topic: this.name
});
this.isAdvertised = false;
};

Expand All @@ -133,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 6c230e0

Please sign in to comment.