go: echo app

This is an example go mojo application that can talk to
other mojo applications. The purpose of this is to have
an example of implementation that can be reused in writing
other go mojo applications. The boilerplate implementation
of mojo application interface will replace by its default
implementation that comes soon.

R=jamesr@chromium.org

Review URL: https://codereview.chromium.org/1008543002
diff --git a/examples/BUILD.gn b/examples/BUILD.gn
index d01422a..b7f2d69 100644
--- a/examples/BUILD.gn
+++ b/examples/BUILD.gn
@@ -46,6 +46,10 @@
     ]
   }
 
+  if (is_android && go_build_tool != "") {
+    deps += [ "//examples/go" ]
+  }
+
   if (is_linux) {
     deps += [ "//examples/python" ]
   }
diff --git a/examples/go/BUILD.gn b/examples/go/BUILD.gn
new file mode 100644
index 0000000..e082a07
--- /dev/null
+++ b/examples/go/BUILD.gn
@@ -0,0 +1,42 @@
+# Copyright 2015 The Chromium 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("//mojo/go/rules.gni")
+
+if (is_android) {
+  group("go") {
+    deps = [
+      ":go_echo_client",
+      ":go_echo_server",
+    ]
+  }
+
+  go_shared_library("go_echo_client") {
+    sources = [
+      "echo_client.go",
+    ]
+    deps = [
+      "//examples/echo:bindings",
+      "//mojo/go:bindings",
+      "//mojo/go:platform_cgo",
+      "//mojo/public/c/system",
+      "//mojo/public/interfaces/application",
+      "//mojo/public/platform/native:system",
+    ]
+  }
+
+  go_shared_library("go_echo_server") {
+    sources = [
+      "echo_server.go",
+    ]
+    deps = [
+      "//examples/echo:bindings",
+      "//mojo/go:bindings",
+      "//mojo/go:platform_cgo",
+      "//mojo/public/c/system",
+      "//mojo/public/interfaces/application",
+      "//mojo/public/platform/native:system",
+    ]
+  }
+}
diff --git a/examples/go/README.txt b/examples/go/README.txt
new file mode 100644
index 0000000..b3af234
--- /dev/null
+++ b/examples/go/README.txt
@@ -0,0 +1,13 @@
+Sample Go echo application.
+
+Instructions to run on android emulator:
+
+1) Follow steps from //mojo/go/sample_app/README.txt
+
+To run client:
+$ mojo/tools/android_mojo_shell.py --url-mappings="mojo:go_echo_client"="http://10.0.2.2:4444/obj/examples/go/go_echo_client","mojo:echo_server"="http://10.0.2.2:4444/echo_server.mojo" "mojo:go_echo_client"
+
+To run server:
+$ mojo/tools/android_mojo_shell.py --url-mappings="mojo:go_echo_server"="http://10.0.2.2:4444/obj/examples/go/go_echo_server","mojo:echo_client"="http://10.0.2.2:4444/echo_client.mojo" "mojo:echo_client"
+
+You can't run two go mojo applications at the same time now as they need separated processes.
diff --git a/examples/go/echo_client.go b/examples/go/echo_client.go
new file mode 100644
index 0000000..a67e1d0
--- /dev/null
+++ b/examples/go/echo_client.go
@@ -0,0 +1,90 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+package main
+
+import (
+	"fmt"
+	"log"
+
+	"code.google.com/p/go.mobile/app"
+
+	"mojo/public/go/bindings"
+	"mojo/public/go/system"
+
+	"examples/echo/echo"
+	"mojo/public/interfaces/application/application"
+	"mojo/public/interfaces/application/service_provider"
+	"mojo/public/interfaces/application/shell"
+)
+
+//#include "mojo/public/c/system/types.h"
+import "C"
+
+// AppImpl implements mojo interface application.
+type AppImpl struct {
+	shell           *shell.ShellProxy
+	serviceProvider *service_provider.ServiceProviderProxy
+	echo            *echo.EchoProxy
+}
+
+func (impl *AppImpl) Initialize(inShell shell.ShellPointer, inArgs *[]string, inUrl string) error {
+	impl.shell = shell.NewShellProxy(inShell, bindings.GetAsyncWaiter())
+	// Connect to another mojo application.
+	request, pointer := service_provider.CreateMessagePipeForServiceProvider()
+	if err := impl.shell.ConnectToApplication("mojo:echo_server", &request, nil); err != nil {
+		return err
+	}
+	impl.serviceProvider = service_provider.NewServiceProviderProxy(pointer, bindings.GetAsyncWaiter())
+	// Connect to service.
+	echoRequest, echoPointer := echo.CreateMessagePipeForEcho()
+	if err := impl.serviceProvider.ConnectToService("mojo::examples::Echo", echoRequest.PassMessagePipe()); err != nil {
+		return err
+	}
+	impl.echo = echo.NewEchoProxy(echoPointer, bindings.GetAsyncWaiter())
+	// Send and receive echo request.
+	response, err := impl.echo.EchoString(bindings.StringPointer("Hello Go world!"))
+	if response != nil {
+		fmt.Println(*response)
+	} else {
+		return fmt.Errorf("nil echo response")
+	}
+	return err
+}
+
+func (impl *AppImpl) AcceptConnection(inRequestorUrl string, inServices *service_provider.ServiceProviderRequest, inExposedServices *service_provider.ServiceProviderPointer, inResolvedUrl string) error {
+	if inServices != nil {
+		inServices.Close()
+	}
+	if inExposedServices != nil {
+		inExposedServices.Close()
+	}
+	return nil
+}
+
+func (impl *AppImpl) RequestQuit() error {
+	impl.echo.Close_proxy()
+	impl.serviceProvider.Close_proxy()
+	impl.shell.Close_proxy()
+	return fmt.Errorf("closed")
+}
+
+//export MojoMain
+func MojoMain(handle C.MojoHandle) C.MojoResult {
+	appHandle := system.GetCore().AcquireNativeHandle(system.MojoHandle(handle)).ToMessagePipeHandle()
+	appRequest := application.ApplicationRequest{bindings.NewMessagePipeHandleOwner(appHandle)}
+	stub := application.NewApplicationStub(appRequest, &AppImpl{}, bindings.GetAsyncWaiter())
+	for {
+		if err := stub.ServeRequest(); err != nil {
+			log.Println(err)
+			stub.Close()
+			break
+		}
+	}
+	return C.MOJO_RESULT_OK
+}
+
+func main() {
+	app.Run(app.Callbacks{})
+}
diff --git a/examples/go/echo_server.go b/examples/go/echo_server.go
new file mode 100644
index 0000000..3106f16
--- /dev/null
+++ b/examples/go/echo_server.go
@@ -0,0 +1,107 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+package main
+
+import (
+	"fmt"
+	"log"
+
+	"code.google.com/p/go.mobile/app"
+
+	"mojo/public/go/bindings"
+	"mojo/public/go/system"
+
+	"examples/echo/echo"
+	"mojo/public/interfaces/application/application"
+	"mojo/public/interfaces/application/service_provider"
+	"mojo/public/interfaces/application/shell"
+)
+
+//#include "mojo/public/c/system/types.h"
+import "C"
+
+// ServiceProviderImpl implements mojo interface echo.
+type EchoImpl struct{}
+
+func (echo *EchoImpl) EchoString(inValue *string) (outValue *string, err error) {
+	log.Println(*inValue)
+	return inValue, nil
+}
+
+// ServiceProviderImpl implements mojo interface service provider.
+type ServiceProviderImpl struct{}
+
+func (impl *ServiceProviderImpl) ConnectToService(inInterfaceName string, inPipe system.MessagePipeHandle) error {
+	if inInterfaceName != "mojo::examples::Echo" {
+		inPipe.Close()
+		return nil
+	}
+	request := echo.EchoRequest{bindings.NewMessagePipeHandleOwner(inPipe)}
+	echoStub := echo.NewEchoStub(request, &EchoImpl{}, bindings.GetAsyncWaiter())
+	go func() {
+		for {
+			if err := echoStub.ServeRequest(); err != nil {
+				log.Println(err)
+				echoStub.Close()
+				break
+			}
+		}
+	}()
+	return nil
+}
+
+// AppImpl implements mojo interface application.
+type AppImpl struct {
+	shell *shell.ShellProxy
+}
+
+func (impl *AppImpl) Initialize(inShell shell.ShellPointer, inArgs *[]string, inUrl string) error {
+	impl.shell = shell.NewShellProxy(inShell, bindings.GetAsyncWaiter())
+	return nil
+}
+
+func (impl *AppImpl) AcceptConnection(inRequestorUrl string, inServices *service_provider.ServiceProviderRequest, inExposedServices *service_provider.ServiceProviderPointer, inResolvedUrl string) error {
+	if inExposedServices != nil {
+		inExposedServices.Close()
+	}
+	if inServices == nil {
+		return nil
+	}
+	serviceProviderStub := service_provider.NewServiceProviderStub(*inServices, &ServiceProviderImpl{}, bindings.GetAsyncWaiter())
+	go func() {
+		for {
+			if err := serviceProviderStub.ServeRequest(); err != nil {
+				log.Println(err)
+				serviceProviderStub.Close()
+				break
+			}
+		}
+	}()
+	return nil
+}
+
+func (impl *AppImpl) RequestQuit() error {
+	impl.shell.Close_proxy()
+	return fmt.Errorf("closed")
+}
+
+//export MojoMain
+func MojoMain(handle C.MojoHandle) C.MojoResult {
+	appHandle := system.GetCore().AcquireNativeHandle(system.MojoHandle(handle)).ToMessagePipeHandle()
+	appRequest := application.ApplicationRequest{bindings.NewMessagePipeHandleOwner(appHandle)}
+	stub := application.NewApplicationStub(appRequest, &AppImpl{}, bindings.GetAsyncWaiter())
+	for {
+		if err := stub.ServeRequest(); err != nil {
+			log.Println(err)
+			stub.Close()
+			break
+		}
+	}
+	return C.MOJO_RESULT_OK
+}
+
+func main() {
+	app.Run(app.Callbacks{})
+}