microsoft/vscode-react-native
Publicmirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable
src/extension/extensionServer.ts
206lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | // Licensed under the MIT license. See LICENSE file in the project root for details. |
| 3 | |
| 4 | import * as net from "net"; |
| 5 | import * as Q from "q"; |
| 6 | import * as vscode from "vscode"; |
| 7 | |
| 8 | import * as em from "../common/extensionMessaging"; |
| 9 | import {Log} from "../common/log/log"; |
| 10 | import {LogLevel} from "../common/log/logHelper"; |
| 11 | import {Packager} from "../common/packager"; |
| 12 | import {PackagerStatus, PackagerStatusIndicator} from "./packagerStatusIndicator"; |
| 13 | import {LogCatMonitor} from "./android/logCatMonitor"; |
| 14 | import {FileSystem} from "../common/node/fileSystem"; |
| 15 | import {ConfigurationReader} from "../common/configurationReader"; |
| 16 | import {SettingsHelper} from "./settingsHelper"; |
| 17 | import {Telemetry} from "../common/telemetry"; |
| 18 | |
| 19 | export class ExtensionServer implements vscode.Disposable { |
| 20 | private serverInstance: net.Server = null; |
| 21 | private messageHandlerDictionary: { [id: number]: ((...argArray: any[]) => Q.Promise<any>) } = {}; |
| 22 | private reactNativePackager: Packager; |
| 23 | private reactNativePackageStatusIndicator: PackagerStatusIndicator; |
| 24 | private pipePath: string; |
| 25 | private logCatMonitor: LogCatMonitor = null; |
| 26 | |
| 27 | public constructor(projectRootPath: string, reactNativePackager: Packager, packagerStatusIndicator: PackagerStatusIndicator) { |
| 28 | |
| 29 | this.pipePath = new em.MessagingChannel(projectRootPath).getPath(); |
| 30 | this.reactNativePackager = reactNativePackager; |
| 31 | this.reactNativePackageStatusIndicator = packagerStatusIndicator; |
| 32 | |
| 33 | /* register handlers for all messages */ |
| 34 | this.messageHandlerDictionary[em.ExtensionMessage.START_PACKAGER] = this.startPackager; |
| 35 | this.messageHandlerDictionary[em.ExtensionMessage.STOP_PACKAGER] = this.stopPackager; |
| 36 | this.messageHandlerDictionary[em.ExtensionMessage.PREWARM_BUNDLE_CACHE] = this.prewarmBundleCache; |
| 37 | this.messageHandlerDictionary[em.ExtensionMessage.START_MONITORING_LOGCAT] = this.startMonitoringLogCat; |
| 38 | this.messageHandlerDictionary[em.ExtensionMessage.STOP_MONITORING_LOGCAT] = this.stopMonitoringLogCat; |
| 39 | this.messageHandlerDictionary[em.ExtensionMessage.GET_PACKAGER_PORT] = this.getPackagerPort; |
| 40 | this.messageHandlerDictionary[em.ExtensionMessage.SEND_TELEMETRY] = this.sendTelemetry; |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Starts the server. |
| 45 | */ |
| 46 | public setup(): Q.Promise<void> { |
| 47 | |
| 48 | let deferred = Q.defer<void>(); |
| 49 | |
| 50 | let launchCallback = (error: any) => { |
| 51 | Log.logInternalMessage(LogLevel.Info, `Extension messaging server started at ${this.pipePath}.`); |
| 52 | if (error) { |
| 53 | deferred.reject(error); |
| 54 | } else { |
| 55 | deferred.resolve(null); |
| 56 | } |
| 57 | }; |
| 58 | |
| 59 | this.serverInstance = net.createServer(this.handleSocket.bind(this)); |
| 60 | this.serverInstance.on("error", this.recoverServer.bind(this)); |
| 61 | this.serverInstance.listen(this.pipePath, launchCallback); |
| 62 | |
| 63 | return deferred.promise; |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Stops the server. |
| 68 | */ |
| 69 | public dispose(): void { |
| 70 | if (this.serverInstance) { |
| 71 | this.serverInstance.close(); |
| 72 | this.serverInstance = null; |
| 73 | } |
| 74 | |
| 75 | this.stopMonitoringLogCat(); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Message handler for GET_PACKAGER_PORT. |
| 80 | */ |
| 81 | private getPackagerPort(): Q.Promise<number> { |
| 82 | return Q(SettingsHelper.getPackagerPort()); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Message handler for START_PACKAGER. |
| 87 | */ |
| 88 | private startPackager(port?: any): Q.Promise<any> { |
| 89 | const portToUse = ConfigurationReader.readIntWithDefaultSync(port, SettingsHelper.getPackagerPort()); |
| 90 | return this.reactNativePackager.start(portToUse) |
| 91 | .then(() => |
| 92 | this.reactNativePackageStatusIndicator.updatePackagerStatus(PackagerStatus.PACKAGER_STARTED)); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Message handler for STOP_PACKAGER. |
| 97 | */ |
| 98 | private stopPackager(): Q.Promise<any> { |
| 99 | return this.reactNativePackager.stop() |
| 100 | .then(() => this.reactNativePackageStatusIndicator.updatePackagerStatus(PackagerStatus.PACKAGER_STOPPED)); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Message handler for PREWARM_BUNDLE_CACHE. |
| 105 | */ |
| 106 | private prewarmBundleCache(platform: string): Q.Promise<any> { |
| 107 | return this.reactNativePackager.prewarmBundleCache(platform); |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Message handler for START_MONITORING_LOGCAT. |
| 112 | */ |
| 113 | private startMonitoringLogCat(deviceId: string, logCatArguments: string): Q.Promise<any> { |
| 114 | this.stopMonitoringLogCat(); // Stop previous logcat monitor if it's running |
| 115 | |
| 116 | // this.logCatMonitor can be mutated, so we store it locally too |
| 117 | const logCatMonitor = this.logCatMonitor = new LogCatMonitor(deviceId, logCatArguments); |
| 118 | logCatMonitor.start() // The LogCat will continue running forever, so we don't wait for it |
| 119 | .catch(error => |
| 120 | Log.logWarning("Error while monitoring LogCat", error)) |
| 121 | .done(); |
| 122 | |
| 123 | return Q.resolve<void>(void 0); |
| 124 | } |
| 125 | |
| 126 | private stopMonitoringLogCat(): Q.Promise<void> { |
| 127 | if (this.logCatMonitor) { |
| 128 | this.logCatMonitor.dispose(); |
| 129 | this.logCatMonitor = null; |
| 130 | } |
| 131 | |
| 132 | return Q.resolve<void>(void 0); |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Sends telemetry |
| 137 | */ |
| 138 | private sendTelemetry(extensionId: string, extensionVersion: string, appInsightsKey: string, eventName: string, properties: {[key: string]: string}, measures: {[key: string]: number}): Q.Promise<any> { |
| 139 | Telemetry.sendExtensionTelemetry(extensionId, extensionVersion, appInsightsKey, eventName, properties, measures); |
| 140 | return Q.resolve({}); |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Extension message handler. |
| 145 | */ |
| 146 | private handleExtensionMessage(messageWithArgs: em.MessageWithArguments): Q.Promise<any> { |
| 147 | let handler = this.messageHandlerDictionary[messageWithArgs.message]; |
| 148 | if (handler) { |
| 149 | Log.logInternalMessage(LogLevel.Info, "Handling message: " + em.ExtensionMessage[messageWithArgs.message]); |
| 150 | return handler.apply(this, messageWithArgs.args); |
| 151 | } else { |
| 152 | return Q.reject("Invalid message: " + messageWithArgs.message); |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Handles connections to the server. |
| 158 | */ |
| 159 | private handleSocket(socket: net.Socket): void { |
| 160 | let handleError = (e: any) => { |
| 161 | Log.logError(e); |
| 162 | socket.end(em.ErrorMarker); |
| 163 | }; |
| 164 | |
| 165 | let dataCallback = (data: any) => { |
| 166 | try { |
| 167 | let messageWithArgs: em.MessageWithArguments = JSON.parse(data); |
| 168 | this.handleExtensionMessage(messageWithArgs) |
| 169 | .then(result => { |
| 170 | socket.end(JSON.stringify(result)); |
| 171 | }) |
| 172 | .catch((e) => { handleError(e); }) |
| 173 | .done(); |
| 174 | } catch (e) { |
| 175 | handleError(e); |
| 176 | } |
| 177 | }; |
| 178 | |
| 179 | socket.on("data", dataCallback); |
| 180 | }; |
| 181 | |
| 182 | /** |
| 183 | * Recovers the server in case the named socket we use already exists, but no other instance of VSCode is active. |
| 184 | */ |
| 185 | private recoverServer(error: any): void { |
| 186 | let errorHandler = (e: any) => { |
| 187 | /* The named socket is not used. */ |
| 188 | if (e.code === "ECONNREFUSED") { |
| 189 | new FileSystem().removePathRecursivelyAsync(this.pipePath) |
| 190 | .then(() => { |
| 191 | this.serverInstance.listen(this.pipePath); |
| 192 | }) |
| 193 | .done(); |
| 194 | } |
| 195 | }; |
| 196 | |
| 197 | /* The named socket already exists. */ |
| 198 | if (error.code === "EADDRINUSE") { |
| 199 | let clientSocket = new net.Socket(); |
| 200 | clientSocket.on("error", errorHandler); |
| 201 | clientSocket.connect(this.pipePath, function() { |
| 202 | clientSocket.end(); |
| 203 | }); |
| 204 | } |
| 205 | } |
| 206 | } |
| 207 | |