|
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--dualroids.ServerModelConnector
The server-side interface to network. ServerModelConnector 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 server's arena |
(package private) static int |
OC_ARENA
opcodes for the network protocol |
(package private) static int |
OC_ASTEROID_LOCATION_AND_ANGLE
opcodes for the network protocol |
(package private) static int |
OC_BLOW_UP_ASTEROID
opcodes for the network protocol |
(package private) static int |
OC_BLOW_UP_SHIP
opcodes for the network protocol |
(package private) static int |
OC_BULLET_FIRED
opcodes for the network protocol |
(package private) static int |
OC_CLIENT_CLOSE
opcodes for the network protocol |
(package private) static int |
OC_EOF
opcodes for the network protocol |
(package private) static int |
OC_GAME_OVER
opcodes for the network protocol |
(package private) static int |
OC_INIT_BULLET
opcodes for the network protocol |
(package private) static int |
OC_INVALIDATE_BULLET
opcodes for the network protocol |
(package private) static int |
OC_PLAYER_NAME
opcodes for the network protocol |
(package private) static int |
OC_SCORE
opcodes for the network protocol |
(package private) static int |
OC_SERVER_CLOSE
opcodes for the network protocol |
(package private) static int |
OC_SHIP_STATE
opcodes for the network protocol |
(package private) static int |
OC_SHIPS_LEFT
opcodes for the network protocol |
private ObjectInputStream |
ois
the ObjectInputStream for this connector |
private ObjectOutputStream |
oos
the ObjectOutputStream for this connector |
private ServerModelConnector |
otherConnector
the other ServerModelConnector |
private String |
playerName
The players' name of the client to which this connector is connected |
private Socket |
s
the socket this connector uses |
(package private) static int |
SERVER_PORT
server listens on this port |
private int |
shipIndex
The index of the ship owned by the client to which this connector is connected. |
Constructor Summary | |
private |
ServerModelConnector(Socket s,
int shipIndex)
The constructor initializes streams from the given socket. |
Method Summary | |
private void |
close(int opCode)
Closes the output stream, if it's not already closed and calls the other connector to close its streams too. |
static ServerModelConnector[] |
createServerModelConnectors(String playerName)
This method should be called to initialize the two connectors. |
private void |
echoPing()
The client sends us the clientTime, and we send the time difference back The echoPing() method should be called only from the createServerModelConnectors() factory method. |
void |
flush()
Flushes the network output stream |
private void |
internalClose(int opCode)
Closes the output stream, if it's not already closed and calls the other connector to close its streams too. |
private void |
readBulletFired()
Tells the server that a bullet has been fired by the ship controlled by this client. |
private void |
readPlayerName()
Read the player name of the client connected to this connector and save it temporarily to the this.playerName field. |
private void |
readShipState()
Reads the state of the space ship controlled by this connector's client, forwards the information to the other connector's client and updates the information to the model. |
void |
run()
JVM runs this method when start() is called for this thread. |
void |
writeArena(Arena arena)
Sends the whole Arena object to the client |
void |
writeAsteroidLocationAndAngle(int index,
double locationX,
double locationY,
double angle,
long timestamp)
Tells the client the location and angle of asteroid number index |
void |
writeBlowUpAsteroid(int index,
double locationX,
double locationY,
long timestamp)
Tells the client to blow up asteroid number index |
void |
writeBlowUpShip(int shipIndex,
long newAppears)
Tells the client that the ship at shipIndex has
blown up. |
void |
writeGameOver()
A public method for initiating the game over procedure from outside this connector |
private void |
writeInitBullet(int shipIndex,
int bulletIndex,
double locationX,
double locationY,
double velocityX,
double velocityY,
double lineDeltaX,
double lineDeltaY,
long timestamp,
long expires)
Tells the client that a bullet has been fired by the ship controlled by the other client. |
void |
writeInvalidateBullet(int shipIndex,
int bulletIndex)
Marks a bullet non-flying (not visible). |
private void |
writePlayerName(int shipIndex,
String playerName)
Write the given playerName of the given
shipIndex . |
void |
writeScore(int shipIndex,
int score)
Inform the client of the new scores of a player. |
void |
writeShipsLeft(int shipIndex,
int shipsLeft)
Tells the client the number of ships a player has left. |
private void |
writeShipState(int shipIndex,
double locationX,
double locationY,
double velocityX,
double velocityY,
double angle,
long timestamp)
Sends the state of the space ship to the client. |
Methods inherited from class java.lang.Object |
|
Field Detail |
static final int SERVER_PORT
static final int OC_EOF
static final int OC_SERVER_CLOSE
static final int OC_CLIENT_CLOSE
static final int OC_GAME_OVER
static final int OC_ARENA
static final int OC_INIT_BULLET
static final int OC_BLOW_UP_ASTEROID
static final int OC_INVALIDATE_BULLET
static final int OC_ASTEROID_LOCATION_AND_ANGLE
static final int OC_BLOW_UP_SHIP
static final int OC_SCORE
static final int OC_SHIPS_LEFT
static final int OC_PLAYER_NAME
static final int OC_BULLET_FIRED
static final int OC_SHIP_STATE
private Socket s
private ObjectOutputStream oos
private ObjectInputStream ois
private ServerModelConnector otherConnector
private Arena arena
private int shipIndex
ClientModelConnector
's
SERVER/CLIENT_SHIP_INDEX
constants.private String playerName
Constructor Detail |
private ServerModelConnector(Socket s, int shipIndex) throws IOException
s
- the socket which will be used for network communicationshipIndex
- the index of the ship owned by the client to
which this connector is connected. The ship indices are determined
in the ClientModelConnector
's
SERVER/CLIENT_SHIP_INDEX
constants.Method Detail |
public static ServerModelConnector[] createServerModelConnectors(String playerName) throws IOException
playerName
- this local client player's namepublic 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
opCode
- the opCode which initiated the close procedureIOException
- if the network fails to run the operationspublic void writeGameOver() throws IOException
IOException
- if the network fails to run the operationsprivate void echoPing() throws IOException
createServerModelConnectors()
factory method.IOException
- if the network fails to run the operationspublic void flush() throws IOException
IOException
- if the network fails to run the operationspublic void writeBlowUpAsteroid(int index, double locationX, double locationY, long timestamp) throws IOException
index
index
- the index of the asteroid to blow uplocationX
- the x coordinate of the explosion locationlocationY
- the y coordinate of the explosion locationtimestamp
- the moment of explosion in game (server) timeIOException
- if the network fails to run the operationspublic void writeBlowUpShip(int shipIndex, long newAppears) throws IOException
shipIndex
has
blown up.shipIndex
- the index of the space shipnewAppears
- the moment in game time when a new ship should appearIOException
- if the network fails to run the operationspublic final void writeShipsLeft(int shipIndex, int shipsLeft) throws IOException
shipIndex
- the index of the space shipshipsLeft
- the number of ships leftIOException
- if the network fails to run the operationspublic final void writeInvalidateBullet(int shipIndex, int bulletIndex) throws IOException
shipIndex
- the index of the ship owning the bulletbulletIndex
- the index of the bulletIOException
- if the network fails to run the operationspublic final void writeArena(Arena arena) throws IOException
arena
- the ready-to-send arenaIOException
- if the network fails to run the operationsprivate final void writeInitBullet(int shipIndex, int bulletIndex, double locationX, double locationY, double velocityX, double velocityY, double lineDeltaX, double lineDeltaY, long timestamp, long expires) throws IOException
shipIndex
- the index of the ship owning the bulletbulletIndex
- 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 readBulletFired() throws IOException
IOException
- if the network fails to run the operationsprivate final void writeShipState(int shipIndex, double locationX, double locationY, double velocityX, double velocityY, double angle, long timestamp) throws IOException
shipIndex
- the index of the space shiplocationX
- 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 rotation 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 operationspublic void writeAsteroidLocationAndAngle(int index, double locationX, double locationY, double angle, long timestamp) throws IOException
index
index
- the index of the asteroid to blow uplocationX
- the x coordinate of the explosion locationlocationY
- the y coordinate of the explosion locationangle
- the rotation angle of the asteroidtimestamp
- the moment of explosion in game (server) timeIOException
- if the network fails to run the operationspublic void writeScore(int shipIndex, int score) throws IOException
shipIndex
- the index of the space shipscore
- the player's scoreIOException
- if the network fails to run the operationsprivate final void readPlayerName() throws IOException
this.playerName
field.IOException
- if the network fails to run the operationsprivate final void writePlayerName(int shipIndex, String playerName) throws IOException
playerName
of the given
shipIndex
.shipIndex
- the index of the space shipplayerName
- the player's nameIOException
- 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