Title: Flight Simulation ClientServer
1Flight Simulation Client/Server
2Introduction
- Using Java features to program Client/Server
applications - Threads, Sockets, inheritance, c.
- (all page number references are to the textbook)
3Topics of Discussion
- Java Threads and Strand Threads
- Socket input buffering with ReEnumerator
- Simulation state updates with Entity and
SimMonitorMessage - Output Synchronization
- Managing clients in SimEngine
- Display updates using SimulationClient and
Radar/WinDisplay
4Java Threads and Strand Threads
- Text implies that Threads may outlive the objects
that contain them. (p. 842) - This is not consistent with the usual approach to
objects - Define Strand class as a more OO Thread
- Strand interface similar to Thread
- new Strand( (Runnable)this )
- Strand will automatically start the thread at
construction and stop the thread upon
finalization (p. 472)
5Java Threads and Strand Threads
- A classs finalizers can be overriden by
subclasses, parent finalizers are not
automaticaly called as with constructors(p. 516) - Avoid this problem by using a helper final class
StrandThreadContainer whos finalize() method
stop()s the Thread.
6Socket input buffering with ReEnumerator
- Why Socket input buffering?
- A multi-user server must not be forced to wait on
an individual Socket - An Enumeration(p. 769) is a convenient way to
package a sequence of messages - A ReEnumerator packages a sequence of Enumerations
7Socket input buffering with ReEnumerator
A Server
Client1
in
in Stream
Socket
Socket
out Stream
out
Client2
in
Socket
in
Socket
out
out
- A multi-user server must not be forced to wait on
an individual Socket
8Socket input buffering with ReEnumerator
A Server
Client1
in
in Stream
Socket
Socket
out Stream
out
Client2
in
Socket
in
Socket
out
out
- A multi-user server must not be forced to wait on
an individual Socket
9Socket input buffering with ReEnumerator
An Enumeration
an Object
nextElement()
an Object
an Object
hasMoreElements()
an boolean
- An Enumeration(p. 769) is a convenient way to
package a sequence of messages
10Socket input buffering with ReEnumerator
- A ReEnumerator contains a Strand Thread which
keeps the main thread from having to wait on a
Stream - nextElement() returns an Enumeration
- That Enumeration supplies those messages that
have already been buffered. - MessageEnumeration is an extended Enumeration
interface for this program that has a method
nextSimMonitorMessage()
11Socket input buffering with ReEnumerator
A ReEnumerator buffer
an Enumeration
nextElement()
an Enumeration
an Enumeration
buffering Strand
a boolean
hasMoreElements()
- A ReEnumerator packages a sequence of Enumerations
12Socket input buffering with ReEnumerator
- A MessageEnumerator wraps(p. 725) an input Stream
with a MessageEnumeration - MessageReEnumerator is a special ReEnumerator
that works with a MessageEnumeration - new MessageReEnumerator( new
MessageEnumerator( in ) ) wraps and buffers the
stream
13Simulation state updates with Entity and
SimMonitorMessage
- Each simulated thing has a corresponding Entity
Object in the SimEngine - Each Entity contains several simulation
parameters relating to it( position, velocity,
orientation, shape, color, name,spin rate c. ) - An Entity can step() itself forward in time to do
its part of the simulation
14Simulation state updates with Entity and
SimMonitorMessage
- A SimMonitorMessage communicates partial update
information about an Entity and can
update(Entity) - There is a subclass of SimMonitorMessage for each
parameter in an Entity - A SimMonitorMessage can be read from a
DataInputStream and can update an Entity - A SimMonitorMessage can be constructed from an
Entity and can write itself to a DataOutputStream
15Output Synchronization
- A SimMonitorMessage writes itself to a
DataOutputStream in many little pieces. - There is more that one Strand(Thread) in the
SimulationServer in which this may occurr. - This must always be done with synchronized
methods(p. 851) - public synchronized static void sendAllMessages(
) - public synchronized static void
sendUserControlMessages()
16Output Synchronization
In Thread A, SimMonitorMessage X writes itself to
out
OutputDataStream out
In Thread B, SimMonitorMessage Y writes itself to
out
X0 X1 X2 Y0 X3 Y1 X4 X5
- in many little pieces. There is more that one
Strand in which this may occurr.
17Managing clients in SimEngine
- The SimEngine starts running a simulation Strand
at construction - The SimulationServer Object adds clients to the
SimEngine by invoking the addMember(Socket)
method - Clients may leave at any time on request or by
just aborting and causing an IOException on a
Socket - The SimulationServer may destroy the SimEngine at
any time in response to the Quit button
18Managing clients in SimEngine
- When the SimulationServer Object adds clients to
the SimEngine by invoking the addMember(Socket)
method, - A new simulation Entity corresponding to the
client must be created and initialized - All existing clients must be updated adding the
new Entity - The new client must be informed of its
corresponding Entity - The new client must be downloaded with the entire
simulation state
19Managing clients in SimEngine
- A client may quit at any time.
- The SimEngine may be informed of this by
- 1 a request from the client
- 2 an IOException on that clients Socket
- The corresponding Entity must be removed, and all
remaining clients must be then informed of that. - When the client requests to quit, the SimEngine
also informs the same client (as well as all the
remaining clients) of its Entity being removed
20Managing clients in SimEngine
- The SimulationServer may destroy the SimEngine at
any time in response to the Quit button A
client may quit at any time. - The SimEngine has a finalize() method which does
the following - Each Entity must be removed and each
corresponding client must be then informed of
that.
21Display updates using SimulationClient and
Radar/WinDisplay
- A SimulationClient connects to the
SimulationServer with a Socket in the usual
way(p. 862) - The simulation state is duplicated on each client
using the same class Entity used on the server - The SimMonitorMessage class is used for all
messages both from and to the SimulationClient
22Display updates using SimulationClient and
Radar/WinDisplay
- A SimulationClient must receive all messages from
the SimulationServer - It must create an Entity Object when informed of
its existence - It must update the corresponding Entity when it
receives a SimMonitorMessage from the
SimulationServer - It must remove an Entity for which it receives a
Quit message
23Display updates using SimulationClient and
Radar/WinDisplay
- A SimulationClient must remember to which Entity
it corresponds - It must render visual images from the point of
view of its corresponding Entity - It must update its Entity and the
SimulationServer whenever the user changes a
control on the Applet Window - It must quit when its Entity is removed by the
SimulationServer
24Display updates using SimulationClient and
Radar/WinDisplay
- A SimulationClient display images are rendered in
the WinDisplay Applet that maintains the primary
display area(by means of the renderAll() method). - Within renderAll ,a polygonal model is extracted
from each Entity - The light on each polygon is adjusted according
to its orientation. - Each polygonal model is geometricaly manipulated
to its relative orientation and location as it
should appear on the display.
25Display updates using SimulationClient and
Radar/WinDisplay
- A shadow polygon at altitude 0 is created in
some cases. - Each polygonal model is clipped to remove parts
that would not be visible within the display
window - After all polygonal models are geometricaly
processed, they are reordered to approximate a
visibility algorithm. - All polygons are then rendered to a clean Image
using the fillPolygon method, which is then
copied to the window by drawImage to minimize
flicker.
26Potential improvements
- Add a dialog to the client for obtaining the
Server host name - Get some better scenery
- Correct the visibility algorithm
- Add realistic flight dynamics
- Improve the appearance of planes
27What This Means
- With Sockets and Threads, you can do fun stuff