abstractRemoteAgentService.ts 6.4 KB
Newer Older
A
Alex Dima 已提交
1 2 3 4 5 6 7 8 9 10
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

import * as nls from 'vs/nls';
import { Disposable } from 'vs/base/common/lifecycle';
import { IChannel, IServerChannel, getDelayedChannel } from 'vs/base/parts/ipc/common/ipc';
import { Client } from 'vs/base/parts/ipc/common/ipc.net';
import { IEnvironmentService } from 'vs/platform/environment/common/environment';
A
Alex Dima 已提交
11
import { connectRemoteAgentManagement, IConnectionOptions, IWebSocketFactory, PersistenConnectionEvent } from 'vs/platform/remote/common/remoteAgentConnection';
A
Alex Dima 已提交
12
import { IRemoteAgentConnection, IRemoteAgentService } from 'vs/workbench/services/remote/common/remoteAgentService';
A
Tweaks  
Alex Dima 已提交
13
import { IRemoteAuthorityResolverService, RemoteAuthorityResolverError } from 'vs/platform/remote/common/remoteAuthorityResolver';
A
Alex Dima 已提交
14 15 16 17 18 19
import { LifecyclePhase } from 'vs/platform/lifecycle/common/lifecycle';
import { RemoteAgentConnectionContext, IRemoteAgentEnvironment } from 'vs/platform/remote/common/remoteAgentEnvironment';
import { IWorkbenchContribution, IWorkbenchContributionsRegistry, Extensions } from 'vs/workbench/common/contributions';
import { Registry } from 'vs/platform/registry/common/platform';
import { RemoteExtensionEnvironmentChannelClient } from 'vs/workbench/services/remote/common/remoteAgentEnvironmentChannel';
import { INotificationService } from 'vs/platform/notification/common/notification';
R
Rachel Macfarlane 已提交
20
import { IDiagnosticInfoOptions, IDiagnosticInfo } from 'vs/platform/diagnostics/common/diagnosticsService';
A
Alex Dima 已提交
21
import { Emitter } from 'vs/base/common/event';
A
Alex Dima 已提交
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

export abstract class AbstractRemoteAgentService extends Disposable implements IRemoteAgentService {

	_serviceBrand: any;

	private _environment: Promise<IRemoteAgentEnvironment | null> | null;

	constructor(
		@IEnvironmentService protected readonly _environmentService: IEnvironmentService
	) {
		super();
	}

	abstract getConnection(): IRemoteAgentConnection | null;

	getEnvironment(bail?: boolean): Promise<IRemoteAgentEnvironment | null> {
		if (!this._environment) {
			const connection = this.getConnection();
			if (connection) {
				const client = new RemoteExtensionEnvironmentChannelClient(connection.getChannel('remoteextensionsenvironment'));
				this._environment = client.getEnvironmentData(connection.remoteAuthority, this._environmentService.extensionDevelopmentLocationURI);
			} else {
				this._environment = Promise.resolve(null);
			}
		}
		return bail ? this._environment : this._environment.then(undefined, () => null);
	}
R
Rachel Macfarlane 已提交
49 50 51 52 53 54 55 56

	getDiagnosticInfo(options: IDiagnosticInfoOptions): Promise<IDiagnosticInfo | undefined> {
		const connection = this.getConnection();
		if (connection) {
			const client = new RemoteExtensionEnvironmentChannelClient(connection.getChannel('remoteextensionsenvironment'));
			return client.getDiagnosticInfo(options);
		}

R
Rob Lourens 已提交
57 58 59 60 61 62 63 64 65 66
		return Promise.resolve(undefined);
	}

	disableTelemetry(): Promise<void> {
		const connection = this.getConnection();
		if (connection) {
			const client = new RemoteExtensionEnvironmentChannelClient(connection.getChannel('remoteextensionsenvironment'));
			return client.disableTelemetry();
		}

R
Rachel Macfarlane 已提交
67 68
		return Promise.resolve(undefined);
	}
A
Alex Dima 已提交
69 70 71 72
}

