jni: Wrapped Vanadium discovery in a jni wrapper.

This change also plumbs the android ble plugin down to the discovery
service

MultiPart: 2/3
Change-Id: I0651349ff0276a1308cae0559ea23408d2ab15fe
diff --git a/impl/google/discovery/jni.go b/impl/google/discovery/jni.go
index 9deeb36..487f21c 100644
--- a/impl/google/discovery/jni.go
+++ b/impl/google/discovery/jni.go
@@ -11,8 +11,12 @@
 	"encoding/binary"
 	"unsafe"
 
+	"v.io/v23/discovery"
+	"v.io/v23/security"
+	idiscovery "v.io/x/ref/lib/discovery"
+
 	jutil "v.io/x/jni/util"
-	"v.io/x/ref/lib/discovery"
+	jcontext "v.io/x/jni/v23/context"
 )
 
 // #include "jni.h"
@@ -20,9 +24,26 @@
 import "C"
 
 var (
-	classSign = jutil.ClassSign("java.lang.Class")
+	updateSign = jutil.ClassSign("io.v.v23.discovery.Update")
+
+
 	// Global reference for java.util.UUID class.
 	jUUIDClass jutil.Class
+
+	// Global reference io.v.impl.google.lib.discovery.ScanHandler
+	jScanHandlerClass jutil.Class
+
+	// Global reference io.v.v23.discovery.Service
+	jServiceClass jutil.Class
+
+	// Global reference io.v.v23.security.BlessingPattern
+	jBlessingPatternClass jutil.Class
+
+	// Global reference io.v.v23.discovery.Update
+	jUpdateClass jutil.Class
+
+	// Global reference io.v.impl.google.lib.discovery.VDiscoveryImpl
+	jVDiscoveryImplClass jutil.Class
 )
 
 // Init initializes the JNI code with the given Java environment. This method
@@ -33,11 +54,35 @@
 	// thread, so we aren't able to invoke FindClass in other threads.
 	var err error
 	jUUIDClass, err = jutil.JFindClass(env, "java/util/UUID")
+	if err != nil {
+		return err
+	}
+	jScanHandlerClass, err = jutil.JFindClass(env, "io/v/impl/google/lib/discovery/ScanHandler")
+	if err != nil {
+		return err
+	}
+	jServiceClass, err = jutil.JFindClass(env, "io/v/v23/discovery/Service")
+	if err != nil {
+		return err
+	}
+	jBlessingPatternClass, err = jutil.JFindClass(env, "io/v/v23/security/BlessingPattern")
+	if err != nil {
+		return err
+	}
+	jUpdateClass, err = jutil.JFindClass(env, "io/v/v23/discovery/Update")
+	if err != nil {
+		return err
+	}
 
-	return err
+	jVDiscoveryImplClass, err = jutil.JFindClass(env, "io/v/impl/google/lib/discovery/VDiscoveryImpl")
+	if err != nil {
+		return err
+	}
+
+	return nil
 }
 
