dualroids
Class ServerModelConnector

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

public final class ServerModelConnector
extends Object
implements Runnable

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
, clone, equals, finalize, getClass, hashCode, notify, notifyAll, registerNatives, toString, wait, wait, wait
 

Field Detail

SERVER_PORT

static final int SERVER_PORT
server listens on this port

OC_EOF

static final int OC_EOF
opcodes for the network protocol

OC_SERVER_CLOSE

static final int OC_SERVER_CLOSE
opcodes for the network protocol

OC_CLIENT_CLOSE

static final int OC_CLIENT_CLOSE
opcodes for the network protocol

OC_GAME_OVER

static final int OC_GAME_OVER
opcodes for the network protocol

OC_ARENA

static final int OC_ARENA
opcodes for the network protocol

OC_INIT_BULLET

static final int OC_INIT_BULLET
opcodes for the network protocol

OC_BLOW_UP_ASTEROID

static final int OC_BLOW_UP_ASTEROID
opcodes for the network protocol

OC_INVALIDATE_BULLET

static final int OC_INVALIDATE_BULLET
opcodes for the network protocol

OC_ASTEROID_LOCATION_AND_ANGLE

static final int OC_ASTEROID_LOCATION_AND_ANGLE
opcodes for the network protocol

OC_BLOW_UP_SHIP

static final int OC_BLOW_UP_SHIP
opcodes for the network protocol

OC_SCORE

static final int OC_SCORE
opcodes for the network protocol

OC_SHIPS_LEFT

static final int OC_SHIPS_LEFT
opcodes for the network protocol

OC_PLAYER_NAME

static final int OC_PLAYER_NAME
opcodes for the network protocol

OC_BULLET_FIRED

static final int OC_BULLET_FIRED
opcodes for the network protocol

OC_SHIP_STATE

static final int OC_SHIP_STATE
opcodes for the network protocol

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

otherConnector

private ServerModelConnector otherConnector
the other ServerModelConnector

arena

private Arena arena
the server's arena

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 ClientModelConnector's SERVER/CLIENT_SHIP_INDEX constants.

playerName

private String playerName
The players' name of the client to which this connector is connected
Constructor Detail

ServerModelConnector

private ServerModelConnector(Socket s,
                             int shipIndex)
                      throws IOException
The constructor initializes streams from the given socket.
Parameters:
s - the socket which will be used for network communication
shipIndex - 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

createServerModelConnectors

public static ServerModelConnector[] createServerModelConnectors(String playerName)
                                                          throws IOException
This method should be called to initialize the two connectors. At first it starts to listen for connections. The local client thread is also launched here so that it connects to the right connector. The two connector objects are created when the clients connect to the server. The opcode listener threads for the server connectors are also launched here.
Parameters:
playerName - this local client player's name
Returns:
the array containing the two connectors

run

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

internalClose

private void internalClose(int opCode)
                    throws IOException
Closes the output stream, if it's not already closed and calls the other connector to close its streams too. Finally closes also the input stream. If the close procedure is originated from the local 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 output stream, if it's not already closed and calls the other connector to close its streams too.
Parameters:
opCode - the opCode which initiated the close procedure
Throws:
IOException - if the network fails to run the operations

writeGameOver

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

echoPing

private void echoPing()
               throws IOException
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.
Throws:
IOException - if the network fails to run the operations

flush

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

writeBlowUpAsteroid

public void writeBlowUpAsteroid(int index,
                                double locationX,
                                double locationY,
                                long timestamp)
                         throws IOException
Tells the client to blow up asteroid number index
Parameters:
index - the index of the asteroid to blow up
locationX - the x coordinate of the explosion location
locationY - the y coordinate of the explosion location
timestamp - the moment of explosion in game (server) time
Throws:
IOException - if the network fails to run the operations

writeBlowUpShip

public void writeBlowUpShip(int shipIndex,
                            long newAppears)
                     throws IOException
Tells the client that the ship at shipIndex has blown up.
Parameters:
shipIndex - the index of the space ship
newAppears - the moment in game time when a new ship should appear
Throws:
IOException - if the network fails to run the operations

writeShipsLeft

public final void writeShipsLeft(int shipIndex,
                                 int shipsLeft)
                          throws IOException
Tells the client the number of ships a player has left.
Parameters:
shipIndex - the index of the space ship
shipsLeft - the number of ships left
Throws:
IOException - if the network fails to run the operations

writeInvalidateBullet

public final void writeInvalidateBullet(int shipIndex,
                                        int bulletIndex)
                                 throws IOException
Marks a bullet non-flying (not visible).
Parameters:
shipIndex - the index of the ship owning the bullet
bulletIndex - the index of the bullet
Throws:
IOException - if the network fails to run the operations

writeArena

public final void writeArena(Arena arena)
                      throws IOException
Sends the whole Arena object to the client
Parameters:
arena - the ready-to-send arena
Throws:
IOException - if the network fails to run the operations

writeInitBullet

private 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
Tells the client that a bullet has been fired by the ship controlled by the other client.
Parameters:
shipIndex - the index of the ship owning the bullet
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

readBulletFired

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

writeShipState

private final void writeShipState(int shipIndex,
                                  double locationX,
                                  double locationY,
                                  double velocityX,
                                  double velocityY,
                                  double angle,
                                  long timestamp)
                           throws IOException
Sends the state of the space ship to the client.
Parameters:
shipIndex - the index of the space ship
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 rotation 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
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.
Throws:
IOException - if the network fails to run the operations

writeAsteroidLocationAndAngle

public void writeAsteroidLocationAndAngle(int index,
                                          double locationX,
                                          double locationY,
                                          double angle,
                                          long timestamp)
                                   throws IOException
Tells the client the location and angle of asteroid number index
Parameters:
index - the index of the asteroid to blow up
locationX - the x coordinate of the explosion location
locationY - the y coordinate of the explosion location
angle - the rotation angle of the asteroid
timestamp - the moment of explosion in game (server) time
Throws:
IOException - if the network fails to run the operations

writeScore

public void writeScore(int shipIndex,
                       int score)
                throws IOException
Inform the client of the new scores of a player.
Parameters:
shipIndex - the index of the space ship
score - the player's score
Throws:
IOException - if the network fails to run the operations

readPlayerName

private final void readPlayerName()
                           throws IOException
Read the player name of the client connected to this connector and save it temporarily to the this.playerName field.
Throws:
IOException - if the network fails to run the operations

writePlayerName

private final void writePlayerName(int shipIndex,
                                   String playerName)
                            throws IOException
Write the given playerName of the given shipIndex.
Parameters:
shipIndex - the index of the space ship
playerName - the player's name
Throws:
IOException - if the network fails to run the operations


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