Java Reference
In-Depth Information
20.6 Client implementation
One of the purposes of splitting the calculation into client and server pieces is
for the server to do most of the work. For this example, our client can be pretty
simple. We do not need the full client design shown in the client collaboration
diagram of Figure 17.2, but we do need to perform the following steps:
1. Lookup the factory.
2. Get a server instance from the factory.
3. Initialize the server.
4. Initialize the simulation.
5. Start the simulation running.
6. Poll for results.
7. Display the results.
Referring to the client collaboration diagram, we see that these steps comprise
sequences 1, 2, and 4. The additional interactions in sequence 3 are needed to
set up a complex client with output plotting frames and input controls. To avoid
clutter in the client description, we simply print the results to standard output and
dispense with the complicated steps in sequence 3.
The first two steps are straightforward:
String factory - server ="//" + host + ":" + port +
"/" + FactoryInterface.FACTORY - NAME;
fFactory = (FactoryInterface) Naming.lookup
(factory - server);
// Get a server instance from the server factory.
fServer = fFactory.getInstance (id);
where host and port are pre-defined with the factory server hostname and the
port number, respectively, and id is the ID parameter discussed in Section 20.3.1.
Notice that we use the normal RMI-style URL when performing the Naming.
lookup() and that we use the FactoryInterface.FACTORY - NAME
constant to name the factory, as planned earlier in Section 20.3.1. Of
course this code must appear in a try/catch block to catch the pos-
sible exceptions MalformedURLException , NotBoundException , and
RemoteException .
Next we initialize the server by calling fServer.initialize
(initparam) where initparam is a String described in the server imple-
mentation above. Since this example does not actually use initparam ,we
can pass anything at all in its place. As usual we must be prepared to catch
RemoteException any time we call any remote method:
try {
fServer.initialize ( " doesn't matter " );
}
Search WWH ::




Custom Search