This commit is contained in:
Orion Kindel 2023-04-08 22:54:31 -07:00
parent b400124096
commit e94f94d1a2
Signed by untrusted user who does not match committer: orion
GPG Key ID: 6D4165AE4C928719
5 changed files with 35 additions and 33 deletions

View File

@ -1,12 +1,14 @@
use std::sync::Once; use std::sync::Once;
use no_std_net::SocketAddr; use no_std_net::SocketAddr;
use toad::net::Addrd;
use toad::platform::Platform; use toad::platform::Platform;
use toad_jni::java::{self, Object}; use toad_jni::java::{self, Object};
use toad::net::Addrd; use toad_msg::alloc::Message;
use toad_msg::{Type, Id, Token, alloc::Message, Code}; use toad_msg::{Code, Id, Token, Type};
use crate::{runtime::Runtime, runtime_config::RuntimeConfig}; use crate::runtime::Runtime;
use crate::runtime_config::RuntimeConfig;
pub fn runtime_init<'a>() -> (Runtime, java::Env<'a>) { pub fn runtime_init<'a>() -> (Runtime, java::Env<'a>) {
let mut _env = crate::test::init(); let mut _env = crate::test::init();
@ -22,7 +24,8 @@ fn runtime_poll_req(runtime: &Runtime, env: &mut java::Env) {
let client = crate::Runtime::try_new("0.0.0.0:5684", Default::default()).unwrap(); let client = crate::Runtime::try_new("0.0.0.0:5684", Default::default()).unwrap();
let request = Message::new(Type::Con, Code::GET, Id(0), Token(Default::default())); let request = Message::new(Type::Con, Code::GET, Id(0), Token(Default::default()));
client.send_msg(Addrd(request, "0.0.0.0:5683".parse().unwrap())).unwrap(); client.send_msg(Addrd(request, "0.0.0.0:5683".parse().unwrap()))
.unwrap();
assert!(runtime.poll_req(env).is_some()); assert!(runtime.poll_req(env).is_some());
} }
@ -32,4 +35,3 @@ fn e2e_test_suite() {
let (runtime, mut env) = runtime_init(); let (runtime, mut env) = runtime_init();
runtime_poll_req(&runtime, &mut env); runtime_poll_req(&runtime, &mut env);
} }

View File

@ -12,12 +12,14 @@ pub struct Runtime(java::lang::Object);
impl Runtime { impl Runtime {
pub fn get_or_init(e: &mut java::Env, cfg: RuntimeConfig) -> Self { pub fn get_or_init(e: &mut java::Env, cfg: RuntimeConfig) -> Self {
static GET_OR_INIT: java::StaticMethod<Runtime, fn(RuntimeConfig) -> Runtime> = java::StaticMethod::new("getOrInit"); static GET_OR_INIT: java::StaticMethod<Runtime, fn(RuntimeConfig) -> Runtime> =
java::StaticMethod::new("getOrInit");
GET_OR_INIT.invoke(e, cfg) GET_OR_INIT.invoke(e, cfg)
} }
pub fn poll_req(&self, e: &mut java::Env) -> Option<MessageRef> { pub fn poll_req(&self, e: &mut java::Env) -> Option<MessageRef> {
static POLL_REQ: java::Method<Runtime, fn() -> java::util::Optional<MessageRef>> = java::Method::new("pollReq"); static POLL_REQ: java::Method<Runtime, fn() -> java::util::Optional<MessageRef>> =
java::Method::new("pollReq");
POLL_REQ.invoke(e, self).to_option(e) POLL_REQ.invoke(e, self).to_option(e)
} }
@ -31,8 +33,8 @@ impl Runtime {
} }
fn init_impl(e: &mut java::Env, cfg: RuntimeConfig) -> i64 { fn init_impl(e: &mut java::Env, cfg: RuntimeConfig) -> i64 {
let r = || let r =
ToadRuntime::try_new(format!("0.0.0.0:{}", cfg.net(e).port(e)), cfg.to_toad(e)).unwrap(); || ToadRuntime::try_new(format!("0.0.0.0:{}", cfg.net(e).port(e)), cfg.to_toad(e)).unwrap();
unsafe { crate::mem::Runtime::alloc(r).addr() as i64 } unsafe { crate::mem::Runtime::alloc(r).addr() as i64 }
} }
@ -42,14 +44,13 @@ impl Runtime {
let mr = MessageRef::new(e, req.data().msg()); let mr = MessageRef::new(e, req.data().msg());
java::util::Optional::<MessageRef>::of(e, mr) java::util::Optional::<MessageRef>::of(e, mr)
}, },
| Err(nb::Error::WouldBlock) => { | Err(nb::Error::WouldBlock) => java::util::Optional::<MessageRef>::empty(e),
java::util::Optional::<MessageRef>::empty(e)
},
| Err(nb::Error::Other(err)) => { | Err(nb::Error::Other(err)) => {
e.throw(format!("{:?}", err)).unwrap(); e.throw(format!("{:?}", err)).unwrap();
java::util::Optional::<MessageRef>::empty(e) java::util::Optional::<MessageRef>::empty(e)
}, },
} } }
}
} }
java::object_newtype!(Runtime); java::object_newtype!(Runtime);
@ -74,8 +75,7 @@ pub extern "system" fn Java_dev_toad_Runtime_pollReq<'local>(mut e: java::Env<'l
runtime: JObject<'local>) runtime: JObject<'local>)
-> jobject { -> jobject {
let e = &mut e; let e = &mut e;
java::lang::Object::from_local(e, runtime) java::lang::Object::from_local(e, runtime).upcast_to::<Runtime>(e)
.upcast_to::<Runtime>(e)
.poll_req_impl(e) .poll_req_impl(e)
.downcast(e) .downcast(e)
.to_local(e) .to_local(e)

View File

@ -4,7 +4,6 @@ import dev.toad.msg.MessageRef;
import java.util.Optional; import java.util.Optional;
public class Runtime { public class Runtime {
static { static {
System.loadLibrary("toad_java_glue"); System.loadLibrary("toad_java_glue");
} }
@ -12,6 +11,7 @@ public class Runtime {
private final long addr; private final long addr;
private static native long init(RuntimeOptions o); private static native long init(RuntimeOptions o);
private native Optional<MessageRef> pollReq(); private native Optional<MessageRef> pollReq();
public static Runtime getOrInit(RuntimeOptions o) { public static Runtime getOrInit(RuntimeOptions o) {