15
15
'use strict' ;
16
16
17
17
const rclnodejs = require ( 'rclnodejs' ) ;
18
+ const ResourceProvider = require ( './resource_provider.js' ) ;
18
19
const debug = require ( 'debug' ) ( 'ros2-web-bridge:Bridge' ) ;
19
20
const EventEmitter = require ( 'events' ) ;
20
21
const uuidv4 = require ( 'uuid/v4' ) ;
@@ -64,17 +65,15 @@ class MessageParser {
64
65
}
65
66
66
67
class Bridge extends EventEmitter {
67
- constructor ( nodeManager , ws ) {
68
+ constructor ( node , ws ) {
68
69
super ( ) ;
69
- this . _nodeManager = nodeManager ;
70
70
this . _ws = ws ;
71
71
this . _parser = new MessageParser ( ) ;
72
72
this . _bridgeId = this . _generateRandomId ( ) ;
73
73
this . _servicesResponse = new Map ( ) ;
74
74
this . _closed = false ;
75
-
75
+ this . _resourceProvider = new ResourceProvider ( node , this . _bridgeId ) ;
76
76
this . _registerConnectionEvent ( ws ) ;
77
-
78
77
this . _rebuildOpMap ( ) ;
79
78
}
80
79
@@ -98,7 +97,7 @@ class Bridge extends EventEmitter {
98
97
99
98
close ( ) {
100
99
if ( ! this . _closed ) {
101
- this . _nodeManager . cleanResourceByBridgeId ( this . _bridgeId ) ;
100
+ this . _resourceProvider . clean ( ) ;
102
101
this . _servicesResponse . clear ( ) ;
103
102
this . _closed = true ;
104
103
}
@@ -146,18 +145,18 @@ class Bridge extends EventEmitter {
146
145
_rebuildOpMap ( ) {
147
146
this . _registerOpMap ( 'advertise' , ( command ) => {
148
147
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 ) ;
150
149
} ) ;
151
150
152
151
this . _registerOpMap ( 'unadvertise' , ( command ) => {
153
152
debug ( `unadvertise a topic: ${ command . topic } ` ) ;
154
- this . _nodeManager . destroyPublisher ( command . topic , this . _bridgeId ) ;
153
+ this . _resourceProvider . destroyPublisher ( command . topic ) ;
155
154
} ) ;
156
155
157
156
this . _registerOpMap ( 'publish' , ( command ) => {
158
157
debug ( `Publish a topic named ${ command . topic } with ${ JSON . stringify ( command . msg ) } ` ) ;
159
158
160
- let publisher = this . _nodeManager . getPublisherByTopic ( command . topic , this . _bridgeId ) ;
159
+ let publisher = this . _resourceProvider . getPublisherByTopicName ( command . topic ) ;
161
160
if ( publisher ) {
162
161
publisher . publish ( command . msg ) ;
163
162
}
@@ -166,21 +165,20 @@ class Bridge extends EventEmitter {
166
165
this . _registerOpMap ( 'subscribe' , ( command ) => {
167
166
debug ( `subscribe a topic named ${ command . topic } ` ) ;
168
167
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 ) ) ;
173
171
} ) ;
174
172
175
173
this . _registerOpMap ( 'unsubscribe' , ( command ) => {
176
174
debug ( `unsubscribe a topic named ${ command . topic } ` ) ;
177
- this . _nodeManager . destroySubscription ( command . topic , this . _bridgeId ) ;
175
+ this . _resourceProvider . destroySubscription ( command . topic ) ;
178
176
} ) ;
179
177
180
178
this . _registerOpMap ( 'call_service' , ( command ) => {
181
179
let serviceName = command . service ;
182
180
let client =
183
- this . _nodeManager . createClient ( this . _exractServiceType ( command . args . type ) , serviceName , this . _bridgeId ) ;
181
+ this . _resourceProvider . createClient ( this . _exractServiceType ( command . args . type ) , serviceName ) ;
184
182
185
183
if ( client ) {
186
184
client . sendRequest ( command . args . request , ( response ) => {
@@ -194,9 +192,9 @@ class Bridge extends EventEmitter {
194
192
195
193
this . _registerOpMap ( 'advertise_service' , ( command ) => {
196
194
let serviceName = command . service ;
197
- let service = this . _nodeManager . createService (
195
+ let service = this . _resourceProvider . createService (
198
196
this . _exractServiceType ( command . type ) ,
199
- serviceName , this . _bridgeId ,
197
+ serviceName ,
200
198
( request , response ) => {
201
199
let id = this . _generateRandomId ( ) ;
202
200
let serviceRequest = { op : 'call_service' , service : command . service , args : request , id : id } ;
0 commit comments