2 Let's Start from Hello World!

(See horb/examples/clientServer for example code.)


In this section we'll try to fly with HORB by writing a small program. You should install the HORB distribution package on your computer before proceeding. For help, see Appendix A HORB Installation.

In this program we use two objects, the Server and Client. The Client will send a name to the Server, and the Server returns a greeting message to the Client. Thus, HORB flies with a message from a client machine to a server machine.

Here is the server-side program we'll call class Server.

When you call the Server method greeting("Baby"), it returns "Hello, Baby!".


2.1 Computing with Classic Java

To begin, we will work locally with a non-HORB version of the "client-side" program. This is a regular Java program.

        // Local.java
        package horb.examples.clientServer;

        class Local {
          public static void main(String argv[]) {
            Server server = new Server();
            String result = server.greeting("Baby");
            System.out.println(result);
          }
        }

By convention, Java programs begin with the main() method. Client creates a new instance of Server as server, then calls the greeting() method of server. The greeting() returns the greeting message.

To run this program, follow these steps:

        C:> javac Server.java     (compile Server.java)
        C:> javac Local.java      (compile Local.java)
        C:> java horb.examples.clientServer.Local     (run Local)
        Hello, Baby!


2.2 True Distributed Computing with HORB

Now, Here is a HORB version of Client.

        // Client.java
        package horb.examples.clientServer;
        class Client {
          public static void main(String argv[]) {

            String host = (argv.length == 1) ? argv[0] : "-";

            Server_Proxy server = new Server_Proxy("horb://"+host+"/");
            String result = server.greeting("World");
            System.out.println(result);
          }
        }

}

There are some small differences in the distributed version of Client from the local version of Client:

  1. An instance of class Server_Proxy is created as remote object reference of Server. A URL String which indicates the location of the server machine must be supplied as the argument.
  2. The remote instance of class Server is contacted by calling the instance of class Server_Proxy.

Again, the following step is the magic word for flying with HORB.

Class Server need not be changed at all. OK, let's compile and run the program! The HORBC compiler generates Server.class, Server_Proxy.class, and Server_Skeleton.class from Server.java. horbc -c Client.java generates just Client.class. You will initialize the HORB server in one window and run the Client class with the Java interpreter in another window. That is, horb and Client run in concurrent sessions on your computer.

        C:> horbc Server.java    (compile and generate proxy classes)
        C:> horbc -c Client.java (just compile, no proxy)
        C:> horb -v              (start HORB server IN SEPARATE SESSION!)
        C:> java horb.examples.clientServer.Client  (run Client in first sesseion)
        Hello, World!

You may want to include the -verbose option for the horb command in order to display the various process steps. Restart the horb command if you change the server class, or if you want to change your CLASSPATH statement. For example, changing CLASSPATH in one session while the HORB server is still running in a second session will not produce the desired effects, as the server is still in tune with the old CLASSPATH statement.

When the Client is started, an instance of the class Server is created in the HORB server process, and, for this example, all programs run on the same machine. As a next step we will separate client and server by placing the server on another machine. To do this on two machines, however, both machines should be connected to either the Internet or a local lan. Now, install the HORB package in the server machine (serverA in this document) and copy the class files you compiled in a directory of the server machine. Things should work fine even if the client machine and the server machine are of different machine architectures, for example, Windows 95 and Solaris; HORB, like Java itself, is completely portable. You client machine and server machine can share the same object files by putting them in a shared directory, for example, on NFS mounted filesystem or on Windows NT server. But pay attention to the CLASSPATH! It is good practice to try the above example-client and server-on the server machine, too, in order to confirm the settings.

        S:> horb -v               (start HORB server on serverA)
        C:> java horb.examples.clientServer.Client serverA
                                    (run Client on the client machine)
        Hello, World!

Does your HORB fly well? If yes, you can now write a program that flies around the world. If no, you should check installation of HORB and Java. (Don't forget to expand the HORB package with long filename option, if you use Windows.)

If the class Client is given a hostname as an argument, it creates an instance of class Server on the host; otherwise, it defaults to the localhost as before. When you create an instance of a proxy class, you have to supply a URL String or a HorbURL object. For example;

        HorbURL url = new HorbURL("horb://www.etl.go.jp:8889/");
        Server_Proxy server = new Server_Proxy(url);

If a proxy class is given this URL at instantiation, it tries to create a corresponding object in the HORB server process at www.etl.go.jp, port 8889. If you don't want to specify the port number each time, you can set a port number as default. To change the default port for outgoing connection, call the following method before creating proxies.

        HORBClient.setPort(8889);

The horb command can take an option to set the port number, For example:

        horb -port 8889

In the current HORB implementation a new TCP/IP connection is created for each creation of a remote object. The connection can be released by calling the _release() method in a proxy object. For example:

        String result = server.op1();
        server._release();

The remote object in the remote system is also removed automatically at the same time if the object is not in use. The _release() method is, however, automatically called when the proxy object is removed from the system. That means you need not call _release() unless you need to remove a remote object explicitly.