dualroids
Class ClientModelConnector

java.lang.Object
  |
  +--dualroids.ClientModelConnector
All Implemented Interfaces:
Runnable

public final class ClientModelConnector
extends Object
implements Runnable

The client-side interface to network. ClientModelConnector acts as both the reader and the writer interface. The reader interface is run in an independent thread. The methods which write to ObjectOutputStream are synchronized and named as writeFoo().


Field Summary
private  Arena arena
          the client's arena
(package private) static int CLIENT_SHIP_INDEX
          the ship indices
private  long clockDiff
          the clock difference between server and client: clockDiff = serverTime - clientTime
private  boolean gameOver
          a boolean indicating whether the game is over or not
private  InputHandler inputHandler
          the client's input handler
private  ObjectInputStream ois
          the ObjectInputStream for this connector
private  ObjectOutputStream oos
          the ObjectOutputStream for this connector
private  boolean otherPlayerHasQuit
          a boolean indicating whether the other player has already quit
(package private) static int PING_COUNT
          The static variables for getPing() method
(package private) static int PING_SLEEP_TIME
          The static variables for getPing() method
private  String[] playerNames
          the players' names
private  ShipStatePump pump
          the client's ShipStatePump
private  Socket s
          the socket this connector uses
private  int[] scoreTable
          the players' scores
(package private) static int SERVER_SHIP_INDEX
          the ship indices
private  int shipIndex
          The index of the ship owned by the client to which this connector is connected.
private  int[] shipsLeft
          the players' unused ships
private  SpaceArea spaceArea
          the space area
 
Constructor Summary
private ClientModelConnector(String hostname, boolean local, String playerName)
          This constructor is called from the factory method below.
 
Method Summary
 void close()
          A public method for initiating the close procedure from outside this connector
private  void close(int opCode)
          Closes the the ship state pump and output stream, if they are not already closed and calls the other connector to close its streams too.
static ClientModelConnector createClientModelConnector(String hostname, boolean local, String playerName)
          This factory method creates a new ClientModelConnector and performs the ping to get the clock difference.
 long currentServerMillis()
          Returns the valid server time, which is the valid game time.
 void flush()
          Flushes the network output stream
 Arena getArena()
          Getter method for the arena
 InputHandler getInputHandler()
          Getter method for the inputHandler
 String getPlayerName(int shipIndex)
          Getter method for the playerName
 int getScore(int shipIndex)
          Getter method for the scoreTable
 int getShipIndex()
          Getter method for the shipIndex
 int getShipsLeft(int shipIndex)
          Getter method for the shipsLeft
private  void internalClose(int opCode)
          Closes the the ship state pump and output stream, if they are not already closed.
private  void ping()
          Determines the difference between client and server machine clocks using socket connections and ObjectInput/OutputStreams.
private  void readArena()
          Receives the Arena object to the client
private  void readAsteroidLocationAndAngle()
          Tells the client the location and angle of asteroid number index
private  void readBlowUpAsteroid()
          Tells the client to blow up asteroid number index
private  void readBlowUpShip()
          Tells the client that the ship at shipIndex has blown up.
private  void readInitBullet()
          Tells the client that a bullet has been fired by the ship controlled by the other client.
private  void readInvalidateBullet()
          Marks a bullet non-flying (not visible).
private  void readPlayerName()
          Read the player name and update it to the score board
private  void readScore()
          Read the new scores of a player.
private  void readShipsLeft()
          Tells the client the number of ships a player has left.
private  void readShipState()
          Tells the client the state of the other space ship.
 void registerSpaceArea(SpaceArea spaceArea)
          Saves the space area reference.
 void run()
          JVM runs this method when start() is called for this thread.
 void setPlayerName(String playerName)
          Setter method for this client's playerName
private  void updateScoreBoard()
          Sends the score board information to space area and marks a need for update.
 void writeBulletFired(int bulletIndex, double locationX, double locationY, double velocityX, double velocityY, double lineDeltaX, double lineDeltaY, long timestamp, long expires)
          Tells the server that a bullet has been fired by the ship controlled by this client.
private  void writePlayerName(String playerName)
          Write this client's playerName
 void writeShipState(double locationX, double locationY, double velocityX, double velocityY, double angle, long timestamp)
          Sends the state of the space ship to the server.
 
