Java Client

This client provides a Java API for interacting with a kRPC server. A jar containing the krpc.client package can be downloaded from GitHub. It requires Java version 1.8.

Using the Library

The kRPC client library depends on the protobuf and javatuples libraries. A prebuilt jar for protobuf is available via Maven.

The following example program connects to the server, queries it for its version and prints it out:

import krpc.client.Connection;
import krpc.client.RPCException;
import krpc.client.services.KRPC;

import java.io.IOException;

public class Basic {
  public static void main(String[] args) throws IOException, RPCException {
    try (Connection connection = Connection.newInstance()) {
      KRPC krpc = KRPC.newInstance(connection);
      System.out.println("Connected to kRPC version " + krpc.getStatus().getVersion());
    }
  }
}

To compile this program using javac on the command line, save the source as Example.java and run the following:

javac -cp krpc-java-0.5.2.jar:protobuf-java-3.4.0.jar:javatuples-1.2.jar Example.java

Note that you may need to change the paths to the JAR files.

Connecting to the Server

To connect to a server, call Connection.newInstance() which returns a connection object. All interaction with the server is done via this object. When constructed without any arguments, it will connect to the local machine on the default port numbers. You can specify different connection settings, and also a descriptive name for the connection, as follows:

import krpc.client.Connection;
import krpc.client.RPCException;
import krpc.client.services.KRPC;

import java.io.IOException;

public class Connecting {
  public static void main(String[] args) throws IOException, RPCException {
    try (Connection connection = Connection.newInstance("Remote example", "my.domain.name", 1000, 1001)) {
      System.out.println(KRPC.newInstance(connection).getStatus().getVersion());
    }
  }
}

Calling Remote Procedures

The kRPC server provides procedures that a client can run. These procedures are arranged in groups called services to keep things organized. The functionality for the services are defined in the package krpc.client.services. For example, all of the functionality provided by the SpaceCenter service is contained in the class krpc.client.services.SpaceCenter.

To interact with a service, you must first instantiate it. You can then call its methods and properties to invoke remote procedures. The following example demonstrates how to do this. It instantiates the SpaceCenter service and calls krpc.client.services.SpaceCenter.SpaceCenter.getActiveVessel() to get an object representing the active vessel (of type krpc.client.services.SpaceCenter.Vessel). It sets the name of the vessel and then prints out its altitude:

import krpc.client.Connection;
import krpc.client.RPCException;
import krpc.client.services.SpaceCenter;
import krpc.client.services.SpaceCenter.Vessel;

import java.io.IOException;

public class RemoteProcedures {
  public static void main(String[] args) throws IOException, RPCException {
    try (Connection connection = Connection.newInstance("Vessel Name")) {
      SpaceCenter spaceCenter = SpaceCenter.newInstance(connection);
      Vessel vessel = spaceCenter.getActiveVessel();
      System.out.println(vessel.getName());
    }
  }
}

Streaming Data from the Server

A common use case for kRPC is to continuously extract data from the game. The naive approach to do this would be to repeatedly call a remote procedure, such as in the following which repeatedly prints the position of the active vessel:

import krpc.client.Connection;
import krpc.client.RPCException;
import krpc.client.services.KRPC;
import krpc.client.services.SpaceCenter;
import krpc.client.services.SpaceCenter.ReferenceFrame;
import krpc.client.services.SpaceCenter.Vessel;

import java.io.IOException;

public class Streaming1 {
  public static void main(String[] args) throws IOException, RPCException {
    try (Connection connection = Connection.newInstance()) {
      SpaceCenter spaceCenter = SpaceCenter.newInstance(connection);
      Vessel vessel = spaceCenter.getActiveVessel();
      ReferenceFrame refframe = vessel.getOrbit().getBody().getReferenceFrame();
      while (true) {
        System.out.println(vessel.position(refframe));
      }
    }
  }
}

This approach requires significant communication overhead as request/response messages are repeatedly sent between the client and server. kRPC provides a more efficient mechanism to achieve this, called streams.

A stream repeatedly executes a procedure on the server (with a fixed set of argument values) and sends the result to the client. It only requires a single message to be sent to the server to establish the stream, which will then continuously send data to the client until the stream is closed.

The following example does the same thing as above using streams:

import krpc.client.Connection;
import krpc.client.RPCException;
import krpc.client.Stream;
import krpc.client.StreamException;
import krpc.client.services.KRPC;
import krpc.client.services.SpaceCenter;
import krpc.client.services.SpaceCenter.ReferenceFrame;
import krpc.client.services.SpaceCenter.Vessel;

import org.javatuples.Triplet;

import java.io.IOException;

public class Streaming2 {
  public static void main(String[] args) throws IOException, RPCException, StreamException {
    try (Connection connection = Connection.newInstance()) {
      SpaceCenter spaceCenter = SpaceCenter.newInstance(connection);
      Vessel vessel = spaceCenter.getActiveVessel();
      ReferenceFrame refframe = vessel.getOrbit().getBody().getReferenceFrame();
      Stream<Triplet<Double,Double,Double>> vesselStream = connection.addStream(vessel, "position", refframe);
      while (true) {
        System.out.println(vesselStream.get());
      }
    }
  }
}

