Java Reference
In-Depth Information
20.5.3 The
Simulation
class
The computational work is done in the
Simulation
class. The constructor does
nothing except store a reference to the
SimData
object:
public Simulation (SimData sd) {
fSimData = sd;
}
The
initialize()
method is called in sequence 3.1 after the client has
called
Server.initializeSimulation()
. Recall that
initialize-
Simulation()
is used by the client to provide initial conditions to the sim-
ulation. In our case, we receive a
float[]
array of values. Each element in the
array represents a known physical quantity - initial amplitude, spring constant,
etc. Both the client and server must agree upon the proper order of values in the
array or else the calculation will be incorrect. A less error-prone technique is
to define a data-only class, perhaps named
SpringParameters
with named
fields for each desired parameter:
class SpringParameters {
float amplitude;
float springConstant;
...
}
This class would best appear in the
javatech.all20.server
package where
the interfaces appear, giving it visibility to both the client and server. The client
would instantiate the class, populate each element, and pass it to the server during
initializeSimulation()
. (If you have read Chapter 19, then this class will
be familiar since it essentially mimics the role played by a CORBA
struct
.)
Since RMI is adept at moving objects across the network, such a technique would
be entirely satisfactory. We have chosen not to do that here because reading data
from a
float
array is slightly faster than dereferencing an object. For initial-
ization, the speed difference is negligible, but when the client starts polling the
server for data updates and providing new input data with each poll, then perfor-
mance becomes more important. In a complex simulation, the input array could
be substantially larger than the example here. There must be a close coupling
between the client and server anyway - the server is computing a known simula-
tion on behalf of the client, after all - so it's not much of a burden to require that
the client and server agree on the order of elements in the array, at the slight cost
of increased risk of error.
Therefore, instead of a
SpringParameters
object, we receive a simple
float[]
array. Let's now decide on the order of elements. We certainly need
the amplitude
A
, spring constant
K
, and mass
m
.From
K
and
m
we can calculate
the frequency
ω
.However, it is more convenient to specify
A
,
K
, and
ω
and
Search WWH ::
Custom Search