chore: rename toad-java-glue-rs -> glue, WIP ffi package with uint types

This commit is contained in:
Orion Kindel
2023-04-05 09:16:32 -07:00
parent 4c981c9c9a
commit 42c30b84dd
27 changed files with 327 additions and 136 deletions

View File

@@ -1,53 +1,3 @@
package dev.toad;
import java.time.Duration;
import java.util.Optional;
public abstract sealed class RetryStrategy {
@Override
public boolean equals(Object other) {
return switch (this) {
case Exponential self -> switch (other) {
case Exponential e -> e.initMin == self.initMin &&
e.initMax == self.initMax;
default -> false;
};
case Linear self -> switch (other) {
case Linear e -> e.min == self.min && e.max == self.max;
default -> false;
};
default -> false;
};
}
public final class Exponential extends RetryStrategy {
public final Duration initMin;
public final Duration initMax;
private static native Exponential fromRust(byte[] mem);
private native byte[] toRust();
public Exponential(Duration initMin, Duration initMax) {
this.initMin = initMin;
this.initMax = initMax;
}
}
public final class Linear extends RetryStrategy {
public final Duration min;
public final Duration max;
private static native Linear fromRust(byte[] mem);
private native byte[] toRust();
public Linear(Duration min, Duration max) {
this.min = min;
this.max = max;
}
}
}
public abstract sealed class RetryStrategy permits RetryStrategyDelay, RetryStrategyExponential { }

View File

@@ -0,0 +1,27 @@
package dev.toad;
import java.time.Duration;
import java.util.Optional;
public final class RetryStrategyDelay extends RetryStrategy {
public final Duration min;
public final Duration max;
private static native RetryStrategyDelay fromRust(byte[] mem);
private native byte[] toRust();
public RetryStrategyDelay(Duration min, Duration max) {
this.min = min;
this.max = max;
}
@Override
public boolean equals(Object other) {
return switch (other) {
case RetryStrategyDelay e -> e.min == this.min && e.max == this.max;
default -> false;
};
}
}

View File

@@ -0,0 +1,27 @@
package dev.toad;
import java.time.Duration;
import java.util.Optional;
public final class RetryStrategyExponential extends RetryStrategy {
public final Duration initMin;
public final Duration initMax;
private static native RetryStrategyExponential fromRust(byte[] mem);
private native byte[] toRust();
public RetryStrategyExponential(Duration initMin, Duration initMax) {
this.initMin = initMin;
this.initMax = initMax;
}
@Override
public boolean equals(Object other) {
return switch (other) {
case RetryStrategyExponential e -> e.initMin == this.initMin && e.initMax == this.initMax;
default -> false;
};
}
}

View File

