Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 20 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
const netUtils = require('./utils/network_utils.js');
const msgUtils = require('./utils/message_utils.js');
const messages = require('./utils/messages.js');
const util = require('util');
msgUtils.findMessageFiles();

// these will be modules, they depend on logger which isn't initialized yet
Expand Down Expand Up @@ -72,6 +73,16 @@ function _validateNodeName(nodeName) {
return nodeName;
}

/**
* Appends a random string of numeric characters to the end
* of the node name. Follows rospy logic.
* @param nodeName {string} string to anonymize
* @return {string} anonymized nodeName
*/
function _anonymizeNodeName(nodeName) {
return util.format('%s_%s_%s', nodeName, process.pid, Date.now());
}

let Rosnodejs = {
/**
* Initializes a ros node for this process. Only one ros node can exist per process
Expand All @@ -81,19 +92,22 @@ let Rosnodejs = {
* @return {Promise} resolved when connection to master is established
*/
initNode(nodeName, options) {
options = options || {};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this FIXME still true? String validation of the node name hasn't changed.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant to remove it when I added the validation that exists. There is, admittedly, a lot more name validation/mapping that we need to do, though this exists for most ROS names and not specifically for node names.

if (options.anonymous) {
nodeName = _anonymizeNodeName(nodeName);
}

nodeName = _validateNodeName(nodeName);

if (rosNode !== null) {
if (nodeName === rosNode.getNodeName()) {
return Promise.resolve(this.getNodeHandle());
}
// else
throw new Error('Unable to initialize node [' + nodeName + '] - node ['
throw new Error('Unable to initialize node [' + nodeName + '] - node ['
+ rosNode.getNodeName() + '] already exists');
}

// FIXME: validate nodeName -- MUST START WITH '/'
options = options || {};
let rosMasterUri = process.env.ROS_MASTER_URI;
if (options.rosMasterUri) {
rosMasterUri = options.rosMasterUri;
Expand Down Expand Up @@ -170,7 +184,7 @@ let Rosnodejs = {
}
var count = types.length;
return new Promise((resolve, reject) => {
types.forEach(function(type) {
types.forEach(function(type) {
messages.getMessage(type, function(error, Message) {
if (--count == 0) {
resolve();
Expand All @@ -179,15 +193,15 @@ let Rosnodejs = {
});
});
},

/** create message classes for all the given types */
_useServices(types) {
if (!types || types.length == 0) {
return Promise.resolve();
}
var count = types.length;
return new Promise((resolve, reject) => {
types.forEach(function(type) {
types.forEach(function(type) {
messages.getServiceRequest(type, function() {
messages.getServiceResponse(type, function() {
if (--count == 0) {
Expand Down
10 changes: 5 additions & 5 deletions utils/message_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ let MessageUtils = {
loadMessagePackage(msgPackage) {
const indexPath = messagePackagePathMap[msgPackage];
if (indexPath === undefined) {
throw new Error('Unable to find message package %s', msgPackage);
throw new Error('Unable to find message package ' + msgPackage);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a JS style guide or linter dictating this change? If not, we should probably choose one. I assume this change is marginally better that it was previous?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

error's won't format strings for you like logging will - so in the log you ended up just seeing
Unable to find message package %s

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Noted! Not a marginal change at all, then.

}
try {
messagePackageMap[msgPackage] = require(indexPath);
Expand Down Expand Up @@ -225,17 +225,17 @@ let MessageUtils = {
let type = parts[1];
return messagePackage.srv[type];
} else {
let request =
let request =
messages.getFromRegistry(rosDataType, ["srv", "Request"]);
let response =
let response =
messages.getFromRegistry(rosDataType, ["srv", "Response"]);
if (request && response) {
return {
return {
Request: request,
Response: response
};
} else {
console.error('Unable to find service package %s: %j %j',
console.error('Unable to find service package %s: %j %j',
msgPackage, request, response);
throw new Error();
}
Expand Down