<commit_before>// 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 (
	"bytes"
	"encoding/binary"
	"unsafe"

	"v.io/v23/context"
	"v.io/v23/discovery"
	"v.io/v23/security"
	"v.io/v23/verror"
	idiscovery "v.io/x/ref/lib/discovery"

	jchannel "v.io/x/jni/impl/google/channel"
	jutil "v.io/x/jni/util"
	jcontext "v.io/x/jni/v23/context"
)

// #include "jni.h"
// #include <stdlib.h>
import "C"

var (
	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
// must be called from the main Java thread.
func Init(env jutil.Env) error {
	// Cache global references to all Java classes used by the package.  This is
	// necessary because JNI gets access to the class loader only in the system
	// 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
	}

	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) idiscovery.Uuid) C.jobject {
	env := jutil.Env(uintptr(unsafe.Pointer(jenv)))
	name := jutil.GoString(env, jutil.Object(uintptr(unsafe.Pointer(jName))))
	uuid := generator(name)
	buf := bytes.NewBuffer(uuid)
	var high, low int64
	binary.Read(buf, binary.BigEndian, &high)
	binary.Read(buf, binary.BigEndian, &low)
	jUUID, err := jutil.NewObject(env, jUUIDClass, []jutil.Sign{jutil.LongSign, jutil.LongSign}, high, low)
	if err != nil {
		jutil.JThrowV(env, err)
		return nil
	}
	return C.jobject(unsafe.Pointer(jUUID))
}

//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, 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) idiscovery.Uuid {
		return idiscovery.NewAttributeUUID(s)
	}
	return convertStringtoUUID(jenv, jclass, jName, converter)
}

func doAdvertise(ctx *context.T, ds discovery.T, trigger *idiscovery.Trigger, service discovery.Service, visibility []security.BlessingPattern, jService jutil.Object, jDoneCallback jutil.Object) (jutil.Object, error) {
	// Blocking call, so don't call GetEnv() beforehand.
	done, err := ds.Advertise(ctx, &service, visibility)
	env, freeFunc := jutil.GetEnv()
	defer freeFunc()
	defer jutil.DeleteGlobalRef(env, jService)
	if err != nil {
		jutil.DeleteGlobalRef(env, jDoneCallback)
		return jutil.NullObject, err
	}
	// Copy back service.InstanceId to jService since it's the only field that would be updated.
	if err = jutil.CallVoidMethod(env, jService, "setInstanceId", []jutil.Sign{jutil.StringSign}, service.InstanceId); err != nil {
		jutil.DeleteGlobalRef(env, jDoneCallback)
		return jutil.NullObject, err
	}
	listenableFutureSign := jutil.ClassSign("com.google.common.util.concurrent.ListenableFuture")
	jDoneFuture, err := jutil.CallObjectMethod(env, jDoneCallback, "getFuture", nil, listenableFutureSign)
	if err != nil {
		jutil.DeleteGlobalRef(env, jDoneCallback)
		return jutil.NullObject, err
	}
	trigger.Add(func() {
		env, freeFunc := jutil.GetEnv()
		defer freeFunc()
		jutil.CallbackOnSuccess(env, jDoneCallback, jutil.NullObject)
		jutil.DeleteGlobalRef(env, jDoneCallback)
	}, done)
	// Must grab a global reference as we free up the env and all local references that come along
	// with it.
	return jutil.NewGlobalRef(env, jDoneFuture), nil // Un-refed in DoAsyncCall
}

//export Java_io_v_impl_google_lib_discovery_VDiscoveryImpl_nativeAdvertise
func Java_io_v_impl_google_lib_discovery_VDiscoveryImpl_nativeAdvertise(jenv *C.JNIEnv, jDiscovery C.jobject, goDiscoveryPtr C.jlong, goTriggerPtr C.jlong, jContext C.jobject, jServiceObj C.jobject, jVisibilityObj C.jobject, jStartCallbackObj C.jobject, jDoneCallbackObj C.jobject) {
	env := jutil.Env(uintptr(unsafe.Pointer(jenv)))
	jService := jutil.Object(uintptr(unsafe.Pointer(jServiceObj)))
	jVisibility := jutil.Object(uintptr(unsafe.Pointer(jVisibilityObj)))
	jStartCallback := jutil.Object(uintptr(unsafe.Pointer(jStartCallbackObj)))
	jDoneCallback := jutil.Object(uintptr(unsafe.Pointer(jDoneCallbackObj)))
	ctx, _, err := jcontext.GoContext(env, jutil.Object(uintptr(unsafe.Pointer(jContext))))
	if err != nil {
		jutil.CallbackOnFailure(env, jStartCallback, err)
		return<commit_msg>v.io/x/jni: fix the discovery call<commit_after>// 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 (
	"bytes"
	"encoding/binary"
	"unsafe"

	"v.io/v23/context"
	"v.io/v23/discovery"
	"v.io/v23/security"
	"v.io/v23/verror"
	idiscovery "v.io/x/ref/lib/discovery"

	jchannel "v.io/x/jni/impl/google/channel"
	jutil "v.io/x/jni/util"
	jcontext "v.io/x/jni/v23/context"
)

// #include "jni.h"
// #include <stdlib.h>
import "C"

var (
	updateSign  = jutil.ClassSign("io.v.v23.discovery.Update")
	contextSign = jutil.ClassSign("io.v.v23.context.VContext")

	// 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
// must be called from the main Java thread.
func Init(env jutil.Env) error {
	// Cache global references to all Java classes used by the package.  This is
	// necessary because JNI gets access to the class loader only in the system
	// 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
	}

	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) idiscovery.Uuid) C.jobject {
	env := jutil.Env(uintptr(unsafe.Pointer(jenv)))
	name := jutil.GoString(env, jutil.Object(uintptr(unsafe.Pointer(jName))))
	uuid := generator(name)
	buf := bytes.NewBuffer(uuid)
	var high, low int64
	binary.Read(buf, binary.BigEndian, &high)
	binary.Read(buf, binary.BigEndian, &low)
	jUUID, err := jutil.NewObject(env, jUUIDClass, []jutil.Sign{jutil.LongSign, jutil.LongSign}, high, low)
	if err != nil {
		jutil.JThrowV(env, err)
		return nil
	}
	return C.jobject(unsafe.Pointer(jUUID))
}

