Skip to content

Commit f81f733

Browse files
author
Minggang Wang
committed
Refactor the original NodeManager class
This patch splits the NodeManager into ResourceProvider and SubscriptionManager. - ResourceProvider will provide the functions of creating/destroying the publisher/subscription/client/service, and manage the handles used within the same bridge(websockets connection). - SubscriptionManager will hold all subscription handles used by different bridges(websockets connection), because we may share a subscription across bridges. Each instance of Bridge(connection) owns a ResourceProvider and all instances of ResourceProvider share a SubscriptionManager. Also the patch extracts the handle class to a separate RefCountingHandle class.
1 parent dad9ff0 commit f81f733

6 files changed

+339
-285
lines changed

index.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
const rclnodejs = require('rclnodejs');
1818
const {Server} = require('ws');
19-
const NodeManager = require('./lib/node_manager.js');
2019
const Bridge = require('./lib/bridge.js');
2120
const debug = require('debug')('ros2-web-bridge:index');
2221

@@ -27,7 +26,6 @@ function createServer(options) {
2726

2827
return rclnodejs.init().then(() => {
2928
let node = rclnodejs.createNode('ros2_web_bridge');
30-
let nodeManager = new NodeManager(node);
3129
let bridgeMap = new Map();
3230

3331
function closeAllBridges() {
@@ -37,7 +35,7 @@ function createServer(options) {
3735
}
3836

3937
server.on('connection', (ws) => {
40-
let bridge = new Bridge(nodeManager, ws);
38+
let bridge = new Bridge(node, ws);
4139
bridgeMap.set(bridge.bridgeId, bridge);
4240

4341
bridge.on('error', (error) => {

lib/bridge.js

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
'use strict';
1616

1717
const rclnodejs = require('rclnodejs');
18+
const ResourceProvider = require('./resource_provider.js');
1819
const debug = require('debug')('ros2-web-bridge:Bridge');
1920
const EventEmitter = require('events');
2021
const uuidv4 = require('uuid/v4');
@@ -64,17 +65,15 @@ class MessageParser {
6465
}
6566

6667
class Bridge extends EventEmitter {
67-
constructor(nodeManager, ws) {
68+
constructor(node, ws) {
6869
super();
69-
this._nodeManager = nodeManager;
7070
this._ws = ws;
7171
this._parser = new MessageParser();
7272
this._bridgeId = this._generateRandomId();
7373
this._servicesResponse = new Map();
7474
this._closed = false;
75-
75+
this._resourceProvider = new ResourceProvider(node, this._bridgeId);
7676
this._registerConnectionEvent(ws);
77-
7877
this._rebuildOpMap();
7978
}
8079

@@ -98,7 +97,7 @@ class Bridge extends EventEmitter {
9897

9998
close() {
10099
if (!this._closed) {
101-
this._nodeManager.cleanResourceByBridgeId(this._bridgeId);
100+
this._resourceProvider.clean();
102101
this._servicesResponse.clear();
103102
this._closed = true;
104103
}
@@ -146,18 +145,18 @@ class Bridge extends EventEmitter {
146145
_rebuildOpMap() {
147146
this._registerOpMap('advertise', (command) => {
148147
debug(`advertise a topic: ${command.topic}`);
149-
this._nodeManager.createPublisher(this._exractMessageType(command.type), command.topic, this._bridgeId);
148+
this._resourceProvider.createPublisher(this._exractMessageType(command.type), command.topic);
150149
});
151150

152151
this._registerOpMap('unadvertise', (command) => {
153152
debug(`unadvertise a topic: ${command.topic}`);
154-
this._nodeManager.destroyPublisher(command.topic, this._bridgeId);
153+
this._resourceProvider.destroyPublisher(command.topic);
155154
});
156155

157156
this._registerOpMap('publish', (command) => {
158157
debug(`Publish a topic named ${command.topic} with ${JSON.stringify(command.msg)}`);
159158

160-
let publisher = this._nodeManager.getPublisherByTopic(command.topic, this._bridgeId);
159+
let publisher = this._resourceProvider.getPublisherByTopicName(command.topic);
161160
if (publisher) {
162161
publisher.publish(command.msg);
163162
}
@@ -166,21 +165,20 @@ class Bridge extends EventEmitter {
166165
this._registerOpMap('subscribe', (command) => {
167166
debug(`subscribe a topic named ${command.topic}`);
168167

169-
this._nodeManager.createSubscription(this._exractMessageType(command.type),
170-
command.topic,
171-
this._bridgeId,
172-
this._sendSubscriptionResponse.bind(this));
168+
this._resourceProvider.createSubscription(this._exractMessageType(command.type),
169+
command.topic,
170+
this._sendSubscriptionResponse.bind(this));
173171
});
174172

175173
this._registerOpMap('unsubscribe', (command) => {
176174
debug(`unsubscribe a topic named ${command.topic}`);
177-
this._nodeManager.destroySubscription(command.topic, this._bridgeId);
175+
this._resourceProvider.destroySubscription(command.topic);
178176
});
179177

180178
this._registerOpMap('call_service', (command) => {
181179
let serviceName = command.service;
182180
let client =
183-
this._nodeManager.createClient(this._exractServiceType(command.args.type), serviceName, this._bridgeId);
181+
this._resourceProvider.createClient(this._exractServiceType(command.args.type), serviceName);
184182

185183
if (client) {
186184
client.sendRequest(command.args.request, (response) => {
@@ -194,9 +192,9 @@ class Bridge extends EventEmitter {
194192

195193
this._registerOpMap('advertise_service', (command) => {
196194
let serviceName = command.service;
197-
let service = this._nodeManager.createService(
195+
let service = this._resourceProvider.createService(
198196
this._exractServiceType(command.type),
199-
serviceName, this._bridgeId,
197+
serviceName,
200198
(request, response) => {
201199
let id = this._generateRandomId();
202200
let serviceRequest = {op: 'call_service', service: command.service, args: request, id: id};

lib/node_manager.js

Lines changed: 0 additions & 266 deletions
This file was deleted.

0 commit comments

Comments
 (0)