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.

  • Encoder and Decoder find a class by the name krpc.client.services.<Service>$<Name>, and call its constructor, fromValue, fieldTypes or fromFieldValues by reflection. A wire type names a structure and omits its fields, so the generated class supplies them.

  • addStream and getCall find a method by name and the run-time classes of its arguments. @RPCInfo names the _Types class holding its descriptors. T is unchecked, so a wrong T throws ClassCastException at the assignment.

  • _Types fills maps in initializers of 50 procedures each, as a single switch for a large service passes the JVM’s 64 KB method limit.

  • A service’s exception types register on the connection in its newInstance. The Connection constructor does this for KRPC only.

  • A stream registers before its first update. The return type in _Types shows that a call adds one, or one GetServices call 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 Cleaner or 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 throws ConnectionException, and streams and events end. A second close() 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 waker threads 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 Error off 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.CancellationException and keep the interrupt flag, and the connection stays open. InterruptedException is 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

RemoteException, a RuntimeException. Every exception is unchecked, as in C# and in current Java libraries

Connection, stream, encoding error

ConnectionException, StreamException, EncodingException

Server error with a native type

The JDK type, from lang/java.py in krpctools. The stubs generate no class for it

Server error of a service, or with no mapped type

ServerException or a generated subclass. KRPC.TimeoutException has a generated class, as java.util.concurrent.TimeoutException is checked

Nullable value

The boxed or reference type, with null. A stub names a nullable parameter’s type with Types.nullable, and the encoder throws IllegalArgumentException for a null elsewhere

Unsigned integer

int or long, bit for bit

Tuple

org.javatuples, Unit to Decade

Condition

A plain monitor from getCondition(), held with synchronized

Infinite timeout

Duration.ZERO to connect, Double.POSITIVE_INFINITY to wait

Static method

Class.method(connection, ...), or Class.bind(connection).method(...). addStream and getCall take the method’s other arguments, as the call runs on their connection. The deprecated form, with the connection first, drops that connection unchecked

Flags enumeration

A final class of constants extending RemoteFlags, as an enum cannot hold a combination. One type names it in every signature, and Decoder decodes it through fromValue as any enumeration. An EnumSet would need its own encoding and a different signature

Scoped stream or event

Try-with-resources. close() calls remove(). The language keeps the block’s exception, with the exception from close() suppressed