@@ -35,6 +35,7 @@ import {
35
35
interval
36
36
} from 'rxjs' ;
37
37
import { parseMessage , initSocket , initPluginUrl } from './readMessages' ;
38
+ import { debug } from 'util' ;
38
39
// Required agent version
39
40
const MIN_VERSION = '1.1.71' ;
40
41
@@ -50,8 +51,8 @@ const LOOKUP_PORT_END = 9000;
50
51
51
52
const CANT_FIND_AGENT_MESSAGE = 'Arduino Create Agent cannot be found' ;
52
53
53
- export const AGENT_STATUS_CONNECTED = 'AGENT_CONNECTED ' ;
54
- export const AGENT_STATUS_DISCONNECTED = 'AGENT_DISCONNECTED ' ;
54
+ export const AGENT_STATUS_FOUND = 'AGENT_FOUND ' ;
55
+ export const AGENT_STATUS_NOT_FOUND = 'AGENT_NOT_FOUND ' ;
55
56
export const WS_STATUS_CONNECTED = 'WS_CONNECTED' ;
56
57
export const WS_STATUS_DISCONNECTED = 'WS_DISCONNECTED' ;
57
58
@@ -61,7 +62,7 @@ export default class SocketDaemon {
61
62
this . agentInfo = { } ;
62
63
this . found = false ;
63
64
64
- this . agentConnectionStatus = new BehaviorSubject ( AGENT_STATUS_DISCONNECTED ) ;
65
+ this . agentDiscoveryStatus = new BehaviorSubject ( AGENT_STATUS_NOT_FOUND ) ;
65
66
this . wsConnectionStatus = new BehaviorSubject ( WS_STATUS_DISCONNECTED ) ;
66
67
this . wsError = new Subject ( ) ;
67
68
}
@@ -71,19 +72,26 @@ export default class SocketDaemon {
71
72
* First search in http://LOOPBACK_ADDRESS, after in https://LOOPBACK_HOSTNAME.
72
73
* @return {object } The found agent info values.
73
74
*/
74
- connect ( ) {
75
- if ( this . found ) {
76
- return fetch ( this . agentInfo [ this . selectedProtocol ] )
77
- . then ( response => response . json ( ) )
78
- . catch ( ( ) => {
79
- this . found = false ;
80
- return Promise . reject ( new Error ( CANT_FIND_AGENT_MESSAGE ) ) ;
75
+ findAgent ( ) {
76
+ const find = ( ) => {
77
+ return this . tryAllPorts ( )
78
+ . catch ( err => {
79
+ this . agentDiscoveryStatus . next ( AGENT_STATUS_NOT_FOUND ) ;
80
+ return err ;
81
+ } )
82
+ . finally ( ( ) => {
83
+ if ( ! this . isConnected ( ) ) {
84
+ setTimeout ( find , 3000 ) ;
85
+ }
81
86
} ) ;
82
- }
87
+ } ;
88
+ return find ( ) ;
89
+ }
83
90
91
+ tryAllPorts ( ) {
84
92
return this . tryPorts ( LOOPBACK_ADDRESS )
85
93
. catch ( ( ) => this . tryPorts ( LOOPBACK_HOSTNAME )
86
- . catch ( ( ) => Promise . reject ( new Error ( CANT_FIND_AGENT_MESSAGE ) ) ) ) ;
94
+ . catch ( err => Promise . reject ( err ) ) ) ;
87
95
}
88
96
89
97
/**
@@ -92,6 +100,7 @@ export default class SocketDaemon {
92
100
* @return {object } info - The agent info values.
93
101
*/
94
102
tryPorts ( hostname ) {
103
+ console . log ( 'tryPorts\n' ) ;
95
104
const pluginLookups = [ ] ;
96
105
97
106
for ( let port = LOOKUP_PORT_START ; port < LOOKUP_PORT_END ; port += 1 ) {
@@ -102,27 +111,27 @@ export default class SocketDaemon {
102
111
// So we have to resolve them with a false value to let the Promise.all catch all the deferred data
103
112
}
104
113
105
- return Promise . all ( pluginLookups ) . then ( responses => {
106
- this . found = responses . some ( r => {
107
- if ( r && r . response && r . response . status === 200 ) {
108
- this . agentInfo = r . data ;
109
- this . agentConnectionStatus . next ( AGENT_STATUS_CONNECTED ) ;
110
- this . wsConnect ( ) ;
111
- if ( r . response . url . indexOf ( PROTOCOL . HTTPS ) === 0 ) {
112
- this . selectedProtocol = PROTOCOL . HTTPS ;
114
+ return Promise . all ( pluginLookups )
115
+ . then ( responses => {
116
+ this . found = responses . some ( r => {
117
+ if ( r && r . response && r . response . status === 200 ) {
118
+ this . agentInfo = r . data ;
119
+ this . agentDiscoveryStatus . next ( AGENT_STATUS_FOUND ) ;
120
+ this . wsConnect ( ) ;
121
+ if ( r . response . url . indexOf ( PROTOCOL . HTTPS ) === 0 ) {
122
+ this . selectedProtocol = PROTOCOL . HTTPS ;
123
+ }
124
+ initPluginUrl ( this . agentInfo [ this . selectedProtocol ] ) ;
125
+ return true ;
113
126
}
114
- initPluginUrl ( this . agentInfo [ this . selectedProtocol ] ) ;
115
- return true ;
127
+ return false ;
128
+ } ) ;
129
+
130
+ if ( this . found ) {
131
+ return this . update ( ) ;
116
132
}
117
- return false ;
133
+ return Promise . reject ( new Error ( ` ${ CANT_FIND_AGENT_MESSAGE } at ${ hostname } ` ) ) ;
118
134
} ) ;
119
-
120
- if ( this . found ) {
121
- return this . update ( )
122
- . then ( ( ) => this . agentInfo ) ;
123
- }
124
- return Promise . reject ( new Error ( `${ CANT_FIND_AGENT_MESSAGE } at ${ hostname } ` ) ) ;
125
- } ) ;
126
135
}
127
136
128
137
/**
@@ -163,7 +172,7 @@ export default class SocketDaemon {
163
172
this . portsPollingSubscription . unsubscribe ( ) ;
164
173
}
165
174
this . wsConnectionStatus . next ( WS_STATUS_DISCONNECTED ) ;
166
- this . wsConnect ( ) ;
175
+ this . findAgent ( ) ;
167
176
} ) ;
168
177
169
178
// Parse messages
@@ -176,7 +185,7 @@ export default class SocketDaemon {
176
185
update ( ) {
177
186
return new Promise ( ( resolve , reject ) => {
178
187
if ( this . agentInfo . version && ( semVerCompare ( this . agentInfo . version , MIN_VERSION ) >= 0 || this . agentInfo . version . indexOf ( 'dev' ) !== - 1 ) ) {
179
- resolve ( this . agentInfo ) ;
188
+ return resolve ( this . agentInfo ) ;
180
189
}
181
190
182
191
return fetch ( `${ this . agentInfo [ this . selectedProtocol ] } /update` , {
0 commit comments