mojo/v23proxy: Add Dart Examples with Service Describer

See https://codereview.chromium.org/1648803006/

The referenced change allows Dart services to expose their type
information. This means the v23proxy can now work with Dart service.

The echo_server and fortune_server were added as Dart examples.

Tested by checking that the Go and Dart clients interoperate with them.

Note: Will need actual Dart tests to follow up.
It would also be good to introduce tests that verify interoperation with
Vanadium clients/servers too.

To use:
When using USE_MOJO_DEV_PROFILE=1, you can try it out yourself.
Otherwise, please wait until the mojo version 7 profile is rolled out.

Change-Id: Iace12ff63156d16f51a8bd36ed487b15874a46a2
diff --git a/Makefile b/Makefile
index aba2a03..73dd52e 100644
--- a/Makefile
+++ b/Makefile
@@ -42,6 +42,9 @@
 lint-dart:
 	dartanalyzer lib/client.dart | grep -v "\[warning\] The imported libraries"
 	dartanalyzer dart-examples/echo/lib/main.dart | grep -v "\[warning\] The imported libraries"
+	dartanalyzer dart-examples/echo_server/lib/main.dart | grep -v "\[warning\] The imported libraries"
+	dartanalyzer dart-examples/fortune/lib/main.dart | grep -v "\[warning\] The imported libraries"
+	dartanalyzer dart-examples/fortune_server/lib/main.dart | grep -v "\[warning\] The imported libraries"
 
 .PHONY: link-mojo_sdk
 link-mojo_sdk:
@@ -114,7 +117,9 @@
 
 gen/echo.mojom.dart: mojom/mojom/examples/echo.mojom | mojo-env-check
 	cd dart-examples/echo && pub get
+	cd dart-examples/echo_server && pub get
 	$(call MOJOM_GEN,$<,mojom,dart-examples/echo/lib/gen,dart,--generate-type-info)
+	$(call MOJOM_GEN,$<,mojom,dart-examples/echo_server/lib/gen,dart,--generate-type-info)
 
 $(BUILD_DIR)/fortune_client.mojo: gen/go/src/mojom/examples/fortune/fortune.mojom.go
 	$(call MOGO_BUILD,examples/fortune/client,$@)
@@ -128,7 +133,9 @@
 
 gen/fortune.mojom.dart: mojom/mojom/examples/fortune.mojom | mojo-env-check
 	cd dart-examples/fortune && pub get
+	cd dart-examples/fortune_server && pub get
 	$(call MOJOM_GEN,$<,mojom,dart-examples/fortune/lib/gen,dart,--generate-type-info)
+	$(call MOJOM_GEN,$<,mojom,dart-examples/fortune_server/lib/gen,dart,--generate-type-info)
 
 $(BUILD_DIR)/v23clientproxy.mojo: $(shell find $(PWD)/go/src/v.io/x/mojo/proxy/clientproxy -name *.go) gen/go/src/mojom/v23clientproxy/v23clientproxy.mojom.go gen/go/src/mojo/public/interfaces/bindings/mojom_types/mojom_types.mojom.go | mojo-env-check
 	$(call MOGO_BUILD,v.io/x/mojo/proxy/clientproxy,$@)
@@ -219,6 +226,8 @@
 # On Android, run with
 # ANDROID={device number} make ARGS="{remote endpoint}//https://mojo.v.io/echo_server.mojo/mojo::examples::RemoteEcho [optional: a string to echo]" start-echo-client
 #
+# To run this versus the dart echo server, use https://mojo.v.io/dart-examples/echo_server/lib/main.dart instead.
+#
 # Note1: Does not use --enable-multiprocess since small Go programs can omit it.
 # Note2: Setting HOME ensures that we avoid a db LOCK that is created per mojo shell instance.
 .PHONY: start-echo-client
@@ -243,6 +252,8 @@
 # On Android, run with
 # ANDROID={device number} make ARGS="{remote endpoint}//https://mojo.v.io/fortune_server.mojo/mojo::examples::Fortune [optional: a fortune to add]" start-fortune-client
 #
+# To run this versus the dart echo server, use https://mojo.v.io/dart-examples/fortune_server/lib/main.dart instead.
+#
 # Note1: Does not use --enable-multiprocess since small Go programs can omit it.
 # Note2: Setting HOME ensures that we avoid a db LOCK that is created per mojo shell instance.
 .PHONY: start-fortune-client
