feat: rework Runtime.Config.Builder to be Runtime.Builder, add explicit bind to address

This commit is contained in:
Orion Kindel 2023-04-11 13:39:43 -05:00
parent a8aa0f8bf7
commit ea59f92cb5
Signed by untrusted user who does not match committer: orion
GPG Key ID: 6D4165AE4C928719
10 changed files with 201 additions and 143 deletions

View File

@ -14,13 +14,6 @@ val path = settingKey[Map[String, String]]("paths")
fork := true
javaOptions += "--enable-preview"
javacOptions ++= Seq(
"--enable-preview",
"--release",
"20",
"-Xlint:unchecked",
"-Xlint:deprecation"
)
lazy val root = project
.in(file("."))
@ -33,6 +26,18 @@ lazy val root = project
"java.sources" -> baseDirectory.value.toGlob / "src" / "main" / "java" / ** / "*.java",
"glue.sources" -> baseDirectory.value.toGlob / "glue" / "src" / ** / "*.rs"
),
Compile / doc / javacOptions ++= Seq(
"--enable-preview",
"--release",
"20",
),
Compile / compile / javacOptions ++= Seq(
"--enable-preview",
"--release",
"20",
"-Xlint:unchecked",
"-Xlint:deprecation"
),
path := Map(
"glue.base" -> (baseDirectory.value / "glue").toString,
"glue.target" -> (baseDirectory.value / "target" / "glue" / "debug").toString,

2
glue/Cargo.lock generated
View File

@ -580,7 +580,7 @@ dependencies = [
[[package]]
name = "toad-jni"
version = "0.5.1"
version = "0.6.0"
dependencies = [
"jni",
"toad-array 0.5.0",

View File

@ -1,18 +1,11 @@
use std::ptr::drop_in_place;
use std::sync::Once;
use std::time::Duration;
use jni::objects::GlobalRef;
use no_std_net::SocketAddr;
use toad::config::Config;
use toad::net::Addrd;
use toad::platform::Platform;
use toad_jni::java::lang::System;
use toad_jni::java::{self, Object, Signature};
use toad_msg::alloc::Message;
use toad_msg::{Code, Id, Token, Type};
use crate::message_ref::MessageRef;
use crate::message_type::MessageType;
use crate::runtime::Runtime;
use crate::runtime_config::RuntimeConfig;
@ -29,8 +22,11 @@ fn init() -> State {
let mut _env = crate::test::init();
let env = &mut _env;
let cfg = RuntimeConfig::new(env, Config::default(), 5683);
let runtime = Runtime::get_or_init(env, cfg);
let cfg = RuntimeConfig::new(env,
Config::default(),
std::net::SocketAddr::new(std::net::Ipv4Addr::UNSPECIFIED.into(),
5683));
let runtime = Runtime::new(env, cfg);
let client = crate::Runtime::try_new("0.0.0.0:5684", Default::default()).unwrap();
State { runtime,

View File

@ -42,6 +42,7 @@ pub mod e2e;
#[cfg(test)]
pub mod test {
use std::net::{Ipv4Addr, SocketAddr};
use std::sync::Once;
use jni::{InitArgsBuilder, JavaVM};
@ -81,7 +82,9 @@ pub mod test {
let mut e = init();
let e = &mut e;
let r = RuntimeConfig::new(e, Config::default(), 5683);
let r = RuntimeConfig::new(e,
Config::default(),
SocketAddr::new(Ipv4Addr::UNSPECIFIED.into(), 5683));
assert_eq!(r.to_toad(e), Config::default());
}

View File

@ -3,7 +3,7 @@ use jni::sys::jobject;
use toad::platform::Platform;
use toad_jni::java::{self, Object};
use crate::mem::SharedMemoryRegion;
use crate::mem::{Shared, SharedMemoryRegion};
use crate::message_ref::MessageRef;
use crate::runtime_config::RuntimeConfig;
use crate::Runtime as ToadRuntime;
@ -11,10 +11,9 @@ use crate::Runtime as ToadRuntime;
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");
GET_OR_INIT.invoke(e, cfg)
pub fn new(e: &mut java::Env, cfg: RuntimeConfig) -> Self {
static CTOR: java::Constructor<Runtime, fn(RuntimeConfig)> = java::Constructor::new();
CTOR.invoke(e, cfg)
}
pub fn poll_req(&self, e: &mut java::Env) -> Option<MessageRef> {
@ -23,25 +22,22 @@ impl Runtime {
POLL_REQ.invoke(e, self).to_option(e)
}
pub fn addr(&self, e: &mut java::Env) -> i64 {
static ADDR: java::Field<Runtime, i64> = java::Field::new("addr");
ADDR.get(e, self)
}
pub fn ref_(&self, e: &mut java::Env) -> &'static ToadRuntime {
unsafe {
crate::mem::Shared::deref::<ToadRuntime>(0, self.addr(e)).as_ref()
.unwrap()
}
pub fn config(&self, e: &mut java::Env) -> RuntimeConfig {
static CONFIG: java::Method<Runtime, fn() -> RuntimeConfig> = java::Method::new("config");
CONFIG.invoke(e, self)
}
fn init_impl(e: &mut java::Env, cfg: RuntimeConfig) -> i64 {
let r = || ToadRuntime::try_new(format!("0.0.0.0:{}", cfg.port(e)), cfg.to_toad(e)).unwrap();
let r = || ToadRuntime::try_new(cfg.addr(e), cfg.to_toad(e)).unwrap();
unsafe { crate::mem::Shared::init(r).addr() as i64 }
}
fn poll_req_impl(&self, e: &mut java::Env) -> java::util::Optional<MessageRef> {
match self.ref_(e).poll_req() {
fn poll_req_impl(e: &mut java::Env, addr: i64) -> java::util::Optional<MessageRef> {
match unsafe {
Shared::deref::<crate::Runtime>(/* TODO */ 0, addr).as_ref()
.unwrap()
}.poll_req()
{
| Ok(req) => {
let mr = MessageRef::new(e, req.unwrap().into());
java::util::Optional::<MessageRef>::of(e, mr)
@ -58,14 +54,14 @@ impl Runtime {
java::object_newtype!(Runtime);
impl java::Class for Runtime {
const PATH: &'static str = package!(dev.toad.Runtime);
const PATH: &'static str = package!(dev.toad.ToadRuntime);
}
#[no_mangle]
pub extern "system" fn Java_dev_toad_Runtime_init<'local>(mut e: java::Env<'local>,
_: JClass<'local>,
cfg: JObject<'local>)
-> i64 {
pub extern "system" fn Java_dev_toad_ToadRuntime_init<'local>(mut e: java::Env<'local>,
_: JClass<'local>,
cfg: JObject<'local>)
-> i64 {
let e = &mut e;
let cfg = java::lang::Object::from_local(e, cfg).upcast_to::<RuntimeConfig>(e);
@ -73,11 +69,10 @@ pub extern "system" fn Java_dev_toad_Runtime_init<'local>(mut e: java::Env<'loca
}
#[no_mangle]
pub extern "system" fn Java_dev_toad_Runtime_pollReq<'local>(mut e: java::Env<'local>,
runtime: JObject<'local>)
-> jobject {
pub extern "system" fn Java_dev_toad_ToadRuntime_pollReq<'local>(mut e: java::Env<'local>,
_: JClass<'local>,
addr: i64)
-> jobject {
let e = &mut e;
java::lang::Object::from_local(e, runtime).upcast_to::<Runtime>(e)
.poll_req_impl(e)
.yield_to_java(e)
Runtime::poll_req_impl(e, addr).yield_to_java(e)
}

View File

@ -1,3 +1,5 @@
use std::net::{Ipv4Addr, SocketAddr};
use jni::objects::JClass;
use jni::sys::jobject;
use toad::config::{self, BytesPerSecond, Config};
@ -12,13 +14,14 @@ pub struct RuntimeConfig(java::lang::Object);
java::object_newtype!(RuntimeConfig);
impl java::Class for RuntimeConfig {
const PATH: &'static str = concat!(package!(dev.toad.Runtime), "$Config");
const PATH: &'static str = concat!(package!(dev.toad.ToadRuntime), "$Config");
}
impl RuntimeConfig {
pub fn port(&self, e: &mut java::Env) -> u16 {
static RUNTIME_CONFIG_PORT: java::Field<RuntimeConfig, uint::u16> = java::Field::new("port");
RUNTIME_CONFIG_PORT.get(e, self).to_rust(e)
pub fn addr(&self, e: &mut java::Env) -> SocketAddr {
static ADDRESS: java::Field<RuntimeConfig, java::net::InetSocketAddress> =
java::Field::new("addr");
ADDRESS.get(e, self).to_std(e)
}
pub fn concurrency(&self, e: &mut java::Env) -> u8 {
@ -32,8 +35,8 @@ impl RuntimeConfig {
RUNTIME_CONFIG_MSG.invoke(e, self)
}
pub fn new(e: &mut java::Env, c: Config, port: u16) -> Self {
static CTOR: java::Constructor<RuntimeConfig, fn(uint::u16, uint::u8, Msg)> =
pub fn new(e: &mut java::Env, c: Config, addr: SocketAddr) -> Self {
static CTOR: java::Constructor<RuntimeConfig, fn(java::net::InetSocketAddress, uint::u8, Msg)> =
java::Constructor::new();
let con = Con::new(e,
@ -48,10 +51,11 @@ impl RuntimeConfig {
con,
non);
let port = uint::u16::from_rust(e, port);
let concurrency = uint::u8::from_rust(e, c.max_concurrent_requests);
let jcfg = CTOR.invoke(e, port, concurrency, msg);
let address = java::net::InetSocketAddress::from_std(e, addr);
let jcfg = CTOR.invoke(e, address, concurrency, msg);
jcfg
}
@ -83,7 +87,7 @@ pub struct Msg(java::lang::Object);
java::object_newtype!(Msg);
impl java::Class for Msg {
const PATH: &'static str = concat!(package!(dev.toad.Runtime), "$Config$Msg");
const PATH: &'static str = concat!(package!(dev.toad.ToadRuntime), "$Config$Msg");
}
impl Msg {
@ -143,7 +147,7 @@ pub struct Con(java::lang::Object);
java::object_newtype!(Con);
impl java::Class for Con {
const PATH: &'static str = concat!(package!(dev.toad.Runtime), "$Config$Msg$Con");
const PATH: &'static str = concat!(package!(dev.toad.ToadRuntime), "$Config$Msg$Con");
}
impl Con {
@ -182,7 +186,7 @@ pub struct Non(java::lang::Object);
java::object_newtype!(Non);
impl java::Class for Non {
const PATH: &'static str = concat!(package!(dev.toad.Runtime), "$Config$Msg$Non");
const PATH: &'static str = concat!(package!(dev.toad.ToadRuntime), "$Config$Msg$Non");
}
impl Non {
@ -209,5 +213,7 @@ impl Non {
pub extern "system" fn Java_dev_toad_Runtime_defaultConfigImpl<'local>(mut env: java::Env<'local>,
_: JClass<'local>)
-> jobject {
RuntimeConfig::new(&mut env, Config::default(), 5683).yield_to_java(&mut env)
RuntimeConfig::new(&mut env,
Config::default(),
SocketAddr::new(Ipv4Addr::UNSPECIFIED.into(), 5683)).yield_to_java(&mut env)
}

View File

@ -2,53 +2,99 @@ package dev.toad;
import dev.toad.ffi.*;
import dev.toad.msg.MessageRef;
import java.net.InetSocketAddress;
import java.time.Duration;
import java.util.Optional;
import java.util.function.Function;
interface BuilderPort {
Runtime.Config.Builder port(short port);
}
public class ToadRuntime {
public class Runtime {
static native Config defaultConfigImpl();
protected static native Config defaultConfigImpl();
static Config defaultConfig = null;
protected static Config defaultConfig = null;
protected static Config defaultConfig() {
if (Runtime.defaultConfig == null) {
Runtime.defaultConfig = Runtime.defaultConfigImpl();
static Config defaultConfig() {
if (ToadRuntime.defaultConfig == null) {
ToadRuntime.defaultConfig = ToadRuntime.defaultConfigImpl();
}
return Runtime.defaultConfig;
return ToadRuntime.defaultConfig;
}
static {
System.loadLibrary("toad_java_glue");
}
private final long addr;
private final Ptr ptr;
private final Config config;
private static native long init(Config o);
private native Optional<MessageRef> pollReq();
private static native Optional<MessageRef> pollReq(long addr);
public static Runtime getOrInit(Config o) {
return new Runtime(o);
Optional<MessageRef> pollReq() {
return ToadRuntime.pollReq(this.ptr.addr());
}
public Runtime(Config o) {
this.addr = Runtime.init(o);
public static BuilderRequiresBindToAddress builder() {
return new Builder();
}
ToadRuntime(Config o) {
this.config = o;
this.ptr = Ptr.register(this.getClass(), this.init(o));
}
public Config config() {
return this.config;
}
public interface BuilderRequiresBindToAddress {
ToadRuntime.Builder port(short port);
ToadRuntime.Builder address(InetSocketAddress addr);
}
public static final class Builder implements BuilderRequiresBindToAddress {
Config.Msg.Builder msg = Config.Msg.builder();
Optional<InetSocketAddress> addr = Optional.empty();
u8 concurrency = ToadRuntime.defaultConfig().concurrency;
Builder() {}
public ToadRuntime build() {
var cfg = new Config(this.addr.get(), this.concurrency, this.msg.build());
return new ToadRuntime(cfg);
}
public Builder msg(Function<Config.Msg.Builder, Config.Msg.Builder> f) {
this.msg = f.apply(this.msg);
return this;
}
public Builder port(short port) {
return this.address(new InetSocketAddress(port));
}
public Builder address(InetSocketAddress addr) {
this.addr = Optional.of(addr);
return this;
}
public Builder concurrency(byte concurrency) {
this.concurrency = new u8(concurrency);
return this;
}
}
public static final class Config {
protected u16 port;
protected u8 concurrency;
protected Msg msg;
final InetSocketAddress addr;
final u8 concurrency;
final Msg msg;
protected Config(u16 port, u8 concurrency, Msg msg) {
this.port = port;
Config(InetSocketAddress addr, u8 concurrency, Msg msg) {
this.addr = addr;
this.concurrency = concurrency;
this.msg = msg;
}
@ -56,15 +102,15 @@ public class Runtime {
@Override
public boolean equals(Object other) {
return switch (other) {
case Config o -> o.port == this.port &&
case Config o -> o.addr == this.addr &&
o.concurrency == this.concurrency &&
o.msg == this.msg;
default -> false;
};
}
public int port() {
return this.port.intValue();
public InetSocketAddress addr() {
return this.addr();
}
public short concurrency() {
@ -75,43 +121,19 @@ public class Runtime {
return this.msg;
}
public static final class Builder implements BuilderPort {
public final Msg.Builder msg = Msg.builder();
protected Optional<u16> port = Optional.empty();
protected u8 concurrency = Runtime.defaultConfig().concurrency;
protected Builder() {}
public Config build() {
return new Config(this.port.get(), this.concurrency, this.msg.build());
}
public Builder port(short port) {
this.port = Optional.of(new u16(port));
return this;
}
public Builder concurrency(byte concurrency) {
this.concurrency = new u8(concurrency);
return this;
}
}
public static final class Msg {
protected u16 tokenSeed;
protected u16 probingRateBytesPerSecond;
protected Duration multicastResponseLeisure;
protected Con con;
protected Non non;
u16 tokenSeed;
u16 probingRateBytesPerSecond;
Duration multicastResponseLeisure;
Con con;
Non non;
public static Builder builder() {
return new Builder();
}
protected Msg(
Msg(
u16 tokenSeed,
u16 probingRateBytesPerSecond,
Duration multicastResponseLeisure,
@ -159,13 +181,12 @@ public class Runtime {
public static final class Builder {
public final Con.Builder con = Con.builder();
public final Non.Builder non = Non.builder();
protected u16 tokenSeed = Runtime.defaultConfig().msg.tokenSeed;
protected u16 probingRateBytesPerSecond = Runtime.defaultConfig()
Con.Builder con = Con.builder();
Non.Builder non = Non.builder();
u16 tokenSeed = ToadRuntime.defaultConfig().msg.tokenSeed;
u16 probingRateBytesPerSecond = ToadRuntime.defaultConfig()
.msg.probingRateBytesPerSecond;
protected Duration multicastResponseLeisure = Runtime.defaultConfig()
Duration multicastResponseLeisure = ToadRuntime.defaultConfig()
.msg.multicastResponseLeisure;
public Msg build() {
@ -178,6 +199,16 @@ public class Runtime {
);
}
public Builder con(Function<Con.Builder, Con.Builder> f) {
this.con = f.apply(this.con);
return this;
}
public Builder non(Function<Non.Builder, Non.Builder> f) {
this.non = f.apply(this.non);
return this;
}
public Builder tokenSeed(u16 tokenSeed) {
this.tokenSeed = tokenSeed;
return this;
@ -197,20 +228,20 @@ public class Runtime {
return this;
}
protected Builder() {}
Builder() {}
}
public static final class Con {
protected RetryStrategy ackedRetryStrategy;
protected RetryStrategy unackedRetryStrategy;
protected u16 maxAttempts;
final RetryStrategy ackedRetryStrategy;
final RetryStrategy unackedRetryStrategy;
final u16 maxAttempts;
public static Builder builder() {
return new Builder();
}
protected Con(
Con(
RetryStrategy unackedRetryStrategy,
RetryStrategy ackedRetryStrategy,
u16 maxAttempts
@ -244,12 +275,11 @@ public class Runtime {
public static final class Builder {
protected RetryStrategy ackedRetryStrategy = Runtime.defaultConfig()
RetryStrategy ackedRetryStrategy = ToadRuntime.defaultConfig()
.msg.con.ackedRetryStrategy;
protected RetryStrategy unackedRetryStrategy = Runtime.defaultConfig()
RetryStrategy unackedRetryStrategy = ToadRuntime.defaultConfig()
.msg.con.unackedRetryStrategy;
protected u16 maxAttempts = Runtime.defaultConfig()
.msg.con.maxAttempts;
u16 maxAttempts = ToadRuntime.defaultConfig().msg.con.maxAttempts;
public Con build() {
return new Con(
@ -276,20 +306,20 @@ public class Runtime {
return this;
}
protected Builder() {}
Builder() {}
}
}
public static final class Non {
protected RetryStrategy retryStrategy;
protected u16 maxAttempts;
final RetryStrategy retryStrategy;
final u16 maxAttempts;
public static Builder builder() {
return new Builder();
}
protected Non(RetryStrategy retryStrategy, u16 maxAttempts) {
Non(RetryStrategy retryStrategy, u16 maxAttempts) {
this.retryStrategy = retryStrategy;
this.maxAttempts = maxAttempts;
}
@ -313,10 +343,9 @@ public class Runtime {
public static final class Builder {
protected RetryStrategy retryStrategy = Runtime.defaultConfig()
RetryStrategy retryStrategy = ToadRuntime.defaultConfig()
.msg.non.retryStrategy;
protected u16 maxAttempts = Runtime.defaultConfig()
.msg.non.maxAttempts;
u16 maxAttempts = ToadRuntime.defaultConfig().msg.non.maxAttempts;
public Non build() {
return new Non(this.retryStrategy, this.maxAttempts);
@ -332,7 +361,7 @@ public class Runtime {
return this;
}
protected Builder() {}
Builder() {}
}
}
}

View File

@ -1,8 +1,11 @@
package dev.toad.msg;
import java.net.InetSocketAddress;
import java.util.List;
public interface Message {
public InetSocketAddress source();
public int id();
public byte[] token();

View File

@ -1,5 +1,6 @@
package dev.toad.msg;
import java.net.InetSocketAddress;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@ -7,12 +8,13 @@ import java.util.stream.Collectors;
public class MessageOwned implements Message {
private static int id;
private static byte[] token;
private static byte[] payload;
private static MessageCode code;
private static MessageType type;
private static List<MessageOption> opts;
private final InetSocketAddress source;
private final int id;
private final byte[] token;
private final byte[] payload;
private final MessageCode code;
private final MessageType type;
private final List<MessageOption> opts;
public MessageOwned(MessageRef ref) {
this.id = ref.id();
@ -20,6 +22,7 @@ public class MessageOwned implements Message {
this.code = ref.code();
this.type = ref.type();
this.payload = ref.payloadBytes().clone();
this.source = ref.source();
this.opts =
Arrays
@ -29,6 +32,10 @@ public class MessageOwned implements Message {
.collect(Collectors.toList());
}
public InetSocketAddress source() {
return this.source;
}
public int id() {
return this.id;
}

View File

@ -1,8 +1,10 @@
package dev.toad.msg;
import dev.toad.ffi.Ptr;
import java.net.InetSocketAddress;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
/**
* A pointer to a [`toad_msg::Message`](https://docs.rs/toad-msg/latest/toad_msg/struct.Message.html)
@ -15,6 +17,10 @@ public class MessageRef implements Message, AutoCloseable {
private Ptr ptr;
private Optional<InetSocketAddress> source = Optional.empty();
private static native InetSocketAddress source(long addr);
private static native int id(long addr);
private static native byte[] token(long addr);
@ -35,6 +41,14 @@ public class MessageRef implements Message, AutoCloseable {
return new MessageOwned(this);
}
public InetSocketAddress source() {
if (this.source.isEmpty()) {
this.source = Optional.of(this.source(this.ptr.addr()));
}
return this.source.get();
}
public int id() {
return this.id(this.ptr.addr());
}