Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(102)

Side by Side Diff: samples/total/src/TotalRunner.dart

Issue 8317006: Added builtin native call to exit the running vm (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Renamed Runtime_Exit to Exit Created 9 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 #!/usr/bin/env dart 1 #!/usr/bin/env dart
2 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file 2 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file
3 // for details. All rights reserved. Use of this source code is governed by a 3 // for details. All rights reserved. Use of this source code is governed by a
4 // BSD-style license that can be found in the LICENSE file. 4 // BSD-style license that can be found in the LICENSE file.
5 5
6 #library("total:runner"); 6 #library("total:runner");
7 7
8 typedef void ExitCallback(int status, String exitString); 8 typedef void ExitCallback(int status, String exitString);
9 9
10 void main() { 10 void main() {
11 String SERVER_EXEC_PATH = './TotalServer.dart'; 11 String SERVER_EXEC_PATH = './TotalServer.dart';
12 String RESTART = "GRACEFUL RESTART!!"; 12 final int RESTART_STATUS = 42;
13 String EXIT = "GRACEFUL EXIT!!"; 13 final int EXIT_STATUS = 0;
14 14
15 void keepServerRunning(int status, ServerRunner runner) { 15 void keepServerRunning(int status, ServerRunner runner) {
16 switch (runner.lastExitString) { 16 switch (status) {
17 case RESTART: 17 case RESTART_STATUS:
18 runner.lastExitString = '';
19 runner.run(keepServerRunning); 18 runner.run(keepServerRunning);
20 break; 19 break;
21 case EXIT: 20 case EXIT_STATUS:
22 break; 21 break;
23 default: 22 default:
24 print("ERROR: exiting due to unknown condition. Exit status: $status." 23 print("ERROR: exiting due to unknown condition. Exit status: $status.");
25 + " Exit string: '${runner.lastExitString}'");
26 break; 24 break;
27 } 25 }
28 } 26 }
29 27
30 new ServerRunner(SERVER_EXEC_PATH, [RESTART, EXIT]).run(keepServerRunning); 28 new ServerRunner(SERVER_EXEC_PATH).run(keepServerRunning);
31 } 29 }
32 30
33 class ServerRunner { 31 class ServerRunner {
34 List<String> _exitStrings;
35 String _serverMain; 32 String _serverMain;
36 33
37 // TODO: fix this to use a bigger buffer when streams work better 34 // TODO: fix this to use a bigger buffer when streams work better
38 int _BUFSIZE = 1; 35 int _BUFSIZE = 1;
39 36
40 String lastExitString;
41
42 // TODO: Release or Debug? We should be able to find automatically 37 // TODO: Release or Debug? We should be able to find automatically
43 static final DART_EXEC_PATH = '../../../runtime/out/Release_ia32/dart_bin'; 38 static final DART_EXEC_PATH = '../../../runtime/out/Release_ia32/dart_bin';
44 static final CR = 0x0d; 39 static final CR = 0x0d;
45 static final LF = 0x0a; 40 static final LF = 0x0a;
46 41
47 ServerRunner(String this._serverMain, List<String> this._exitStrings); 42 ServerRunner(String this._serverMain);
48 43
49 void run(ExitCallback exitCallback) { 44 void run(ExitCallback exitCallback) {
50 Process dart = new Process(DART_EXEC_PATH, [_serverMain]); 45 Process dart = new Process(DART_EXEC_PATH, [_serverMain]);
51 46
52 dart.setExitHandler((int status) { 47 dart.setExitHandler((int status) {
53 dart.close(); 48 dart.close();
54 exitCallback(status, this); 49 exitCallback(status, this);
55 }); 50 });
56 51
57 dart.start(); 52 dart.start();
(...skipping 13 matching lines...) Expand all
71 66
72 void processBuffer(List<int> buf, StringBuffer readSoFar) { 67 void processBuffer(List<int> buf, StringBuffer readSoFar) {
73 buf.forEach((int i) { 68 buf.forEach((int i) {
74 if (i != CR && i != LF) { 69 if (i != CR && i != LF) {
75 readSoFar.add(new String.fromCharCodes([i])); 70 readSoFar.add(new String.fromCharCodes([i]));
76 } else { 71 } else {
77 String line = readSoFar.toString(); 72 String line = readSoFar.toString();
78 readSoFar.clear(); 73 readSoFar.clear();
79 if (line.length != 0) { 74 if (line.length != 0) {
80 print(line); 75 print(line);
81 _updateLastExitString(line);
82 } 76 }
83 } 77 }
84 }); 78 });
85 } 79 }
86
87 void _updateLastExitString(String line) {
88 _exitStrings.some((String e) {
89 if (line.contains(e, 0)) {
90 lastExitString = e;
91 return true;
92 } else {
93 return false;
94 }
95 });
96 }
97
98 } 80 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698