-func convertStringtoUUID(jenv *C.JNIEnv, _ C.jclass, jName C.jstring, generator func(string) discovery.Uuid) C.jobject {
+func convertStringtoUUID(jenv *C.JNIEnv, _ C.jclass, jName C.jstring, generator func(string) idiscovery.Uuid) C.jobject {
 	env := jutil.Env(uintptr(unsafe.Pointer(jenv)))
 	name := jutil.GoString(env, jutil.Object(uintptr(unsafe.Pointer(jName))))
 	uuid := generator(name)
@@ -55,13 +100,128 @@
 
 //export Java_io_v_impl_google_lib_discovery_UUIDUtil_UUIDForInterfaceName
 func Java_io_v_impl_google_lib_discovery_UUIDUtil_UUIDForInterfaceName(jenv *C.JNIEnv, jclass C.jclass, jName C.jstring) C.jobject {
-	return convertStringtoUUID(jenv, jclass, jName, discovery.NewServiceUUID)
+	return convertStringtoUUID(jenv, jclass, jName, idiscovery.NewServiceUUID)
 }
 
 //export Java_io_v_impl_google_lib_discovery_UUIDUtil_UUIDForAttributeKey
 func Java_io_v_impl_google_lib_discovery_UUIDUtil_UUIDForAttributeKey(jenv *C.JNIEnv, jclass C.jclass, jName C.jstring) C.jobject {
-	converter := func(s string) discovery.Uuid {
-		return discovery.NewAttributeUUID(s)
+	converter := func(s string) idiscovery.Uuid {
+		return idiscovery.NewAttributeUUID(s)
 	}
 	return convertStringtoUUID(jenv, jclass, jName, converter)
 }
+
+
+//export Java_io_v_impl_google_lib_discovery_VDiscoveryImpl_nativeFinalize
+func Java_io_v_impl_google_lib_discovery_VDiscoveryImpl_nativeFinalize(jenv *C.JNIEnv, _ C.jobject, discovery C.jlong, trigger C.jlong) {
+	jutil.GoUnref(jutil.NativePtr(discovery))
+	jutil.GoUnref(jutil.NativePtr(trigger))
+}
+
+//export Java_io_v_impl_google_lib_discovery_VDiscoveryImpl_advertise
+func Java_io_v_impl_google_lib_discovery_VDiscoveryImpl_advertise(jenv *C.JNIEnv, jDiscoveryObj C.jobject, jContext C.jobject, jServiceObject C.jobject, jPerms C.jobject, jCallback C.jobject) {
+	env := jutil.Env(uintptr(unsafe.Pointer(jenv)))
+	ctx, err := jcontext.GoContext(env, jutil.Object(uintptr(unsafe.Pointer(jContext))))
+	if err != nil {
+		return
+	}
+	jService := jutil.Object(uintptr(unsafe.Pointer(jServiceObject)))
+	var service discovery.Service
+	if err := jutil.GoVomCopy(env, jService, jServiceClass, &service); err != nil {
+		jutil.JThrowV(env, err)
+		return
+	}
+
+	permsArray, err := jutil.GoObjectList(env, jutil.Object(uintptr(unsafe.Pointer(jPerms))))
+	if err != nil {
+		jutil.JThrowV(env, err)
+		return
+	}
+	permsSlice := make([]security.BlessingPattern, len(permsArray))
+	for i, jPattern := range permsArray {
+		if err := jutil.GoVomCopy(env, jPattern, jBlessingPatternClass, &permsSlice[i]); err != nil {
+			jutil.JThrowV(env, err)
+			return
+		}
+	}
+	jDiscovery := jutil.Object(uintptr(unsafe.Pointer(jDiscoveryObj)))
+	discoveryPtr, err := jutil.JLongField(env, jDiscovery, "nativeDiscovery")
+	if err != nil {
+		jutil.JThrowV(env, err)
+		return
+	}
+
+	ds := *(*discovery.T)(jutil.NativePtr(discoveryPtr))
+
+	triggerPtr, err := jutil.JLongField(env, jDiscovery, "nativeTrigger")
+
+	if err != nil {
+		jutil.JThrowV(env, err)
+		return
+	}
+
+	trigger := (*idiscovery.Trigger)(jutil.NativePtr(triggerPtr))
+
+	done, err := ds.Advertise(ctx, service, permsSlice)
+
+	if err != nil {
+		jutil.JThrowV(env, err)
+		return
+	}
+	cb := jutil.Object(uintptr(unsafe.Pointer(jCallback)))
+	cb = jutil.NewGlobalRef(env, cb)
+	trigger.Add(func() {
+		env, freeFunc := jutil.GetEnv()
+		defer freeFunc()
+		// TODO(bjornick): What should we do on errors?
+		if err := jutil.CallVoidMethod(env, cb, "done", nil); err != nil {
+			ctx.Errorf("failed to call done:", err)
+		}
+		jutil.DeleteGlobalRef(env, cb)
+	}, done)
+}
+
+//export Java_io_v_impl_google_lib_discovery_VDiscoveryImpl_scan
+func Java_io_v_impl_google_lib_discovery_VDiscoveryImpl_scan(jenv *C.JNIEnv, jDiscoveryObj C.jobject, jContext C.jobject, jQuery C.jstring, jCallback C.jobject) {
+	env := jutil.Env(uintptr(unsafe.Pointer(jenv)))
+	ctx, err := jcontext.GoContext(env, jutil.Object(uintptr(unsafe.Pointer(jContext))))
+	if err != nil {
+		return
+	}
+	query := jutil.GoString(env, jutil.Object(uintptr(unsafe.Pointer(jQuery))))
+
+	jDiscovery := jutil.Object(uintptr(unsafe.Pointer(jDiscoveryObj)))
+	discoveryPtr, err := jutil.JLongField(env, jDiscovery, "nativeDiscovery")
+	if err != nil {
+		jutil.JThrowV(env, err)
+		return
+	}
+	ds := *(*discovery.T)(jutil.NativePtr(discoveryPtr))
+
+	updates, err := ds.Scan(ctx, query)
+
+	jutil.NewGlobalRef(env, jutil.Object(uintptr(unsafe.Pointer(jCallback))))
+	if err != nil {
+		jutil.JThrowV(env, err)
+		return
+	}
+	cb := jutil.Object(uintptr(unsafe.Pointer(jCallback)))
+	cb = jutil.NewGlobalRef(env, cb)
+	go func() {
+		env, freeFunc := jutil.GetEnv()
+		defer freeFunc()
+		for v := range updates {
+			jUpdate, err := jutil.JVomCopy(env, v, jUpdateClass)
+			if err != nil {
+				ctx.Errorf("Failed to convert update: %v", err)
+				continue
+			}
+			err = jutil.CallVoidMethod(env, cb, "handleUpdate", []jutil.Sign{updateSign}, jUpdate)
+			if err != nil {
+				ctx.Errorf("Failed to call handler: %v", err)
+				continue
+			}
+		}
+		jutil.DeleteGlobalRef(env, cb)
+	}()
+}
diff --git a/impl/google/discovery/util.go b/impl/google/discovery/util.go
new file mode 100644
index 0000000..d788469
--- /dev/null
+++ b/impl/google/discovery/util.go
@@ -0,0 +1,29 @@
+// 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 discovery
+
+import (
+	jutil "v.io/x/jni/util"
+
+	"v.io/v23/discovery"
+	idiscovery "v.io/x/ref/lib/discovery"
+)
+// #include "jni.h"
+import "C"
+
+// JavaDiscovery converts a Go discovery instance into a Java discovery instance.
+func JavaDiscovery(env jutil.Env, d discovery.T) (jutil.Object, error) {
+	trigger := idiscovery.NewTrigger()
+	// This reference will get unrefed when the jDiscovery object below is finalized.
+	jutil.GoRef(trigger)
+
+	jDiscovery, err := jutil.NewObject(env, jVDiscoveryImplClass, []jutil.Sign{jutil.LongSign, jutil.LongSign}, int64(jutil.PtrValue(&d)), int64(jutil.PtrValue(trigger)))
+	if err != nil {
+		return jutil.NullObject, err
+	}
+	return jDiscovery, nil
+}
diff --git a/impl/google/rt/jni.go b/impl/google/rt/jni.go
index c5239ba..f9d7415 100644
--- a/impl/google/rt/jni.go
+++ b/impl/google/rt/jni.go
@@ -11,12 +11,12 @@
 
 	"v.io/v23"
 	"v.io/v23/context"
-	_ "v.io/x/ref/runtime/factories/roaming"
 
 	jns "v.io/x/jni/impl/google/namespace"
 	jrpc "v.io/x/jni/impl/google/rpc"
 	jutil "v.io/x/jni/util"
 	jcontext "v.io/x/jni/v23/context"
+	jdiscovery "v.io/x/jni/impl/google/discovery"
 	jsecurity "v.io/x/jni/v23/security"
 )
 
@@ -27,6 +27,7 @@
 	contextSign = jutil.ClassSign("io.v.v23.context.VContext")
 	serverSign  = jutil.ClassSign("io.v.v23.rpc.Server")
 
+	// Global reference io.v.impl.google.rt.VRuntimeImpl
 	jVRuntimeImplClass jutil.Class
 )
 
@@ -39,6 +40,7 @@
 	if err != nil {
 		return err
 	}
+
 	return nil
 }
 
