V23GoRunner: Add ability to add go functions based on a unique string
key and select and run them from the V23GoRunner app.
MultiPart: 1/2
Change-Id: I7d461fac973b53470f6ee51e6442a905c25b7ded
diff --git a/impl/google/services/jni.go b/impl/google/services/jni.go
index 3e79ffc..365da10 100644
--- a/impl/google/services/jni.go
+++ b/impl/google/services/jni.go
@@ -10,7 +10,7 @@
jgroups "v.io/x/jni/impl/google/services/groups"
jmounttable "v.io/x/jni/impl/google/services/mounttable"
jsyncbase "v.io/x/jni/impl/google/services/syncbase"
- _ "v.io/x/jni/impl/google/services/v23_go_runnable"
+ _ "v.io/x/jni/impl/google/services/v23_go_runner"
jutil "v.io/x/jni/util"
)
diff --git a/impl/google/services/v23_go_runnable/jni.go b/impl/google/services/v23_go_runnable/jni.go
deleted file mode 100644
index 7247401..0000000
--- a/impl/google/services/v23_go_runnable/jni.go
+++ /dev/null
@@ -1,41 +0,0 @@
-// 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.
-
-// +build java android
-
-package v23_go_runnable
-
-import (
- "unsafe"
- "v.io/v23/context"
-
- jutil "v.io/x/jni/util"
- jcontext "v.io/x/jni/v23/context"
-)
-
-// #include "jni.h"
-import "C"
-
-// V23GoRunnableFunc is the go code that is run by java/android callers of
-// V23GoRunnable.run().
-// Users must edit this function and rebuild the java lib/android-lib.
-// Then android apps may run this Go code.
-func V23GoRunnableFunc(ctx *context.T) error {
- ctx.Infof("Running V23GoRunnableFunc")
- return nil
-}
-
-//export Java_io_v_android_util_V23GoRunnable_nativeGoContextCall
-func Java_io_v_android_util_V23GoRunnable_nativeGoContextCall(jenv *C.JNIEnv, jV23GoRunner C.jobject, jContext C.jobject) {
- env := jutil.Env(uintptr(unsafe.Pointer(jenv)))
- ctx, _, err := jcontext.GoContext(env, jutil.Object(uintptr(unsafe.Pointer(jContext))))
- if err != nil {
- jutil.JThrowV(env, err)
- return
- }
- if err := V23GoRunnableFunc(ctx); err != nil {
- jutil.JThrowV(env, err)
- return
- }
-}
diff --git a/impl/google/services/v23_go_runner/funcs.go b/impl/google/services/v23_go_runner/funcs.go
new file mode 100644
index 0000000..34d5bef
--- /dev/null
+++ b/impl/google/services/v23_go_runner/funcs.go
@@ -0,0 +1,22 @@
+// 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.
+
+// +build java android
+
+package v23_go_runner
+
+import (
+ "v.io/v23/context"
+)
+
+// v23GoRunnerFuncs is a map containing go functions keys by unique strings
+// intended to be run by java/android applications using V23GoRunner.run(key).
+// Users must add function entries to this map and rebuild lib/android-lib in
+// the vanadium java repository.
+var v23GoRunnerFuncs = map[string]func(*context.T) error{
+ "bt-rpc": func(ctx *context.T) error {
+ ctx.Errorf("bt-rpc test to be implemented")
+ return nil
+ },
+}
diff --git a/impl/google/services/v23_go_runner/jni.go b/impl/google/services/v23_go_runner/jni.go
new file mode 100644
index 0000000..ce53c32
--- /dev/null
+++ b/impl/google/services/v23_go_runner/jni.go
@@ -0,0 +1,38 @@
+// 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.
+
+// +build java android
+
+package v23_go_runner
+
+import (
+ "fmt"
+ "unsafe"
+
+ jutil "v.io/x/jni/util"
+ jcontext "v.io/x/jni/v23/context"
+)
+
+// #include "jni.h"
+import "C"
+
+//export Java_io_v_android_util_V23GoRunner_nativeGoContextCall
+func Java_io_v_android_util_V23GoRunner_nativeGoContextCall(jenv *C.JNIEnv, jV23GoRunner C.jobject, jContext C.jobject, jKey C.jstring) {
+ env := jutil.Env(uintptr(unsafe.Pointer(jenv)))
+ key := jutil.GoString(env, jutil.Object(uintptr(unsafe.Pointer(jKey))))
+ ctx, _, err := jcontext.GoContext(env, jutil.Object(uintptr(unsafe.Pointer(jContext))))
+ if err != nil {
+ jutil.JThrowV(env, err)
+ return
+ }
+ f, ok := v23GoRunnerFuncs[key]
+ if !ok {
+ jutil.JThrowV(env, fmt.Errorf("v23GoRunnerFunc key %q doesn't exist", key))
+ return
+ }
+ if err := f(ctx); err != nil {
+ jutil.JThrowV(env, err)
+ return
+ }
+}