Exemple #1
0
// Exports the discovery scan API to CGO using JSON to marshal ads
//export swift_io_v_v23_discovery_scan
func swift_io_v_v23_discovery_scan(ctxHandle C.GoContextHandle, discoveryHandle C.GoDiscoveryHandle, query C.SwiftCString, asyncId C.AsyncCallbackIdentifier, callbackBlock C.SwiftAsyncJsonCallback, errOut *C.SwiftVError) bool {
	ctx := scontext.GoContext(uint64(ctxHandle))
	d := GoDiscoveryT(uint64(discoveryHandle))
	goQuery := sutil.GoString(unsafe.Pointer(&query), false)
	ch, err := d.Scan(ctx, goQuery)
	if err != nil {
		sutil.ThrowSwiftError(ctx, err, unsafe.Pointer(errOut))
		return false
	}
	go func() {
		for update := range ch {
			data := struct {
				IsLost bool
				Ad     discovery.Advertisement
			}{
				IsLost: update.IsLost(),
				Ad:     update.Advertisement(),
			}
			b, err := json.Marshal(data)
			if err != nil {
				ctx.Fatal("Unable to JSON serialize discovery update: ", err)
			}
			ba := swiftBytesCopy(b)
			C.CallScanCallback(callbackBlock, asyncId, ba)
			C.free(ba.data)
		}
		C.CallScanCallback(callbackBlock, asyncId, emptySwiftByteArray())
	}()
	return true
}
Exemple #2
0
//export swift_io_v_impl_google_rpc_ClientImpl_nativeStartCallAsync
func swift_io_v_impl_google_rpc_ClientImpl_nativeStartCallAsync(ctxHandle C.GoContextHandle, cName *C.char, cMethod *C.char, cVomArgs C.SwiftByteArrayArray, skipServerAuth bool, asyncId C.AsyncCallbackIdentifier, successCallback C.SwiftAsyncSuccessHandleCallback, failureCallback C.SwiftAsyncFailureCallback) {
	name := C.GoString(cName)
	method := C.GoString(cMethod)
	ctx := scontext.GoContext(uint64(ctxHandle))
	client := v23.GetClient(ctx)

	// TODO Get args (we don't have VOM yet in Swift so nothing to get until then)
	//	args, err := decodeArgs(env, jVomArgs)
	//	if err != nil {
	//		sutil.ThrowSwiftError(ctx, err, unsafe.Pointer(errOut))
	//		return C.GoClientCallHandle(0)
	//	}
	args := make([]interface{}, 0)

	go func() {
		result, err := doStartCall(ctx, name, method, skipServerAuth == true, client, args)
		if err != nil {
			var swiftVError C.SwiftVError
			sutil.ThrowSwiftError(ctx, err, unsafe.Pointer(&swiftVError))
			sutil.DoFailureCallback(unsafe.Pointer(failureCallback), int32(asyncId), unsafe.Pointer(&swiftVError))
		} else {
			handle := C.GoClientCallHandle(SwiftClientCall(result))
			sutil.DoSuccessHandlerCallback(unsafe.Pointer(successCallback), int32(asyncId), uint64(handle))
		}
	}()
}
Exemple #3
0
//export swift_io_v_impl_google_rpc_ClientCallImpl_nativeCloseSend
func swift_io_v_impl_google_rpc_ClientCallImpl_nativeCloseSend(ctxHandle C.GoContextHandle, callHandle C.GoClientCallHandle, errOut *C.SwiftVError) {
	ctx := scontext.GoContext(uint64(ctxHandle))
	call := GoClientCall(uint64(callHandle))
	if err := call.CloseSend(); err != nil {
		sutil.ThrowSwiftError(ctx, err, unsafe.Pointer(errOut))
		return
	}
}
Exemple #4
0
//export swift_io_v_impl_google_rt_VRuntimeImpl_nativeShutdown
func swift_io_v_impl_google_rt_VRuntimeImpl_nativeShutdown(ctxHandle C.GoContextHandle) {
	ctx := scontext.GoContext(uint64(ctxHandle))
	value := ctx.Value(shutdownKey{})

	if shutdownFunc, ok := value.(v23.Shutdown); ok {
		shutdownFunc()
	}
}
Exemple #5
0
//export swift_io_v_v23_discovery_new
func swift_io_v_v23_discovery_new(ctxHandle C.GoContextHandle, errOut *C.SwiftVError) C.GoDiscoveryHandle {
	ctx := scontext.GoContext(uint64(ctxHandle))
	d, err := v23.NewDiscovery(ctx)
	if err != nil {
		sutil.ThrowSwiftError(ctx, err, unsafe.Pointer(errOut))
		return C.GoDiscoveryHandle(0)
	}
	return C.GoDiscoveryHandle(sutil.GoNewRef(&d))
}
Exemple #6
0
//export swift_io_v_v23_security_simple_nativePublicKey
func swift_io_v_v23_security_simple_nativePublicKey(ctxHandle C.GoContextHandle, errOut *C.SwiftVError) *C.char {
	ctx := context.GoContext(uint64(ctxHandle))
	der, err := v23.GetPrincipal(ctx).PublicKey().MarshalBinary()
	if err != nil {
		util.ThrowSwiftError(nil, err, unsafe.Pointer(errOut))
		return nil
	}
	// Swift will have to free the allocation
	return C.CString(base64.URLEncoding.EncodeToString(der))
}
Exemple #7
0
//export swift_io_v_impl_google_rt_VRuntimeImpl_nativeWithNewClient
func swift_io_v_impl_google_rt_VRuntimeImpl_nativeWithNewClient(ctxHandle C.GoContextHandle, errOut *C.SwiftVError) C.GoContextHandle {
	ctx := scontext.GoContext(uint64(ctxHandle))
	// No options supported yet.
	newCtx, _, err := v23.WithNewClient(ctx)
	if err != nil {
		sutil.ThrowSwiftError(ctx, err, unsafe.Pointer(errOut))
		return C.GoContextHandle(0)
	}

	return C.GoContextHandle(scontext.SwiftContext(newCtx))
}
Exemple #8
0
//export swift_io_v_impl_google_rpc_ClientCallImpl_nativeFinishAsync
func swift_io_v_impl_google_rpc_ClientCallImpl_nativeFinishAsync(ctxHandle C.GoContextHandle, callHandle C.GoClientCallHandle, numResults int, asyncId C.AsyncCallbackIdentifier, successCallback C.SwiftAsyncSuccessByteArrayArrayCallback, failureCallback C.SwiftAsyncFailureCallback) {
	ctx := scontext.GoContext(uint64(ctxHandle))
	call := GoClientCall(uint64(callHandle))
	go func() {
		result, err := doFinish(call, numResults)
		if err != nil {
			var swiftVError C.SwiftVError
			sutil.ThrowSwiftError(ctx, err, unsafe.Pointer(&swiftVError))
			sutil.DoFailureCallback(unsafe.Pointer(failureCallback), int32(asyncId), unsafe.Pointer(&swiftVError))
		} else {
			sutil.DoSuccessByteArrayArrayCallback(unsafe.Pointer(successCallback), int32(asyncId), unsafe.Pointer(&result))
		}
	}()
}
Exemple #9
0
//export swift_io_v_v23_security_simple_nativeSetBlessings
func swift_io_v_v23_security_simple_nativeSetBlessings(ctxHandle C.GoContextHandle, encodedSwiftBlessings C.SwiftByteArray, errOut *C.SwiftVError) {
	ctx := context.GoContext(uint64(ctxHandle))
	encodedBlessings := util.GoBytesNoCopy(unsafe.Pointer(&encodedSwiftBlessings))
	var blessings security.Blessings
	if err := vom.Decode(encodedBlessings, &blessings); err != nil {
		ctx.Error("Unable to decode:", err)
		util.ThrowSwiftError(nil, err, unsafe.Pointer(errOut))
		return
	}
	principal := v23.GetPrincipal(ctx)
	if err := principal.BlessingStore().SetDefault(blessings); err != nil {
		util.ThrowSwiftError(nil, err, unsafe.Pointer(errOut))
		return
	}
}
Exemple #10
0
// Exports the discovery advertise API to CGO using JSON to marshal ads
//export swift_io_v_v23_discovery_advertise
func swift_io_v_v23_discovery_advertise(ctxHandle C.GoContextHandle, discoveryHandle C.GoDiscoveryHandle, adJson C.SwiftByteArray, visibilityArray C.SwiftCStringArray, asyncId C.AsyncCallbackIdentifier, doneCallback C.SwiftAsyncSuccessCallback, errOut *C.SwiftVError) bool {
	ctx := scontext.GoContext(uint64(ctxHandle))
	d := GoDiscoveryT(uint64(discoveryHandle))
	ad := discovery.Advertisement{}
	if err := json.Unmarshal(sutil.GoBytesNoCopy(unsafe.Pointer(&adJson)), &ad); err != nil {
		sutil.ThrowSwiftError(ctx, err, unsafe.Pointer(errOut))
		return false
	}
	var visibility []security.BlessingPattern
	for _, v := range visibilityArray.toStrings() {
		visibility = append(visibility, security.BlessingPattern(v))
	}
	doneChan, err := d.Advertise(ctx, &ad, visibility)
	if err != nil {
		sutil.ThrowSwiftError(ctx, err, unsafe.Pointer(errOut))
		return false
	}
	go func() {
		<-doneChan
		C.CallAdvertisingCallback(doneCallback, asyncId)
	}()
	return true
}
Exemple #11
0
//export swift_io_v_impl_google_rpc_ClientImpl_nativeClose
func swift_io_v_impl_google_rpc_ClientImpl_nativeClose(ctxHandle C.GoContextHandle) {
	ctx := scontext.GoContext(uint64(ctxHandle))
	client := v23.GetClient(ctx)
	<-client.Closed()
}
Exemple #12
0
//export swift_io_v_v23_security_simple_nativeBlessingsDebugString
func swift_io_v_v23_security_simple_nativeBlessingsDebugString(ctxHandle C.GoContextHandle) *C.char {
	ctx := context.GoContext(uint64(ctxHandle))
	blessings, _ := v23.GetPrincipal(ctx).BlessingStore().Default()
	return C.CString(fmt.Sprintf("%v", blessings))
}