Methods inherited from class java.lang.Object
, clone, equals, finalize, getClass, hashCode, notify, notifyAll, registerNatives, toString, wait, wait, wait
 

Field Detail

PING_COUNT

static final int PING_COUNT
The static variables for getPing() method

PING_SLEEP_TIME

static final int PING_SLEEP_TIME
The static variables for getPing() method

SERVER_SHIP_INDEX

static final int SERVER_SHIP_INDEX
the ship indices

CLIENT_SHIP_INDEX

static final int CLIENT_SHIP_INDEX
the ship indices

s

private Socket s
the socket this connector uses

oos

private ObjectOutputStream oos
the ObjectOutputStream for this connector

ois

private ObjectInputStream ois
the ObjectInputStream for this connector

clockDiff

private long clockDiff
the clock difference between server and client: clockDiff = serverTime - clientTime

shipIndex

private int shipIndex
The index of the ship owned by the client to which this connector is connected. The ship indices are determined in the SERVER/CLIENT_SHIP_INDEX constants.

arena

private Arena arena
the client's arena

inputHandler

private InputHandler inputHandler
the client's input handler

pump

private ShipStatePump pump
the client's ShipStatePump

scoreTable

private int[] scoreTable
the players' scores

shipsLeft

private int[] shipsLeft
the players' unused ships

playerNames

private String[] playerNames
the players' names

spaceArea

private SpaceArea spaceArea
the space area

otherPlayerHasQuit

private boolean otherPlayerHasQuit
a boolean indicating whether the other player has already quit

gameOver

private boolean gameOver
a boolean indicating whether the game is over or not
Constructor Detail

ClientModelConnector

private ClientModelConnector(String hostname,
                             boolean local,
                             String playerName)
                      throws IOException
This constructor is called from the factory method below. It initializes the network connection and the input/output streams.
Parameters:
hostname - the hostname of the server
local - tells whether the server is local or not, the shipIndex field will be determined from it
Throws:
IOException - if the network fails to run the operations
Method Detail

createClientModelConnector

public static ClientModelConnector createClientModelConnector(String hostname,
                                                              boolean local,
                                                              String playerName)
                                                       throws IOException
This factory method creates a new ClientModelConnector and performs the ping to get the clock difference. Finally the opcode listener thread is launched.
Parameters:
hostname - the hostname of the server
local - tells whether the server is local or not: if local is true clock difference will not be determined
Throws:
IOException - if the network fails to run the operations

run

public void run()
JVM runs this method when start() is called for this thread. Starts to listen for the opcodes.
Specified by:
run in interface Runnable

internalClose

private void internalClose(int opCode)
                    throws IOException
Closes the the ship state pump and output stream, if they are not already closed. Finally closes also the input stream. If the close procedure is originated from this client, an exit() is also done. This method is called only from the opcode listener loop, when it receives a close opcode.
Parameters:
opCode - the opCode which initiated the close procedure
Throws:
IOException - if the network fails to run the operations

close

private void close(int opCode)
            throws IOException
Closes the the ship state pump and output stream, if they are not already closed and calls the other connector to close its streams too. If this is the second close procedure, do an exit().
Parameters:
opCode - the opCode which initiated the close procedure
Throws:
IOException - if the network fails to run the operations

close

public void close()
           throws IOException
A public method for initiating the close procedure from outside this connector
Throws:
IOException - if the network fails to run the operations

registerSpaceArea

public final void registerSpaceArea(SpaceArea spaceArea)
Saves the space area reference.
Parameters:
spaceArea - the space area

updateScoreBoard

private final void updateScoreBoard()
Sends the score board information to space area and marks a need for update.

flush

public void flush()
           throws IOException
Flushes the network output stream
Throws:
IOException - if the network fails to run the operations

getArena

public Arena getArena()
Getter method for the arena
Returns:
the connector's arena

getShipIndex

public int getShipIndex()
Getter method for the shipIndex
Returns:
the connector's shipIndex

getPlayerName

public final String getPlayerName(int shipIndex)
Getter method for the playerName
Parameters:
shipIndex - the player's shipIndex
Returns:
the player name

setPlayerName

public final void setPlayerName(String playerName)
Setter method for this client's playerName
Parameters:
playerName - the player name

getShipsLeft

