edu.uiuc.cs397rhc.game
Class GameDaemon

java.lang.Object
  extended by edu.uiuc.cs397rhc.game.GameDaemon
All Implemented Interfaces:
GameEventSource, java.lang.Runnable

public class GameDaemon
extends java.lang.Object
implements java.lang.Runnable, GameEventSource

Listens to the net server for game events and fires them to registered listeners. The net server sends a stream of TCP packets that represent various game events and the game state.

Author:
Steven N. Severinghaus
See Also:
Network Basics

Field Summary
static java.lang.String DEFAULT_HOST
          A random default server.
static int DEFAULT_PORT
          The default port that the net server runs on.
 
Constructor Summary
GameDaemon()
          Constructs a game daemon listening to the default host and port.
GameDaemon(java.lang.String host)
           
GameDaemon(java.lang.String host, int port)
          Constructs a game daemon listening to the specified port on the specified host.
 
Method Summary
 void addGameListener(GameListener l)
          Registers a GameListener to receive events from this source.
 void fireGameEvent(GameEvent ge)
          Fires a game event off to every registered listener.
static float leFloatToBEFloat(int lefloat)
          Converts a 4-byte (int) little-endian representation of a float into a usable big-endian float.
static long leUIntToBELong(int leuint)
          Converts a 4-byte (int) little-endian representation of an unsigned integer into a usable big-endian long.
static int leUIntToBEUInt(int leuint)
          Converts a 4-byte (int) little-endian representation of an unsigned integer into a make-believe Java uint.
 void removeGameListener(GameListener l)
          Removes a registered listener from the list receiving events.
 void run()
          Runs the main listening and event firing loop.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_PORT

public static final int DEFAULT_PORT
The default port that the net server runs on.

See Also:
Constant Field Values

DEFAULT_HOST

public static final java.lang.String DEFAULT_HOST
A random default server. No test server is currently running.

See Also:
Constant Field Values
Constructor Detail

GameDaemon

public GameDaemon()
Constructs a game daemon listening to the default host and port.


GameDaemon

public GameDaemon(java.lang.String host)

GameDaemon

public GameDaemon(java.lang.String host,
                  int port)
Constructs a game daemon listening to the specified port on the specified host.

Method Detail

run

public void run()
Runs the main listening and event firing loop. A connection to the host is established, and packets are read as they come in. The net packets are translated into various GameEvents and fired off to registered listeners.

Specified by:
run in interface java.lang.Runnable

leFloatToBEFloat

public static float leFloatToBEFloat(int lefloat)
Converts a 4-byte (int) little-endian representation of a float into a usable big-endian float. Designed to be used with readInt() and the like, which may be operating on little-endian streams.

Parameters:
lefloat - the int representation of the little-endian float
Returns:
a big-endian float

leUIntToBELong

public static long leUIntToBELong(int leuint)
Converts a 4-byte (int) little-endian representation of an unsigned integer into a usable big-endian long. Designed to be used with readInt() and the like, which may be operating on little-endian streams.

Parameters:
leuint - the int representation of the little-endian unsigned integer
Returns:
a big-endian long

leUIntToBEUInt

public static int leUIntToBEUInt(int leuint)
Converts a 4-byte (int) little-endian representation of an unsigned integer into a make-believe Java uint. All this actually does is reverse the bytes in the specified integer. Designed to be used with readInt() and the like, which may be operating on little-endian streams.

Parameters:
leuint - the int representation of the little-endian unsigned integer
Returns:
a fake big-endian uint

addGameListener

public void addGameListener(GameListener l)
Registers a GameListener to receive events from this source.

Specified by:
addGameListener in interface GameEventSource

removeGameListener

public void removeGameListener(GameListener l)
Removes a registered listener from the list receiving events.

Specified by:
removeGameListener in interface GameEventSource

fireGameEvent

public void fireGameEvent(GameEvent ge)
Fires a game event off to every registered listener.

Specified by:
fireGameEvent in interface GameEventSource
Parameters:
ge - the game event to fire