@@ -272,3 +274,20 @@
 	}
 	return C.jobject(unsafe.Pointer(jSpec))
 }
+
+//export Java_io_v_impl_google_rt_VRuntimeImpl_nativeGetDiscovery
+func Java_io_v_impl_google_rt_VRuntimeImpl_nativeGetDiscovery(jenv *C.JNIEnv, jRuntime C.jclass, jContext C.jobject) 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 nil
+	}
+
+	jDiscovery, err := jdiscovery.JavaDiscovery(env, v23.GetDiscovery(ctx))
+	if err != nil {
+		jutil.JThrowV(env, err);
+		return nil
+	}
+	return C.jobject(unsafe.Pointer(jDiscovery))
+}
diff --git a/jni_android.go b/jni_android.go
index fe97370..88bd374 100644
--- a/jni_android.go
+++ b/jni_android.go
@@ -11,20 +11,30 @@
 
 	"v.io/v23/context"
 	"v.io/x/lib/vlog"
+	_ "v.io/x/ref/runtime/factories/android"
+	"v.io/x/ref/lib/discovery/factory"
 
 	jutil "v.io/x/jni/util"
 	jcontext "v.io/x/jni/v23/context"
+	jdiscovery "v.io/x/jni/libs/discovery"
 )
 
 // #include "jni.h"
 import "C"
 