public final int getShipsLeft(int shipIndex)
Getter method for the shipsLeft
Parameters:
shipIndex - the player's shipIndex
Returns:
the ships left for this player

getScore

public final int getScore(int shipIndex)
Getter method for the scoreTable
Parameters:
shipIndex - the player's shipIndex
Returns:
the player's score

getInputHandler

public InputHandler getInputHandler()
Getter method for the inputHandler
Returns:
the connector's inputHandler

currentServerMillis

public long currentServerMillis()
Returns the valid server time, which is the valid game time.
Returns:
the server time

ping

private void ping()
           throws IOException
Determines the difference between client and server machine clocks using socket connections and ObjectInput/OutputStreams. You get the correct gameTime = serverTime in client by adding up the difference and clientTime: gateTime = clientTime + difference. The ping() method should be called only from the createClientModelConnector() factory method.
Throws:
IOException - if the network fails to run the operations

readArena

private final void readArena()
                      throws IOException
Receives the Arena object to the client
Throws:
IOException - if the network fails to run the operations

readInvalidateBullet

private final void readInvalidateBullet()
                                 throws IOException
Marks a bullet non-flying (not visible).
Throws:
IOException - if the network fails to run the operations

readBlowUpAsteroid

private final void readBlowUpAsteroid()
                               throws IOException
Tells the client to blow up asteroid number index
Throws:
IOException - if the network fails to run the operations

readAsteroidLocationAndAngle

private final void readAsteroidLocationAndAngle()
                                         throws IOException
Tells the client the location and angle of asteroid number index
Throws:
IOException - if the network fails to run the operations

writeBulletFired

public final void writeBulletFired(int bulletIndex,
                                   double locationX,
                                   double locationY,
                                   double velocityX,
                                   double velocityY,
                                   double lineDeltaX,
                                   double lineDeltaY,
                                   long timestamp,
                                   long expires)
                            throws IOException
Tells the server that a bullet has been fired by the ship controlled by this client.
Parameters:
bulletIndex - the index of the bullet
locationX - the x coordinate at which the bullet was fired
locationY - the y coordinate at which the bullet was fired
velocityX - the x component of the velocity of the bullet
velocityY - the y component of the velocity of the bullet
lineDeltaX - the x difference of the between the center of bullet the end of the bullet line
lineDeltaY - the x difference of the between the center of bullet the end of the bullet line
timestamp - the moment at which the bullet was fired
expires - the moment in time at which the bullet expires
Throws:
IOException - if the network fails to run the operations

readInitBullet

private final void readInitBullet()
                           throws IOException
Tells the client that a bullet has been fired by the ship controlled by the other client.
Throws:
IOException - if the network fails to run the operations

writeShipState

public final void writeShipState(double locationX,
                                 double locationY,
                                 double velocityX,
                                 double velocityY,
                                 double angle,
                                 long timestamp)
                          throws IOException
Sends the state of the space ship to the server.
Parameters:
locationX - the x coordinate of the space ship
locationY - the y coordinate of the space ship
velocityX - the x component of the velocity of the space ship
velocityY - the y component of the velocity of the space ship
angle - the rotational angle of the space ship
timestamp - the current server (game) time
Throws:
IOException - if the network fails to run the operations

readShipState

private final void readShipState()
                          throws IOException
Tells the client the state of the other space ship.
Throws:
IOException - if the network fails to run the operations

readBlowUpShip

private final void readBlowUpShip()
                           throws IOException
Tells the client that the ship at shipIndex has blown up.
Throws:
IOException - if the network fails to run the operations

readShipsLeft

private final void readShipsLeft()
                          throws IOException
Tells the client the number of ships a player has left.
Throws:
IOException - if the network fails to run the operations

readScore

private final void readScore()
                      throws IOException
Read the new scores of a player.
Throws:
IOException - if the network fails to run the operations

readPlayerName

private final void readPlayerName()
                           throws IOException
Read the player name and update it to the score board
Throws:
IOException - if the network fails to run the operations

writePlayerName

private final void writePlayerName(String playerName)
                            throws IOException
Write this client's playerName
Parameters:
playerName - the player name string
Throws:
IOException - if the network fails to run the operations


Dualroids was written by Oskar Ojala, Yrjö Kari-Koskinen and Henri Sivonen