Open a Josceleton Connection

Following section provides samples about connecting to Josceleton with different approaches, which are:

  1. Invoke logic directly via the static Josceleton facade class (bad style, bad for testing, static cling).
  2. Instantiate JosceletonFacade statically once, then pass instance further (good enough, but could be still improved)
  3. Use the same Dependency Injection framework as Josceleton does: Guice
  4. Configure your framework of choice to inject JosceletonFacade instance.

Use the static Facade

Josceleton.openConnection()

The following code snippet uses the facade class Josceleton to open a connection (on the default port 7110) and add a listener for arbitrary messages and any user:

Sourcecode: Establish a basic connection by using the Josceleton facade
Connection connection = Josceleton.openConnection();

connection.addListener(new ConnectionListener() {
  public void onUserMessage(UserMessage userMessage) {
    String output = "[user " + userMessage.getUser().getOsceletonId() + "] ";
    output += userMessage.getUserState().name();
    System.out.println(output); // prints "[user 1] WAITING" (or PROCESSING, DEAD)
  }

  public void onJointMessage(JointMessage jointMessage) {
    // this will be invoked quiet often ;)

    String output = "[user " + jointMessage.getUser().getOsceletonId() + "] moved";
    if(jointMessage.getJointPart() == Body.HAND().LEFT()) {
  	  output += "left hand: ";
    }

    output += jointMessage.getCoordinate().x() + "/";
    output += jointMessage.getCoordinate().y() + "/";
    output += jointMessage.getCoordinate().z();
    System.out.println(output); // prints "[user 1] moved left hand: 0.342/0.981/1.524"
  }
});

System.out.println("Running ...");

// as the connection is asynchronously we have to close it as well some time later
// connection.close();

Pass an interface typed instance

Josceleton.newFacade()

Create a JosceletonFacade instance and pass it (or inject it) into your classes as needed. This approach is slightly better, as the static access is reduced to one single location.

The following snippet shows the evolution process of how to access such an instance in four steps. The final step is actually equivalent to the first one; this is just what is going on behind the scenes.

Sourcecode: Usage of the non-static facade and it's internal structure
// #1 - this is the most common way you will use (recommended approach)
// ===============================================================================
JosceletonFacade facade = Josceleton.newFacade();
... = facade.openConnection();

// #2 - internally instantiates a facade implementation
// ===============================================================================
JosceletonFacade facade = new JosceletonFacadeImpl(Josceleton.newGuiceInjector());
... = facade.openConnection();

// #3 - then invokes guice directly to create an injector
// ===============================================================================
Injector injector = Guice.createInjector(new JosceletonGuiceModule());
JosceletonFacade facade = new JosceletonFacadeImpl(injector);
... = facade.openConnection();

// #4 - tweak: outline module creation (this represents what is actually done internally)
// ===============================================================================
Module module = new JosceletonGuiceModule();
Injector injector = Guice.createInjector(module);
JosceletonFacade facade = new JosceletonFacadeImpl(injector);
... = facade.openConnection();