export class RemoteAgentConnection extends Disposable implements IRemoteAgentConnection {

A
Alex Dima 已提交
73 74 75
	private readonly _onReconnecting = this._register(new Emitter<void>());
	public readonly onReconnecting = this._onReconnecting.event;

A
Alex Dima 已提交
76 77 78
	private readonly _onDidStateChange = this._register(new Emitter<PersistenConnectionEvent>());
	public readonly onDidStateChange = this._onDidStateChange.event;

A
Alex Dima 已提交
79 80 81 82 83
	readonly remoteAuthority: string;
	private _connection: Promise<Client<RemoteAgentConnectionContext>> | null;

	constructor(
		remoteAuthority: string,
A
Alex Dima 已提交
84 85 86 87
		private readonly _commit: string | undefined,
		private readonly _webSocketFactory: IWebSocketFactory,
		private readonly _environmentService: IEnvironmentService,
		private readonly _remoteAuthorityResolverService: IRemoteAuthorityResolverService
A
Alex Dima 已提交
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
	) {
		super();
		this.remoteAuthority = remoteAuthority;
		this._connection = null;
	}

	getChannel<T extends IChannel>(channelName: string): T {
		return <T>getDelayedChannel(this._getOrCreateConnection().then(c => c.getChannel(channelName)));
	}

	registerChannel<T extends IServerChannel<RemoteAgentConnectionContext>>(channelName: string, channel: T): void {
		this._getOrCreateConnection().then(client => client.registerChannel(channelName, channel));
	}

	private _getOrCreateConnection(): Promise<Client<RemoteAgentConnectionContext>> {
		if (!this._connection) {
			this._connection = this._createConnection();
		}
		return this._connection;
	}

	private async _createConnection(): Promise<Client<RemoteAgentConnectionContext>> {
A
Alex Dima 已提交
110
		let firstCall = true;
A
Alex Dima 已提交
111 112 113 114 115 116
		const options: IConnectionOptions = {
			isBuilt: this._environmentService.isBuilt,
			commit: this._commit,
			webSocketFactory: this._webSocketFactory,
			addressProvider: {
				getAddress: async () => {
A
Alex Dima 已提交
117 118 119 120 121
					if (firstCall) {
						firstCall = false;
					} else {
						this._onReconnecting.fire(undefined);
					}
A
Alex Dima 已提交
122 123 124 125 126
					const { host, port } = await this._remoteAuthorityResolverService.resolveAuthority(this.remoteAuthority);
					return { host, port };
				}
			}
		};
A
Alex Dima 已提交
127 128
		const connection = this._register(await connectRemoteAgentManagement(options, this.remoteAuthority, `renderer`));
		this._register(connection.onDidStateChange(e => this._onDidStateChange.fire(e)));
A
Alex Dima 已提交
129 130 131 132 133 134 135 136 137 138 139 140
		return connection.client;
	}
}

class RemoteConnectionFailureNotificationContribution implements IWorkbenchContribution {

	constructor(
		@IRemoteAgentService remoteAgentService: IRemoteAgentService,
		@INotificationService notificationService: INotificationService,
	) {
		// Let's cover the case where connecting to fetch the remote extension info fails
		remoteAgentService.getEnvironment(true)
A
Tweaks  
Alex Dima 已提交
141 142 143 144 145
			.then(undefined, err => {
				if (!RemoteAuthorityResolverError.isHandledNotAvailable(err)) {
					notificationService.error(nls.localize('connectionError', "Failed to connect to the remote extension host server (Error: {0})", err ? err.message : ''));
				}
			});
A
Alex Dima 已提交
146 147 148 149 150 151
	}

}

const workbenchRegistry = Registry.as<IWorkbenchContributionsRegistry>(Extensions.Workbench);
workbenchRegistry.registerWorkbenchContribution(RemoteConnectionFailureNotificationContribution, LifecyclePhase.Ready);