Java Client Design#
The internal design of the Java client. This page builds on the shared client design and the client specification. Usage is in
the Java guide. Paths are relative to client/java/.
Generics and Generated Stubs#
Erasure removes the element type of a List<Integer> at run time. Generated stubs therefore pass
a krpc.schema.KRPC.Type descriptor to Encoder.encode and Decoder.decode, and cast the
result.
clientgen writes one class per service in the fixed package krpc.client.services, and the
jar includes the stubs of the shipped services.
EncoderandDecoderfind a class by the namekrpc.client.services.<Service>$<Name>, and call its constructor,fromValue,fieldTypesorfromFieldValuesby reflection. A wire type names a structure and omits its fields, so the generated class supplies them.addStreamandgetCallfind a method by name and the run-time classes of its arguments.@RPCInfonames the_Typesclass holding its descriptors.Tis unchecked, so a wrongTthrowsClassCastExceptionat the assignment._Typesfills maps in initializers of 50 procedures each, as a singleswitchfor a large service passes the JVM’s 64 KB method limit.A service’s exception types register on the connection in its
newInstance. TheConnectionconstructor does this forKRPConly.A stream registers before its first update. The return type in
_Typesshows that a call adds one, or oneGetServicescall if the stubs are not loaded.
Ownership#
krpc.client.Connection owns the socket and is AutoCloseable. Services, objects, streams and
events each hold the connection strongly, and none of them owns it:
SpaceCenter.Vessel vessel;
try (Connection connection = Connection.newInstance()) {
vessel = SpaceCenter.newInstance(connection).getActiveVessel();
}
vessel.getMET(); // Throws ConnectionException
References#
The connection holds its Session and StreamManager, which also hold each other. The
Session holds the Transport.
The objects the program keeps also hold the connection. A Service, RemoteObject or
Stream holds the Connection. An Event holds its Stream, and a Stream holds its
StreamImpl. A StreamImpl holds the Connection and the StreamManager.
The threads keep the connection alive. The reader and dispatcher threads hold the Session, which
holds the StreamManager, which holds the Connection. An unclosed connection is therefore
never collected, and the cycles matter only once the threads end.
The stream manager holds each open StreamImpl strongly. Its weak set registered holds every
registered StreamImpl. The set finds a stream whose condition the closing thread holds,
including one the client removed, and pins no removed stream.
A callback pins what it captures until its stream ends.
Lifetime#
The connection closes on
close(), on a connection failure, or when the JVM exits.The client has no
Cleaneror finalizer. An unclosed connection holds two threads, three selectors and the socket until the JVM exits, and does not delay that exit.After
close(), a call throwsConnectionException, and streams and events end. A secondclose()does nothing.A connection failure closes the channel and selectors and ends both threads, with no
close().A stream runs until
remove(), a server ending or the end of the connection. Dropping every handle leaves the stream and its callbacks running.
Threads#
The threads follow the shared design, and are named
krpc-reader and krpc-dispatcher.
kRPC stream wakerthreads wake conditions during a close, as a monitor has no timed lock. A waker blocked on a held condition stays until it is released, and another takes over after 10 ms.Lock order is condition, update lock, state lock. The reading thread takes only the last two, so a held condition never stops a read.
Plain reads see a value once it is stored under the update lock. A thread holding the condition sees a frozen value until the dispatcher announces the next one.
A callback’s exception goes to the thread’s uncaught exception handler.
An
Erroroff the dispatcher propagates from the add, the remove or the close.A callback waiting for a condition the closing thread holds finishes after that thread releases it.
Interrupts and Transports#
TCP/IP and local sockets both use SocketChannel. java.net.Socket has no unix domain support,
so a local socket needs a channel.
The client compiles for and requires Java 17. UnixDomainSocketAddress arrived in Java 16, and 17
is the oldest release with a registered Bazel toolchain.
The channel turns non-blocking at Session.start, as a blocking channel closes when its thread is
interrupted. Selector waits are capped at 1 s, as closing the channel does not wake a selector.
An interrupted call abandons its id, and an interrupted wait ends. Both throw
java.util.concurrent.CancellationExceptionand keep the interrupt flag, and the connection stays open.InterruptedExceptionis checked, so the unchecked API cannot throw it.A write holds an interrupt back until it ends, as a partial message breaks the framing.
Mapping the Spec to Java#
Spec |
Java |
|---|---|
Remote error |
|
Connection, stream, encoding error |
|
Server error with a native type |
The JDK type, from |
Server error of a service, or with no mapped type |
|
Nullable value |
The boxed or reference type, with |
Unsigned integer |
|
Tuple |
|
Condition |
A plain monitor from |
Infinite timeout |
|
Static method |
|
Flags enumeration |
A final class of constants extending |
Scoped stream or event |
Try-with-resources. |