microsoft/vscode-react-native

Public

mirrored from https://github.com/microsoft/vscode-react-nativeAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
0.1.4

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

src/extension/extensionServer.ts

195lines · modeblame

0502b7a8dlebu10 years ago1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the MIT license. See LICENSE file in the project root for details.
3
6b6eb911dlebu10 years ago4import * as net from "net";
710f8655digeff10 years ago5import * as Q from "q";
acf08bc2dlebu10 years ago6import * as vscode from "vscode";
7
0502b7a8dlebu10 years ago8import * as em from "../common/extensionMessaging";
a1005420dlebu10 years ago9import {HostPlatform} from "../common/hostPlatform";
a289475bMeena Kunnathur Balakrishnan10 years ago10import {Log} from "../common/log/log";
f29c7bfeMeena Kunnathur Balakrishnan10 years ago11import {LogLevel} from "../common/log/logHelper";
710f8655digeff10 years ago12import {Packager} from "../common/packager";
ffffa686Meena Kunnathur Balakrishnan10 years ago13import {PackagerStatus, PackagerStatusIndicator} from "./packagerStatusIndicator";
710f8655digeff10 years ago14import {LogCatMonitor} from "./android/logCatMonitor";
15import {FileSystem} from "../common/node/fileSystem";
6e4d7a62Joshua Skelton10 years ago16import {Telemetry} from "../common/telemetry";
0502b7a8dlebu10 years ago17
18export class ExtensionServer implements vscode.Disposable {
6b6eb911dlebu10 years ago19private serverInstance: net.Server = null;
3c013cdedlebu10 years ago20private messageHandlerDictionary: { [id: number]: ((...argArray: any[]) => Q.Promise<any>) } = {};
a822ac85dlebu10 years ago21private reactNativePackager: Packager;
ffffa686Meena Kunnathur Balakrishnan10 years ago22private reactNativePackageStatusIndicator: PackagerStatusIndicator;
8411fd8dDaniel Lebu10 years ago23private pipePath: string;
710f8655digeff10 years ago24private logCatMonitor: LogCatMonitor = null;
a822ac85dlebu10 years ago25
ffffa686Meena Kunnathur Balakrishnan10 years ago26public constructor(reactNativePackager: Packager, packagerStatusIndicator: PackagerStatusIndicator) {
445b113fdlebu10 years ago27
a1005420dlebu10 years ago28this.pipePath = HostPlatform.getExtensionPipePath();
a822ac85dlebu10 years ago29this.reactNativePackager = reactNativePackager;
ffffa686Meena Kunnathur Balakrishnan10 years ago30this.reactNativePackageStatusIndicator = packagerStatusIndicator;
a822ac85dlebu10 years ago31
32/* register handlers for all messages */
2743f19cdlebu10 years ago33this.messageHandlerDictionary[em.ExtensionMessage.START_PACKAGER] = this.startPackager;
34this.messageHandlerDictionary[em.ExtensionMessage.STOP_PACKAGER] = this.stopPackager;
35this.messageHandlerDictionary[em.ExtensionMessage.PREWARM_BUNDLE_CACHE] = this.prewarmBundleCache;
710f8655digeff10 years ago36this.messageHandlerDictionary[em.ExtensionMessage.START_MONITORING_LOGCAT] = this.startMonitoringLogCat;
c2bf3c4fdigeff10 years ago37this.messageHandlerDictionary[em.ExtensionMessage.STOP_MONITORING_LOGCAT] = this.stopMonitoringLogCat;
6e4d7a62Joshua Skelton10 years ago38this.messageHandlerDictionary[em.ExtensionMessage.SEND_TELEMETRY] = this.sendTelemetry;
a822ac85dlebu10 years ago39}
0502b7a8dlebu10 years ago40
41/**
42* Starts the server.
43*/
44public setup(): Q.Promise<void> {
45
46let deferred = Q.defer<void>();
47
48let launchCallback = (error: any) => {
f29c7bfeMeena Kunnathur Balakrishnan10 years ago49Log.logInternalMessage(LogLevel.Info, "Extension messaging server started.");
0502b7a8dlebu10 years ago50if (error) {
51deferred.reject(error);
52} else {
53deferred.resolve(null);
54}
55};
56
6b6eb911dlebu10 years ago57this.serverInstance = net.createServer(this.handleSocket.bind(this));
58this.serverInstance.on("error", this.recoverServer.bind(this));
8411fd8dDaniel Lebu10 years ago59this.serverInstance.listen(this.pipePath, launchCallback);
0502b7a8dlebu10 years ago60
61return deferred.promise;
62}
63
64/**
65* Stops the server.
66*/
67public dispose(): void {
68if (this.serverInstance) {
69this.serverInstance.close();
acf08bc2dlebu10 years ago70this.serverInstance = null;
0502b7a8dlebu10 years ago71}
710f8655digeff10 years ago72
c2bf3c4fdigeff10 years ago73this.stopMonitoringLogCat();
0502b7a8dlebu10 years ago74}
75
a822ac85dlebu10 years ago76/**
77* Message handler for START_PACKAGER.
78*/
79private startPackager(): Q.Promise<any> {
ffffa686Meena Kunnathur Balakrishnan10 years ago80return this.reactNativePackager.start()
81.then(() => this.reactNativePackageStatusIndicator.updatePackagerStatus(PackagerStatus.PACKAGER_STARTED));
a822ac85dlebu10 years ago82}
83
84/**
85* Message handler for STOP_PACKAGER.
86*/
87private stopPackager(): Q.Promise<any> {
ffffa686Meena Kunnathur Balakrishnan10 years ago88return this.reactNativePackager.stop()
89.then(() => this.reactNativePackageStatusIndicator.updatePackagerStatus(PackagerStatus.PACKAGER_STOPPED));
a822ac85dlebu10 years ago90}
91
2743f19cdlebu10 years ago92/**
93* Message handler for PREWARM_BUNDLE_CACHE.
94*/
3c013cdedlebu10 years ago95private prewarmBundleCache(platform: string): Q.Promise<any> {
2743f19cdlebu10 years ago96return this.reactNativePackager.prewarmBundleCache(platform);
97}
98
710f8655digeff10 years ago99/**
100* Message handler for START_MONITORING_LOGCAT.
101*/
c2bf3c4fdigeff10 years ago102private startMonitoringLogCat(deviceId: string, logCatArguments: string): Q.Promise<any> {
103this.stopMonitoringLogCat(); // Stop previous logcat monitor if it's running
710f8655digeff10 years ago104
c2bf3c4fdigeff10 years ago105// this.logCatMonitor can be mutated, so we store it locally too
106const logCatMonitor = this.logCatMonitor = new LogCatMonitor(deviceId, logCatArguments);
107logCatMonitor.start() // The LogCat will continue running forever, so we don't wait for it
710f8655digeff10 years ago108.catch(error =>
c2bf3c4fdigeff10 years ago109Log.logWarning("Error while monitoring LogCat", error))
710f8655digeff10 years ago110.done();
111
112return Q.resolve<void>(void 0);
113}
114
c2bf3c4fdigeff10 years ago115private stopMonitoringLogCat(): Q.Promise<void> {
710f8655digeff10 years ago116if (this.logCatMonitor) {
117this.logCatMonitor.dispose();
118this.logCatMonitor = null;
119}
c2bf3c4fdigeff10 years ago120
121return Q.resolve<void>(void 0);
710f8655digeff10 years ago122}
123
6e4d7a62Joshua Skelton10 years ago124/**
125* Sends telemetry
126*/
127private sendTelemetry(extensionId: string, extensionVersion: string, appInsightsKey: string, eventName: string, properties: {[key: string]: string}, measures: {[key: string]: number}): Q.Promise<any> {
128Telemetry.sendExtensionTelemetry(extensionId, extensionVersion, appInsightsKey, eventName, properties, measures);
129return Q.resolve({});
130}
131
0502b7a8dlebu10 years ago132/**
6b6eb911dlebu10 years ago133* Extension message handler.
0502b7a8dlebu10 years ago134*/
6b6eb911dlebu10 years ago135private handleExtensionMessage(messageWithArgs: em.MessageWithArguments): Q.Promise<any> {
136let handler = this.messageHandlerDictionary[messageWithArgs.message];
137if (handler) {
f29c7bfeMeena Kunnathur Balakrishnan10 years ago138Log.logInternalMessage(LogLevel.Info, "Handling message: " + em.ExtensionMessage[messageWithArgs.message]);
6b6eb911dlebu10 years ago139return handler.apply(this, messageWithArgs.args);
140} else {
141return Q.reject("Invalid message: " + messageWithArgs.message);
142}
143}
0502b7a8dlebu10 years ago144
6b6eb911dlebu10 years ago145/**
146* Handles connections to the server.
147*/
148private handleSocket(socket: net.Socket): void {
d40a8b0fdlebu10 years ago149let handleError = (e: any) => {
522fe1abdigeff10 years ago150Log.logError(e);
d40a8b0fdlebu10 years ago151socket.end(em.ErrorMarker);
152};
153
6b6eb911dlebu10 years ago154let dataCallback = (data: any) => {
35c2ec28dlebu10 years ago155try {
6b6eb911dlebu10 years ago156let messageWithArgs: em.MessageWithArguments = JSON.parse(data);
157this.handleExtensionMessage(messageWithArgs)
35c2ec28dlebu10 years ago158.then(result => {
6b6eb911dlebu10 years ago159socket.end(JSON.stringify(result));
35c2ec28dlebu10 years ago160})
d40a8b0fdlebu10 years ago161.catch((e) => { handleError(e); })
35c2ec28dlebu10 years ago162.done();
163} catch (e) {
d40a8b0fdlebu10 years ago164handleError(e);
2743f19cdlebu10 years ago165}
6b6eb911dlebu10 years ago166};
167
168socket.on("data", dataCallback);
169};
0502b7a8dlebu10 years ago170
171/**
6b6eb911dlebu10 years ago172* Recovers the server in case the named socket we use already exists, but no other instance of VSCode is active.
0502b7a8dlebu10 years ago173*/
6b6eb911dlebu10 years ago174private recoverServer(error: any): void {
175let errorHandler = (e: any) => {
176/* The named socket is not used. */
177if (e.code === "ECONNREFUSED") {
8411fd8dDaniel Lebu10 years ago178new FileSystem().removePathRecursivelyAsync(this.pipePath)
6b6eb911dlebu10 years ago179.then(() => {
8411fd8dDaniel Lebu10 years ago180this.serverInstance.listen(this.pipePath);
6b6eb911dlebu10 years ago181})
182.done();
183}
184};
185
186/* The named socket already exists. */
187if (error.code === "EADDRINUSE") {
188let clientSocket = new net.Socket();
189clientSocket.on("error", errorHandler);
8411fd8dDaniel Lebu10 years ago190clientSocket.connect(this.pipePath, function() {
6b6eb911dlebu10 years ago191clientSocket.end();
192});
0502b7a8dlebu10 years ago193}
194}
195}