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

Unified Diff: runtime/bin/directory_exp.dart

Issue 8536006: rpc example for directory file listing (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: fixed comment 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 side-by-side diff with in-line comments
Download patch
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);
+ }
+}

Powered by Google App Engine
This is Rietveld 408576698