-//export Java_io_v_android_v23_V_nativeInitLogging
-func Java_io_v_android_v23_V_nativeInitLogging(jenv *C.JNIEnv, jVClass C.jclass, jContext C.jobject, jOptions C.jobject) C.jobject {
+//export Java_io_v_android_v23_V_nativeInit
+func Java_io_v_android_v23_V_nativeInit(jenv *C.JNIEnv, jVClass C.jclass, jContext C.jobject, jAndroidContext C.jobject, jOptions C.jobject) C.jobject {
 	env := jutil.Env(uintptr(unsafe.Pointer(jenv)))
 	jCtx := jutil.Object(uintptr(unsafe.Pointer(jContext)))
 	jOpts := jutil.Object(uintptr(unsafe.Pointer(jOptions)))
 
+	if err := jdiscovery.Init(env); err != nil {
+		jutil.JThrowV(env, err)
+		return nil
+	}
+
+	factory.SetBleFactory(jdiscovery.NewBleCreator(env, jutil.Object(uintptr(unsafe.Pointer(jAndroidContext)))))
+
 	_, _, level, vmodule, err := loggingOpts(env, jOpts)
 	if err != nil {
 		jutil.JThrowV(env, err)
diff --git a/jni_java.go b/jni_java.go
new file mode 100644
index 0000000..614c455
--- /dev/null
+++ b/jni_java.go
@@ -0,0 +1,11 @@
+// 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
+
+package jni
+
+import (
+	_ "v.io/x/ref/runtime/factories/roaming"
+)
\ No newline at end of file
diff --git a/libs/discovery/jni.go b/libs/discovery/jni.go
new file mode 100644
index 0000000..e6bf2a3
--- /dev/null
+++ b/libs/discovery/jni.go
@@ -0,0 +1,73 @@
+// 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 android
+
+package discovery
+
+import (
+	"unsafe"
+
+	jutil "v.io/x/jni/util"
+	"v.io/x/ref/lib/discovery"
+)
+// #include "jni.h"
+import "C"
+var (
+	androidContextSign = jutil.ClassSign("android.content.Context")
+	contextSign = jutil.ClassSign("io.v.v23.context.VContext")
+	advertisementSign = jutil.ClassSign("io.v.x.ref.lib.discovery.Advertisement")
+	uuidSign = jutil.ClassSign("java.util.UUID")
+	scanHandlerSign = jutil.ClassSign("io.v.impl.google.lib.discovery.ScanHandler")
+
+	// Global reference for io.v.android.libs.discovery.ble.BlePlugin
+	jBlePluginClass jutil.Class
+	// Global reference for io.v.x.ref.lib.discovery.Advertisement
+	jAdvertisementClass jutil.Class
+	// Global reference for java.util.UUID
+	jUUIDClass jutil.Class
+	// Global reference for io.v.android.libs.discovery.ble.NativeScanHandler
+	jNativeScanHandlerClass jutil.Class
+)
+
+func Init(env jutil.Env) error {
+	var err error
+
+	jUUIDClass, err = jutil.JFindClass(env, "java/util/UUID")
+	if err != nil {
+		return err
+	}
+
+	jNativeScanHandlerClass, err = jutil.JFindClass(env, "io/v/android/libs/discovery/ble/NativeScanHandler")
+	if err != nil {
+		return err
+	}
+
+	jBlePluginClass, err = jutil.JFindClass(env, "io/v/android/libs/discovery/ble/BlePlugin")
+	if err != nil {
+		return err
+	}
+
+	jAdvertisementClass, err = jutil.JFindClass(env, "io/v/x/ref/lib/discovery/Advertisement")
+	return err
+}
+
+//export Java_io_v_android_libs_discovery_ble_NativeScanHandler_nativeHandleUpdate
+func Java_io_v_android_libs_discovery_ble_NativeScanHandler_nativeHandleUpdate(jenv *C.JNIEnv, _ C.jobject, jAdvObj C.jobject, goPtr C.jlong)  {
+	env := jutil.Env(uintptr(unsafe.Pointer(jenv)))
+	ch := (*(*chan <-discovery.Advertisement)(jutil.NativePtr(goPtr)))
+
+	jAdv := jutil.Object(uintptr(unsafe.Pointer(jAdvObj)))
+	var adv discovery.Advertisement
+	if err := jutil.GoVomCopy(env, jAdv, jAdvertisementClass, &adv); err != nil {
+		jutil.JThrowV(env, err)
+		return
+	}
+	ch <-adv
+}
+
+//export Java_io_v_android_libs_discovery_ble_NativeScanHandler_nativeFinalize
+func Java_io_v_android_libs_discovery_ble_NativeScanHandler_nativeFinalize(jenv *C.JNIEnv, _ C.jobject, goPtr C.jlong) {
+	jutil.GoUnref(jutil.NativePtr(goPtr))
+}
diff --git a/libs/discovery/plugin.go b/libs/discovery/plugin.go
new file mode 100644
index 0000000..8632349
--- /dev/null
+++ b/libs/discovery/plugin.go
@@ -0,0 +1,119 @@
+// 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 android
+
+package discovery
+import (
+	"bytes"
+	"encoding/binary"
+	"runtime"
+
+	"v.io/v23/context"
+
+	"v.io/x/ref/lib/discovery"
+
+	jcontext "v.io/x/jni/v23/context"
+	jutil "v.io/x/jni/util"
+)
+
+// #include "jni.h"
+import "C"
+
+func NewBleCreator(env jutil.Env, context jutil.Object) func(string) (discovery.Plugin, error) {
+	// Reference Android Context; it will be de-referenced when the plugin
+	// created below is garbage-collected (through the finalizer callback we
+	// setup in the function below).  This function should only be executed once.
+	jContext := jutil.NewGlobalRef(env, context)
+	return func(host string) (discovery.Plugin, error) {
+		env, freeFunc := jutil.GetEnv()
+		defer freeFunc()
+		jPlugin, err := jutil.NewObject(env, jBlePluginClass, []jutil.Sign{androidContextSign}, jContext)
+
+		jutil.DeleteGlobalRef(env, jContext)
+		if err != nil {
+			return nil, err
+		}
+		// Reference Android BlePlugin; it will be de-referenced when the plugin
+		// created below is garbage-collected (through the finalizer callback we
+		// setup below).
+		jPlugin = jutil.NewGlobalRef(env, jPlugin)
+		p := &plugin{
+			trigger: discovery.NewTrigger(),
+			jPlugin: jPlugin,
+		}
+		runtime.SetFinalizer(p, func(p *plugin) {
+			env, freeFunc := jutil.GetEnv()
+			defer freeFunc()
+			jutil.DeleteGlobalRef(env, p.jPlugin)
+		})
+		return p, nil
+	}
+
+}
+
+type plugin struct {
+	trigger *discovery.Trigger
+	jPlugin jutil.Object
+}
+
+func (p *plugin) Advertise(ctx *context.T, ad discovery.Advertisement, done func()) error {
+	env, freeFunc := jutil.GetEnv()
+	defer freeFunc()
+	jContext, err := jcontext.JavaContext(env, ctx)
+	if err != nil {
+		return err
+	}
+	jAdv, err := jutil.JVomCopy(env, ad, jAdvertisementClass)
+	if err != nil {
+		return err
+	}
+
+	err = jutil.CallVoidMethod(env, p.jPlugin, "addAdvertisement", []jutil.Sign{contextSign, advertisementSign}, jContext, jAdv)
+
+	p.trigger.Add(done, ctx.Done())
+
+	return err
+}
+
+func (p *plugin) Scan(ctx *context.T, serviceUuid discovery.Uuid, ch chan<- discovery.Advertisement, done func()) error {
+	env, freeFunc := jutil.GetEnv()
+	defer freeFunc()
+	jContext, err := jcontext.JavaContext(env, ctx)
+	if err != nil {
+		return err
+	}
+
+	jUuid, err := JavaUUID(env, serviceUuid)
+
+	if err != nil {
+		return err
+	}
+
+
+	jutil.GoRef(&ch)
+	jNativeScanHandler, err := jutil.NewObject(env, jNativeScanHandlerClass, []jutil.Sign{jutil.LongSign}, int64(jutil.PtrValue(&ch)))
+	if err != nil {
+		return err
+	}
+
+	err = jutil.CallVoidMethod(env, p.jPlugin, "addScanner", []jutil.Sign{contextSign, uuidSign, scanHandlerSign},
+		jContext, jUuid, jNativeScanHandler)
+
+	if err != nil {
+		return err
+	}
+	p.trigger.Add(done, ctx.Done())
+	return nil
+}
+
+
+// JavaUUID converts a Go UUID type to a Java UUID object.
+func JavaUUID(env jutil.Env, uuid discovery.Uuid) (jutil.Object, error) {
+	buf := bytes.NewBuffer(uuid)
+	var high, low int64
+	binary.Read(buf, binary.BigEndian, &high)
+	binary.Read(buf, binary.BigEndian, &low)
+	return jutil.NewObject(env, jUUIDClass, []jutil.Sign{jutil.LongSign, jutil.LongSign}, high, low)
+}