forked from arduino/arduino-ide
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmonitor-service.ts
722 lines (651 loc) · 23.2 KB
/
monitor-service.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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
import { ClientDuplexStream } from '@grpc/grpc-js';
import { Disposable, Emitter, ILogger } from '@theia/core';
import { inject, named, postConstruct } from '@theia/core/shared/inversify';
import { diff, Operation } from 'just-diff';
import { Board, Port, Status, Monitor } from '../common/protocol';
import {
EnumerateMonitorPortSettingsRequest,
EnumerateMonitorPortSettingsResponse,
MonitorPortConfiguration,
MonitorPortSetting,
MonitorRequest,
MonitorResponse,
} from './cli-protocol/cc/arduino/cli/commands/v1/monitor_pb';
import { CoreClientAware } from './core-client-provider';
import { WebSocketProvider } from './web-socket/web-socket-provider';
import { Port as RpcPort } from './cli-protocol/cc/arduino/cli/commands/v1/port_pb';
import {
MonitorSettings,
PluggableMonitorSettings,
MonitorSettingsProvider,
} from './monitor-settings/monitor-settings-provider';
import { Deferred } from '@theia/core/lib/common/promise-util';
import { MonitorServiceFactoryOptions } from './monitor-service-factory';
export const MonitorServiceName = 'monitor-service';
type DuplexHandlerKeys =
| 'close'
| 'end'
| 'error'
| 'data'
| 'status'
| 'metadata';
interface DuplexHandler {
key: DuplexHandlerKeys;
callback: (...args: any) => void;
}
const MAX_WRITE_TO_STREAM_TRIES = 10;
const WRITE_TO_STREAM_TIMEOUT_MS = 30000;
export class MonitorService extends CoreClientAware implements Disposable {
@inject(ILogger)
@named(MonitorServiceName)
private readonly logger: ILogger;
@inject(MonitorSettingsProvider)
private readonly monitorSettingsProvider: MonitorSettingsProvider;
@inject(WebSocketProvider)
private readonly webSocketProvider: WebSocketProvider;
// Bidirectional gRPC stream used to receive and send data from the running
// pluggable monitor managed by the Arduino CLI.
private duplex: ClientDuplexStream<MonitorRequest, MonitorResponse> | null;
// Settings used by the currently running pluggable monitor.
// They can be freely modified while running.
private settings: MonitorSettings = {};
// List of messages received from the running pluggable monitor.
// These are flushed from time to time to the frontend.
private messages: string[] = [];
// Handles messages received from the frontend via websocket.
private onMessageReceived?: Disposable;
// Sends messages to the frontend from time to time.
private flushMessagesInterval?: NodeJS.Timeout;
// Triggered each time the number of clients connected
// to the this service WebSocket changes.
private onWSClientsNumberChanged?: Disposable;
// Used to notify that the monitor is being disposed
private readonly onDisposeEmitter = new Emitter<void>();
readonly onDispose = this.onDisposeEmitter.event;
private _initialized = new Deferred<void>();
private creating: Deferred<Status>;
private readonly board: Board;
private readonly port: Port;
private readonly monitorID: string;
private readonly streamingTextDecoder = new TextDecoder('utf8');
/**
* The lightweight representation of the port configuration currently in use for the running monitor.
* IDE2 stores this object after starting the monitor. On every monitor settings change request, IDE2 compares
* the current config with the new settings, and only sends the diff as the new config to overcome https://github.com/arduino/arduino-ide/issues/375.
*/
private currentPortConfigSnapshot:
| MonitorPortConfiguration.AsObject
| undefined;
constructor(
@inject(MonitorServiceFactoryOptions) options: MonitorServiceFactoryOptions
) {
super();
this.board = options.board;
this.port = options.port;
this.monitorID = options.monitorID;
}
@postConstruct()
protected init(): void {
this.onWSClientsNumberChanged =
this.webSocketProvider.onClientsNumberChanged(async (clients: number) => {
if (clients === 0) {
// There are no more clients that want to receive
// data from this monitor, we can freely close
// and dispose it.
this.dispose();
return;
}
this.updateClientsSettings(this.settings);
});
this.portMonitorSettings(this.port.protocol, this.board.fqbn!).then(
async (settings) => {
this.settings = {
...this.settings,
pluggableMonitorSettings:
await this.monitorSettingsProvider.getSettings(
this.monitorID,
settings
),
};
this._initialized.resolve();
}
);
}
get initialized(): Promise<void> {
return this._initialized.promise;
}
getWebsocketAddressPort(): number {
return this.webSocketProvider.getAddress().port;
}
dispose(): void {
this.stop();
this.onDisposeEmitter.fire();
this.onWSClientsNumberChanged?.dispose();
}
/**
* isStarted is used to know if the currently running pluggable monitor is started.
* @returns true if pluggable monitor communication duplex is open,
* false in all other cases.
*/
isStarted(): boolean {
return !!this.duplex;
}
/**
* Start and connects a monitor using currently set board and port.
* If a monitor is already started or board fqbn, port address and/or protocol
* are missing nothing happens.
* @returns a status to verify connection has been established.
*/
async start(): Promise<Status> {
if (this.creating?.state === 'unresolved') return this.creating.promise;
this.creating = new Deferred();
if (this.duplex) {
this.updateClientsSettings({
monitorUISettings: { connected: true, serialPort: this.port.address },
});
this.creating.resolve(Status.ALREADY_CONNECTED);
return this.creating.promise;
}
if (!this.board?.fqbn || !this.port?.address || !this.port?.protocol) {
this.updateClientsSettings({ monitorUISettings: { connected: false } });
this.creating.resolve(Status.CONFIG_MISSING);
return this.creating.promise;
}
this.logger.info('starting monitor');
// get default monitor settings from the CLI
const defaultSettings = await this.portMonitorSettings(
this.port.protocol,
this.board.fqbn
);
// get actual settings from the settings provider
this.settings = {
...this.settings,
pluggableMonitorSettings: {
...this.settings.pluggableMonitorSettings,
...(await this.monitorSettingsProvider.getSettings(
this.monitorID,
defaultSettings
)),
},
};
const coreClient = await this.coreClient;
const { instance } = coreClient;
const monitorRequest = new MonitorRequest();
monitorRequest.setInstance(instance);
if (this.board?.fqbn) {
monitorRequest.setFqbn(this.board.fqbn);
}
if (this.port?.address && this.port?.protocol) {
const rpcPort = new RpcPort();
rpcPort.setAddress(this.port.address);
rpcPort.setProtocol(this.port.protocol);
monitorRequest.setPort(rpcPort);
}
const config = new MonitorPortConfiguration();
for (const id in this.settings.pluggableMonitorSettings) {
const s = new MonitorPortSetting();
s.setSettingId(id);
s.setValue(this.settings.pluggableMonitorSettings[id].selectedValue);
config.addSettings(s);
}
monitorRequest.setPortConfiguration(config);
const wroteToStreamSuccessfully = await this.pollWriteToStream(
monitorRequest
);
if (wroteToStreamSuccessfully) {
// Only store the config, if the monitor has successfully started.
this.currentPortConfigSnapshot = MonitorPortConfiguration.toObject(
false,
config
);
this.logger.info(
`Using port configuration for ${this.port.protocol}:${
this.port.address
}: ${JSON.stringify(this.currentPortConfigSnapshot)}`
);
this.startMessagesHandlers();
this.logger.info(
`started monitor to ${this.port?.address} using ${this.port?.protocol}`
);
this.updateClientsSettings({
monitorUISettings: { connected: true, serialPort: this.port.address },
});
this.creating.resolve(Status.OK);
return this.creating.promise;
} else {
this.logger.warn(
`failed starting monitor to ${this.port?.address} using ${this.port?.protocol}`
);
this.creating.resolve(Status.NOT_CONNECTED);
return this.creating.promise;
}
}
async createDuplex(): Promise<
ClientDuplexStream<MonitorRequest, MonitorResponse>
> {
const coreClient = await this.coreClient;
return coreClient.client.monitor();
}
setDuplexHandlers(
duplex: ClientDuplexStream<MonitorRequest, MonitorResponse>,
additionalHandlers: DuplexHandler[]
): void {
// default handlers
duplex
.on('close', () => {
this.duplex = null;
this.updateClientsSettings({
monitorUISettings: { connected: false },
});
this.logger.info(
`monitor to ${this.port?.address} using ${this.port?.protocol} closed by client`
);
})
.on('end', () => {
this.duplex = null;
this.updateClientsSettings({
monitorUISettings: { connected: false },
});
this.logger.info(
`monitor to ${this.port?.address} using ${this.port?.protocol} closed by server`
);
});
for (const handler of additionalHandlers) {
duplex.on(handler.key, handler.callback);
}
}
pollWriteToStream(request: MonitorRequest): Promise<boolean> {
let attemptsRemaining = MAX_WRITE_TO_STREAM_TRIES;
const writeTimeoutMs = WRITE_TO_STREAM_TIMEOUT_MS;
const createWriteToStreamExecutor =
(duplex: ClientDuplexStream<MonitorRequest, MonitorResponse>) =>
(resolve: (value: boolean) => void, reject: () => void) => {
const resolvingDuplexHandlers: DuplexHandler[] = [
{
key: 'error',
callback: async (err: Error) => {
this.logger.error(err);
resolve(false);
// TODO
// this.theiaFEClient?.notifyError()
},
},
{
key: 'data',
callback: async (monitorResponse: MonitorResponse) => {
if (monitorResponse.getError()) {
// TODO: Maybe disconnect
this.logger.error(monitorResponse.getError());
return;
}
if (monitorResponse.getSuccess()) {
resolve(true);
return;
}
const data = monitorResponse.getRxData();
const message =
typeof data === 'string'
? data
: this.streamingTextDecoder.decode(data, {stream:true});
this.messages.push(...splitLines(message));
},
},
];
this.setDuplexHandlers(duplex, resolvingDuplexHandlers);
setTimeout(() => {
reject();
}, writeTimeoutMs);
duplex.write(request);
};
const pollWriteToStream = new Promise<boolean>((resolve) => {
const startPolling = async () => {
// here we create a new duplex but we don't yet
// set "this.duplex", nor do we use "this.duplex" in our poll
// as duplex 'end' / 'close' events (which we do not "await")
// will set "this.duplex" to null
const createdDuplex = await this.createDuplex();
let pollingIsSuccessful;
// attempt a "writeToStream" and "await" CLI response: success (true) or error (false)
// if we get neither within WRITE_TO_STREAM_TIMEOUT_MS or an error we get undefined
try {
const writeToStream = createWriteToStreamExecutor(createdDuplex);
pollingIsSuccessful = await new Promise(writeToStream);
} catch (error) {
this.logger.error(error);
}
// CLI confirmed port opened successfully
if (pollingIsSuccessful) {
this.duplex = createdDuplex;
resolve(true);
return;
}
// if "pollingIsSuccessful" is false
// the CLI gave us an error, lets try again
// after waiting 2 seconds if we've not already
// reached MAX_WRITE_TO_STREAM_TRIES
if (pollingIsSuccessful === false) {
attemptsRemaining -= 1;
if (attemptsRemaining > 0) {
setTimeout(startPolling, 2000);
return;
} else {
resolve(false);
return;
}
}
// "pollingIsSuccessful" remains undefined:
// we got no response from the CLI within 30 seconds
// resolve to false and end the duplex connection
resolve(false);
createdDuplex.end();
return;
};
startPolling();
});
return pollWriteToStream;
}
/**
* Pauses the currently running monitor, it still closes the gRPC connection
* with the underlying monitor process but it doesn't stop the message handlers
* currently running.
* This is mainly used to handle upload with the board/port combination
* the monitor is listening to.
* @returns
*/
async pause(): Promise<void> {
return new Promise(async (resolve) => {
if (!this.duplex) {
this.logger.warn(
`monitor to ${this.port?.address} using ${this.port?.protocol} already stopped`
);
return resolve();
}
// It's enough to close the connection with the client
// to stop the monitor process
this.duplex.end();
this.logger.info(
`stopped monitor to ${this.port?.address} using ${this.port?.protocol}`
);
this.duplex.on('end', resolve);
});
}
/**
* Stop the monitor currently running
*/
async stop(): Promise<void> {
return this.pause().finally(this.stopMessagesHandlers.bind(this));
}
/**
* Send a message to the running monitor, a well behaved monitor
* will then send that message to the board.
* We MUST NEVER send a message that wasn't a user's input to the board.
* @param message string sent to running monitor
* @returns a status to verify message has been sent.
*/
async send(message: string): Promise<Status> {
if (!this.duplex) {
return Status.NOT_CONNECTED;
}
const coreClient = await this.coreClient;
const { instance } = coreClient;
const req = new MonitorRequest();
req.setInstance(instance);
req.setTxData(new TextEncoder().encode(message));
return new Promise<Status>((resolve) => {
if (this.duplex) {
this.duplex?.write(req, () => {
resolve(Status.OK);
});
return;
}
this.stop().then(() => resolve(Status.NOT_CONNECTED));
});
}
/**
*
* @returns map of current monitor settings
*/
async currentSettings(): Promise<MonitorSettings> {
await this.initialized;
return this.settings;
}
// TODO: move this into MonitorSettingsProvider
/**
* Returns the possible configurations used to connect a monitor
* to the board specified by fqbn using the specified protocol
* @param protocol the protocol of the monitor we want get settings for
* @param fqbn the fqbn of the board we want to monitor
* @returns a map of all the settings supported by the monitor
*/
private async portMonitorSettings(
protocol: string,
fqbn: string
): Promise<PluggableMonitorSettings> {
const coreClient = await this.coreClient;
const { client, instance } = coreClient;
const req = new EnumerateMonitorPortSettingsRequest();
req.setInstance(instance);
req.setPortProtocol(protocol);
req.setFqbn(fqbn);
const res = await new Promise<EnumerateMonitorPortSettingsResponse>(
(resolve, reject) => {
client.enumerateMonitorPortSettings(req, (err, resp) => {
if (!!err) {
reject(err);
}
resolve(resp);
});
}
);
const settings: PluggableMonitorSettings = {};
for (const iterator of res.getSettingsList()) {
settings[iterator.getSettingId()] = {
id: iterator.getSettingId(),
label: iterator.getLabel(),
type: iterator.getType(),
values: iterator.getEnumValuesList(),
selectedValue: iterator.getValue(),
};
}
return settings;
}
/**
* Set monitor settings, if there is a running monitor they'll be sent
* to it, otherwise they'll be used when starting one.
* Only values in settings parameter will be change, other values won't
* be changed in any way.
* @param settings map of monitor settings to change
* @returns a status to verify settings have been sent.
*/
async changeSettings(settings: MonitorSettings): Promise<Status> {
const config = new MonitorPortConfiguration();
const { pluggableMonitorSettings } = settings;
const reconciledSettings = await this.monitorSettingsProvider.setSettings(
this.monitorID,
pluggableMonitorSettings || {}
);
if (reconciledSettings) {
for (const id in reconciledSettings) {
const s = new MonitorPortSetting();
s.setSettingId(id);
s.setValue(reconciledSettings[id].selectedValue);
config.addSettings(s);
}
}
this.updateClientsSettings({
monitorUISettings: {
...settings.monitorUISettings,
connected: !!this.duplex,
serialPort: this.port.address,
},
pluggableMonitorSettings: reconciledSettings,
});
if (!this.duplex) {
return Status.NOT_CONNECTED;
}
const diffConfig = this.maybeUpdatePortConfigSnapshot(config);
if (!diffConfig) {
this.logger.info(
`No port configuration changes have been detected. No need to send configure commands to the running monitor ${this.port.protocol}:${this.port.address}.`
);
return Status.OK;
}
const coreClient = await this.coreClient;
const { instance } = coreClient;
this.logger.info(
`Sending monitor request with new port configuration: ${JSON.stringify(
MonitorPortConfiguration.toObject(false, diffConfig)
)}`
);
const req = new MonitorRequest();
req.setInstance(instance);
req.setPortConfiguration(diffConfig);
this.duplex.write(req);
return Status.OK;
}
/**
* Function to calculate a diff between the `otherPortConfig` argument and the `currentPortConfigSnapshot`.
*
* If the current config snapshot and the snapshot derived from `otherPortConfig` are the same, no snapshot update happens,
* and the function returns with undefined. Otherwise, the current snapshot config value will be updated from the snapshot
* derived from the `otherPortConfig` argument, and this function returns with a `MonitorPortConfiguration` instance
* representing only the difference between the two snapshot configs to avoid sending unnecessary monitor to configure commands to the CLI.
* See [#1703 (comment)](https://github.com/arduino/arduino-ide/pull/1703#issuecomment-1327913005) for more details.
*/
private maybeUpdatePortConfigSnapshot(
otherPortConfig: MonitorPortConfiguration
): MonitorPortConfiguration | undefined {
const otherPortConfigSnapshot = MonitorPortConfiguration.toObject(
false,
otherPortConfig
);
if (!this.currentPortConfigSnapshot) {
throw new Error(
`The current port configuration object was undefined when tried to merge in ${JSON.stringify(
otherPortConfigSnapshot
)}.`
);
}
const snapshotDiff = diff(
this.currentPortConfigSnapshot,
otherPortConfigSnapshot
);
if (!snapshotDiff.length) {
return undefined;
}
const diffConfig = snapshotDiff.reduce((acc, curr) => {
if (!this.isValidMonitorPortSettingChange(curr)) {
throw new Error(
`Expected only 'replace' operation: a 'value' change in the 'settingsList'. Calculated diff a ${JSON.stringify(
snapshotDiff
)} between ${JSON.stringify(
this.currentPortConfigSnapshot
)} and ${JSON.stringify(
otherPortConfigSnapshot
)} snapshots. Current JSON-patch entry was ${JSON.stringify(curr)}.`
);
}
const { path, value } = curr;
const [, index] = path;
if (!this.currentPortConfigSnapshot?.settingsList) {
throw new Error(
`'settingsList' is missing from current port config snapshot: ${JSON.stringify(
this.currentPortConfigSnapshot
)}`
);
}
const changedSetting = this.currentPortConfigSnapshot.settingsList[index];
const setting = new MonitorPortSetting();
setting.setValue(value);
setting.setSettingId(changedSetting.settingId);
acc.addSettings(setting);
return acc;
}, new MonitorPortConfiguration());
this.currentPortConfigSnapshot = otherPortConfigSnapshot;
this.logger.info(
`Updated the port configuration for ${this.port.protocol}:${
this.port.address
}: ${JSON.stringify(this.currentPortConfigSnapshot)}`
);
return diffConfig;
}
private isValidMonitorPortSettingChange(entry: {
op: Operation;
path: (string | number)[];
value: unknown;
}): entry is {
op: 'replace';
path: ['settingsList', number, string];
value: string;
} {
const { op, path, value } = entry;
return (
op === 'replace' &&
path.length === 3 &&
path[0] === 'settingsList' &&
typeof path[1] === 'number' &&
path[2] === 'value' &&
typeof value === 'string'
);
}
/**
* Starts the necessary handlers to send and receive
* messages to and from the frontend and the running monitor
*/
private startMessagesHandlers(): void {
if (!this.flushMessagesInterval) {
const flushMessagesToFrontend = () => {
if (this.messages.length) {
this.webSocketProvider.sendMessage(JSON.stringify(this.messages));
this.messages = [];
}
};
this.flushMessagesInterval = setInterval(flushMessagesToFrontend, 32);
}
if (!this.onMessageReceived) {
this.onMessageReceived = this.webSocketProvider.onMessageReceived(
(msg: string) => {
const message: Monitor.Message = JSON.parse(msg);
switch (message.command) {
case Monitor.ClientCommand.SEND_MESSAGE:
this.send(message.data as string);
break;
case Monitor.ClientCommand.CHANGE_SETTINGS:
this.changeSettings(message.data as MonitorSettings);
break;
}
}
);
}
}
updateClientsSettings(settings: MonitorSettings): void {
this.settings = { ...this.settings, ...settings };
const command: Monitor.Message = {
command: Monitor.MiddlewareCommand.ON_SETTINGS_DID_CHANGE,
data: settings,
};
this.webSocketProvider.sendMessage(JSON.stringify(command));
}
/**
* Stops the necessary handlers to send and receive messages to
* and from the frontend and the running monitor
*/
private stopMessagesHandlers(): void {
if (this.flushMessagesInterval) {
clearInterval(this.flushMessagesInterval);
this.flushMessagesInterval = undefined;
}
if (this.onMessageReceived) {
this.onMessageReceived.dispose();
this.onMessageReceived = undefined;
}
}
}
/**
* Splits a string into an array without removing newline char.
* @param s string to split into lines
* @returns an lines array
*/
function splitLines(s: string): string[] {
return s.split(/(?<=\n)/);
}