Class StunObserver
java.lang.Object
org.cloudburstmc.netty.util.nethernet.StunObserver
- All Implemented Interfaces:
AutoCloseable
Asks one STUN server for this host's public address from the ICE port, through a native monitor
on the socket the peers share. Built-in and external signaling both announce what it finds, and
it logs the same way for both.
It keeps the latest mapping for each of the server's addresses, which counts while younger than
the max age and goes as soon as the server reports another. A monitor that fails is closed, its
mappings withdrawn, and opened again after RETRY_DELAY. A server given by name is also
looked up again after a max age without a mapping, and asked on one address once its other one
proves unreachable.
Its owner polls it, one thread at a time; mappings may be read from any.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceA native STUN monitor, or a test's stand-in for one.static interfacestatic final recordOne address of the server as of the last poll.static final recordWhat the native monitor reports for one address of the server: the mapping and its age are empty before the first answer, and libjuice keeps the last mapping through later failures. -
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionStunObserver(InetSocketAddress server, Duration maxAge, int icePort, StunObserver.MonitorFactory factory, LongSupplier nanoTime) -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Stops for good.longepoch()How many monitors were opened, so a reading can be told from one of an earlier monitor.booleanfailed()Whether the monitor failed and has not been opened again yet.mappings()The fresh mappings players elsewhere can reach, as of the last poll.static StunObserver.Monitormonitor(tel.schich.libdatachannel.StunUdpMuxMonitor monitor) Reads a native monitor, every address libjuice resolved the server to.static StunObserver.MonitorFactorynativeMonitors(@Nullable InetAddress bind, int port) Monitors of their own on the ICE port, sharing the mux of peers bound to the same address and port.observed()Every address of the server as of the last poll, including ones carried over a reopen.voidpoll()Reads the monitor, opening it first when it has none and is not waiting to retry.static StunObserver.Readingreading(tel.schich.libdatachannel.StunBinding binding) voidreplace(InetSocketAddress server) Withdraws what was found and asks another server from the next poll.voidreport(boolean covered) Logs what changed since the last call.voidstart()Opens the monitor now rather than on the first poll.static List<InetSocketAddress>withIcePort(Collection<InetSocketAddress> mappings, int icePort) Each mapping, and the same address on the ICE port when the mapping is on another, for a router that moved the port on the way out but forwards the ICE port itself.
-
Field Details
-
RETRY_DELAY
How long a failed monitor waits before it is opened again. -
FIRST_ANSWER
How long a server may stay silent after opening before that is reported.
-
-
Constructor Details
-
StunObserver
public StunObserver(InetSocketAddress server, Duration maxAge, int icePort, StunObserver.MonitorFactory factory, LongSupplier nanoTime) - Parameters:
server- The server, as a numeric address or an unresolved namemaxAge- How long a mapping counts after its last answericePort- The port the monitor and the peers sharefactory- Opens the monitornanoTime- The clock
-
-
Method Details
-
nativeMonitors
Monitors of their own on the ICE port, sharing the mux of peers bound to the same address and port. -
monitor
Reads a native monitor, every address libjuice resolved the server to. -
reading
- Throws:
IllegalStateException- If libjuice reported an address that is not numeric
-
withIcePort
public static List<InetSocketAddress> withIcePort(Collection<InetSocketAddress> mappings, int icePort) Each mapping, and the same address on the ICE port when the mapping is on another, for a router that moved the port on the way out but forwards the ICE port itself. -
start
public void start()Opens the monitor now rather than on the first poll.- Throws:
RuntimeException- If it cannot be opened
-
poll
public void poll()Reads the monitor, opening it first when it has none and is not waiting to retry. -
report
public void report(boolean covered) Logs what changed since the last call.- Parameters:
covered- Whether another server gives this host a public address, which turns a silent or lost server from a warning into a debug line
-
mappings
The fresh mappings players elsewhere can reach, as of the last poll. -
observed
Every address of the server as of the last poll, including ones carried over a reopen. -
epoch
public long epoch()How many monitors were opened, so a reading can be told from one of an earlier monitor. -
failed
public boolean failed()Whether the monitor failed and has not been opened again yet. -
replace
Withdraws what was found and asks another server from the next poll.- Throws:
IllegalStateException- If closedRuntimeException- If the old monitor fails to close again, keeping it and the old server
-
close
public void close()Stops for good. Closing again retries a monitor that failed to close.- Specified by:
closein interfaceAutoCloseable- Throws:
RuntimeException- If the monitor fails to close
-