Grid Ops Java - Host

The com.nanosai.gridops.host.Host class represents a single-threaded loop which reads messages from
a TcpSocketsPort and passes the message to a NodeContainer for processing.
How the Host works is briefly illustrated here:

Creating a Host

Here is an example of how to create a Host:

Host host = GridOps.hostBuilder().build();

This example creates an empty Host object - which is not very useful. A Host needs
a TcpSocketsPort to read messages from, and a NodeContainer to deliver them to.

Setting a TcpSocketsPort on the Host

Here is how you create a TcpServer and TcpSocketsPort and connect them to the
Host:

Starting the Host

Once the Host is created you need to start it before it does anything. A Host object
implements Runnable so you can run it by calling its run() method. This will cause the
current thread to execute the Host. Here is how that looks:

host.run();

If you want to run the Host in its own thread you can do so like this:

new Thread(host).start();

The HostBuilder also has a convenience method for this called buildAndStart(). Here
is how calling buildAndStart() looks:

Once the Host is built and running you can connect to its TCP server with an IAP client and
send messages to it. The messages will be read via the TcpSocketsPort and forwarded to the
NodeContainer for processing. Remember to set the receiving node id, semantic protocol id
and message type correctly in the messages sent, or they won't reach the correct message reactor.

Stopping the Host

Once the Host is running it will keep running until you stop it or shut down the JVM. You can stop
a Host by calling its stop() method. Here is how you stop a running Host: