Skip to content

Commit

Permalink
Merge pull request #21 from minggangw/refactor-node-manager
Browse files Browse the repository at this point in the history
Refactor the original NodeManager class
  • Loading branch information
Minggang Wang authored Jan 3, 2018
2 parents dad9ff0 + f81f733 commit 7820e7b
Show file tree
Hide file tree
Showing 6 changed files with 339 additions and 285 deletions.
4 changes: 1 addition & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

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

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

return rclnodejs.init().then(() => {
let node = rclnodejs.createNode('ros2_web_bridge');
let nodeManager = new NodeManager(node);
let bridgeMap = new Map();

function closeAllBridges() {
Expand All @@ -37,7 +35,7 @@ function createServer(options) {
}

server.on('connection', (ws) => {
let bridge = new Bridge(nodeManager, ws);
let bridge = new Bridge(node, ws);
bridgeMap.set(bridge.bridgeId, bridge);

bridge.on('error', (error) => {
Expand Down
30 changes: 14 additions & 16 deletions lib/bridge.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
'use strict';

const rclnodejs = require('rclnodejs');
const ResourceProvider = require('./resource_provider.js');
const debug = require('debug')('ros2-web-bridge:Bridge');
const EventEmitter = require('events');
const uuidv4 = require('uuid/v4');
Expand Down Expand Up @@ -64,17 +65,15 @@ class MessageParser {
}

class Bridge extends EventEmitter {
constructor(nodeManager, ws) {
constructor(node, ws) {
super();
this._nodeManager = nodeManager;
this._ws = ws;
this._parser = new MessageParser();
this._bridgeId = this._generateRandomId();
this._servicesResponse = new Map();
this._closed = false;

this._resourceProvider = new ResourceProvider(node, this._bridgeId);
this._registerConnectionEvent(ws);

this._rebuildOpMap();
}

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

close() {
if (!this._closed) {
this._nodeManager.cleanResourceByBridgeId(this._bridgeId);
this._resourceProvider.clean();
this._servicesResponse.clear();
this._closed = true;
}
Expand Down Expand Up @@ -146,18 +145,18 @@ class Bridge extends EventEmitter {
_rebuildOpMap() {
this._registerOpMap('advertise', (command) => {
debug(`advertise a topic: ${command.topic}`);
this._nodeManager.createPublisher(this._exractMessageType(command.type), command.topic, this._bridgeId);
this._resourceProvider.createPublisher(this._exractMessageType(command.type), command.topic);
});

this._registerOpMap('unadvertise', (command) => {
debug(`unadvertise a topic: ${command.topic}`);
this._nodeManager.destroyPublisher(command.topic, this._bridgeId);
this._resourceProvider.destroyPublisher(command.topic);
});

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

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

this._nodeManager.createSubscription(this._exractMessageType(command.type),
command.topic,
this._bridgeId,
this._sendSubscriptionResponse.bind(this));
this._resourceProvider.createSubscription(this._exractMessageType(command.type),
command.topic,
this._sendSubscriptionResponse.bind(this));
});

this._registerOpMap('unsubscribe', (command) => {
debug(`unsubscribe a topic named ${command.topic}`);
this._nodeManager.destroySubscription(command.topic, this._bridgeId);
this._resourceProvider.destroySubscription(command.topic);
});

this._registerOpMap('call_service', (command) => {
let serviceName = command.service;
let client =
this._nodeManager.createClient(this._exractServiceType(command.args.type), serviceName, this._bridgeId);
this._resourceProvider.createClient(this._exractServiceType(command.args.type), serviceName);

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

this._registerOpMap('advertise_service', (command) => {
let serviceName = command.service;
let service = this._nodeManager.createService(
let service = this._resourceProvider.createService(
this._exractServiceType(command.type),
serviceName, this._bridgeId,
serviceName,
(request, response) => {
let id = this._generateRandomId();
let serviceRequest = {op: 'call_service', service: command.service, args: request, id: id};
Expand Down
266 changes: 0 additions & 266 deletions lib/node_manager.js

This file was deleted.

Loading

0 comments on commit 7820e7b

Please sign in to comment.