It calls Connection.addStream once at the start of the program to create the stream, and then repeatedly prints the position returned by the stream. The stream is automatically closed when the client disconnects.

A stream can be created for any method call by calling Connection.addStream and passing it information about which method to stream. The example above passes a remote object, the name of the method to call, followed by the arguments to pass to the method (if any). Connection.addStream returns a stream object of type Stream. The most recent value of the stream can be obtained by calling Stream.get. A stream can be stopped and removed from the server by calling Stream.remove on the stream object. All of a clients streams are automatically stopped when it disconnects.

Synchronizing with Stream Updates

A common use case for kRPC is to wait until the value returned by a method or attribute changes, and then take some action. kRPC provides two mechanisms to do this efficiently: condition variables and callbacks.

Condition Variables

Each stream has a condition variable associated with it, that is notified whenever the value of the stream changes. These can be used to block the current thread of execution until the value of the stream changes.

The following example waits until the abort button is pressed in game, by waiting for the value of krpc.client.services.SpaceCenter.Control.getAbort() to change to true:

import krpc.client.Connection;
import krpc.client.RPCException;
import krpc.client.Stream;
import krpc.client.StreamException;
import krpc.client.services.KRPC;
import krpc.client.services.SpaceCenter;
import krpc.client.services.SpaceCenter.Control;

import java.io.IOException;

public class ConditionVariables {
  public static void main(String[] args) throws IOException, RPCException, StreamException {
    try (Connection connection = Connection.newInstance()) {
      SpaceCenter spaceCenter = SpaceCenter.newInstance(connection);
      Control control = spaceCenter.getActiveVessel().getControl();
      Stream<Boolean> abort = connection.addStream(control, "getAbort");
      synchronized (abort.getCondition()) {
        while (!abort.get()) {
          abort.waitForUpdate();
        }
      }
    }
  }
}

This code creates a stream, acquires a lock on the streams condition variable (by using a synchronized block) and then repeatedly checks the value of getAbort. It leaves the loop when it changes to true.

The body of the loop calls waitForUpdate on the stream, which causes the program to block until the value changes. This prevents the loop from ‘spinning’ and so it does not consume processing resources whilst waiting.

Note

The stream does not start receiving updates until the first call to waitForUpdate. This means that the example code will not miss any updates to the streams value, as it will have already locked the condition variable before the first stream update is received.

Callbacks

Streams allow you to register callback functions that are called whenever the value of the stream changes. Callback functions should take a single argument, which is the new value of the stream, and should return nothing.

For example the following program registers two callbacks that are invoked when the value of krpc.client.services.SpaceCenter.Control.getAbort() changes:

import krpc.client.Connection;
import krpc.client.RPCException;
import krpc.client.Stream;
import krpc.client.StreamException;
import krpc.client.services.KRPC;
import krpc.client.services.SpaceCenter;
import krpc.client.services.SpaceCenter.Control;

import java.io.IOException;

public class Callbacks {
  public static void main(String[] args) throws IOException, RPCException, StreamException {
    try (Connection connection = Connection.newInstance()) {
      SpaceCenter spaceCenter = SpaceCenter.newInstance(connection);
      Control control = spaceCenter.getActiveVessel().getControl();
      Stream<Boolean> abort = connection.addStream(control, "getAbort");
      abort.addCallback(
        (Boolean x) -> {
          System.out.println("Abort 1 called with a value of " + x);
        });
      abort.addCallback(
        (Boolean x) -> {
          System.out.println("Abort 2 called with a value of " + x);
        });
      abort.start();

      // Keep the program running...
      while (true) {
      }
    }
  }
}

Note

When a stream is created it does not start receiving updates until start is called. This is implicitly called when accessing the value of a stream, but as this example does not do this an explicit call to start is required.

Note

The callbacks are registered before the call to start so that stream updates are not missed.

Note

The callback function may be called from a different thread to that which created the stream. Any changes to shared state must therefore be protected with appropriate synchronization.

Custom Events

Some procedures return event objects of type Event. These allow you to wait until an event occurs, by calling Event.waitFor. Under the hood, these are implemented using streams and condition variables.

Custom events can also be created. An expression API allows you to create code that runs on the server and these can be used to build a custom event. For example, the following creates the expression MeanAltitude > 1000 and then creates an event that will be triggered when the expression returns true:

import krpc.client.Connection;
import krpc.client.RPCException;
import krpc.client.Event;
import krpc.client.StreamException;
import krpc.client.services.KRPC;
import krpc.client.services.KRPC.Expression;
import krpc.client.services.SpaceCenter;
import krpc.client.services.SpaceCenter.Flight;
import krpc.schema.KRPC.ProcedureCall;

import java.io.IOException;

