Grid Ops Java - Host
Jakob Jenkov |
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
:
TcpServer tcpServer = GridOps.tcpServerBuilder().buildAndStart(); TcpSocketsPort tcpSocketsPort = GridOps.tcpSocketsPortBuilder().tcpServer(tcpServer).build(); Host host = GridOps.hostBuilder() .tcpSocketsPort(tcpSocketsPort) .build();
The TcpServer
class is explained in more detail in the text about the TcpServer.
The TcpSocketsPort
class is explained in more detail in the text about the TcpSocketsPort
Setting a NodeContainer on the Host
Here is how you create a NodeContainer
with a NodeReactor
, ProtocolReactor
and MessageReactor
which can handle incoming messages:
byte[] messageType = new byte[]{11}; byte[] protocolId = new byte[]{22}; byte[] protocolVersion = new byte[]{ 0}; byte[] nodeId = new byte[]{33}; MessageReactor messageReactor = new MessageReactor(messageType) { @Override public void react(IonReader ionReader, IapMessageFields iapMessageFields, TcpSocketsPort tcpSocketsPort) { System.out.println("Reacting to message"); } }; ProtocolReactor protocolReactor = GridOps.protocolReactor(protocolId, protocolVersion, messageReactor); NodeReactor nodeReactor = GridOps.nodeReactor (nodeId , protocolReactor); NodeContainer nodeContainer = GridOps.nodeContainer (nodeReactor);
The NodeContainer
is explained in more detail in the text about the NodeContainer.
Once the NodeContainer
is created you need to set it on the HostBuilder
. Here is a full
example setting the the NodeContainer
on the HostBuilder
:
TcpServer tcpServer = GridOps.tcpServerBuilder().buildAndStart(); TcpSocketsPort tcpSocketsPort = GridOps.tcpSocketsPortBuilder().tcpServer(tcpServer).build(); byte[] messageType = new byte[]{11}; byte[] protocolId = new byte[]{22}; byte[] protocolVersion = new byte[]{ 0}; byte[] nodeId = new byte[]{33}; MessageReactor messageReactor = new MessageReactor(messageType) { @Override public void react(IonReader ionReader, IapMessageFields iapMessageFields, TcpSocketsPort tcpSocketsPort) { System.out.println("Reacting to message"); } }; ProtocolReactor protocolReactor = GridOps.protocolReactor(protocolId, protocolVersion, messageReactor); NodeReactor nodeReactor = GridOps.nodeReactor (nodeId , protocolReactor); NodeContainer nodeContainer = GridOps.nodeContainer (nodeReactor); Host host = GridOps.hostBuilder() .tcpSocketsPort(tcpSocketsPort) .nodeContainer(nodeContainer) .build();
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:
Host host = GridOps.hostBuilder() .tcpSocketsPort(tcpSocketsPort) .nodeContainer(nodeContainer) .buildAndStart();
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
:
host.stop();
Tweet | |
Jakob Jenkov |