Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2011, 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 /** | |
| 6 * EXPERIMENTAL. | |
| 7 * | |
| 8 * (NOTE - DO NOT USE. This is just an example to illustrate | |
| 9 * how to use the RPC system and isolates to provide an asynchronous API | |
| 10 * on top of a system service that is synchronous.) | |
| 11 * | |
| 12 * Provides a synchronous API to list the files in a directory. | |
| 13 */ | |
| 14 class DirectorySync { | |
| 15 final String _path; | |
| 16 | |
| 17 DirectorySync(this._path); | |
| 18 | |
| 19 bool isDirectory() { | |
| 20 // this returns an error message string if there was a file system error | |
| 21 var isDir = _isDirectory(_path); | |
| 22 if (isDir is bool) { | |
| 23 return isDir; | |
| 24 } | |
| 25 String message = isDir; | |
| 26 throw new DirectorySyncException(message); | |
| 27 } | |
| 28 | |
| 29 List<String> list() { | |
| 30 if (!isDirectory()) { | |
| 31 return null; | |
| 32 } | |
| 33 return _list(_path); | |
| 34 } | |
| 35 | |
| 36 static List<String> _list(String path) native "FilePath_List"; | |
| 37 static Object _isDirectory(String path) native "FilePath_IsDirectory"; | |
| 38 } | |
| 39 | |
| 40 /** | |
| 41 * Thrown if any of the native calls return an error message. This will | |
| 42 * propagate back through the RPC system to the calling isolate. | |
| 43 */ | |
| 44 class DirectorySyncException implements Exception { | |
| 45 final String message; | |
| 46 DirectorySyncException(this.message); | |
| 47 String toString() => "DirectorySyncException: $message"; | |
| 48 } | |
| 49 | |
| 50 /** | |
| 51 * Provides an asynchronous API on top of the synchronous DirectorySync class. | |
| 52 * (See RpcProxy for more details.) | |
| 53 */ | |
| 54 class DirectoryProxy extends RpcProxy { | |
| 55 final String path; | |
| 56 DirectoryProxy(this.path, Future<SendPort> futurePort) : super(futurePort) { | |
| 57 sendCommand("init", [path]); | |
| 58 } | |
| 59 | |
| 60 Future<List<String>> list() { | |
| 61 return sendCommand("list", null); | |
| 62 } | |
| 63 | |
| 64 Future<bool> isDirectory() { | |
| 65 return sendCommand("isDirectory", null); | |
| 66 } | |
| 67 | |
| 68 static DirectoryProxy create(String path) { | |
| 69 return new DirectoryProxy(path, new DirectoryIsolate().spawn()); | |
| 70 } | |
| 71 | |
| 72 String toString() { | |
| 73 return path; | |
| 74 } | |
| 75 } | |
| 76 | |
| 77 /** | |
| 78 * Wraps the DirectorySync target object. | |
| 79 */ | |
| 80 class DirectoryReceiver extends RpcReceiver { | |
| 81 DirectorySync directorySync; | |
| 82 DirectoryReceiver(ReceivePort receivePort) : super(receivePort); | |
| 83 | |
| 84 Object receiveCommand(String command, List args) { | |
| 85 switch(command) { | |
| 86 case "init": | |
| 87 String path = args[0]; | |
| 88 directorySync = new DirectorySync(path); | |
| 89 return; | |
| 90 case "list": | |
| 91 return directorySync.list(); | |
| 92 case "isDirectory": | |
| 93 return directorySync.isDirectory(); | |
| 94 } | |
| 95 } | |
| 96 } | |
| 97 | |
| 98 /** | |
| 99 * Used to spawn the directory service isolate. | |
| 100 */ | |
| 101 class DirectoryIsolate extends Isolate { | |
|
Siggi Cherem (dart-lang)
2011/11/11 23:41:51
what about have RpcReceiver extend from isolate in
Ben Laurie (Google)
2011/11/11 23:45:38
That's not a good idea: you control who can send b
Siggi Cherem (dart-lang)
2011/11/11 23:54:08
makes sense - I guess in this example it was clear
| |
| 102 main() { | |
| 103 new DirectoryReceiver(port); | |
| 104 } | |
| 105 } | |
| OLD | NEW |