@@ -1,5 +1,6 @@
package dev.toad;
import dev.toad.ffi.*;
import java.time.Duration;
import java.util.Optional;
import java.util.function.Function;
@@ -44,13 +45,13 @@ public final class RuntimeOptions implements Cloneable {
public final class Net implements Cloneable {
private short port;
private short concurrency;
private u16 port;
private u8 concurrency;
private Msg msg;
public Net() {
this.port = 5683;
this.concurrency = 1;
this.port = new u16(5683);
this.concurrency = new u8((short)1);
this.msg = new Msg();
}
@@ -77,12 +78,12 @@ public final class RuntimeOptions implements Cloneable {
return self;
}
public short port() {
return this.port;
public int port() {
return this.port.intValue();
}
public short concurrency() {
return this.concurrency;
return this.concurrency.shortValue();
}
public Msg msg() {
@@ -91,14 +92,14 @@ public final class RuntimeOptions implements Cloneable {
public Net withPort(short port) {
return this.with(self -> {
self.port = port;
self.port = new u16(port);
return self;
});
}
public Net withConcurrency(short conc) {
return this.with(self -> {
self.concurrency = conc;
self.concurrency = new u8(conc);
return self;
});
}
@@ -113,8 +114,8 @@ public final class RuntimeOptions implements Cloneable {
public final class Msg implements Cloneable {
private Optional<Integer> tokenSeed = Optional.empty();
private Optional<Integer> probingRateBytesPerSecond = Optional.empty();
private Optional<u16> tokenSeed = Optional.empty();
private Optional<u16> probingRateBytesPerSecond = Optional.empty();
private Optional<Duration> multicastResponseLeisure = Optional.empty();
private Con con;
private Non non;
@@ -152,11 +153,11 @@ public final class RuntimeOptions implements Cloneable {
}
public Optional<Integer> tokenSeed() {
return this.tokenSeed;
return this.tokenSeed.map(u16 -> u16.intValue());
}
public Optional<Integer> probingRateBytesPerSecond() {
return this.probingRateBytesPerSecond;
return this.probingRateBytesPerSecond.map(u16 -> u16.intValue());
}
public Optional<Duration> multicastResponseLeisure() {
@@ -173,14 +174,14 @@ public final class RuntimeOptions implements Cloneable {
public Msg withTokenSeed(int tokenSeed) {
return this.with(self -> {
self.tokenSeed = Optional.of(tokenSeed);
self.tokenSeed = Optional.of(new u16(tokenSeed));
return self;
});
}
public Msg withProbingRateBytesBerSecond(int bps) {
return this.with(m -> {
m.probingRateBytesPerSecond = Optional.of(bps);
m.probingRateBytesPerSecond = Optional.of(new u16(bps));
return m;
});
}
@@ -210,7 +211,7 @@ public final class RuntimeOptions implements Cloneable {
private Optional<RetryStrategy> ackedRetryStrategy = Optional.empty();
private Optional<RetryStrategy> unackedRetryStrategy = Optional.empty();
private Optional<Integer> maxAttempts = Optional.empty();
private Optional<u16> maxAttempts = Optional.empty();
public Con() {}
@@ -237,7 +238,7 @@ public final class RuntimeOptions implements Cloneable {
}
public Optional<Integer> maxAttempts() {
return this.maxAttempts;
return this.maxAttempts.map(u16 -> u16.intValue());
}
public Con withAckedRetryStrategy(RetryStrategy r) {
@@ -256,7 +257,7 @@ public final class RuntimeOptions implements Cloneable {
public Con withMaxAttempts(int a) {
return this.with(s -> {
s.maxAttempts = Optional.of(a);
s.maxAttempts = Optional.of(new u16(a));
return s;
});
}
@@ -270,7 +271,7 @@ public final class RuntimeOptions implements Cloneable {
public final class Non implements Cloneable {
private Optional<RetryStrategy> retryStrategy = Optional.empty();
private Optional<Integer> maxAttempts = Optional.empty();
private Optional<u16> maxAttempts = Optional.empty();
public Non() {}
@@ -292,7 +293,7 @@ public final class RuntimeOptions implements Cloneable {
}
public Optional<Integer> maxAttempts() {
return this.maxAttempts;
return this.maxAttempts.map(u16 -> u16.intValue());
}
public Non withRetryStrategy(RetryStrategy r) {
@@ -304,7 +305,7 @@ public final class RuntimeOptions implements Cloneable {
public Non withMaxAttempts(int a) {
return this.with(s -> {
s.maxAttempts = Optional.of(a);
s.maxAttempts = Optional.of(new u16(a));
return s;
});
}

View File

@@ -0,0 +1,15 @@
package dev.toad.ffi;
public final class u16 {
public static final int MAX = (int)(Math.pow(2, 16) - 1);
private final int l;
public u16(int l) {
uint.assertWithinRange(this.MAX, l);
this.l = l;
}
public int intValue() {
return this.l;
}
}

View File

@@ -0,0 +1,15 @@
package dev.toad.ffi;
public final class u32 {
public static final long MAX = (long)(Math.pow(2, 32) - 1);
private final long l;
public u32(long l) {
uint.assertWithinRange(this.MAX, l);
this.l = l;
}
public long longValue() {
return this.l;
}
}

View File

@@ -0,0 +1,17 @@
package dev.toad.ffi;
public final class u64 {
public static final double MAX = Math.pow(2, 64) - 1;
private final double l;
public u64(double l) {
uint.assertWithinRange(this.MAX, l);
uint.assertNatural(l);
this.l = l;
}
public double doubleValue() {
return this.l;
}
}

View File

@@ -0,0 +1,15 @@
package dev.toad.ffi;
public final class u8 {
public static final short MAX = (short)(Math.pow(2, 8) - 1);
private final short l;
public u8(short l) {
uint.assertWithinRange(this.MAX, l);
this.l = l;
}
public short shortValue() {
return this.l;
}
}

View File

@@ -0,0 +1,15 @@
package dev.toad.ffi;
public class uint {
public static void assertWithinRange(double max, double n) {
if (n < 0 || n > max) {
throw new IllegalArgumentException(String.format("% must be between 0 and %", n, max));
}
}
public static void assertNatural(double n) {
if (n % 1 > 0.0) {
throw new IllegalArgumentException(String.format("% must be a whole integer", n));
}
}
}