-
-
Notifications
You must be signed in to change notification settings - Fork 436
/
Copy pathboard-discovery.ts
320 lines (300 loc) · 10.3 KB
/
board-discovery.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
import type { ClientReadableStream } from '@grpc/grpc-js';
import {
Disposable,
DisposableCollection,
} from '@theia/core/lib/common/disposable';
import { Emitter, Event } from '@theia/core/lib/common/event';
import { ILogger } from '@theia/core/lib/common/logger';
import { deepClone } from '@theia/core/lib/common/objects';
import { Deferred } from '@theia/core/lib/common/promise-util';
import type { Mutable } from '@theia/core/lib/common/types';
import { BackendApplicationContribution } from '@theia/core/lib/node/backend-application';
import { UUID } from '@theia/core/shared/@phosphor/coreutils';
import { inject, injectable, named } from '@theia/core/shared/inversify';
import { isDeepStrictEqual } from 'util';
import { Unknown } from '../common/nls';
import {
Board,
DetectedPort,
DetectedPorts,
NotificationServiceServer,
Port,
} from '../common/protocol';
import {
BoardListWatchRequest,
BoardListWatchResponse,
DetectedPort as RpcDetectedPort,
} from './cli-protocol/cc/arduino/cli/commands/v1/board_pb';
import { ArduinoCoreServiceClient } from './cli-protocol/cc/arduino/cli/commands/v1/commands_grpc_pb';
import type { Port as RpcPort } from './cli-protocol/cc/arduino/cli/commands/v1/port_pb';
import { CoreClientAware } from './core-client-provider';
import { ServiceError } from './service-error';
type Stream = ClientReadableStream<BoardListWatchResponse>;
interface StreamWrapper extends Disposable {
readonly stream: Stream;
readonly uuid: string; // For logging only
}
/**
* Singleton service for tracking the available ports and board and broadcasting the
* changes to all connected frontend instances.
*
* Unlike other services, this is not connection scoped.
*/
@injectable()
export class BoardDiscovery
extends CoreClientAware
implements BackendApplicationContribution
{
@inject(ILogger)
@named('discovery-log')
private readonly logger: ILogger;
@inject(NotificationServiceServer)
private readonly notificationService: NotificationServiceServer;
private watching: Deferred<void> | undefined;
private stopping: Deferred<void> | undefined;
private wrapper: StreamWrapper | undefined;
private readonly onStreamDidEndEmitter = new Emitter<void>(); // sent from the CLI when the discovery process is killed for example after the indexes update and the core client re-initialization.
private readonly onStreamDidCancelEmitter = new Emitter<void>(); // when the watcher is canceled by the IDE2
private readonly toDisposeOnStopWatch = new DisposableCollection();
private _detectedPorts: DetectedPorts = {};
get detectedPorts(): DetectedPorts {
return this._detectedPorts;
}
onStart(): void {
this.start();
}
onStop(): void {
this.stop();
}
async stop(restart = false): Promise<void> {
this.logger.info('stop');
if (this.stopping) {
this.logger.info('stop already stopping');
return this.stopping.promise;
}
if (!this.watching) {
return;
}
this.stopping = new Deferred();
this.logger.info('>>> Stopping boards watcher...');
return new Promise<void>((resolve, reject) => {
const timeout = this.createTimeout(10_000, reject);
const toDispose = new DisposableCollection();
const waitForEvent = (event: Event<unknown>) =>
event(() => {
this.logger.info('stop received event: either end or cancel');
toDispose.dispose();
this.stopping?.resolve();
this.stopping = undefined;
this.logger.info('stop stopped');
resolve();
if (restart) {
this.start();
}
});
toDispose.pushAll([
timeout,
waitForEvent(this.onStreamDidEndEmitter.event),
waitForEvent(this.onStreamDidCancelEmitter.event),
]);
this.logger.info('Canceling boards watcher...');
this.toDisposeOnStopWatch.dispose();
});
}
private createTimeout(
after: number,
onTimeout: (error: Error) => void
): Disposable {
const timer = setTimeout(
() => onTimeout(new Error(`Timed out after ${after} ms.`)),
after
);
return Disposable.create(() => clearTimeout(timer));
}
private async createWrapper(
client: ArduinoCoreServiceClient,
req: BoardListWatchRequest
): Promise<StreamWrapper> {
if (this.wrapper) {
throw new Error(`Duplex was already set.`);
}
const stream = client
.boardListWatch(req)
.on('end', () => {
this.logger.info('received end');
this.onStreamDidEndEmitter.fire();
})
.on('error', (error) => {
this.logger.info('error received');
if (ServiceError.isCancel(error)) {
this.logger.info('cancel error received!');
this.onStreamDidCancelEmitter.fire();
} else {
this.logger.error(
'Unexpected error occurred during the boards discovery.',
error
);
// TODO: terminate? restart? reject?
}
});
const wrapper = {
stream,
uuid: UUID.uuid4(),
dispose: () => {
this.logger.info('disposing requesting cancel');
// Cancelling the stream will kill the discovery `builtin:mdns-discovery process`.
// The client (this class) will receive a `{"eventType":"quit","error":""}` response from the CLI.
stream.cancel();
this.logger.info('disposing canceled');
this.wrapper = undefined;
},
};
this.toDisposeOnStopWatch.pushAll([
wrapper,
Disposable.create(() => {
this.watching?.reject(new Error(`Stopping watcher.`));
this.watching = undefined;
}),
]);
return wrapper;
}
async start(): Promise<void> {
this.logger.info('start');
if (this.stopping) {
this.logger.info('start is stopping wait');
await this.stopping.promise;
this.logger.info('start stopped');
}
if (this.watching) {
this.logger.info('start already watching');
return this.watching.promise;
}
this.watching = new Deferred();
this.logger.info('start new deferred');
const { client, instance } = await this.coreClient;
const wrapper = await this.createWrapper(
client,
new BoardListWatchRequest().setInstance(instance)
);
wrapper.stream.on('data', (resp) => this.onBoardListWatchResponse(resp));
this.watching.resolve();
this.logger.info('start resolved watching');
}
protected onBoardListWatchResponse(resp: BoardListWatchResponse): void {
this.logger.info(JSON.stringify(resp.toObject(false)));
const eventType = EventType.parse(resp.getEventType());
if (eventType === EventType.Quit) {
this.logger.info('quit received');
this.stop();
return;
}
const rpcDetectedPort = resp.getPort();
if (rpcDetectedPort) {
const detectedPort = this.fromRpc(rpcDetectedPort);
if (detectedPort) {
this.fireSoon({ detectedPort, eventType });
} else {
this.logger.warn(
`Could not extract the detected port from ${rpcDetectedPort.toObject(
false
)}`
);
}
} else if (resp.getError()) {
this.logger.error(
`Could not extract any detected 'port' from the board list watch response. An 'error' has occurred: ${resp.getError()}`
);
}
}
private fromRpc(detectedPort: RpcDetectedPort): DetectedPort | undefined {
const rpcPort = detectedPort.getPort();
if (!rpcPort) {
return undefined;
}
const port = createApiPort(rpcPort);
const boards = detectedPort.getMatchingBoardsList().map(
(board) =>
({
fqbn: board.getFqbn() || undefined, // prefer undefined fqbn over empty string
name: board.getName() || Unknown,
} as Board)
);
return {
boards,
port,
};
}
private fireSoonHandle: NodeJS.Timeout | undefined;
private readonly bufferedEvents: DetectedPortChangeEvent[] = [];
private fireSoon(event: DetectedPortChangeEvent): void {
this.bufferedEvents.push(event);
clearTimeout(this.fireSoonHandle);
this.fireSoonHandle = setTimeout(() => {
const current = deepClone(this.detectedPorts);
const newState = this.calculateNewState(this.bufferedEvents, current);
if (!isDeepStrictEqual(current, newState)) {
this._detectedPorts = newState;
this.notificationService.notifyDetectedPortsDidChange({
detectedPorts: this._detectedPorts,
});
}
this.bufferedEvents.length = 0;
}, 100);
}
private calculateNewState(
events: DetectedPortChangeEvent[],
prevState: Mutable<DetectedPorts>
): DetectedPorts {
const newState = deepClone(prevState);
for (const { detectedPort, eventType } of events) {
const { port, boards } = detectedPort;
const key = Port.keyOf(port);
if (eventType === EventType.Add) {
// Note that, the serial discovery might detect port details (such as addressLabel) in chunks.
// For example, first, the Teensy 4.1 port is detected with the `[no_device] Triple Serial` address label,
// Then, when more refinements are available, the same port is detected with `/dev/cu.usbmodem127902301 Triple Serial` address label.
// In such cases, an `add` event is received from the CLI, and the the detected port is overridden in the state.
newState[key] = { port, boards };
} else if (eventType === EventType.Remove) {
delete newState[key];
}
}
return newState;
}
}
enum EventType {
Add,
Remove,
Quit,
}
namespace EventType {
export function parse(type: string): EventType {
const normalizedType = type.toLowerCase();
switch (normalizedType) {
case 'add':
return EventType.Add;
case 'remove':
return EventType.Remove;
case 'quit':
return EventType.Quit;
default:
throw new Error(
`Unexpected 'BoardListWatchResponse' event type: '${type}.'`
);
}
}
}
interface DetectedPortChangeEvent {
readonly detectedPort: DetectedPort;
readonly eventType: EventType.Add | EventType.Remove;
}
export function createApiPort(rpcPort: RpcPort): Port {
return {
address: rpcPort.getAddress(),
addressLabel: rpcPort.getLabel(),
protocol: rpcPort.getProtocol(),
protocolLabel: rpcPort.getProtocolLabel(),
properties: Port.Properties.create(rpcPort.getPropertiesMap().toObject()),
hardwareId: rpcPort.getHardwareId() || undefined, // prefer undefined over empty string
};
}