Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 import 'dart:async'; | 5 import 'dart:async'; |
| 6 import 'dart:convert'; | 6 import 'dart:convert'; |
| 7 import 'dart:io'; | 7 import 'dart:io'; |
| 8 | 8 |
| 9 /** | 9 /** |
| 10 * [AnalysisManager] is used to launch and manage an analysis server | 10 * [AnalysisManager] is used to launch and manage an analysis server |
| 11 * running in a separate process using the static [start] method. | 11 * running in a separate process using the static [start] method. |
| 12 */ | 12 */ |
| 13 class AnalysisManager { | 13 class AnalysisManager { |
| 14 // TODO dynamically allocate port and/or allow client to specify port | 14 // TODO dynamically allocate port and/or allow client to specify port |
| 15 static const int PORT = 3333; | 15 static const int PORT = 3333; |
| 16 | 16 |
| 17 /** | 17 /** |
| 18 * The analysis server process being managed. | 18 * The analysis server process being managed |
| 19 * or [:null:] if managing an analysis server that was already running. | |
|
Brian Wilkerson
2014/02/22 16:57:06
nit: We have been using backquotes (`) in the rest
danrubel
2014/02/24 17:39:24
Done.
| |
| 19 */ | 20 */ |
| 20 final Process process; | 21 final Process process; |
| 21 | 22 |
| 22 /** | 23 /** |
| 23 * The websocket used to communicate with the analysis server. | 24 * The websocket used to communicate with the analysis server. |
| 24 */ | 25 */ |
| 25 final WebSocket socket; | 26 final WebSocket socket; |
| 26 | 27 |
| 27 /** | 28 /** |
| 28 * Launch analysis server in a separate process and return a | 29 * Launch analysis server in a separate process and return a |
| 29 * [Future<AnalysisManager>] for managing that analysis server. | 30 * [Future<AnalysisManager>] for managing that analysis server. |
| 30 */ | 31 */ |
| 31 static Future<AnalysisManager> start(String pathToServer) { | 32 static Future<AnalysisManager> start(String pathToServer) { |
| 32 // TODO dynamically allocate port and/or allow client to specify port | 33 // TODO dynamically allocate port and/or allow client to specify port |
| 33 return Process.start(Platform.executable, [pathToServer, "--port", | 34 return Process.start(Platform.executable, [pathToServer, "--port", |
| 34 PORT.toString()]).then(_connect); | 35 PORT.toString()]).then(_attach); |
|
Brian Wilkerson
2014/02/22 16:57:06
We should have a 'catchError' to report when the s
danrubel
2014/02/24 17:39:24
Done.
| |
| 35 } | 36 } |
| 36 | 37 |
| 37 /** | 38 /** |
| 38 * Open a connection to the analysis server. | 39 * Open a connection to a running analysis server |
| 39 */ | 40 */ |
| 40 static Future<AnalysisManager> _connect(Process process) { | 41 static Future<AnalysisManager> connect(String url) { |
| 42 return WebSocket.connect(url) | |
| 43 .then((WebSocket socket) => new AnalysisManager(null, socket)); | |
| 44 } | |
| 45 | |
| 46 /** | |
| 47 * Attach this process to the newly launched analysis server process, | |
| 48 * and open a connection to the analysis server. | |
| 49 */ | |
| 50 static Future<AnalysisManager> _attach(Process process) { | |
|
Brian Wilkerson
2014/02/22 16:57:06
I think we also want to use the 'exitCode' getter
danrubel
2014/02/24 17:39:24
Done.
| |
| 41 var url = 'ws://${InternetAddress.LOOPBACK_IP_V4.address}:$PORT/'; | 51 var url = 'ws://${InternetAddress.LOOPBACK_IP_V4.address}:$PORT/'; |
| 42 process.stderr.pipe(stderr); | 52 process.stderr.pipe(stderr); |
| 43 Stream out = process.stdout.transform(UTF8.decoder).asBroadcastStream(); | 53 Stream out = process.stdout.transform(UTF8.decoder).asBroadcastStream(); |
| 44 out.listen((line) { | 54 out.listen((line) { |
| 45 print(line); | 55 print(line); |
| 46 }); | 56 }); |
| 47 return out | 57 return out |
| 48 .any((String line) => line.startsWith("Listening on port")) | 58 .any((String line) => line.startsWith("Listening on port")) |
| 49 .then((bool listening) { | 59 .then((bool listening) { |
| 50 if (!listening) { | 60 if (!listening) { |
| 51 throw "Expected analysis server to listen on a port"; | 61 throw "Expected analysis server to listen on a port"; |
| 52 } | 62 } |
| 53 }) | 63 }) |
| 54 .then((_) => WebSocket.connect(url)) | 64 .then((_) => WebSocket.connect(url)) |
| 55 .then((WebSocket socket) => new AnalysisManager(process, socket)) | 65 .then((WebSocket socket) => new AnalysisManager(process, socket)) |
| 56 .catchError((error) { | 66 .catchError((error) { |
| 57 process.kill(); | 67 process.kill(); |
| 58 throw error; | 68 throw error; |
| 59 }); | 69 }); |
| 60 } | 70 } |
| 61 | 71 |
| 62 /** | 72 /** |
| 63 * Create a new instance that manages the specified analysis server process. | 73 * Create a new instance that manages the specified analysis server process. |
| 64 */ | 74 */ |
| 65 AnalysisManager(this.process, this.socket); | 75 AnalysisManager(this.process, this.socket); |
| 66 | 76 |
| 67 /** | 77 /** |
| 68 * Stop the analysis server. | 78 * Stop the analysis server. |
| 69 * | 79 * |
| 70 * Returns [:true:] if the signal is successfully sent and process is killed. | 80 * Returns [:true:] if the signal is successfully sent and process is killed. |
|
Brian Wilkerson
2014/02/22 16:57:06
nit: As above
danrubel
2014/02/24 17:39:24
Done.
| |
| 71 * Otherwise the signal could not be sent, usually meaning that the process | 81 * Otherwise there was no attached process or the signal could not be sent, |
| 72 * is already dead. | 82 * usually meaning that the process is already dead. |
| 73 */ | 83 */ |
| 74 bool stop() => process.kill(); | 84 bool stop() => process != null ? process.kill() : false; |
|
Brian Wilkerson
2014/02/22 16:57:06
I still think you should ask the server nicely to
danrubel
2014/02/24 17:39:24
Agreed. Not quite ready to wire that up. Added a c
| |
| 75 } | 85 } |
| OLD | NEW |