//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, 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) idiscovery.Uuid {
		return idiscovery.NewAttributeUUID(s)
	}
	return convertStringtoUUID(jenv, jclass, jName, converter)
}

func doAdvertise(ctx *context.T, ds discovery.T, trigger *idiscovery.Trigger, service discovery.Service, visibility []security.BlessingPattern, jService jutil.Object, jDoneCallback jutil.Object) (jutil.Object, error) {
	// Blocking call, so don't call GetEnv() beforehand.
	done, err := ds.Advertise(ctx, &service, visibility)
	env, freeFunc := jutil.GetEnv()
	defer freeFunc()
	defer jutil.DeleteGlobalRef(env, jService)
	if err != nil {
		jutil.DeleteGlobalRef(env, jDoneCallback)
		return jutil.NullObject, err
	}
	// Copy back service.InstanceId to jService since it's the only field that would be updated.
	if err = jutil.CallVoidMethod(env, jService, "setInstanceId", []jutil.Sign{jutil.StringSign}, service.InstanceId); err != nil {
		jutil.DeleteGlobalRef(env, jDoneCallback)
		return jutil.NullObject, err
	}
	jContext, err := jcontext.JavaContext(env, ctx, nil)
	if err != nil {
		return jutil.NullObject, err
	}
	listenableFutureSign := jutil.ClassSign("com.google.common.util.concurrent.ListenableFuture")
	jDoneFuture, err := jutil.CallObjectMethod(env, jDoneCallback, "getFuture", []jutil.Sign{contextSign}, listenableFutureSign, jContext)
	if err != nil {
		jutil.DeleteGlobalRef(env, jDoneCallback)
		return jutil.NullObject, err
	}
	trigger.Add(func() {
		env, freeFunc := jutil.GetEnv()
		defer freeFunc()
		jutil.CallbackOnSuccess(env, jDoneCallback, jutil.NullObject)
		jutil.DeleteGlobalRef(env, jDoneCallback)
	}, done)
	// Must grab a global reference as we free up the env and all local references that come along
	// with it.
	return jutil.NewGlobalRef(env, jDoneFuture), nil // Un-refed in DoAsyncCall
}

//export Java_io_v_impl_google_lib_discovery_VDiscoveryImpl_nativeAdvertise
func Java_io_v_impl_google_lib_discovery_VDiscoveryImpl_nativeAdvertise(jenv *C.JNIEnv, jDiscovery C.jobject, goDiscoveryPtr C.jlong, goTriggerPtr C.jlong, jContext C.jobject, jServiceObj C.jobject, jVisibilityObj C.jobject, jStartCallbackObj C.jobject, jDoneCallbackObj C.jobject) {
	env := jutil.Env(uintptr(unsafe.Pointer(jenv)))
	jService := jutil.Object(uintptr(unsafe.Pointer(jServiceObj)))
	jVisibility := jutil.Object(uintptr(unsafe.Pointer(jVisibilityObj)))
	jStartCallback := jutil.Object(uintptr(unsafe.Pointer(jStartCallbackObj)))
	jDoneCallback := jutil.Object(uintptr(unsafe.Pointer(jDoneCallbackObj)))
	ctx, _, err := jcontext.GoContext(env, jutil.Object(uintptr(unsafe.Pointer(jContext))))
	if err != nil {
		jutil.CallbackOnFailure(env, jStartCallback, err)
		return
{'fraction_non_alphanumeric': 0.11531048353305959, 'fraction_numerical': 0.00519567585686751, 'mean_word_length': 5.009063444108762, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2985215', 'n_tokens_mistral': 4338, 'n_tokens_neox': 4209, 'n_words': 943}