-
Notifications
You must be signed in to change notification settings - Fork 2
hello_world
This simple example, based on the classic "Hello World" application, illustrates the basic steps for creating and running HS programs that communicate via router.
Although we could simply start up HS and issue the command:
put ( "Hello, world!" );
HS is more appropriately characterized as an application that can execute this command remotely.
Thus, this example is actually two separate HS programs, one that acts as a server, and the other as a client. The server HS, which we will call hello_server, defines a method, HELLO, that prints out the "Hello World" message. The difference is that this method will be executed by the client HS, hello_client.
Before proceeding, make sure that the HS Environment has been set up, and that router is running. For the "Hello World" application, it is most convenient to open three terminal windows (e.g. xterms) on your desktop. One should start up AutoRouter in the first window, while the other two windows will be running the hello_server and _hello_client_programs. For the purpose of this demonstration, the computer is a UNIX system designated as node.
First we set up the hello_server object. In the second terminal window, start up HS as follows:
$ hs -i -t hello_server
HS is now ready to process commands. The steps are:
- Define a method, HELLO, that prints out "Hello, world!".
- ENABLE the HELLO method for execution by an EVENT message.
- Call the idle method to enter the IDLE state, waiting for the EVENT message.
Enter the following HS commands (comments are included for clarity) to perform these steps:
/* Define the HELLO method */
HELLO () {
put ( "Hello, world!" );
}
/* Enable the HELLO method */
enable ( HELLO );
/* Enter the IDLE state */
idle ();
You should see the following output in the hello_server terminal window:
...returning to IDLE state
...sending a 'connect' message to router
--Sending----
|router|event|connect|hello_server|7cfe850f|20000228:180426|||
Connected to input message channel '../fifo/hello_server'
hello_server is now in the IDLE state, waiting for an EVENT message (from hello_client). The output also shows that a connection was established with AutoRouter.
Now we start up the hello_client HS in the third terminal window as follows:
$ hs -i -t hello_client
and call the event method to execute the HELLO method on hello_server:
event ( "hello_server", "HELLO" );
which produces the following output in the hello_client window:
--Sending----
|hello_server@node|event|HELLO|hello_client@node|2f19bbf5|20000228:180441|||
and this output in the hello_server window:
--Received---
|hello_server@node|event|HELLO|hello_client@node|2f19bbf5|20000228:180441||
"Hello, world!"
...returning to IDLE state
hello_server prints out the "Hello World" message, and a returns to the IDLE state to wait for another EVENT message.
In this example, the router routes messages between hello_server and hello_client. These messages follow the Ab Initio Protocol, whose structure can be seen in the output lines containing the strings delimited by the '|' character. When hello_server called the idle method, a CONNECT message was sent to AutoRouter. This message was received by AutoRouter in a UNIX FIFO device. AutoRouter read the message, then created another read-only device (another UNIX FIFO) for hello_server where messages are deposited. This can be seen in the output of router window:
==FIFO Read==
--To ROUTER--
20000228:180426:|router@node|event|connect|hello_server@node|7cfe850f|20000228:180426|||
20000228:180426:Setting 'hello_server' to connected
20000228:180426:Created read socket (10) for client FIFO '../fifo/hello_server'
-------------
When hello_client called the event method to execute the HELLO method on hello_server, router received the message in its FIFO, then routed the message to the hello_server FIFO. This message was logged by router as:
==FIFO Read==
--To FIFO----
20000228:180441:|hello_server@node|event|HELLO|hello_client@node|2f19bbf5|20000228:180441|||
-------------
Consult the router documentation for more information.
For More examples, see the Code Samples index.