2.3. Hello, World!

Now it’s time to write some code and run the most basic SwimOS server.

The server.recon File

Assuming you’ve followed one of the two previous pages to set up your project, you should have at least a blank server.recon file in your project. This file serves as the primary configuration file for the SwimOS server. While it has the capacity to define various aspects, we’ll focus on a few key elements for now, saving more intricate features for upcoming chapters.

The file is written in Recon, think of it as Json with a few extra features that allow us to be a bit more expressive (mainly attributes, identifiable by the @).

Firstly we define our ‘space’ (tutorial) and ‘plane’ (MainPlane). Exactly what these are, is out of scope for our ‘Hello, world!’ server, so for now just think of the space as the server definition and the plane as the entry point.

File: src/main/resources/server.recon

tutorial: @fabric {
    @plane(class: "tutorial.MainPlane")
    
}

@web(port: 9001) {
    space: "tutorial"
}

You’ll observe that we’ve designated the entry point of our server as the empty Java class established during project setup - we’ll delve into this in the following section.

The lower half of the file indicates that we want to open the tutorial space, defined above, on port 9001.

The MainPlane Class

Finally, we come to the entry point of our code, the MainPlane class.

File: src/main/java/tutorial/MainPlane.java

package tutorial;

import swim.api.plane.AbstractPlane;
import swim.kernel.Kernel;
import swim.server.ServerLoader;
import swim.structure.Text;
import swim.structure.Value;

public class MainPlane extends AbstractPlane {

  public static void main(String[] args) {
    final Kernel kernel = ServerLoader.loadServer();
    System.out.println("Starting server...");
    kernel.start();
    System.out.println("Running server...");
    kernel.run();
  }
}

A few things to notice here:

Hello, world!

To finish our ‘Hello, world!’ server, we are going to use a feature that is core to SwimOS, lifecycle callbacks. As we defined our SwimOS plane to be the MainPlane class, an instance is going to be started when we start the server - with start(). We can therefore make use of the lifecycle callback methods of the AbstractPlane. The most commonly used callback is didStart, which intuitively is called after the subject has been started. All we need do to add the lifecycle callback is override the method and add our logic:

File: src/main/java/tutorial/MainPlane.java

  @Override
  public void didStart() {
    System.out.println("Hello, world!");
  }

We are done, give the server a test by running it with Gradle:

$ ./gradlew run

> Task :run
Starting server...
Hello, world!
Running server...
<==========----> 75% EXECUTING [1s]

In summary, we have set up and configured a SwimOS server to run on a given port. Then, using a lifecycle callback, we log a message once the plane has been created.

It may not seem like much but this is a huge first step to building a SwimOS streaming application. With project and server setup out the way, we can continue this example on the next page, making use of SwimOS’s fundamental building blocks, agents.