Chromium Code Reviews| Index: runtime/bin/directory_exp.dart |
| diff --git a/runtime/bin/directory_exp.dart b/runtime/bin/directory_exp.dart |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..cfa5e33dfb1b59be8099218ae34cc1405b1b323f |
| --- /dev/null |
| +++ b/runtime/bin/directory_exp.dart |
| @@ -0,0 +1,105 @@ |
| +// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file |
| +// for details. All rights reserved. Use of this source code is governed by a |
| +// BSD-style license that can be found in the LICENSE file. |
| + |
| +/** |
| + * EXPERIMENTAL. |
| + * |
| + * (NOTE - DO NOT USE. This is just an example to illustrate |
| + * how to use the RPC system and isolates to provide an asynchronous API |
| + * on top of a system service that is synchronous.) |
| + * |
| + * Provides a synchronous API to list the files in a directory. |
| + */ |
| +class DirectorySync { |
| + final String _path; |
| + |
| + DirectorySync(this._path); |
| + |
| + bool isDirectory() { |
| + // this returns an error message string if there was a file system error |
| + var isDir = _isDirectory(_path); |
| + if (isDir is bool) { |
| + return isDir; |
| + } |
| + String message = isDir; |
| + throw new DirectorySyncException(message); |
| + } |
| + |
| + List<String> list() { |
| + if (!isDirectory()) { |
| + return null; |
| + } |
| + return _list(_path); |
| + } |
| + |
| + static List<String> _list(String path) native "FilePath_List"; |
| + static Object _isDirectory(String path) native "FilePath_IsDirectory"; |
| +} |
| + |
| +/** |
| + * Thrown if any of the native calls return an error message. This will |
| + * propagate back through the RPC system to the calling isolate. |
| + */ |
| +class DirectorySyncException implements Exception { |
| + final String message; |
| + DirectorySyncException(this.message); |
| + String toString() => "DirectorySyncException: $message"; |
| +} |
| + |
| +/** |
| + * Provides an asynchronous API on top of the synchronous DirectorySync class. |
| + * (See RpcProxy for more details.) |
| + */ |
| +class DirectoryProxy extends RpcProxy { |
| + final String path; |
| + DirectoryProxy(this.path, Future<SendPort> futurePort) : super(futurePort) { |
| + sendCommand("init", [path]); |
| + } |
| + |
| + Future<List<String>> list() { |
| + return sendCommand("list", null); |
| + } |
| + |
| + Future<bool> isDirectory() { |
| + return sendCommand("isDirectory", null); |
| + } |
| + |
| + static DirectoryProxy create(String path) { |
| + return new DirectoryProxy(path, new DirectoryIsolate().spawn()); |
| + } |
| + |
| + String toString() { |
| + return path; |
| + } |
| +} |
| + |
| +/** |
| + * Wraps the DirectorySync target object. |
| + */ |
| +class DirectoryReceiver extends RpcReceiver { |
| + DirectorySync directorySync; |
| + DirectoryReceiver(ReceivePort receivePort) : super(receivePort); |
| + |
| + Object receiveCommand(String command, List args) { |
| + switch(command) { |
| + case "init": |
| + String path = args[0]; |
| + directorySync = new DirectorySync(path); |
| + return; |
| + case "list": |
| + return directorySync.list(); |
| + case "isDirectory": |
| + return directorySync.isDirectory(); |
| + } |
| + } |
| +} |
| + |
| +/** |
| + * Used to spawn the directory service isolate. |
| + */ |
| +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
|
| + main() { |
| + new DirectoryReceiver(port); |
| + } |
| +} |