|
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--dualroids.ClientModelConnector
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 |
|
Field Detail |
static final int PING_COUNT
static final int PING_SLEEP_TIME
static final int SERVER_SHIP_INDEX
static final int CLIENT_SHIP_INDEX
private Socket s
private ObjectOutputStream oos
private ObjectInputStream ois
private long clockDiff
private int shipIndex
SERVER/CLIENT_SHIP_INDEX
constants.private Arena arena
private InputHandler inputHandler
private ShipStatePump pump
ShipStatePump
private int[] scoreTable
private int[] shipsLeft
private String[] playerNames
private SpaceArea spaceArea
private boolean otherPlayerHasQuit
private boolean gameOver
Constructor Detail |
private ClientModelConnector(String hostname, boolean local, String playerName) throws IOException
hostname
- the hostname of the serverlocal
- tells whether the server is local or not, the
shipIndex
field will be determined from itIOException
- if the network fails to run the operationsMethod Detail |
public static ClientModelConnector createClientModelConnector(String hostname, boolean local, String playerName) throws IOException
hostname
- the hostname of the serverlocal
- tells whether the server is local or not: if local is
true
clock difference will not be determinedIOException
- if the network fails to run the operationspublic void run()
run
in interface Runnable
private void internalClose(int opCode) throws IOException
exit()
is also done. This method is called only from the opcode
listener loop, when it receives a close opcode.opCode
- the opCode which initiated the close procedureIOException
- if the network fails to run the operationsprivate void close(int opCode) throws IOException
exit()
.opCode
- the opCode which initiated the close procedureIOException
- if the network fails to run the operationspublic void close() throws IOException
IOException
- if the network fails to run the operationspublic final void registerSpaceArea(SpaceArea spaceArea)
spaceArea
- the space areaprivate final void updateScoreBoard()
public void flush() throws IOException
IOException
- if the network fails to run the operationspublic Arena getArena()
public int getShipIndex()
shipIndex
shipIndex
public final String getPlayerName(int shipIndex)
playerName
shipIndex
- the player's shipIndex
public final void setPlayerName(String playerName)
playerName
playerName
- the player namepublic final int getShipsLeft(int shipIndex)
shipsLeft
shipIndex
- the player's shipIndex
public final int getScore(int shipIndex)
scoreTable
shipIndex
- the player's shipIndex
public InputHandler getInputHandler()
inputHandler
inputHandler
public long currentServerMillis()
private void ping() throws IOException
gateTime = clientTime + difference.
The ping() method should be called only from the
createClientModelConnector() factory method.IOException
- if the network fails to run the operationsprivate final void readArena() throws IOException
IOException
- if the network fails to run the operationsprivate final void readInvalidateBullet() throws IOException
IOException
- if the network fails to run the operationsprivate final void readBlowUpAsteroid() throws IOException
index
IOException
- if the network fails to run the operationsprivate final void readAsteroidLocationAndAngle() throws IOException
index
IOException
- if the network fails to run the operationspublic final void writeBulletFired(int bulletIndex, double locationX, double locationY, double velocityX, double velocityY, double lineDeltaX, double lineDeltaY, long timestamp, long expires) throws IOException
bulletIndex
- the index of the bulletlocationX
- the x coordinate at which the bullet was firedlocationY
- the y coordinate at which the bullet was firedvelocityX
- the x component of the velocity of the bulletvelocityY
- the y component of the velocity of the bulletlineDeltaX
- the x difference of the between the center of
bullet the end of the bullet linelineDeltaY
- the x difference of the between the center of
bullet the end of the bullet linetimestamp
- the moment at which the bullet was firedexpires
- the moment in time at which the bullet expiresIOException
- if the network fails to run the operationsprivate final void readInitBullet() throws IOException
IOException
- if the network fails to run the operationspublic final void writeShipState(double locationX, double locationY, double velocityX, double velocityY, double angle, long timestamp) throws IOException
locationX
- the x coordinate of the space shiplocationY
- the y coordinate of the space shipvelocityX
- the x component of the velocity of the space shipvelocityY
- the y component of the velocity of the space shipangle
- the rotational angle of the space shiptimestamp
- the current server (game) timeIOException
- if the network fails to run the operationsprivate final void readShipState() throws IOException
IOException
- if the network fails to run the operationsprivate final void readBlowUpShip() throws IOException
shipIndex
has
blown up.IOException
- if the network fails to run the operationsprivate final void readShipsLeft() throws IOException
IOException
- if the network fails to run the operationsprivate final void readScore() throws IOException
IOException
- if the network fails to run the operationsprivate final void readPlayerName() throws IOException
IOException
- if the network fails to run the operationsprivate final void writePlayerName(String playerName) throws IOException
playerName
playerName
- the player name stringIOException
- if the network fails to run the operations
|
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
Dualroids was written by Oskar Ojala, Yrjö Kari-Koskinen and Henri Sivonen