CS 542
Purpose
From this example you will learn about:
-
Basic Java applet programming
-
Simple Java GUI programming
-
Java RMI
The objective is to make you familiar about programming with Java RMI.
Description
You will develop a simple Java RMI application, including three servers
and a client. The servers run on remote machines and are registered in
an RMI registry on those machines. A window should pop up when each server
comes up declaring that the server is up.
You shall write the client as a Java applet. It provides a simple GUI:
it contains two buttons and two text fields. The text fields are there
to enter the server name and the host name on which the server is running.
There is one `Exchange' button and one `Reset' button. Upon clicking the
`Exchange' button, the server and the client should exchange a message.
The message received at each end should be displayed in a new window. Clicking
`Reset' clears the text fields.
On-line Help
Please read on-line document Java
Remote Method Invocation (RMI) to learn about Java RMI programming.
To learn to write a Java applet, you may want to read on-line document
The
Java Tutorial, in particular, the part 'Writing
Applets'.
Guidelines
-
I strongly recommend you to follow the 'Hello, world!' example in on-line
document
Java
Remote Method Invocation (RMI), section Getting
Started. First make it work with your Xinu account (you have to
change a lot of things, of course), then you can write your own application
easily, supposing you are sort of familar with Java applet and GUI programming.
-
Since you can not refer to your HTML file on Xinu with http type of URL,
you need to use local file type of URL, such as file:/.xinuserver/u56/username/myclasses/,
which is the same as file:/homes/username/myclasses/.
For example: To start the server, run
java -Djava.rmi.server.codebase=file:/.xinuserver/u56/username/public_html/myclasses/
-Djava.security.policy=$HOME/mysrc/policy examples.hello.HelloImpl
or
java -Djava.rmi.server.codebase=file:/homes/username/public_html/myclasses/
-Djava.security.policy=$HOME/mysrc/policy examples.hello.HelloImpl
-
For the 'Hello, world!' example, in statements
Naming.rebind("//myhost/HelloServer", obj);
and
obj = (Hello)Naming.lookup("//myhost/HelloServer");
myhost should be the host where you run your rmiregistry
and server, such as xinu1.cs.purdue.edu, etc.
Note: In the client code you should not use
obj = (Hello)Naming.lookup("//" + getCodeBase().getHost() + "/HelloServer");
because this would give the name of the host from which the applet is
downloaded, which would not be the host on which your server is running,
unless you run appletviewer later on the same machine to view the applet.
-
It is a little tricky to run rmiregistry and your server. On your server
machine, you should do the following:
-
Take the directory in which your classes are residing out of your CLASSPATH
(you may use unsetenv)
-
run rmiregistry &
-
set CLASSPATH to be the directory in which your classes are residing
-
run your server
-
You cannot view your applet from Netscape, because the applet is allowed
to make connections only to the host from which it is downloaded, therefore,
it is not allowed to communicate with rmiregistry running on another machine.
However, an applet run by appletviewer has no such security restriction,
so we may use appletviewer to view the applet.
You could use appletviewer in the following way:
-
get on another xinu machine (or the same machine as the one on which your
rmiregistry and server run)
-
set CLASSPATH to be the class directory myclasses/ or
change current directory to that directory with command cd.
-
Suppose your HTML file hello.html is in your home directory, then
run appletviewer:
appletviewer file:/homes/username/hello.html &
-
The package structure used in the 'Hello, world!' example is only for code
management, but not necessary for java RMI. You don't have to use it if
you don't like to.