Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 1 | // +build android |
| 2 | |
Srdjan Petrovic | e2f5fce | 2014-08-21 15:07:24 -0700 | [diff] [blame] | 3 | package ipc |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 4 | |
| 5 | import ( |
Srdjan Petrovic | 1637662 | 2014-06-04 13:49:42 -0700 | [diff] [blame] | 6 | "io" |
| 7 | "time" |
Srdjan Petrovic | 6f6b2c0 | 2014-07-11 12:36:23 -0700 | [diff] [blame] | 8 | "unsafe" |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 9 | |
Srdjan Petrovic | e2f5fce | 2014-08-21 15:07:24 -0700 | [diff] [blame] | 10 | jnisecurity "veyron/jni/runtimes/google/security" |
| 11 | "veyron/jni/runtimes/google/util" |
Srdjan Petrovic | 1637662 | 2014-06-04 13:49:42 -0700 | [diff] [blame] | 12 | "veyron2" |
Srdjan Petrovic | 9692896 | 2014-06-20 10:03:15 -0700 | [diff] [blame] | 13 | ctx "veyron2/context" |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 14 | "veyron2/ipc" |
| 15 | "veyron2/rt" |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 16 | ) |
| 17 | |
Srdjan Petrovic | 6f6b2c0 | 2014-07-11 12:36:23 -0700 | [diff] [blame] | 18 | // #cgo LDFLAGS: -ljniwrapper |
| 19 | // #include "jni_wrapper.h" |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 20 | // #include <stdlib.h> |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 21 | import "C" |
| 22 | |
| 23 | var ( |
Srdjan Petrovic | 1637662 | 2014-06-04 13:49:42 -0700 | [diff] [blame] | 24 | // Global reference for com.veyron.runtimes.google.ipc.Runtime$ServerCall class. |
| 25 | jServerCallClass C.jclass |
Srdjan Petrovic | 9692896 | 2014-06-20 10:03:15 -0700 | [diff] [blame] | 26 | // Global reference for com.veyron.runtimes.google.ipc.VDLInvoker class. |
| 27 | jVDLInvokerClass C.jclass |
Srdjan Petrovic | 86560d4 | 2014-08-01 17:23:15 -0700 | [diff] [blame] | 28 | // Global reference for com.veyron2.OptionDefs class. |
| 29 | jOptionDefsClass C.jclass |
Srdjan Petrovic | 1637662 | 2014-06-04 13:49:42 -0700 | [diff] [blame] | 30 | // Global reference for java.io.EOFException class. |
| 31 | jEOFExceptionClass C.jclass |
Srdjan Petrovic | 6f6b2c0 | 2014-07-11 12:36:23 -0700 | [diff] [blame] | 32 | // Global reference for java.lang.String class. |
| 33 | jStringClass C.jclass |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 34 | ) |
| 35 | |
Srdjan Petrovic | 6f6b2c0 | 2014-07-11 12:36:23 -0700 | [diff] [blame] | 36 | // Init initializes the JNI code with the given Java environment. This method |
| 37 | // must be called from the main Java thread. |
| 38 | // NOTE: Because CGO creates package-local types and because this method may be |
| 39 | // invoked from a different package, Java environment is passed in an empty |
| 40 | // interface and then cast into the package-local environment type. |
| 41 | func Init(jEnv interface{}) { |
| 42 | env := (*C.JNIEnv)(unsafe.Pointer(util.PtrValue(jEnv))) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 43 | // Cache global references to all Java classes used by the package. This is |
| 44 | // necessary because JNI gets access to the class loader only in the system |
| 45 | // thread, so we aren't able to invoke FindClass in other threads. |
Srdjan Petrovic | 6f6b2c0 | 2014-07-11 12:36:23 -0700 | [diff] [blame] | 46 | jServerCallClass = C.jclass(util.JFindClassPtrOrDie(env, "com/veyron/runtimes/google/Runtime$ServerCall")) |
| 47 | jVDLInvokerClass = C.jclass(util.JFindClassPtrOrDie(env, "com/veyron/runtimes/google/VDLInvoker")) |
Srdjan Petrovic | 86560d4 | 2014-08-01 17:23:15 -0700 | [diff] [blame] | 48 | jOptionDefsClass = C.jclass(util.JFindClassPtrOrDie(env, "com/veyron2/OptionDefs")) |
Srdjan Petrovic | 6f6b2c0 | 2014-07-11 12:36:23 -0700 | [diff] [blame] | 49 | jEOFExceptionClass = C.jclass(util.JFindClassPtrOrDie(env, "java/io/EOFException")) |
| 50 | jStringClass = C.jclass(util.JFindClassPtrOrDie(env, "java/lang/String")) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 51 | } |
| 52 | |
Srdjan Petrovic | 9692896 | 2014-06-20 10:03:15 -0700 | [diff] [blame] | 53 | //export Java_com_veyron_runtimes_google_Runtime_nativeInit |
Srdjan Petrovic | 86560d4 | 2014-08-01 17:23:15 -0700 | [diff] [blame] | 54 | func Java_com_veyron_runtimes_google_Runtime_nativeInit(env *C.JNIEnv, jRuntime C.jclass, jOptions C.jobject) C.jlong { |
| 55 | opts := goRuntimeOpts(env, jOptions) |
| 56 | r := rt.Init(opts...) |
Srdjan Petrovic | 6f6b2c0 | 2014-07-11 12:36:23 -0700 | [diff] [blame] | 57 | util.GoRef(&r) // Un-refed when the Java Runtime object is finalized. |
| 58 | return C.jlong(util.PtrValue(&r)) |
Srdjan Petrovic | 1637662 | 2014-06-04 13:49:42 -0700 | [diff] [blame] | 59 | } |
| 60 | |
Srdjan Petrovic | 86560d4 | 2014-08-01 17:23:15 -0700 | [diff] [blame] | 61 | //export Java_com_veyron_runtimes_google_Runtime_nativeNewRuntime |
| 62 | func Java_com_veyron_runtimes_google_Runtime_nativeNewRuntime(env *C.JNIEnv, jRuntime C.jclass, jOptions C.jobject) C.jlong { |
| 63 | opts := goRuntimeOpts(env, jOptions) |
| 64 | r, err := rt.New(opts...) |
| 65 | if err != nil { |
| 66 | util.JThrowV(env, err) |
| 67 | return C.jlong(0) |
| 68 | } |
| 69 | util.GoRef(&r) |
| 70 | return C.jlong(util.PtrValue(&r)) |
| 71 | } |
| 72 | |
| 73 | // goRuntimeOpts converts Java runtime options into Go runtime options. |
| 74 | func goRuntimeOpts(env *C.JNIEnv, jOptions C.jobject) (ret []veyron2.ROpt) { |
| 75 | // Process RuntimeIDOpt. |
| 76 | runtimeIDKey := util.JStaticStringField(env, jOptionDefsClass, "RUNTIME_ID") |
| 77 | if util.CallBooleanMethodOrCatch(env, jOptions, "has", []util.Sign{util.StringSign}, runtimeIDKey) { |
| 78 | jPrivateID := C.jobject(util.CallObjectMethodOrCatch(env, jOptions, "get", []util.Sign{util.StringSign}, util.ObjectSign, runtimeIDKey)) |
| 79 | id := jnisecurity.NewPrivateID(env, jPrivateID) |
| 80 | ret = append(ret, veyron2.RuntimeID(id)) |
| 81 | } |
Srdjan Petrovic | 7602c27 | 2014-08-21 13:56:28 -0700 | [diff] [blame] | 82 | // Process RuntimePublicIDStoreOpt |
| 83 | runtimePublicIDStoreKey := util.JStaticStringField(env, jOptionDefsClass, "RUNTIME_PUBLIC_ID_STORE") |
| 84 | if util.CallBooleanMethodOrCatch(env, jOptions, "has", []util.Sign{util.StringSign}, runtimePublicIDStoreKey) { |
| 85 | jStore := C.jobject(util.CallObjectMethodOrCatch(env, jOptions, "get", []util.Sign{util.StringSign}, util.ObjectSign, runtimePublicIDStoreKey)) |
| 86 | store := jnisecurity.NewPublicIDStore(env, jStore) |
| 87 | ret = append(ret, veyron2.RuntimePublicIDStore(store)) |
| 88 | } |
Srdjan Petrovic | 86560d4 | 2014-08-01 17:23:15 -0700 | [diff] [blame] | 89 | return |
| 90 | } |
| 91 | |
Srdjan Petrovic | 9692896 | 2014-06-20 10:03:15 -0700 | [diff] [blame] | 92 | //export Java_com_veyron_runtimes_google_Runtime_nativeNewClient |
Matt Rosencrantz | 137b8d2 | 2014-08-18 09:56:15 -0700 | [diff] [blame] | 93 | // TODO(mattr): Eliminate timeoutMillis, it is no longer functional. |
Srdjan Petrovic | 9692896 | 2014-06-20 10:03:15 -0700 | [diff] [blame] | 94 | func Java_com_veyron_runtimes_google_Runtime_nativeNewClient(env *C.JNIEnv, jRuntime C.jobject, goRuntimePtr C.jlong, timeoutMillis C.jlong) C.jlong { |
Srdjan Petrovic | 6f6b2c0 | 2014-07-11 12:36:23 -0700 | [diff] [blame] | 95 | r := (*veyron2.Runtime)(util.Ptr(goRuntimePtr)) |
Matt Rosencrantz | 137b8d2 | 2014-08-18 09:56:15 -0700 | [diff] [blame] | 96 | rc, err := (*r).NewClient() |
Srdjan Petrovic | 1637662 | 2014-06-04 13:49:42 -0700 | [diff] [blame] | 97 | if err != nil { |
Srdjan Petrovic | 6f6b2c0 | 2014-07-11 12:36:23 -0700 | [diff] [blame] | 98 | util.JThrowV(env, err) |
Srdjan Petrovic | 1637662 | 2014-06-04 13:49:42 -0700 | [diff] [blame] | 99 | return C.jlong(0) |
| 100 | } |
| 101 | c := newClient(rc) |
Srdjan Petrovic | 6f6b2c0 | 2014-07-11 12:36:23 -0700 | [diff] [blame] | 102 | util.GoRef(c) // Un-refed when the Java Client object is finalized. |
| 103 | return C.jlong(util.PtrValue(c)) |
Srdjan Petrovic | 1637662 | 2014-06-04 13:49:42 -0700 | [diff] [blame] | 104 | } |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 105 | |
Srdjan Petrovic | 9692896 | 2014-06-20 10:03:15 -0700 | [diff] [blame] | 106 | //export Java_com_veyron_runtimes_google_Runtime_nativeNewServer |
| 107 | func Java_com_veyron_runtimes_google_Runtime_nativeNewServer(env *C.JNIEnv, jRuntime C.jobject, goRuntimePtr C.jlong) C.jlong { |
Srdjan Petrovic | 6f6b2c0 | 2014-07-11 12:36:23 -0700 | [diff] [blame] | 108 | r := (*veyron2.Runtime)(util.Ptr(goRuntimePtr)) |
Srdjan Petrovic | 1637662 | 2014-06-04 13:49:42 -0700 | [diff] [blame] | 109 | s, err := (*r).NewServer() |
| 110 | if err != nil { |
Srdjan Petrovic | 6f6b2c0 | 2014-07-11 12:36:23 -0700 | [diff] [blame] | 111 | util.JThrowV(env, err) |
Srdjan Petrovic | 1637662 | 2014-06-04 13:49:42 -0700 | [diff] [blame] | 112 | return C.jlong(0) |
| 113 | } |
Srdjan Petrovic | 6f6b2c0 | 2014-07-11 12:36:23 -0700 | [diff] [blame] | 114 | util.GoRef(&s) // Un-refed when the Java Server object is finalized. |
| 115 | return C.jlong(util.PtrValue(&s)) |
Srdjan Petrovic | 1637662 | 2014-06-04 13:49:42 -0700 | [diff] [blame] | 116 | } |
| 117 | |
Srdjan Petrovic | 9692896 | 2014-06-20 10:03:15 -0700 | [diff] [blame] | 118 | //export Java_com_veyron_runtimes_google_Runtime_nativeGetClient |
| 119 | func Java_com_veyron_runtimes_google_Runtime_nativeGetClient(env *C.JNIEnv, jRuntime C.jobject, goRuntimePtr C.jlong) C.jlong { |
Srdjan Petrovic | 6f6b2c0 | 2014-07-11 12:36:23 -0700 | [diff] [blame] | 120 | r := (*veyron2.Runtime)(util.Ptr(goRuntimePtr)) |
Srdjan Petrovic | 1637662 | 2014-06-04 13:49:42 -0700 | [diff] [blame] | 121 | rc := (*r).Client() |
| 122 | c := newClient(rc) |
Srdjan Petrovic | 6f6b2c0 | 2014-07-11 12:36:23 -0700 | [diff] [blame] | 123 | util.GoRef(c) // Un-refed when the Java Client object is finalized. |
| 124 | return C.jlong(util.PtrValue(c)) |
Srdjan Petrovic | 1637662 | 2014-06-04 13:49:42 -0700 | [diff] [blame] | 125 | } |
| 126 | |
Srdjan Petrovic | 9692896 | 2014-06-20 10:03:15 -0700 | [diff] [blame] | 127 | //export Java_com_veyron_runtimes_google_Runtime_nativeNewContext |
| 128 | func Java_com_veyron_runtimes_google_Runtime_nativeNewContext(env *C.JNIEnv, jRuntime C.jobject, goRuntimePtr C.jlong) C.jlong { |
Srdjan Petrovic | 6f6b2c0 | 2014-07-11 12:36:23 -0700 | [diff] [blame] | 129 | r := (*veyron2.Runtime)(util.Ptr(goRuntimePtr)) |
Srdjan Petrovic | 1637662 | 2014-06-04 13:49:42 -0700 | [diff] [blame] | 130 | c := (*r).NewContext() |
Srdjan Petrovic | 6f6b2c0 | 2014-07-11 12:36:23 -0700 | [diff] [blame] | 131 | util.GoRef(&c) // Un-refed when the Java context object is finalized. |
| 132 | return C.jlong(util.PtrValue(&c)) |
Srdjan Petrovic | 1637662 | 2014-06-04 13:49:42 -0700 | [diff] [blame] | 133 | } |
| 134 | |
Srdjan Petrovic | 731e840 | 2014-08-22 09:58:32 -0700 | [diff] [blame] | 135 | //export Java_com_veyron_runtimes_google_Runtime_nativeGetPublicIDStore |
| 136 | func Java_com_veyron_runtimes_google_Runtime_nativeGetPublicIDStore(env *C.JNIEnv, jRuntime C.jobject, goRuntimePtr C.jlong) C.jlong { |
| 137 | r := (*veyron2.Runtime)(util.Ptr(goRuntimePtr)) |
| 138 | s := (*r).PublicIDStore() |
| 139 | util.GoRef(&s) // Un-refed when the Java PublicIDStore object is finalized. |
| 140 | return C.jlong(util.PtrValue(&s)) |
| 141 | } |
| 142 | |
Srdjan Petrovic | 9692896 | 2014-06-20 10:03:15 -0700 | [diff] [blame] | 143 | //export Java_com_veyron_runtimes_google_Runtime_nativeFinalize |
| 144 | func Java_com_veyron_runtimes_google_Runtime_nativeFinalize(env *C.JNIEnv, jRuntime C.jobject, goRuntimePtr C.jlong) { |
Srdjan Petrovic | 6f6b2c0 | 2014-07-11 12:36:23 -0700 | [diff] [blame] | 145 | util.GoUnref((*veyron2.Runtime)(util.Ptr(goRuntimePtr))) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 146 | } |
| 147 | |
Srdjan Petrovic | 9692896 | 2014-06-20 10:03:15 -0700 | [diff] [blame] | 148 | //export Java_com_veyron_runtimes_google_Runtime_00024Server_nativeListen |
| 149 | func Java_com_veyron_runtimes_google_Runtime_00024Server_nativeListen(env *C.JNIEnv, server C.jobject, goServerPtr C.jlong, protocol C.jstring, address C.jstring) C.jstring { |
Srdjan Petrovic | 6f6b2c0 | 2014-07-11 12:36:23 -0700 | [diff] [blame] | 150 | s := (*ipc.Server)(util.Ptr(goServerPtr)) |
| 151 | ep, err := (*s).Listen(util.GoString(env, protocol), util.GoString(env, address)) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 152 | if err != nil { |
Srdjan Petrovic | 6f6b2c0 | 2014-07-11 12:36:23 -0700 | [diff] [blame] | 153 | util.JThrowV(env, err) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 154 | return nil |
| 155 | } |
Srdjan Petrovic | 6f6b2c0 | 2014-07-11 12:36:23 -0700 | [diff] [blame] | 156 | return C.jstring(util.JStringPtr(env, ep.String())) |
| 157 | } |
| 158 | |
| 159 | //export Java_com_veyron_runtimes_google_Runtime_00024Server_nativeServe |
| 160 | func Java_com_veyron_runtimes_google_Runtime_00024Server_nativeServe(env *C.JNIEnv, jServer C.jobject, goServerPtr C.jlong, name C.jstring, jDispatcher C.jobject) { |
| 161 | s := (*ipc.Server)(util.Ptr(goServerPtr)) |
| 162 | d, err := newDispatcher(env, jDispatcher) |
| 163 | if err != nil { |
| 164 | util.JThrowV(env, err) |
| 165 | return |
| 166 | } |
| 167 | if err := (*s).Serve(util.GoString(env, name), d); err != nil { |
| 168 | util.JThrowV(env, err) |
| 169 | return |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | //export Java_com_veyron_runtimes_google_Runtime_00024Server_nativeGetPublishedNames |
| 174 | func Java_com_veyron_runtimes_google_Runtime_00024Server_nativeGetPublishedNames(env *C.JNIEnv, jServer C.jobject, goServerPtr C.jlong) C.jobjectArray { |
| 175 | names, err := (*(*ipc.Server)(util.Ptr(goServerPtr))).Published() |
| 176 | if err != nil { |
| 177 | util.JThrowV(env, err) |
| 178 | return nil |
| 179 | } |
| 180 | ret := C.NewObjectArray(env, C.jsize(len(names)), jStringClass, nil) |
| 181 | for i, name := range names { |
| 182 | C.SetObjectArrayElement(env, ret, C.jsize(i), C.jobject(util.JStringPtr(env, string(name)))) |
| 183 | } |
| 184 | return ret |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 185 | } |
| 186 | |
Benjamin Prosnitz | a0603d4 | 2014-06-27 09:27:56 -0700 | [diff] [blame] | 187 | //export Java_com_veyron_runtimes_google_Runtime_00024Server_nativeStop |
| 188 | func Java_com_veyron_runtimes_google_Runtime_00024Server_nativeStop(env *C.JNIEnv, server C.jobject, goServerPtr C.jlong) { |
Srdjan Petrovic | 6f6b2c0 | 2014-07-11 12:36:23 -0700 | [diff] [blame] | 189 | s := (*ipc.Server)(util.Ptr(goServerPtr)) |
Srdjan Petrovic | 1637662 | 2014-06-04 13:49:42 -0700 | [diff] [blame] | 190 | if err := (*s).Stop(); err != nil { |
Srdjan Petrovic | 6f6b2c0 | 2014-07-11 12:36:23 -0700 | [diff] [blame] | 191 | util.JThrowV(env, err) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 192 | return |
| 193 | } |
| 194 | } |
| 195 | |
Srdjan Petrovic | ec6f511 | 2014-07-22 14:41:54 -0700 | [diff] [blame] | 196 | //export Java_com_veyron_runtimes_google_Runtime_00024Server_nativeFinalize |
| 197 | func Java_com_veyron_runtimes_google_Runtime_00024Server_nativeFinalize(env *C.JNIEnv, server C.jobject, goServerPtr C.jlong) { |
Srdjan Petrovic | 6f6b2c0 | 2014-07-11 12:36:23 -0700 | [diff] [blame] | 198 | util.GoUnref((*ipc.Server)(util.Ptr(goServerPtr))) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 199 | } |
| 200 | |
Srdjan Petrovic | 9692896 | 2014-06-20 10:03:15 -0700 | [diff] [blame] | 201 | //export Java_com_veyron_runtimes_google_Runtime_00024Client_nativeStartCall |
| 202 | func Java_com_veyron_runtimes_google_Runtime_00024Client_nativeStartCall(env *C.JNIEnv, jClient C.jobject, goClientPtr C.jlong, jContext C.jobject, name C.jstring, method C.jstring, jsonArgs C.jobjectArray, jPath C.jstring, timeoutMillis C.jlong) C.jlong { |
Srdjan Petrovic | 6f6b2c0 | 2014-07-11 12:36:23 -0700 | [diff] [blame] | 203 | c := (*client)(util.Ptr(goClientPtr)) |
| 204 | call, err := c.StartCall(env, jContext, util.GoString(env, name), util.GoString(env, method), jsonArgs, jPath, timeoutMillis) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 205 | if err != nil { |
Srdjan Petrovic | 6f6b2c0 | 2014-07-11 12:36:23 -0700 | [diff] [blame] | 206 | util.JThrowV(env, err) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 207 | return C.jlong(0) |
| 208 | } |
Srdjan Petrovic | 6f6b2c0 | 2014-07-11 12:36:23 -0700 | [diff] [blame] | 209 | util.GoRef(call) |
| 210 | return C.jlong(util.PtrValue(call)) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 211 | } |
| 212 | |
Srdjan Petrovic | 9692896 | 2014-06-20 10:03:15 -0700 | [diff] [blame] | 213 | //export Java_com_veyron_runtimes_google_Runtime_00024Client_nativeClose |
| 214 | func Java_com_veyron_runtimes_google_Runtime_00024Client_nativeClose(env *C.JNIEnv, jClient C.jobject, goClientPtr C.jlong) { |
Srdjan Petrovic | 6f6b2c0 | 2014-07-11 12:36:23 -0700 | [diff] [blame] | 215 | (*client)(util.Ptr(goClientPtr)).Close() |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 216 | } |
| 217 | |
Srdjan Petrovic | 9692896 | 2014-06-20 10:03:15 -0700 | [diff] [blame] | 218 | //export Java_com_veyron_runtimes_google_Runtime_00024Client_nativeFinalize |
| 219 | func Java_com_veyron_runtimes_google_Runtime_00024Client_nativeFinalize(env *C.JNIEnv, jClient C.jobject, goClientPtr C.jlong) { |
Srdjan Petrovic | 6f6b2c0 | 2014-07-11 12:36:23 -0700 | [diff] [blame] | 220 | util.GoUnref((*client)(util.Ptr(goClientPtr))) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 221 | } |
| 222 | |
Srdjan Petrovic | 9692896 | 2014-06-20 10:03:15 -0700 | [diff] [blame] | 223 | //export Java_com_veyron_runtimes_google_Runtime_00024Context_nativeFinalize |
| 224 | func Java_com_veyron_runtimes_google_Runtime_00024Context_nativeFinalize(env *C.JNIEnv, jClient C.jobject, goContextPtr C.jlong) { |
Srdjan Petrovic | 6f6b2c0 | 2014-07-11 12:36:23 -0700 | [diff] [blame] | 225 | util.GoUnref((*ctx.T)(util.Ptr(goContextPtr))) |
Srdjan Petrovic | 1637662 | 2014-06-04 13:49:42 -0700 | [diff] [blame] | 226 | } |
| 227 | |
Srdjan Petrovic | 9692896 | 2014-06-20 10:03:15 -0700 | [diff] [blame] | 228 | //export Java_com_veyron_runtimes_google_Runtime_00024Stream_nativeSend |
| 229 | func Java_com_veyron_runtimes_google_Runtime_00024Stream_nativeSend(env *C.JNIEnv, jStream C.jobject, goStreamPtr C.jlong, jItem C.jstring) { |
Srdjan Petrovic | 6f6b2c0 | 2014-07-11 12:36:23 -0700 | [diff] [blame] | 230 | (*stream)(util.Ptr(goStreamPtr)).Send(env, jItem) |
Srdjan Petrovic | 1637662 | 2014-06-04 13:49:42 -0700 | [diff] [blame] | 231 | } |
| 232 | |
Srdjan Petrovic | 9692896 | 2014-06-20 10:03:15 -0700 | [diff] [blame] | 233 | //export Java_com_veyron_runtimes_google_Runtime_00024Stream_nativeRecv |
| 234 | func Java_com_veyron_runtimes_google_Runtime_00024Stream_nativeRecv(env *C.JNIEnv, jStream C.jobject, goStreamPtr C.jlong) C.jstring { |
Srdjan Petrovic | 6f6b2c0 | 2014-07-11 12:36:23 -0700 | [diff] [blame] | 235 | ret, err := (*stream)(util.Ptr(goStreamPtr)).Recv(env) |
Srdjan Petrovic | 1637662 | 2014-06-04 13:49:42 -0700 | [diff] [blame] | 236 | if err != nil { |
| 237 | if err == io.EOF { |
Srdjan Petrovic | 6f6b2c0 | 2014-07-11 12:36:23 -0700 | [diff] [blame] | 238 | util.JThrow(env, jEOFExceptionClass, err.Error()) |
Srdjan Petrovic | 1637662 | 2014-06-04 13:49:42 -0700 | [diff] [blame] | 239 | return nil |
| 240 | } |
Srdjan Petrovic | 6f6b2c0 | 2014-07-11 12:36:23 -0700 | [diff] [blame] | 241 | util.JThrowV(env, err) |
Srdjan Petrovic | 1637662 | 2014-06-04 13:49:42 -0700 | [diff] [blame] | 242 | return nil |
| 243 | } |
| 244 | return ret |
| 245 | } |
| 246 | |
Srdjan Petrovic | 9692896 | 2014-06-20 10:03:15 -0700 | [diff] [blame] | 247 | //export Java_com_veyron_runtimes_google_Runtime_00024ClientCall_nativeFinish |
| 248 | func Java_com_veyron_runtimes_google_Runtime_00024ClientCall_nativeFinish(env *C.JNIEnv, jClientCall C.jobject, goClientCallPtr C.jlong) C.jobjectArray { |
Srdjan Petrovic | 6f6b2c0 | 2014-07-11 12:36:23 -0700 | [diff] [blame] | 249 | ret, err := (*clientCall)(util.Ptr(goClientCallPtr)).Finish(env) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 250 | if err != nil { |
Srdjan Petrovic | 6f6b2c0 | 2014-07-11 12:36:23 -0700 | [diff] [blame] | 251 | util.JThrowV(env, err) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 252 | return nil |
| 253 | } |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 254 | return ret |
| 255 | } |
| 256 | |
Srdjan Petrovic | 9692896 | 2014-06-20 10:03:15 -0700 | [diff] [blame] | 257 | //export Java_com_veyron_runtimes_google_Runtime_00024ClientCall_nativeCancel |
| 258 | func Java_com_veyron_runtimes_google_Runtime_00024ClientCall_nativeCancel(env *C.JNIEnv, jClientCall C.jobject, goClientCallPtr C.jlong) { |
Srdjan Petrovic | 6f6b2c0 | 2014-07-11 12:36:23 -0700 | [diff] [blame] | 259 | (*clientCall)(util.Ptr(goClientCallPtr)).Cancel() |
Srdjan Petrovic | 1637662 | 2014-06-04 13:49:42 -0700 | [diff] [blame] | 260 | } |
| 261 | |
Srdjan Petrovic | 9692896 | 2014-06-20 10:03:15 -0700 | [diff] [blame] | 262 | //export Java_com_veyron_runtimes_google_Runtime_00024ClientCall_nativeFinalize |
| 263 | func Java_com_veyron_runtimes_google_Runtime_00024ClientCall_nativeFinalize(env *C.JNIEnv, jClientCall C.jobject, goClientCallPtr C.jlong) { |
Srdjan Petrovic | 6f6b2c0 | 2014-07-11 12:36:23 -0700 | [diff] [blame] | 264 | util.GoUnref((*clientCall)(util.Ptr(goClientCallPtr))) |
| 265 | } |
| 266 | |
| 267 | //export Java_com_veyron_runtimes_google_Runtime_00024ServerCall_nativeBlessing |
| 268 | func Java_com_veyron_runtimes_google_Runtime_00024ServerCall_nativeBlessing(env *C.JNIEnv, jServerCall C.jobject, goServerCallPtr C.jlong) C.jlong { |
| 269 | id := (*serverCall)(util.Ptr(goServerCallPtr)).Blessing() |
| 270 | util.GoRef(&id) // Un-refed when the Java PublicID object is finalized. |
| 271 | return C.jlong(util.PtrValue(&id)) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 272 | } |
| 273 | |
Srdjan Petrovic | 9692896 | 2014-06-20 10:03:15 -0700 | [diff] [blame] | 274 | //export Java_com_veyron_runtimes_google_Runtime_00024ServerCall_nativeDeadline |
| 275 | func Java_com_veyron_runtimes_google_Runtime_00024ServerCall_nativeDeadline(env *C.JNIEnv, jServerCall C.jobject, goServerCallPtr C.jlong) C.jlong { |
Srdjan Petrovic | 6f6b2c0 | 2014-07-11 12:36:23 -0700 | [diff] [blame] | 276 | s := (*serverCall)(util.Ptr(goServerCallPtr)) |
Srdjan Petrovic | 1637662 | 2014-06-04 13:49:42 -0700 | [diff] [blame] | 277 | var d time.Time |
| 278 | if s == nil { |
| 279 | // Error, return current time as deadline. |
| 280 | d = time.Now() |
| 281 | } else { |
Matt Rosencrantz | 137b8d2 | 2014-08-18 09:56:15 -0700 | [diff] [blame] | 282 | // TODO(mattr): Deal with missing deadlines by adjusting the JAVA api to allow it. |
| 283 | d, _ = s.Deadline() |
Srdjan Petrovic | 1637662 | 2014-06-04 13:49:42 -0700 | [diff] [blame] | 284 | } |
| 285 | return C.jlong(d.UnixNano() / 1000) |
| 286 | } |
| 287 | |
Srdjan Petrovic | 9692896 | 2014-06-20 10:03:15 -0700 | [diff] [blame] | 288 | //export Java_com_veyron_runtimes_google_Runtime_00024ServerCall_nativeClosed |
| 289 | func Java_com_veyron_runtimes_google_Runtime_00024ServerCall_nativeClosed(env *C.JNIEnv, jServerCall C.jobject, goServerCallPtr C.jlong) C.jboolean { |
Srdjan Petrovic | 6f6b2c0 | 2014-07-11 12:36:23 -0700 | [diff] [blame] | 290 | s := (*serverCall)(util.Ptr(goServerCallPtr)) |
Matt Rosencrantz | 137b8d2 | 2014-08-18 09:56:15 -0700 | [diff] [blame] | 291 | select { |
| 292 | case <-s.Done(): |
Srdjan Petrovic | 1637662 | 2014-06-04 13:49:42 -0700 | [diff] [blame] | 293 | return C.JNI_TRUE |
Matt Rosencrantz | 137b8d2 | 2014-08-18 09:56:15 -0700 | [diff] [blame] | 294 | default: |
| 295 | return C.JNI_FALSE |
Srdjan Petrovic | 1637662 | 2014-06-04 13:49:42 -0700 | [diff] [blame] | 296 | } |
Srdjan Petrovic | 1637662 | 2014-06-04 13:49:42 -0700 | [diff] [blame] | 297 | } |