blob: b776ea82a49abf6de0748184ddcdcae8fd2ad17e [file] [log] [blame]
// 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"
"v.io/v23/context"
"v.io/v23/discovery"
idiscovery "v.io/x/ref/lib/discovery"
fdiscovery "v.io/x/ref/lib/discovery/factory"
"v.io/x/ref/lib/discovery/plugins/mock"
jutil "v.io/x/jni/util"
)
// JavaDiscovery converts a Go discovery instance into a Java discovery instance.
func JavaDiscovery(env jutil.Env, d discovery.T) (jutil.Object, error) {
jDiscovery, err := jutil.NewObject(env, jDiscoveryImplClass, []jutil.Sign{jutil.LongSign}, int64(jutil.PtrValue(&d)))
if err != nil {
return jutil.NullObject, err
}
jutil.GoRef(&d) // Will be unrefed when jDiscovery is finalized.
return jDiscovery, nil
}
// javaUpdate converts a Go update instance into a Java update instance.
func javaUpdate(env jutil.Env, update discovery.Update) (jutil.Object, error) {
jAd, err := jutil.JVomCopy(env, update.Advertisement(), jAdvertisementClass)
if err != nil {
return jutil.NullObject, err
}
jUpdate, err := jutil.NewObject(env, jUpdateImplClass, []jutil.Sign{jutil.LongSign, jutil.BoolSign, advertisementSign}, int64(jutil.PtrValue(&update)), update.IsLost(), jAd)
if err != nil {
return jutil.NullObject, err
}
jutil.GoRef(&update) // Will be unrefed when jUpdate is finalized.
return jUpdate, nil
}
// javaUUID converts a Go UUID into a Java UUID instance.
func javaUUID(env jutil.Env, uuid idiscovery.Uuid) (jutil.Object, error) {
var high, low int64
buf := bytes.NewReader(uuid)
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)
}
// injectMockDiscovery injects a discovery instance with a mock plugin into a runtime.
func injectMockDiscovery(ctx *context.T) error {
df, err := idiscovery.NewFactory(ctx, mock.New())
if err != nil {
return err
}
fdiscovery.InjectFactory(df)
return nil
}