feat: rework Runtime.Config.Builder to be Runtime.Builder, add explicit bind to address
This commit is contained in:
parent
a8aa0f8bf7
commit
ea59f92cb5
19
build.sbt
19
build.sbt
@ -14,13 +14,6 @@ val path = settingKey[Map[String, String]]("paths")
|
|||||||
fork := true
|
fork := true
|
||||||
|
|
||||||
javaOptions += "--enable-preview"
|
javaOptions += "--enable-preview"
|
||||||
javacOptions ++= Seq(
|
|
||||||
"--enable-preview",
|
|
||||||
"--release",
|
|
||||||
"20",
|
|
||||||
"-Xlint:unchecked",
|
|
||||||
"-Xlint:deprecation"
|
|
||||||
)
|
|
||||||
|
|
||||||
lazy val root = project
|
lazy val root = project
|
||||||
.in(file("."))
|
.in(file("."))
|
||||||
@ -33,6 +26,18 @@ lazy val root = project
|
|||||||
"java.sources" -> baseDirectory.value.toGlob / "src" / "main" / "java" / ** / "*.java",
|
"java.sources" -> baseDirectory.value.toGlob / "src" / "main" / "java" / ** / "*.java",
|
||||||
"glue.sources" -> baseDirectory.value.toGlob / "glue" / "src" / ** / "*.rs"
|
"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(
|
path := Map(
|
||||||
"glue.base" -> (baseDirectory.value / "glue").toString,
|
"glue.base" -> (baseDirectory.value / "glue").toString,
|
||||||
"glue.target" -> (baseDirectory.value / "target" / "glue" / "debug").toString,
|
"glue.target" -> (baseDirectory.value / "target" / "glue" / "debug").toString,
|
||||||
|
2
glue/Cargo.lock
generated
2
glue/Cargo.lock
generated
@ -580,7 +580,7 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "toad-jni"
|
name = "toad-jni"
|
||||||
version = "0.5.1"
|
version = "0.6.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"jni",
|
"jni",
|
||||||
"toad-array 0.5.0",
|
"toad-array 0.5.0",
|
||||||
|
@ -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 no_std_net::SocketAddr;
|
||||||
use toad::config::Config;
|
use toad::config::Config;
|
||||||
use toad::net::Addrd;
|
use toad::net::Addrd;
|
||||||
use toad::platform::Platform;
|
use toad::platform::Platform;
|
||||||
use toad_jni::java::lang::System;
|
|
||||||
use toad_jni::java::{self, Object, Signature};
|
use toad_jni::java::{self, Object, Signature};
|
||||||
use toad_msg::alloc::Message;
|
use toad_msg::alloc::Message;
|
||||||
use toad_msg::{Code, Id, Token, Type};
|
use toad_msg::{Code, Id, Token, Type};
|
||||||
|
|
||||||
use crate::message_ref::MessageRef;
|
|
||||||
use crate::message_type::MessageType;
|
use crate::message_type::MessageType;
|
||||||
use crate::runtime::Runtime;
|
use crate::runtime::Runtime;
|
||||||
use crate::runtime_config::RuntimeConfig;
|
use crate::runtime_config::RuntimeConfig;
|
||||||
@ -29,8 +22,11 @@ fn init() -> State {
|
|||||||
let mut _env = crate::test::init();
|
let mut _env = crate::test::init();
|
||||||
let env = &mut _env;
|
let env = &mut _env;
|
||||||
|
|
||||||
let cfg = RuntimeConfig::new(env, Config::default(), 5683);
|
let cfg = RuntimeConfig::new(env,
|
||||||
let runtime = Runtime::get_or_init(env, cfg);
|
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();
|
let client = crate::Runtime::try_new("0.0.0.0:5684", Default::default()).unwrap();
|
||||||
|
|
||||||
State { runtime,
|
State { runtime,
|
||||||
|
@ -42,6 +42,7 @@ pub mod e2e;
|
|||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
pub mod test {
|
pub mod test {
|
||||||
|
use std::net::{Ipv4Addr, SocketAddr};
|
||||||
use std::sync::Once;
|
use std::sync::Once;
|
||||||
|
|
||||||
use jni::{InitArgsBuilder, JavaVM};
|
use jni::{InitArgsBuilder, JavaVM};
|
||||||
@ -81,7 +82,9 @@ pub mod test {
|
|||||||
let mut e = init();
|
let mut e = init();
|
||||||
let e = &mut e;
|
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());
|
assert_eq!(r.to_toad(e), Config::default());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3,7 +3,7 @@ use jni::sys::jobject;
|
|||||||
use toad::platform::Platform;
|
use toad::platform::Platform;
|
||||||
use toad_jni::java::{self, Object};
|
use toad_jni::java::{self, Object};
|
||||||
|
|
||||||
use crate::mem::SharedMemoryRegion;
|
use crate::mem::{Shared, SharedMemoryRegion};
|
||||||
use crate::message_ref::MessageRef;
|
use crate::message_ref::MessageRef;
|
||||||
use crate::runtime_config::RuntimeConfig;
|
use crate::runtime_config::RuntimeConfig;
|
||||||
use crate::Runtime as ToadRuntime;
|
use crate::Runtime as ToadRuntime;
|
||||||
@ -11,10 +11,9 @@ use crate::Runtime as ToadRuntime;
|
|||||||
pub struct Runtime(java::lang::Object);
|
pub struct Runtime(java::lang::Object);
|
||||||
|
|
||||||
impl Runtime {
|
impl Runtime {
|
||||||
pub fn get_or_init(e: &mut java::Env, cfg: RuntimeConfig) -> Self {
|
pub fn new(e: &mut java::Env, cfg: RuntimeConfig) -> Self {
|
||||||
static GET_OR_INIT: java::StaticMethod<Runtime, fn(RuntimeConfig) -> Runtime> =
|
static CTOR: java::Constructor<Runtime, fn(RuntimeConfig)> = java::Constructor::new();
|
||||||
java::StaticMethod::new("getOrInit");
|
CTOR.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> {
|
||||||
@ -23,25 +22,22 @@ impl Runtime {
|
|||||||
POLL_REQ.invoke(e, self).to_option(e)
|
POLL_REQ.invoke(e, self).to_option(e)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn addr(&self, e: &mut java::Env) -> i64 {
|
pub fn config(&self, e: &mut java::Env) -> RuntimeConfig {
|
||||||
static ADDR: java::Field<Runtime, i64> = java::Field::new("addr");
|
static CONFIG: java::Method<Runtime, fn() -> RuntimeConfig> = java::Method::new("config");
|
||||||
ADDR.get(e, self)
|
CONFIG.invoke(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()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn init_impl(e: &mut java::Env, cfg: RuntimeConfig) -> i64 {
|
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 }
|
unsafe { crate::mem::Shared::init(r).addr() as i64 }
|
||||||
}
|
}
|
||||||
|
|
||||||
fn poll_req_impl(&self, e: &mut java::Env) -> java::util::Optional<MessageRef> {
|
fn poll_req_impl(e: &mut java::Env, addr: i64) -> java::util::Optional<MessageRef> {
|
||||||
match self.ref_(e).poll_req() {
|
match unsafe {
|
||||||
|
Shared::deref::<crate::Runtime>(/* TODO */ 0, addr).as_ref()
|
||||||
|
.unwrap()
|
||||||
|
}.poll_req()
|
||||||
|
{
|
||||||
| Ok(req) => {
|
| Ok(req) => {
|
||||||
let mr = MessageRef::new(e, req.unwrap().into());
|
let mr = MessageRef::new(e, req.unwrap().into());
|
||||||
java::util::Optional::<MessageRef>::of(e, mr)
|
java::util::Optional::<MessageRef>::of(e, mr)
|
||||||
@ -58,14 +54,14 @@ impl Runtime {
|
|||||||
java::object_newtype!(Runtime);
|
java::object_newtype!(Runtime);
|
||||||
|
|
||||||
impl java::Class for Runtime {
|
impl java::Class for Runtime {
|
||||||
const PATH: &'static str = package!(dev.toad.Runtime);
|
const PATH: &'static str = package!(dev.toad.ToadRuntime);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub extern "system" fn Java_dev_toad_Runtime_init<'local>(mut e: java::Env<'local>,
|
pub extern "system" fn Java_dev_toad_ToadRuntime_init<'local>(mut e: java::Env<'local>,
|
||||||
_: JClass<'local>,
|
_: JClass<'local>,
|
||||||
cfg: JObject<'local>)
|
cfg: JObject<'local>)
|
||||||
-> i64 {
|
-> i64 {
|
||||||
let e = &mut e;
|
let e = &mut e;
|
||||||
let cfg = java::lang::Object::from_local(e, cfg).upcast_to::<RuntimeConfig>(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]
|
#[no_mangle]
|
||||||
pub extern "system" fn Java_dev_toad_Runtime_pollReq<'local>(mut e: java::Env<'local>,
|
pub extern "system" fn Java_dev_toad_ToadRuntime_pollReq<'local>(mut e: java::Env<'local>,
|
||||||
runtime: JObject<'local>)
|
_: JClass<'local>,
|
||||||
-> jobject {
|
addr: i64)
|
||||||
|
-> jobject {
|
||||||
let e = &mut e;
|
let e = &mut e;
|
||||||
java::lang::Object::from_local(e, runtime).upcast_to::<Runtime>(e)
|
Runtime::poll_req_impl(e, addr).yield_to_java(e)
|
||||||
.poll_req_impl(e)
|
|
||||||
.yield_to_java(e)
|
|
||||||
}
|
}
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
|
use std::net::{Ipv4Addr, SocketAddr};
|
||||||
|
|
||||||
use jni::objects::JClass;
|
use jni::objects::JClass;
|
||||||
use jni::sys::jobject;
|
use jni::sys::jobject;
|
||||||
use toad::config::{self, BytesPerSecond, Config};
|
use toad::config::{self, BytesPerSecond, Config};
|
||||||
@ -12,13 +14,14 @@ pub struct RuntimeConfig(java::lang::Object);
|
|||||||
|
|
||||||
java::object_newtype!(RuntimeConfig);
|
java::object_newtype!(RuntimeConfig);
|
||||||
impl java::Class for 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 {
|
impl RuntimeConfig {
|
||||||
pub fn port(&self, e: &mut java::Env) -> u16 {
|
pub fn addr(&self, e: &mut java::Env) -> SocketAddr {
|
||||||
static RUNTIME_CONFIG_PORT: java::Field<RuntimeConfig, uint::u16> = java::Field::new("port");
|
static ADDRESS: java::Field<RuntimeConfig, java::net::InetSocketAddress> =
|
||||||
RUNTIME_CONFIG_PORT.get(e, self).to_rust(e)
|
java::Field::new("addr");
|
||||||
|
ADDRESS.get(e, self).to_std(e)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn concurrency(&self, e: &mut java::Env) -> u8 {
|
pub fn concurrency(&self, e: &mut java::Env) -> u8 {
|
||||||
@ -32,8 +35,8 @@ impl RuntimeConfig {
|
|||||||
RUNTIME_CONFIG_MSG.invoke(e, self)
|
RUNTIME_CONFIG_MSG.invoke(e, self)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn new(e: &mut java::Env, c: Config, port: u16) -> Self {
|
pub fn new(e: &mut java::Env, c: Config, addr: SocketAddr) -> Self {
|
||||||
static CTOR: java::Constructor<RuntimeConfig, fn(uint::u16, uint::u8, Msg)> =
|
static CTOR: java::Constructor<RuntimeConfig, fn(java::net::InetSocketAddress, uint::u8, Msg)> =
|
||||||
java::Constructor::new();
|
java::Constructor::new();
|
||||||
|
|
||||||
let con = Con::new(e,
|
let con = Con::new(e,
|
||||||
@ -48,10 +51,11 @@ impl RuntimeConfig {
|
|||||||
con,
|
con,
|
||||||
non);
|
non);
|
||||||
|
|
||||||
let port = uint::u16::from_rust(e, port);
|
|
||||||
let concurrency = uint::u8::from_rust(e, c.max_concurrent_requests);
|
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
|
jcfg
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -83,7 +87,7 @@ pub struct Msg(java::lang::Object);
|
|||||||
java::object_newtype!(Msg);
|
java::object_newtype!(Msg);
|
||||||
|
|
||||||
impl java::Class for 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 {
|
impl Msg {
|
||||||
@ -143,7 +147,7 @@ pub struct Con(java::lang::Object);
|
|||||||
|
|
||||||
java::object_newtype!(Con);
|
java::object_newtype!(Con);
|
||||||
impl java::Class for 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 {
|
impl Con {
|
||||||
@ -182,7 +186,7 @@ pub struct Non(java::lang::Object);
|
|||||||
|
|
||||||
java::object_newtype!(Non);
|
java::object_newtype!(Non);
|
||||||
impl java::Class for 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 {
|
impl Non {
|
||||||
@ -209,5 +213,7 @@ impl Non {
|
|||||||
pub extern "system" fn Java_dev_toad_Runtime_defaultConfigImpl<'local>(mut env: java::Env<'local>,
|
pub extern "system" fn Java_dev_toad_Runtime_defaultConfigImpl<'local>(mut env: java::Env<'local>,
|
||||||
_: JClass<'local>)
|
_: JClass<'local>)
|
||||||
-> jobject {
|
-> 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)
|
||||||
}
|
}
|
||||||
|
@ -2,53 +2,99 @@ package dev.toad;
|
|||||||
|
|
||||||
import dev.toad.ffi.*;
|
import dev.toad.ffi.*;
|
||||||
import dev.toad.msg.MessageRef;
|
import dev.toad.msg.MessageRef;
|
||||||
|
import java.net.InetSocketAddress;
|
||||||
import java.time.Duration;
|
import java.time.Duration;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
import java.util.function.Function;
|
||||||
|
|
||||||
interface BuilderPort {
|
public class ToadRuntime {
|
||||||
Runtime.Config.Builder port(short port);
|
|
||||||
}
|
|
||||||
|
|
||||||
public class Runtime {
|
static native Config defaultConfigImpl();
|
||||||
|
|
||||||
protected static native Config defaultConfigImpl();
|
static Config defaultConfig = null;
|
||||||
|
|
||||||
protected static Config defaultConfig = null;
|
static Config defaultConfig() {
|
||||||
|
if (ToadRuntime.defaultConfig == null) {
|
||||||
protected static Config defaultConfig() {
|
ToadRuntime.defaultConfig = ToadRuntime.defaultConfigImpl();
|
||||||
if (Runtime.defaultConfig == null) {
|
|
||||||
Runtime.defaultConfig = Runtime.defaultConfigImpl();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return Runtime.defaultConfig;
|
return ToadRuntime.defaultConfig;
|
||||||
}
|
}
|
||||||
|
|
||||||
static {
|
static {
|
||||||
System.loadLibrary("toad_java_glue");
|
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 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) {
|
Optional<MessageRef> pollReq() {
|
||||||
return new Runtime(o);
|
return ToadRuntime.pollReq(this.ptr.addr());
|
||||||
}
|
}
|
||||||
|
|
||||||
public Runtime(Config o) {
|
public static BuilderRequiresBindToAddress builder() {
|
||||||
this.addr = Runtime.init(o);
|
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 {
|
public static final class Config {
|
||||||
|
|
||||||
protected u16 port;
|
final InetSocketAddress addr;
|
||||||
protected u8 concurrency;
|
final u8 concurrency;
|
||||||
protected Msg msg;
|
final Msg msg;
|
||||||
|
|
||||||
protected Config(u16 port, u8 concurrency, Msg msg) {
|
Config(InetSocketAddress addr, u8 concurrency, Msg msg) {
|
||||||
this.port = port;
|
this.addr = addr;
|
||||||
this.concurrency = concurrency;
|
this.concurrency = concurrency;
|
||||||
this.msg = msg;
|
this.msg = msg;
|
||||||
}
|
}
|
||||||
@ -56,15 +102,15 @@ public class Runtime {
|
|||||||
@Override
|
@Override
|
||||||
public boolean equals(Object other) {
|
public boolean equals(Object other) {
|
||||||
return switch (other) {
|
return switch (other) {
|
||||||
case Config o -> o.port == this.port &&
|
case Config o -> o.addr == this.addr &&
|
||||||
o.concurrency == this.concurrency &&
|
o.concurrency == this.concurrency &&
|
||||||
o.msg == this.msg;
|
o.msg == this.msg;
|
||||||
default -> false;
|
default -> false;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
public int port() {
|
public InetSocketAddress addr() {
|
||||||
return this.port.intValue();
|
return this.addr();
|
||||||
}
|
}
|
||||||
|
|
||||||
public short concurrency() {
|
public short concurrency() {
|
||||||
@ -75,43 +121,19 @@ public class Runtime {
|
|||||||
return this.msg;
|
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 {
|
public static final class Msg {
|
||||||
|
|
||||||
protected u16 tokenSeed;
|
u16 tokenSeed;
|
||||||
protected u16 probingRateBytesPerSecond;
|
u16 probingRateBytesPerSecond;
|
||||||
protected Duration multicastResponseLeisure;
|
Duration multicastResponseLeisure;
|
||||||
protected Con con;
|
Con con;
|
||||||
protected Non non;
|
Non non;
|
||||||
|
|
||||||
public static Builder builder() {
|
public static Builder builder() {
|
||||||
return new Builder();
|
return new Builder();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected Msg(
|
Msg(
|
||||||
u16 tokenSeed,
|
u16 tokenSeed,
|
||||||
u16 probingRateBytesPerSecond,
|
u16 probingRateBytesPerSecond,
|
||||||
Duration multicastResponseLeisure,
|
Duration multicastResponseLeisure,
|
||||||
@ -159,13 +181,12 @@ public class Runtime {
|
|||||||
|
|
||||||
public static final class Builder {
|
public static final class Builder {
|
||||||
|
|
||||||
public final Con.Builder con = Con.builder();
|
Con.Builder con = Con.builder();
|
||||||
public final Non.Builder non = Non.builder();
|
Non.Builder non = Non.builder();
|
||||||
|
u16 tokenSeed = ToadRuntime.defaultConfig().msg.tokenSeed;
|
||||||
protected u16 tokenSeed = Runtime.defaultConfig().msg.tokenSeed;
|
u16 probingRateBytesPerSecond = ToadRuntime.defaultConfig()
|
||||||
protected u16 probingRateBytesPerSecond = Runtime.defaultConfig()
|
|
||||||
.msg.probingRateBytesPerSecond;
|
.msg.probingRateBytesPerSecond;
|
||||||
protected Duration multicastResponseLeisure = Runtime.defaultConfig()
|
Duration multicastResponseLeisure = ToadRuntime.defaultConfig()
|
||||||
.msg.multicastResponseLeisure;
|
.msg.multicastResponseLeisure;
|
||||||
|
|
||||||
public Msg build() {
|
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) {
|
public Builder tokenSeed(u16 tokenSeed) {
|
||||||
this.tokenSeed = tokenSeed;
|
this.tokenSeed = tokenSeed;
|
||||||
return this;
|
return this;
|
||||||
@ -197,20 +228,20 @@ public class Runtime {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected Builder() {}
|
Builder() {}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static final class Con {
|
public static final class Con {
|
||||||
|
|
||||||
protected RetryStrategy ackedRetryStrategy;
|
final RetryStrategy ackedRetryStrategy;
|
||||||
protected RetryStrategy unackedRetryStrategy;
|
final RetryStrategy unackedRetryStrategy;
|
||||||
protected u16 maxAttempts;
|
final u16 maxAttempts;
|
||||||
|
|
||||||
public static Builder builder() {
|
public static Builder builder() {
|
||||||
return new Builder();
|
return new Builder();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected Con(
|
Con(
|
||||||
RetryStrategy unackedRetryStrategy,
|
RetryStrategy unackedRetryStrategy,
|
||||||
RetryStrategy ackedRetryStrategy,
|
RetryStrategy ackedRetryStrategy,
|
||||||
u16 maxAttempts
|
u16 maxAttempts
|
||||||
@ -244,12 +275,11 @@ public class Runtime {
|
|||||||
|
|
||||||
public static final class Builder {
|
public static final class Builder {
|
||||||
|
|
||||||
protected RetryStrategy ackedRetryStrategy = Runtime.defaultConfig()
|
RetryStrategy ackedRetryStrategy = ToadRuntime.defaultConfig()
|
||||||
.msg.con.ackedRetryStrategy;
|
.msg.con.ackedRetryStrategy;
|
||||||
protected RetryStrategy unackedRetryStrategy = Runtime.defaultConfig()
|
RetryStrategy unackedRetryStrategy = ToadRuntime.defaultConfig()
|
||||||
.msg.con.unackedRetryStrategy;
|
.msg.con.unackedRetryStrategy;
|
||||||
protected u16 maxAttempts = Runtime.defaultConfig()
|
u16 maxAttempts = ToadRuntime.defaultConfig().msg.con.maxAttempts;
|
||||||
.msg.con.maxAttempts;
|
|
||||||
|
|
||||||
public Con build() {
|
public Con build() {
|
||||||
return new Con(
|
return new Con(
|
||||||
@ -276,20 +306,20 @@ public class Runtime {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected Builder() {}
|
Builder() {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static final class Non {
|
public static final class Non {
|
||||||
|
|
||||||
protected RetryStrategy retryStrategy;
|
final RetryStrategy retryStrategy;
|
||||||
protected u16 maxAttempts;
|
final u16 maxAttempts;
|
||||||
|
|
||||||
public static Builder builder() {
|
public static Builder builder() {
|
||||||
return new Builder();
|
return new Builder();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected Non(RetryStrategy retryStrategy, u16 maxAttempts) {
|
Non(RetryStrategy retryStrategy, u16 maxAttempts) {
|
||||||
this.retryStrategy = retryStrategy;
|
this.retryStrategy = retryStrategy;
|
||||||
this.maxAttempts = maxAttempts;
|
this.maxAttempts = maxAttempts;
|
||||||
}
|
}
|
||||||
@ -313,10 +343,9 @@ public class Runtime {
|
|||||||
|
|
||||||
public static final class Builder {
|
public static final class Builder {
|
||||||
|
|
||||||
protected RetryStrategy retryStrategy = Runtime.defaultConfig()
|
RetryStrategy retryStrategy = ToadRuntime.defaultConfig()
|
||||||
.msg.non.retryStrategy;
|
.msg.non.retryStrategy;
|
||||||
protected u16 maxAttempts = Runtime.defaultConfig()
|
u16 maxAttempts = ToadRuntime.defaultConfig().msg.non.maxAttempts;
|
||||||
.msg.non.maxAttempts;
|
|
||||||
|
|
||||||
public Non build() {
|
public Non build() {
|
||||||
return new Non(this.retryStrategy, this.maxAttempts);
|
return new Non(this.retryStrategy, this.maxAttempts);
|
||||||
@ -332,7 +361,7 @@ public class Runtime {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected Builder() {}
|
Builder() {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,8 +1,11 @@
|
|||||||
package dev.toad.msg;
|
package dev.toad.msg;
|
||||||
|
|
||||||
|
import java.net.InetSocketAddress;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public interface Message {
|
public interface Message {
|
||||||
|
public InetSocketAddress source();
|
||||||
|
|
||||||
public int id();
|
public int id();
|
||||||
|
|
||||||
public byte[] token();
|
public byte[] token();
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
package dev.toad.msg;
|
package dev.toad.msg;
|
||||||
|
|
||||||
|
import java.net.InetSocketAddress;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@ -7,12 +8,13 @@ import java.util.stream.Collectors;
|
|||||||
|
|
||||||
public class MessageOwned implements Message {
|
public class MessageOwned implements Message {
|
||||||
|
|
||||||
private static int id;
|
private final InetSocketAddress source;
|
||||||
private static byte[] token;
|
private final int id;
|
||||||
private static byte[] payload;
|
private final byte[] token;
|
||||||
private static MessageCode code;
|
private final byte[] payload;
|
||||||
private static MessageType type;
|
private final MessageCode code;
|
||||||
private static List<MessageOption> opts;
|
private final MessageType type;
|
||||||
|
private final List<MessageOption> opts;
|
||||||
|
|
||||||
public MessageOwned(MessageRef ref) {
|
public MessageOwned(MessageRef ref) {
|
||||||
this.id = ref.id();
|
this.id = ref.id();
|
||||||
@ -20,6 +22,7 @@ public class MessageOwned implements Message {
|
|||||||
this.code = ref.code();
|
this.code = ref.code();
|
||||||
this.type = ref.type();
|
this.type = ref.type();
|
||||||
this.payload = ref.payloadBytes().clone();
|
this.payload = ref.payloadBytes().clone();
|
||||||
|
this.source = ref.source();
|
||||||
|
|
||||||
this.opts =
|
this.opts =
|
||||||
Arrays
|
Arrays
|
||||||
@ -29,6 +32,10 @@ public class MessageOwned implements Message {
|
|||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public InetSocketAddress source() {
|
||||||
|
return this.source;
|
||||||
|
}
|
||||||
|
|
||||||
public int id() {
|
public int id() {
|
||||||
return this.id;
|
return this.id;
|
||||||
}
|
}
|
||||||
|
@ -1,8 +1,10 @@
|
|||||||
package dev.toad.msg;
|
package dev.toad.msg;
|
||||||
|
|
||||||
import dev.toad.ffi.Ptr;
|
import dev.toad.ffi.Ptr;
|
||||||
|
import java.net.InetSocketAddress;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
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)
|
* 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 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 int id(long addr);
|
||||||
|
|
||||||
private static native byte[] token(long addr);
|
private static native byte[] token(long addr);
|
||||||
@ -35,6 +41,14 @@ public class MessageRef implements Message, AutoCloseable {
|
|||||||
return new MessageOwned(this);
|
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() {
|
public int id() {
|
||||||
return this.id(this.ptr.addr());
|
return this.id(this.ptr.addr());
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user