public class CustomEvent {
  public static void main(String[] args) throws IOException, RPCException, StreamException {
    try (Connection connection = Connection.newInstance()) {
      KRPC krpc = KRPC.newInstance(connection);
      SpaceCenter spaceCenter = SpaceCenter.newInstance(connection);
      Flight flight = spaceCenter.getActiveVessel().flight(null);

      // Get the remote procedure call as a message object,
      // so it can be passed to the server
      ProcedureCall meanAltitude = connection.getCall(flight, "getMeanAltitude");

      // Create an expression on the server
      Expression expr = Expression.greaterThan(
        connection,
        Expression.call(connection, meanAltitude),
        Expression.constantDouble(connection, 1000));

      Event event = krpc.addEvent(expr);
      synchronized (event.getCondition()) {
        event.waitFor();
        System.out.println("Altitude reached 1000m");
      }
    }
  }
}

Client API Reference

class Connection

A connection to the kRPC server. All interaction with kRPC is performed via an instance of this class.

static Connection newInstance()
static Connection newInstance(String name)
static Connection newInstance(String name, String address)
static Connection newInstance(String name, String address, int rpcPort, int streamPort)
static Connection newInstance(String name, java.net.InetAddress address)
static Connection newInstance(String name, java.net.InetAddress address, int rpcPort, int streamPort)

Create a connection to the server, using the given connection details.

Parameters:
  • name (String) – A descriptive name for the connection. This is passed to the server and appears in the in-game server window.

  • address (String) – The address of the server to connect to. Can either be a hostname, an IP address as a string or a java.net.InetAddress object. Defaults to 127.0.0.1.

  • rpc_port (int) – The port number of the RPC Server. Defaults to 50000. This should match the RPC port number of the server you want to connect to.

  • stream_port (int) – The port number of the Stream Server. Defaults to 50001. This should match the stream port number of the server you want to connect to.

Stream<T> addStream(Class<?> clazz, String method, Object... args)

Create a stream for a static method call to the given class.

Stream<T> addStream(RemoteObject instance, String method, Object... args)

Create a stream for a method call to the given remote object.

krpc.schema.KRPC.ProcedureCall getCall(Class<?> clazz, String method, Object... args)

Returns a procedure call message for the given static method call. This allows descriptions of procedure calls to be passed to the server, for example when constructing custom events. See Custom Events.

krpc.schema.KRPC.ProcedureCall getCall(RemoteObject instance, String method, Object... args)

Returns a procedure call message for the given method call. This allows descriptions of procedure calls to be passed to the server, for example when constructing custom events. See Custom Events.

void close()

Close the connection.

class Stream<T>

This class represents a stream. See Streaming Data from the Server.

Stream objects implement hashCode and equals such that two stream objects are equal if they are bound to the same stream on the server.

void start()
void startAndWait()

Starts the stream. When a stream is created it does not start sending updates to the client until this method is called.

The startAndWait method will block until at least one update has been received from the server.

The start method starts the stream and returns immediately. Subsequent calls to get() may throw a StreamException.

float getRate()
void setRate(float rate)

The update rate of the stream in Hertz. When set to zero, the rate is unlimited.

T get()

Returns the most recent value for the stream. If executing the remote procedure for the stream throws an exception, calling this method will rethrow the exception. Raises a StreamException if no update has been received from the server.

If the stream has not been started this method calls startAndWait() to start the stream and wait until at least one update has been received.

Object getCondition()

A condition variable that is notified (using notifyAll) whenever the value of the stream changes.

void waitForUpdate()
void waitForUpdateWithTimeout(double timeout)

These methods block until the value of the stream changes or the operation times out.

The streams condition variable must be locked before calling this method.

If timeout is specified it is the timeout in seconds for the operation.

If the stream has not been started this method calls start to start the stream (without waiting for at least one update to be received).

int addCallback(java.util.function.Consumer<T> callback)

Adds a callback function that is invoked whenever the value of the stream changes. The callback function should take one argument, which is passed the new value of the stream. Returns a unique identifier for the callback which can be used to remove it.

Note

The callback function may be called from a different thread to that which created the stream. Any changes to shared state must therefore be protected with appropriate synchronization.

void removeCallback(int tag)

Removes a callback function from the stream. The tag is the identifier returned when the callback was added.

void remove()

Remove the stream from the server.

class Event

This class represents an event. See Custom Events. It is wrapper around a Stream that indicates when the event occurs.

Event objects implement hashCode and equals such that two event objects are equal if they are bound to the same underlying stream on the server.

void start()

Starts the event. When an event is created, it will not receive updates from the server until this method is called.

Object getCondition()

The condition variable that is notified (using notifyAll) whenever the event occurs.

void waitFor()
void waitForWithTimeout(double timeout)

These methods block until the event occurs or the operation times out.

The events condition variable must be locked before calling this method.

If timeout is specified it is the timeout in seconds for the operation.

If the event has not been started this method calls start() to start the underlying stream.

int addCallback(java.lang.Callable callback)

Adds a callback function that is invoked whenever the event occurs. The callback function should be a function that takes zero arguments. Returns an integer tag identifying the callback which can be used to remove it later.

void removeCallback(int tag)

Removes a callback function from the event.

void remove()

Removes the event from the server.

Stream<Boolean> getStream()

Returns the underlying stream for the event.

abstract class RemoteObject

The abstract base class for all remote objects.