@@ -34,8 +34,10 @@ import { detect } from 'detect-browser';
34
34
import {
35
35
Subject ,
36
36
BehaviorSubject ,
37
- interval
37
+ interval ,
38
+ timer
38
39
} from 'rxjs' ;
40
+ import { filter , startWith , takeUntil } from 'rxjs/operators' ;
39
41
import ReaderWriter from './reader-writer' ;
40
42
41
43
// Required agent version
@@ -52,6 +54,8 @@ const LOOPBACK_HOSTNAME = 'localhost';
52
54
const LOOKUP_PORT_START = 8991 ;
53
55
const LOOKUP_PORT_END = 9000 ;
54
56
57
+ const POLLING_INTERVAL = 1000 ;
58
+
55
59
const CANT_FIND_AGENT_MESSAGE = 'Arduino Create Agent cannot be found' ;
56
60
let updateAttempts = 0 ;
57
61
@@ -65,64 +69,53 @@ export default class SocketDaemon {
65
69
constructor ( ) {
66
70
this . selectedProtocol = PROTOCOL . HTTP ;
67
71
this . agentInfo = { } ;
68
-
69
72
this . agentFound = new BehaviorSubject ( false ) ;
70
73
this . wsConnected = new BehaviorSubject ( false ) ;
71
- this . wsError = new Subject ( ) ;
72
-
74
+ this . error = new Subject ( ) ;
73
75
this . readerWriter = new ReaderWriter ( ) ;
76
+
74
77
this . wsConnected
75
- . subscribe ( status => {
76
- if ( status ) {
78
+ . subscribe ( wsConnected => {
79
+ if ( wsConnected ) {
77
80
this . readerWriter . initSocket ( this . socket ) ;
81
+ interval ( POLLING_INTERVAL )
82
+ . pipe ( startWith ( 0 ) )
83
+ . pipe ( takeUntil ( this . wsConnected . pipe ( filter ( status => ! status ) ) ) )
84
+ . subscribe ( ( ) => this . socket . emit ( 'command' , 'list' ) ) ;
78
85
}
79
86
else {
80
- this . findAgent ( ) ;
87
+ this . agentFound . next ( false ) ;
81
88
}
82
89
} ) ;
83
90
84
91
this . agentFound
85
- . subscribe ( status => {
86
- if ( status ) {
87
- this . wsConnect ( ) ;
92
+ . subscribe ( agentFound => {
93
+ if ( agentFound ) {
94
+ this . _wsConnect ( ) ;
88
95
}
89
96
else {
90
- this . agentInfo = { } ;
97
+ this . findAgent ( ) ;
91
98
}
92
99
} ) ;
93
100
}
94
101
95
102
/**
96
103
* Look for the agent endpoint.
97
104
* First search in http://LOOPBACK_ADDRESS, after in https://LOOPBACK_HOSTNAME if in Chrome or Firefox, otherwise vice versa.
98
- * @return {object } The found agent info values.
99
105
*/
100
106
findAgent ( ) {
101
- const find = ( ) => this . tryAllPorts ( )
102
- . catch ( err => {
103
- this . agentFound . next ( false ) ;
104
- return err ;
105
- } )
106
- . finally ( ( ) => {
107
- if ( ! this . isConnected ( ) ) {
108
- setTimeout ( find , 3000 ) ;
109
- }
110
- } ) ;
111
- return find ( ) ;
112
- }
113
-
114
- tryAllPorts ( ) {
115
- return this . tryPorts ( orderedPluginAddresses [ 0 ] )
116
- . catch ( ( ) => this . tryPorts ( orderedPluginAddresses [ 1 ] )
117
- . catch ( err => Promise . reject ( err ) ) ) ;
107
+ this . _tryPorts ( orderedPluginAddresses [ 0 ] )
108
+ . catch ( ( ) => this . _tryPorts ( orderedPluginAddresses [ 1 ] ) )
109
+ . then ( ( ) => this . agentFound . next ( true ) )
110
+ . catch ( ( ) => timer ( POLLING_INTERVAL ) . subscribe ( ( ) => this . findAgent ( ) ) ) ;
118
111
}
119
112
120
113
/**
121
114
* Try ports for the selected hostname. From LOOKUP_PORT_START to LOOKUP_PORT_END
122
115
* @param {string } hostname - The hostname value (LOOPBACK_ADDRESS or LOOPBACK_HOSTNAME).
123
- * @return {object } info - The agent info values.
116
+ * @return {Promise } info - A promise resolving with the agent info values.
124
117
*/
125
- tryPorts ( hostname ) {
118
+ _tryPorts ( hostname ) {
126
119
const pluginLookups = [ ] ;
127
120
128
121
for ( let port = LOOKUP_PORT_START ; port < LOOKUP_PORT_END ; port += 1 ) {
@@ -146,7 +139,6 @@ export default class SocketDaemon {
146
139
this . agentInfo [ this . selectedProtocol ] = this . agentInfo [ this . selectedProtocol ] . replace ( 'localhost' , '127.0.0.1' ) ;
147
140
}
148
141
this . readerWriter . initPluginUrl ( this . agentInfo [ this . selectedProtocol ] ) ;
149
- this . agentFound . next ( true ) ;
150
142
return true ;
151
143
}
152
144
return false ;
@@ -170,14 +162,8 @@ export default class SocketDaemon {
170
162
171
163
/**
172
164
* Uses the websocket protocol to connect to the agent
173
- *
174
- * @return {Promise }
175
165
*/
176
- wsConnect ( ) {
177
- if ( this . isConnected ( ) ) {
178
- return ;
179
- }
180
-
166
+ _wsConnect ( ) {
181
167
const wsProtocol = this . selectedProtocol === PROTOCOL . HTTPS ? 'ws' : 'wss' ;
182
168
const address = this . agentInfo [ wsProtocol ] ;
183
169
this . socket = io ( address , { reconnection : false , forceNew : true } ) ;
@@ -188,20 +174,11 @@ export default class SocketDaemon {
188
174
this . socket . emit ( 'command' , 'downloadtool bossac 1.7.0 arduino keep' ) ;
189
175
190
176
this . wsConnected . next ( true ) ;
191
-
192
- // Periodically asks for the ports
193
- if ( ! this . portsPollingSubscription ) {
194
- this . portsPollingSubscription = interval ( 1500 ) . subscribe ( ( ) => this . socket . emit ( 'command' , 'list' ) ) ;
195
- }
196
177
} ) ;
197
178
198
- this . socket . on ( 'error' , error => this . wsError . next ( error ) ) ;
179
+ this . socket . on ( 'error' , error => this . error . next ( error ) ) ;
199
180
200
- // Reconnect on disconnect
201
181
this . socket . on ( 'disconnect' , ( ) => {
202
- if ( this . portsPollingSubscription ) {
203
- this . portsPollingSubscription . unsubscribe ( ) ;
204
- }
205
182
this . wsConnected . next ( false ) ;
206
183
} ) ;
207
184
}
@@ -225,20 +202,12 @@ export default class SocketDaemon {
225
202
* @param {function } createDeviceCb used to create the device associated to the user
226
203
*/
227
204
configureBoard ( compiledSketch , board , createDeviceCb ) {
228
- if ( ! this . isConnected ( ) ) {
205
+ if ( ! this . wsConnect . getValue ( ) ) {
229
206
return Promise . reject ( new Error ( 'We were not able to generate the CSR.' ) ) ;
230
207
}
231
208
return this . configure ( compiledSketch , board , createDeviceCb ) ;
232
209
}
233
210
234
- /**
235
- * Check if socket connected.
236
- * @return {boolean } The connection status flag.
237
- */
238
- isConnected ( ) {
239
- return this . socket && this . socket . connected ;
240
- }
241
-
242
211
/**
243
212
* Pauses the plugin
244
213
* @return {Promise }
0 commit comments