// GoNetworkAddress converts a Java NetworkAddress object into Go net.Addr. func GoNetworkAddress(env jutil.Env, jAddr jutil.Object) (net.Addr, error) { network, err := jutil.CallStringMethod(env, jAddr, "network", nil) if err != nil { return nil, err } addr, err := jutil.CallStringMethod(env, jAddr, "address", nil) if err != nil { return nil, err } return &jniAddr{network, addr}, nil }
//export Java_io_v_v23_syncbase_DatabaseImpl_nativeListenForInvites func Java_io_v_v23_syncbase_DatabaseImpl_nativeListenForInvites(jenv *C.JNIEnv, jDatabase C.jobject, jContext C.jobject, jInviteHandler C.jobject) { env := jutil.Env(uintptr(unsafe.Pointer(jenv))) database := jutil.Object(uintptr(unsafe.Pointer(jDatabase))) handler := jutil.Object(uintptr(unsafe.Pointer(jInviteHandler))) ctx, _, err := jcontext.GoContext(env, jutil.Object(uintptr(unsafe.Pointer(jContext)))) if err != nil { jutil.JThrowV(env, err) return } jDbId, err := jutil.CallObjectMethod(env, database, "id", nil, idSign) if err != nil { jutil.JThrowV(env, err) return } dbName, err := jutil.CallStringMethod(env, jDbId, "getName", nil) if err != nil { jutil.JThrowV(env, err) return } dbBlessing, err := jutil.CallStringMethod(env, jDbId, "getBlessing", nil) if err != nil { jutil.JThrowV(env, err) return } dbId := wire.Id{Name: dbName, Blessing: dbBlessing} // Note: There is no need to use a buffered channel here. ListenForInvites // spawns a goroutine for this listener that acts as an infinite buffer so we can // process invites at our own pace. ch := make(chan discovery.Invite) if err := discovery.ListenForInvites(ctx, dbId, ch); err != nil { jutil.JThrowV(env, err) return } handler = jutil.NewGlobalRef(env, handler) go func() { for invite := range ch { if err := handleInvite(invite, handler); err != nil { // TODO(mattr): We should cancel the stream and return an error to // the user here. ctx.Errorf("couldn't call invite handler: %v", err) } } env, free := jutil.GetEnv() jutil.DeleteGlobalRef(env, handler) free() }() }
func (d *driver) DebugString() string { env, freeFunc := jutil.GetEnv() defer freeFunc() s, _ := jutil.CallStringMethod(env, d.jDriver, "debugString", nil) return s }
func (l *btListener) Addr() net.Addr { env, freeFunc := jutil.GetEnv() defer freeFunc() addr, err := jutil.CallStringMethod(env, l.jListener, "address", nil) if err != nil { return &btAddr{""} } return &btAddr{addr} }
func (l *bleListener) Addr() net.Addr { env, freeFunc := jutil.GetEnv() defer freeFunc() addr, err := jutil.CallStringMethod(env, l.jListener, "address", nil) if err != nil { return bleAddr("") } return bleAddr(addr) }
func (c *callImpl) callStringMethod(methodName string) string { env, freeFunc := jutil.GetEnv() defer freeFunc() ret, err := jutil.CallStringMethod(env, c.jCall, methodName, nil) if err != nil { log.Printf("Couldn't call Java %q method: %v", methodName, err) return "" } return ret }
func (r *blessingStore) DebugString() string { env, freeFunc := jutil.GetEnv() defer freeFunc() result, err := jutil.CallStringMethod(env, r.jBlessingStore, "debugString", nil) if err != nil { log.Printf("Couldn't call Java debugString: %v", err) return "" } return result }
func (r *blessingRoots) DebugString() string { env, freeFunc := jutil.GetEnv() defer freeFunc() ret, err := jutil.CallStringMethod(env, r.jBlessingRoots, "debugString", nil) if err != nil { log.Printf("Couldn't get Java DebugString: %v", err) return "" } return ret }
// GoListenAddrs converts Java ListenSpec$Address array into a Go // rpc.ListenAddrs value. func GoListenAddrs(env jutil.Env, jAddrs jutil.Object) (rpc.ListenAddrs, error) { addrarr, err := jutil.GoObjectArray(env, jAddrs) if err != nil { return nil, err } addrs := make(rpc.ListenAddrs, len(addrarr)) for i, jAddr := range addrarr { var err error addrs[i].Protocol, err = jutil.CallStringMethod(env, jAddr, "getProtocol", nil) if err != nil { return nil, err } addrs[i].Address, err = jutil.CallStringMethod(env, jAddr, "getAddress", nil) if err != nil { return nil, err } } return addrs, nil }
// newConnection creates a new Go connection. The passed-in Java Stream object // is assumed to hold a global reference. func newConnection(env jutil.Env, jStream jutil.Object) flow.Conn { c := &btReadWriteCloser{jStream} runtime.SetFinalizer(c, func(c *btReadWriteCloser) { env, freeFunc := jutil.GetEnv() defer freeFunc() jutil.DeleteGlobalRef(env, c.jStream) }) addrStr, _ := jutil.CallStringMethod(env, jStream, "localAddress", nil) localAddr := &btAddr{addrStr} return btConn{framer.New(c), localAddr} }
// GoListenSpec converts the provided Java ListenSpec into a Go ListenSpec. func GoListenSpec(env jutil.Env, jSpec jutil.Object) (rpc.ListenSpec, error) { jAddrs, err := jutil.CallObjectMethod(env, jSpec, "getAddresses", nil, jutil.ArraySign(listenAddrSign)) if err != nil { return rpc.ListenSpec{}, err } addrs, err := GoListenAddrs(env, jAddrs) if err != nil { return rpc.ListenSpec{}, err } proxy, err := jutil.CallStringMethod(env, jSpec, "getProxy", nil) if err != nil { return rpc.ListenSpec{}, err } jChooser, err := jutil.CallObjectMethod(env, jSpec, "getChooser", nil, addressChooserSign) if err != nil { return rpc.ListenSpec{}, err } chooser := GoAddressChooser(env, jChooser) return rpc.ListenSpec{ Addrs: addrs, Proxy: proxy, AddressChooser: chooser, }, nil }
//export Java_io_v_impl_google_services_mounttable_MountTableServer_nativeWithNewServer func Java_io_v_impl_google_services_mounttable_MountTableServer_nativeWithNewServer(jenv *C.JNIEnv, jMountTableServerClass C.jclass, jContext C.jobject, jMountTableServerParams C.jobject) C.jobject { env := jutil.Env(uintptr(unsafe.Pointer(jenv))) jCtx := jutil.Object(uintptr(unsafe.Pointer(jContext))) jParams := jutil.Object(uintptr(unsafe.Pointer(jMountTableServerParams))) // Read and translate all of the server params. mountName, err := jutil.CallStringMethod(env, jParams, "getName", nil) if err != nil { jutil.JThrowV(env, err) return nil } rootDir, err := jutil.CallStringMethod(env, jParams, "getStorageRootDir", nil) if err != nil { jutil.JThrowV(env, err) return nil } permsJMap, err := jutil.CallMapMethod(env, jParams, "getPermissions", nil) if err != nil { jutil.JThrowV(env, err) return nil } permsMap := make(map[string]access.Permissions) for jPath, jPerms := range permsJMap { path := jutil.GoString(env, jPath) perms, err := jaccess.GoPermissions(env, jPerms) if err != nil { jutil.JThrowV(env, err) return nil } permsMap[path] = perms } // Write JSON-encoded permissions to a file. jsonPerms, err := json.Marshal(permsMap) if err != nil { jutil.JThrowV(env, fmt.Errorf("Couldn't JSON-encode path-permissions: %v", err)) return nil } permsFile, err := ioutil.TempFile(rootDir, "jni_permissions") if err != nil { jutil.JThrowV(env, fmt.Errorf("Couldn't create permissions file: %v", err)) return nil } w := bufio.NewWriter(permsFile) if _, err := w.Write(jsonPerms); err != nil { jutil.JThrowV(env, fmt.Errorf("Couldn't write to permissions file: %v", err)) return nil } if err := w.Flush(); err != nil { jutil.JThrowV(env, fmt.Errorf("Couldn't flush to permissions file: %v", err)) } statsPrefix, err := jutil.CallStringMethod(env, jParams, "getStatsPrefix", nil) if err != nil { jutil.JThrowV(env, err) return nil } // Start the mounttable server. ctx, cancel, err := jcontext.GoContext(env, jCtx) if err != nil { jutil.JThrowV(env, err) return nil } d, err := mounttablelib.NewMountTableDispatcher(ctx, permsFile.Name(), rootDir, statsPrefix) if err != nil { jutil.JThrowV(env, err) return nil } newCtx, s, err := v23.WithNewDispatchingServer(ctx, mountName, d, options.ServesMountTable(true)) if err != nil { jutil.JThrowV(env, err) return nil } jNewCtx, err := jcontext.JavaContext(env, newCtx, cancel) if err != nil { jutil.JThrowV(env, err) return nil } jServer, err := jrpc.JavaServer(env, s) if err != nil { jutil.JThrowV(env, err) return nil } // Attach a server to the new context. jServerAttCtx, err := jutil.CallStaticObjectMethod(env, jVRuntimeImplClass, "withServer", []jutil.Sign{contextSign, serverSign}, contextSign, jNewCtx, jServer) if err != nil { jutil.JThrowV(env, err) return nil } return C.jobject(unsafe.Pointer(jServerAttCtx)) }
//export Java_io_v_impl_google_services_syncbase_SyncbaseServer_nativeWithNewServer func Java_io_v_impl_google_services_syncbase_SyncbaseServer_nativeWithNewServer(jenv *C.JNIEnv, jSyncbaseServerClass C.jclass, jContext C.jobject, jSyncbaseServerParams C.jobject) C.jobject { env := jutil.Env(uintptr(unsafe.Pointer(jenv))) jCtx := jutil.Object(uintptr(unsafe.Pointer(jContext))) jParams := jutil.Object(uintptr(unsafe.Pointer(jSyncbaseServerParams))) // Read and translate all of the server params. jPerms, err := jutil.CallObjectMethod(env, jParams, "getPermissions", nil, permissionsSign) if err != nil { jutil.JThrowV(env, err) return nil } perms, err := jaccess.GoPermissions(env, jPerms) if err != nil { jutil.JThrowV(env, err) return nil } name, err := jutil.CallStringMethod(env, jParams, "getName", nil) if err != nil { jutil.JThrowV(env, err) return nil } rootDir, err := jutil.CallStringMethod(env, jParams, "getStorageRootDir", nil) if err != nil { jutil.JThrowV(env, err) return nil } if rootDir == "" { rootDir = filepath.Join(os.TempDir(), "syncbaseserver") if err := os.Mkdir(rootDir, 0755); err != nil && !os.IsExist(err) { jutil.JThrowV(env, err) return nil } } jEngine, err := jutil.CallObjectMethod(env, jParams, "getStorageEngine", nil, storageEngineSign) if err != nil { jutil.JThrowV(env, err) return nil } engine, err := GoStorageEngine(env, jEngine) if err != nil { jutil.JThrowV(env, err) return nil } ctx, cancel, err := jcontext.GoContext(env, jCtx) if err != nil { jutil.JThrowV(env, err) return nil } // Create the rpc server before the service so that connections are shared between // clients in the service and the rpc server. (i.e. connections are shared if the // context returned from WithNewDispatchingServer is used for client calls). d := dispatcher.NewDispatcherWrapper() ctx, s, err := v23.WithNewDispatchingServer(ctx, name, d, options.ChannelTimeout(vsync.NeighborConnectionTimeout)) if err != nil { cancel() jutil.JThrowV(env, err) return nil } service, err := server.NewService(ctx, server.ServiceOptions{ Perms: perms, RootDir: rootDir, Engine: engine, }) if err != nil { cancel() jutil.JThrowV(env, err) return nil } // Set the dispatcher in the dispatcher wrapper for the server to start responding // to incoming rpcs. d.SetDispatcher(server.NewDispatcher(service)) if err := service.AddNames(ctx, s); err != nil { cancel() jutil.JThrowV(env, err) return nil } jNewCtx, err := jcontext.JavaContext(env, ctx, cancel) if err != nil { jutil.JThrowV(env, err) return nil } jServer, err := jrpc.JavaServer(env, s) if err != nil { jutil.JThrowV(env, err) return nil } // Attach a server to the new context. jServerAttCtx, err := jutil.CallStaticObjectMethod(env, jVRuntimeImplClass, "withServer", []jutil.Sign{contextSign, serverSign}, contextSign, jNewCtx, jServer) if err != nil { jutil.JThrowV(env, err) return nil } return C.jobject(unsafe.Pointer(jServerAttCtx)) }
// GoStorageEngine converts the provided Java SyncbaseServer.StorageEngine // enum object into a Go storage engine string. func GoStorageEngine(env jutil.Env, jEngine jutil.Object) (string, error) { if jEngine.IsNull() { return "leveldb", nil } return jutil.CallStringMethod(env, jEngine, "getValue", []jutil.Sign{}) }
//export Java_io_v_impl_google_services_groups_GroupServer_nativeWithNewServer func Java_io_v_impl_google_services_groups_GroupServer_nativeWithNewServer(jenv *C.JNIEnv, jGroupServerClass C.jclass, jContext C.jobject, jGroupServerParams C.jobject) C.jobject { env := jutil.Env(uintptr(unsafe.Pointer(jenv))) jCtx := jutil.Object(uintptr(unsafe.Pointer(jContext))) jParams := jutil.Object(uintptr(unsafe.Pointer(jGroupServerParams))) // Read and translate all of the server params. name, err := jutil.CallStringMethod(env, jParams, "getName", nil) if err != nil { jutil.JThrowV(env, err) return nil } rootDir, err := jutil.CallStringMethod(env, jParams, "getStorageRootDir", nil) if err != nil { jutil.JThrowV(env, err) return nil } if rootDir == "" { rootDir = filepath.Join(os.TempDir(), "groupserver") if err := os.Mkdir(rootDir, 0755); err != nil && !os.IsExist(err) { jutil.JThrowV(env, err) return nil } } jEngine, err := jutil.CallObjectMethod(env, jParams, "getStorageEngine", nil, storageEngineSign) if err != nil { jutil.JThrowV(env, err) return nil } engine, err := GoStorageEngine(env, jEngine) if err != nil { jutil.JThrowV(env, err) return nil } // Start the server. ctx, cancel, err := jcontext.GoContext(env, jCtx) if err != nil { jutil.JThrowV(env, err) return nil } dispatcher, err := lib.NewGroupsDispatcher(rootDir, engine) if err != nil { jutil.JThrowV(env, err) return nil } newCtx, s, err := v23.WithNewDispatchingServer(ctx, name, dispatcher) if err != nil { jutil.JThrowV(env, err) return nil } jNewCtx, err := jcontext.JavaContext(env, newCtx, cancel) if err != nil { jutil.JThrowV(env, err) return nil } jServer, err := jrpc.JavaServer(env, s) if err != nil { jutil.JThrowV(env, err) return nil } // Attach a server to the new context. jServerAttCtx, err := jutil.CallStaticObjectMethod(env, jVRuntimeImplClass, "withServer", []jutil.Sign{contextSign, serverSign}, contextSign, jNewCtx, jServer) if err != nil { jutil.JThrowV(env, err) return nil } return C.jobject(unsafe.Pointer(jServerAttCtx)) }