Skip to content

Commit e87ad12

Browse files
author
Alberto Iannaccone
committed
polling for agent
1 parent 4edb83d commit e87ad12

File tree

3 files changed

+46
-37
lines changed

3 files changed

+46
-37
lines changed

src/socket-daemon.js

Lines changed: 41 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import {
3535
interval
3636
} from 'rxjs';
3737
import { parseMessage, initSocket, initPluginUrl } from './readMessages';
38+
import { debug } from 'util';
3839
// Required agent version
3940
const MIN_VERSION = '1.1.71';
4041

@@ -50,8 +51,8 @@ const LOOKUP_PORT_END = 9000;
5051

5152
const CANT_FIND_AGENT_MESSAGE = 'Arduino Create Agent cannot be found';
5253

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';
5556
export const WS_STATUS_CONNECTED = 'WS_CONNECTED';
5657
export const WS_STATUS_DISCONNECTED = 'WS_DISCONNECTED';
5758

@@ -61,7 +62,7 @@ export default class SocketDaemon {
6162
this.agentInfo = {};
6263
this.found = false;
6364

64-
this.agentConnectionStatus = new BehaviorSubject(AGENT_STATUS_DISCONNECTED);
65+
this.agentDiscoveryStatus = new BehaviorSubject(AGENT_STATUS_NOT_FOUND);
6566
this.wsConnectionStatus = new BehaviorSubject(WS_STATUS_DISCONNECTED);
6667
this.wsError = new Subject();
6768
}
@@ -71,19 +72,26 @@ export default class SocketDaemon {
7172
* First search in http://LOOPBACK_ADDRESS, after in https://LOOPBACK_HOSTNAME.
7273
* @return {object} The found agent info values.
7374
*/
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+
}
8186
});
82-
}
87+
};
88+
return find();
89+
}
8390

91+
tryAllPorts() {
8492
return this.tryPorts(LOOPBACK_ADDRESS)
8593
.catch(() => this.tryPorts(LOOPBACK_HOSTNAME)
86-
.catch(() => Promise.reject(new Error(CANT_FIND_AGENT_MESSAGE))));
94+
.catch(err => Promise.reject(err)));
8795
}
8896

8997
/**
@@ -92,6 +100,7 @@ export default class SocketDaemon {
92100
* @return {object} info - The agent info values.
93101
*/
94102
tryPorts(hostname) {
103+
console.log('tryPorts\n');
95104
const pluginLookups = [];
96105

97106
for (let port = LOOKUP_PORT_START; port < LOOKUP_PORT_END; port += 1) {
@@ -102,27 +111,27 @@ export default class SocketDaemon {
102111
// So we have to resolve them with a false value to let the Promise.all catch all the deferred data
103112
}
104113

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;
113126
}
114-
initPluginUrl(this.agentInfo[this.selectedProtocol]);
115-
return true;
127+
return false;
128+
});
129+
130+
if (this.found) {
131+
return this.update();
116132
}
117-
return false;
133+
return Promise.reject(new Error(`${CANT_FIND_AGENT_MESSAGE} at ${hostname}`));
118134
});
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-
});
126135
}
127136

128137
/**
@@ -163,7 +172,7 @@ export default class SocketDaemon {
163172
this.portsPollingSubscription.unsubscribe();
164173
}
165174
this.wsConnectionStatus.next(WS_STATUS_DISCONNECTED);
166-
this.wsConnect();
175+
this.findAgent();
167176
});
168177

169178
// Parse messages
@@ -176,7 +185,7 @@ export default class SocketDaemon {
176185
update() {
177186
return new Promise((resolve, reject) => {
178187
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);
180189
}
181190

182191
return fetch(`${this.agentInfo[this.selectedProtocol]}/update`, {

test/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,6 @@ <h1>Test Daemon</h1>
2323
<ul id="boards-list"></ul>
2424
<p id="error"></p>
2525

26-
<button id="connect">Connnect</button>
26+
<button id="connect">Connect</button>
2727
</body>
2828
</html>

test/index.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import Daemon from '../src';
2-
import { WS_STATUS_CONNECTED, AGENT_STATUS_CONNECTED } from '../src/socket-daemon';
2+
import { WS_STATUS_CONNECTED, AGENT_STATUS_FOUND } from '../src/socket-daemon';
33

44
function showError(error) {
55
document.getElementById('error').innerText = error.message;
@@ -9,8 +9,8 @@ function clearError() {
99
document.getElementById('error').innerText = '';
1010
}
1111

12-
Daemon.agentConnectionStatus.subscribe(status => {
13-
document.getElementById('agent-status').style.color = status === AGENT_STATUS_CONNECTED ? 'green' : 'red';
12+
Daemon.agentDiscoveryStatus.subscribe(status => {
13+
document.getElementById('agent-status').style.color = status === AGENT_STATUS_FOUND ? 'green' : 'red';
1414
document.getElementById('agent-status').innerText = status;
1515
document.getElementById('agent-info').innerHTML = JSON.stringify(Daemon.agentInfo, null, 2);
1616
});
@@ -29,6 +29,6 @@ document.getElementById('connect').addEventListener('click', () => {
2929
// document.getElementById('ws-status').innerText = '-';
3030
// document.getElementById('ws-status').style.color = null;
3131
clearError();
32-
Daemon.connect()
32+
Daemon.findAgent()
3333
.catch(showError);
3434
});

0 commit comments

Comments
 (0)