Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | |
| 2 // for details. All rights reserved. Use of this source code is governed by a | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 import 'dart:async'; | |
| 6 import 'dart:convert'; | |
| 7 import 'dart:io'; | |
| 8 | |
| 9 /** | |
| 10 * [AnalysisManager] is used to launch and manage an analysis server | |
| 11 * running in a separate process using the static [start] method. | |
| 12 */ | |
| 13 class AnalysisManager { | |
| 14 // TODO dynamically allocate port and/or allow client to specify port | |
| 15 static const int PORT = 3333; | |
| 16 | |
| 17 /** | |
| 18 * The analysis server process being managed. | |
| 19 */ | |
| 20 final Process process; | |
| 21 | |
| 22 /** | |
| 23 * The websocket used to communicate with the analysis server. | |
| 24 */ | |
| 25 final WebSocket socket; | |
| 26 | |
| 27 /** | |
| 28 * Launch analysis server in a separate process and return a | |
| 29 * [Future<AnalysisManager>] for managing that analysis server. | |
| 30 */ | |
| 31 static Future<AnalysisManager> start(String pathToServer) { | |
| 32 // TODO dynamically allocate port and/or allow client to specify port | |
| 33 return Process.start(Platform.executable, [pathToServer, "--port", | |
| 34 PORT.toString()]).then(_connect); | |
| 35 } | |
| 36 | |
| 37 /** | |
| 38 * Open a connection to the analysis server. | |
| 39 */ | |
| 40 static Future<AnalysisManager> _connect(Process process) { | |
| 41 var url = 'ws://${InternetAddress.LOOPBACK_IP_V4.address}:$PORT/'; | |
| 42 process.stderr.pipe(stderr); | |
| 43 Stream out = process.stdout.transform(UTF8.decoder).asBroadcastStream(); | |
| 44 out.listen((line) { | |
| 45 print(line); | |
| 46 }); | |
| 47 return out | |
| 48 .any((String line) => line.startsWith("Listening on port")) | |
| 49 .then((bool listening) { | |
| 50 if (!listening) { | |
| 51 throw "Expected analysis server to listen on a port"; | |
| 52 } | |
| 53 }) | |
| 54 .then((_) => WebSocket.connect(url)) | |
| 55 .then((WebSocket socket) => new AnalysisManager(process, socket)) | |
| 56 .catchError((error) { | |
| 57 process.kill(); | |
| 58 throw error; | |
| 59 }); | |
| 60 } | |
| 61 | |
| 62 /** | |
| 63 * Create a new instance that manages the specified analysis server process. | |
| 64 */ | |
| 65 AnalysisManager(this.process, this.socket); | |
| 66 | |
| 67 /** | |
| 68 * Stop the analysis server. | |
| 69 * | |
| 70 * Returns [:true:] if the signal is successfully sent and process is killed. | |
| 71 * Otherwise the signal could not be sent, usually meaning that the process | |
| 72 * is already dead. | |
| 73 */ | |
| 74 bool stop() => process.kill(); | |
|
Brian Wilkerson
2014/02/21 15:09:54
It would be nicer to ask the server to quit than t
| |
| 75 } | |
| OLD | NEW |