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

@ -52,9 +52,9 @@ lazy val root = project
},
cargoBuild := {
println(Seq("sh", "-c", "cd glue; cargo rustc -- -Awarnings") !!)
//println(
// println(
// Seq("sh", "-c", "cd glue; RUST_BACKTRACE=full cargo test --quiet --features e2e") !!
//) // very important: test suite validates interfaces
// ) // very important: test suite validates interfaces
},
fullBuild := {
cargoBuild.value

View File

@ -1,12 +1,14 @@
use std::sync::Once;
use no_std_net::SocketAddr;
use toad::net::Addrd;
use toad::platform::Platform;
use toad_jni::java::{self, Object};
use toad::net::Addrd;
use toad_msg::{Type, Id, Token, alloc::Message, Code};
use toad_msg::alloc::Message;
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>) {
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 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());
}
@ -32,4 +35,3 @@ fn e2e_test_suite() {
let (runtime, mut env) = runtime_init();
runtime_poll_req(&runtime, &mut env);
}

View File

@ -34,7 +34,7 @@ pub extern "system" fn JNI_OnLoad(jvm: JavaVM, _: *const c_void) -> i32 {
#[no_mangle]
pub extern "system" fn JNI_OnUnload(_: JavaVM, _: *const c_void) {
unsafe {mem::Runtime::dealloc()}
unsafe { mem::Runtime::dealloc() }
}
#[cfg(all(test, feature = "e2e"))]
@ -53,7 +53,7 @@ pub mod test {
use crate::runtime_config::RuntimeConfig;
pub fn init<'a>() -> java::Env<'a> {
static INIT: Once = Once::new();
static INIT: Once = Once::new();
INIT.call_once(|| {
let jvm =
JavaVM::new(InitArgsBuilder::new().option("-Djava.library.path=/home/orion/src/toad-lib/toad-java/target/glue/debug/")

View File

@ -12,12 +12,14 @@ pub struct Runtime(java::lang::Object);
impl Runtime {
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)
}
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)
}
@ -31,25 +33,24 @@ impl Runtime {
}
fn init_impl(e: &mut java::Env, cfg: RuntimeConfig) -> i64 {
let r = ||
ToadRuntime::try_new(format!("0.0.0.0:{}", cfg.net(e).port(e)), cfg.to_toad(e)).unwrap();
let r =
|| 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 }
}
fn poll_req_impl(&self, e: &mut java::Env) -> java::util::Optional<MessageRef> {
match self.ref_(e).poll_req() {
| Ok(req) => {
let mr = MessageRef::new(e, req.data().msg());
java::util::Optional::<MessageRef>::of(e, mr)
},
| Err(nb::Error::WouldBlock) => {
java::util::Optional::<MessageRef>::empty(e)
},
| Err(nb::Error::Other(err)) => {
e.throw(format!("{:?}", err)).unwrap();
java::util::Optional::<MessageRef>::empty(e)
},
} }
match self.ref_(e).poll_req() {
| Ok(req) => {
let mr = MessageRef::new(e, req.data().msg());
java::util::Optional::<MessageRef>::of(e, mr)
},
| Err(nb::Error::WouldBlock) => java::util::Optional::<MessageRef>::empty(e),
| Err(nb::Error::Other(err)) => {
e.throw(format!("{:?}", err)).unwrap();
java::util::Optional::<MessageRef>::empty(e)
},
}
}
}
java::object_newtype!(Runtime);
@ -74,10 +75,9 @@ pub extern "system" fn Java_dev_toad_Runtime_pollReq<'local>(mut e: java::Env<'l
runtime: JObject<'local>)
-> jobject {
let e = &mut e;
java::lang::Object::from_local(e, runtime)
.upcast_to::<Runtime>(e)
.poll_req_impl(e)
.downcast(e)
.to_local(e)
.as_raw()
java::lang::Object::from_local(e, runtime).upcast_to::<Runtime>(e)
.poll_req_impl(e)
.downcast(e)
.to_local(e)
.as_raw()
}

View File

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