| OLD | NEW |
| 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 String lastExitString; | |
| 38 | |
| 39 // TODO: Release or Debug? We should be able to find automatically | 34 // TODO: Release or Debug? We should be able to find automatically |
| 40 static final DART_EXEC_PATH = '../../../runtime/out/Release_ia32/dart_bin'; | 35 static final DART_EXEC_PATH = '../../../runtime/out/Release_ia32/dart_bin'; |
| 41 static final CR = 0x0d; | 36 static final CR = 0x0d; |
| 42 static final LF = 0x0a; | 37 static final LF = 0x0a; |
| 43 | 38 |
| 44 ServerRunner(String this._serverMain, List<String> this._exitStrings); | 39 ServerRunner(String this._serverMain); |
| 45 | 40 |
| 46 void run(ExitCallback exitCallback) { | 41 void run(ExitCallback exitCallback) { |
| 47 Process dart = new Process(DART_EXEC_PATH, [_serverMain]); | 42 Process dart = new Process(DART_EXEC_PATH, [_serverMain]); |
| 48 | 43 |
| 49 dart.setExitHandler((int status) { | 44 dart.setExitHandler((int status) { |
| 50 dart.close(); | 45 dart.close(); |
| 51 exitCallback(status, this); | 46 exitCallback(status, this); |
| 52 }); | 47 }); |
| 53 | 48 |
| 54 dart.start(); | 49 dart.start(); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 65 | 60 |
| 66 void processBuffer(List<int> buf, StringBuffer readSoFar) { | 61 void processBuffer(List<int> buf, StringBuffer readSoFar) { |
| 67 buf.forEach((int i) { | 62 buf.forEach((int i) { |
| 68 if (i != CR && i != LF) { | 63 if (i != CR && i != LF) { |
| 69 readSoFar.add(new String.fromCharCodes([i])); | 64 readSoFar.add(new String.fromCharCodes([i])); |
| 70 } else { | 65 } else { |
| 71 String line = readSoFar.toString(); | 66 String line = readSoFar.toString(); |
| 72 readSoFar.clear(); | 67 readSoFar.clear(); |
| 73 if (line.length != 0) { | 68 if (line.length != 0) { |
| 74 print(line); | 69 print(line); |
| 75 _updateLastExitString(line); | |
| 76 } | 70 } |
| 77 } | 71 } |
| 78 }); | 72 }); |
| 79 } | 73 } |
| 80 | |
| 81 void _updateLastExitString(String line) { | |
| 82 _exitStrings.some((String e) { | |
| 83 if (line.contains(e, 0)) { | |
| 84 lastExitString = e; | |
| 85 return true; | |
| 86 } else { | |
| 87 return false; | |
| 88 } | |
| 89 }); | |
| 90 } | |
| 91 | |
| 92 } | 74 } |
| OLD | NEW |