@@ -270,5 +281,7 @@
 clean-dart:
 	rm -rf lib/gen
 	rm -rf dart-examples/echo/lib/gen
+	rm -rf dart-examples/echo_server/lib/gen
 	rm -rf dart-examples/fortune/lib/gen
+	rm -rf dart-examples/fortune_server/lib/gen
 	rm -rf packages
diff --git a/dart-examples/echo_server/lib/main.dart b/dart-examples/echo_server/lib/main.dart
new file mode 100644
index 0000000..4ae298a
--- /dev/null
+++ b/dart-examples/echo_server/lib/main.dart
@@ -0,0 +1,46 @@
+#!mojo mojo:dart_content_handler
+// Copyright 2015 The Vanadium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+import 'gen/dart-gen/mojom/lib/mojo/examples/echo.mojom.dart';
+import 'package:mojo/application.dart';
+import 'package:mojo/core.dart';
+
+class RemoteEchoImpl implements RemoteEcho {
+  RemoteEchoStub _stub; // TODO(alexfandrianto): Do we need to _stub.close()?
+  Application _application; // This isn't needed, is it...?
+
+  RemoteEchoImpl(this._application, MojoMessagePipeEndpoint endpoint) {
+    _stub = new RemoteEchoStub.fromEndpoint(endpoint, this);
+  }
+
+  @override
+  dynamic echoString(String value,[Function responseFactory]) {
+    print("Dart Server: EchoString got ${value}!");
+    return responseFactory(value);
+  }
+
+  @override
+  dynamic echoX(List<bool> arg1, AInArg arg2, [Function responseFactory]) {
+    print("Dart Server: EchoX got ${arg1} and ${arg2}");
+    return responseFactory(new OutArgTypes()..res = Result.b);
+  }
+}
+
+class EchoServer extends Application {
+  EchoServer.fromHandle(MojoHandle handle) : super.fromHandle(handle);
+
+  @override
+  void acceptConnection(String requestorUrl, String resolvedUrl,
+      ApplicationConnection connection) {
+    connection.provideService(RemoteEcho.serviceName,
+        (endpoint) => new RemoteEchoImpl(this, endpoint),
+        description: RemoteEchoStub.serviceDescription);
+  }
+}
+
+main(List args) {
+  MojoHandle appHandle = new MojoHandle(args[0]);
+  new EchoServer.fromHandle(appHandle);
+}
diff --git a/dart-examples/echo_server/pubspec.yaml b/dart-examples/echo_server/pubspec.yaml
new file mode 100644
index 0000000..192e75d
--- /dev/null
+++ b/dart-examples/echo_server/pubspec.yaml
@@ -0,0 +1,18 @@
+author: Vanadium Authors
+description: Echo Server is an example Mojo Dart application that uses the v23proxy.
+homepage: https://github.com/vanadium/mojo.v23proxy
+name: v23proxy-echo-server
+version: 0.0.1
+dependencies:
+  mojo: '>=0.3.0 <0.4.0'
+  v23proxy: '>=0.0.1'
+dev_dependencies:
+  dart_style: any
+  test: any
+environment:
+  sdk: '>=1.12.0 <2.0.0'
+dependency_overrides:
+  mojo:
+    path: ../../.mojo_sdk/mojo/src/out/Debug/gen/dart-pkg/mojo
+  v23proxy:
+    path: ../..
diff --git a/dart-examples/fortune/lib/main.dart b/dart-examples/fortune/lib/main.dart
index 2191686..53cc4f5 100644
--- a/dart-examples/fortune/lib/main.dart
+++ b/dart-examples/fortune/lib/main.dart
@@ -39,7 +39,7 @@
     if (fortunestr == null) {
       // Get Fortune
       print("Asking fortune server for a fortune...");
-      var response = await fortuneProxy.ptr.get(fortunestr);
+      var response = await fortuneProxy.ptr.get();
       print("Received fortune: ${response.value}");
     } else {
       // Add Fortune
diff --git a/dart-examples/fortune/pubspec.yaml b/dart-examples/fortune/pubspec.yaml
index 1647935..c73f56d 100644
--- a/dart-examples/fortune/pubspec.yaml
+++ b/dart-examples/fortune/pubspec.yaml
@@ -1,7 +1,7 @@
 author: Vanadium Authors
 description: Fortune is an example Mojo Dart application that uses the v23proxy.
 homepage: https://github.com/vanadium/mojo.v23proxy
-name: v23proxy-echo
+name: v23proxy-fortune
 version: 0.0.1
 dependencies:
   mojo: '>=0.3.0 <0.4.0'
diff --git a/dart-examples/fortune_server/lib/main.dart b/dart-examples/fortune_server/lib/main.dart
new file mode 100644
index 0000000..f9484f8
--- /dev/null
+++ b/dart-examples/fortune_server/lib/main.dart
@@ -0,0 +1,60 @@
+#!mojo mojo:dart_content_handler
+// Copyright 2015 The Vanadium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+import 'dart:async';
+import 'dart:math' as math;
+
+import 'gen/dart-gen/mojom/lib/mojo/examples/fortune.mojom.dart';
+import 'package:mojo/application.dart';
+import 'package:mojo/core.dart';
+
+class FortuneImpl implements Fortune {
+  FortuneStub _stub; // TODO(alexfandrianto): Do we need to _stub.close()?
+  Application _application; // This isn't needed, is it...?
+  final List<String> _fortunes;
+
+  FortuneImpl(this._application, MojoMessagePipeEndpoint endpoint,
+      this._fortunes) {
+    _stub = new FortuneStub.fromEndpoint(endpoint, this);
+  }
+
+  @override
+  dynamic get([Function responseFactory = null]) {
+    print("Dart Server: Get was called.");
+    String fortune = _fortunes[new math.Random().nextInt(_fortunes.length)];
+    print("Dart Server: Going to send back ${fortune}");
+    return responseFactory(fortune);
+  }
+
+  @override
+  dynamic add(String wisdom,[Function responseFactory = null]) {
+    print("Dart Server: Add called with '${wisdom}'");
+    _fortunes.add(wisdom);
+    return responseFactory();
+  }
+}
+
+class FortuneServer extends Application {
+  final List<String> _fortunes = <String>[
+   "You will reach the heights of success.",
+   "Conquer your fears or they will conquer you.",
+   "Today is your lucky day!",
+  ];
+
+  FortuneServer.fromHandle(MojoHandle handle) : super.fromHandle(handle);
+
+  @override
+  void acceptConnection(String requestorUrl, String resolvedUrl,
+      ApplicationConnection connection) {
+    connection.provideService(Fortune.serviceName,
+        (endpoint) => new FortuneImpl(this, endpoint, _fortunes),
+        description: FortuneStub.serviceDescription);
+  }
+}
+
+main(List args) {
+  MojoHandle appHandle = new MojoHandle(args[0]);
+  new FortuneServer.fromHandle(appHandle);
+}
diff --git a/dart-examples/fortune_server/pubspec.yaml b/dart-examples/fortune_server/pubspec.yaml
new file mode 100644
index 0000000..3eb56ca
--- /dev/null
+++ b/dart-examples/fortune_server/pubspec.yaml
@@ -0,0 +1,18 @@
+author: Vanadium Authors
+description: Fortune Server is an example Mojo Dart application that uses the v23proxy.
+homepage: https://github.com/vanadium/mojo.v23proxy
+name: v23proxy-fortune-server
+version: 0.0.1
+dependencies:
+  mojo: '>=0.3.0 <0.4.0'
+  v23proxy: '>=0.0.1'
+dev_dependencies:
+  dart_style: any
+  test: any
+environment:
+  sdk: '>=1.12.0 <2.0.0'
+dependency_overrides:
+  mojo:
+    path: ../../.mojo_sdk/mojo/src/out/Debug/gen/dart-pkg/mojo
+  v23proxy:
+    path: ../..
diff --git a/lib/client.dart b/lib/client.dart
index 7ebb87c..b743a44 100644
--- a/lib/client.dart
+++ b/lib/client.dart
@@ -27,11 +27,12 @@
 
   // This is a service_describer.ServiceDescription.
   var serviceDescription = dynproxyimpl.serviceDescription;
+  Function identityResponseFactory = (v) => v;
 
   v23proxy.ptr.setupClientProxy(
     v23Name,
-    serviceDescription.getTopLevelInterface(),
-    serviceDescription.getAllTypeDefinitions(),
+    serviceDescription.getTopLevelInterface(identityResponseFactory),
+    serviceDescription.getAllTypeDefinitions(identityResponseFactory),
     proxy.serviceName,
     pipe.endpoints[1]);
 }