Skip to content

Commit e2d518b

Browse files
authored
Small action updates (#111)
* Fix missing shutdown listener in ActionClient. * Bring goalId generation back to ActionClientInterface
1 parent 9066454 commit e2d518b

3 files changed

Lines changed: 43 additions & 4 deletions

File tree

src/actions/ActionClient.js

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@ const msgUtils = require('../utils/message_utils.js');
2222
const ActionClientInterface = require('../lib/ActionClientInterface.js');
2323

2424
const EventEmitter = require('events');
25+
const Ultron = require('ultron');
2526

2627
const ClientGoalHandle = require('./ClientGoalHandle.js');
2728
const Time = require('../lib/Time.js');
2829

2930
const log = require('../lib/Logging.js').getLogger('actionlib_nodejs');
3031
const ThisNode = require('../lib/ThisNode.js');
31-
32-
let GOAL_COUNT = 0;
32+
const GoalIdGenerator = require('./GoalIdGenerator.js');
3333

3434
/**
3535
* @class ActionClient
@@ -57,10 +57,27 @@ class ActionClient extends EventEmitter {
5757
};
5858

5959
this._goalLookup = {};
60+
61+
this._shutdown = false;
62+
this._ultron = new Ultron(ThisNode);
63+
64+
// FIXME: how to handle shutdown? Should user be responsible?
65+
// should we check for shutdown in interval instead of listening
66+
// to events here?
67+
this._ultron.once('shutdown', () => { this.shutdown(); });
6068
}
6169

6270
shutdown() {
63-
return this._acInterface.shutdown();
71+
if (!this._shutdown) {
72+
this._shutdown = true;
73+
74+
this._ultron.destroy();
75+
this._ultron = null;
76+
77+
return this._acInterface.shutdown();
78+
}
79+
// else
80+
return Promise.resolve();
6481
}
6582

6683
sendGoal(goal, transitionCb = null, feedbackCb = null) {
@@ -69,7 +86,7 @@ class ActionClient extends EventEmitter {
6986
const now = Time.now();
7087
actionGoal.header.stamp = now;
7188
actionGoal.goal_id.stamp = now;
72-
const goalIdStr = `${ThisNode.getNodeName()}-${GOAL_COUNT++}-${now.secs}.${now.nsecs}`;
89+
const goalIdStr = GoalIdGenerator(now);
7390
actionGoal.goal_id.id = goalIdStr;
7491
actionGoal.goal = goal;
7592

src/actions/GoalIdGenerator.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const ThisNode = require('../lib/ThisNode.js');
2+
const Time = require('../lib/Time.js');
3+
4+
let GOAL_COUNT = 0;
5+
6+
module.exports = function(now) {
7+
if (!now || now.secs === undefined || now.nsecs === undefined) {
8+
now = Time.now();
9+
}
10+
11+
++GOAL_COUNT;
12+
if (GOAL_COUNT > Number.MAX_SAFE_INTEGER) {
13+
GOAL_COUNT = 0;
14+
}
15+
16+
return `${ThisNode.getNodeName()}-${GOAL_COUNT}-${now.secs}.${now.nsecs}`;
17+
}

src/lib/ActionClientInterface.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
const msgUtils = require('../utils/message_utils.js');
2121
const EventEmitter = require('events');
2222
const Time = require('./Time.js');
23+
const GoalIdGenerator = require('../actions/GoalIdGenerator.js');
2324
let GoalID = null;
2425
let Header = null;
2526

@@ -130,6 +131,10 @@ class ActionClientInterface extends EventEmitter {
130131
});
131132
}
132133

134+
generateGoalId(now) {
135+
return GoalIdGenerator(now);
136+
}
137+
133138
isServerConnected() {
134139
return this._hasStatus &&
135140
this._goalPub.getNumSubscribers() > 0 &&

0 commit comments

Comments
 (0)