tipgame.net
Class GameConnections

java.lang.Object
  extended by tipgame.net.GameConnections

public class GameConnections
extends java.lang.Object

This is used to receive and transmit data for consistency. The server is the authority of all information. The server sends regular updates and sends a common clock sample. Upon a client connecting, the server sends:

  1. id of the client as an int
  2. keyboard array
  3. mouse array
  4. message array
      When the server starts running, at each frame advance it reads from each client if available:
      1. keyboard
      2. mouse
      3. message
      then it sends to each client:
      1. the current time in seconds as a double
          if any of the keyboard or mouse information has changed, it sends the complete:
        1. keyboard array
        2. mouse array
        3. message array
      The current time is used to keep the clocks in sync. *

      Author:
      Jam Jenkins

      Nested Class Summary
      (package private)  class GameConnections.WriterThread
                repeatedly writes to the clients
       
      Field Summary
      private  boolean[] activeStreams
                the state of the game connections to the server.
      private  double currentTime
                the global clock time in seconds.
      private  java.lang.String gameName
                name of the game
      private  boolean hasReadClients
                dirty bit indicating if the server has read new information from the clients.
      protected  java.io.ObjectInputStream[] in
                all connections inbound.
      private  boolean isPaused
                the state of the advancing of the game
      private  Keyboard[] keyboard
                all of the keyboards of the computers connected
      private  java.lang.Object[] message
                currently unused
      private  Mouse[] mouse
                all of the mouses of the computers connected
      private  java.io.ObjectOutputStream[] out
                all connections outbound.
      private  java.lang.String sessionName
                name of this session
      static int TIME_BETWEEN_UPDATES
                time between sending updates to the clients
      private  java.util.Timer timer
                used to repeatedly write to the clients
       
      Constructor Summary
      GameConnections(java.lang.String gameName, java.lang.String sessionName, int players)
                constructs a server without blocking.
       
      Method Summary
       boolean addConnection(java.io.ObjectOutputStream outStream, java.io.ObjectInputStream inStream)
                adds a connection to the server's list of clients.
      private  void clearInputDevices()
                clears all keys typed and mouses clicked to make sure they are only sent once.
      private  void flushClients()
                flushes what's currently in the output streams to the clients.
      private  int getFirstInactiveStream()
                determines where the next active stream should be added
      private  void handleBrokenConnection(int clientIndex)
                handles exceptions of broken connections.
       boolean isActive()
                determines if any stream is active
       boolean isFull()
                determines if all connections are alive
      private  void keepThreadAlive()
                necessary to keep the timer alive
      private  void readAvailable()
                reads any available new information from the clients.
       boolean readAvailable(int clientIndex)
                reads what's available from the given client without blocking
      private  void sendFirstClientMessage()
                sends the information the client needs to start communications: keyboard array mouse array message array This method sends the arrays as complete objects the first time so that the existing objects can simply be updated on later transmissions.
       void startThreads()
                starts the writing to the clients.
       void write()
                writes to all clients if the game is not paused
      private  void writeClient(int clientIndex)
                writes an update to the client.
      private  void writeClients()
                writes an update to all client.
       
      Methods inherited from class java.lang.Object
      clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
       

      Field Detail

      keyboard

      private Keyboard[] keyboard
      all of the keyboards of the computers connected


      mouse

      private Mouse[] mouse
      all of the mouses of the computers connected


      isPaused

      private boolean isPaused
      the state of the advancing of the game


      TIME_BETWEEN_UPDATES

      public static final int TIME_BETWEEN_UPDATES
      time between sending updates to the clients

      See Also:
      Constant Field Values

      message

      private java.lang.Object[] message
      currently unused


      timer

      private java.util.Timer timer
      used to repeatedly write to the clients


      hasReadClients

      private boolean hasReadClients
      dirty bit indicating if the server has read new information from the clients. This is set to false after each server message is sent and set to true when any client information is read.


      in

      protected java.io.ObjectInputStream[] in
      all connections inbound. For clients this is just a single connection with the server and it is used to receive the array of mouses and keyboards and the current time to keep consistent clocks. For servers, there is one connection per client used to receive the individual keyboards and mouses.


      out

      private java.io.ObjectOutputStream[] out
      all connections outbound. For clients this is just a single connection with the server used to write this client's keyboard and mouse. For servers, there is one connection per client used to write the keyboard and mouse arrays and the current time.


      activeStreams

      private boolean[] activeStreams
      the state of the game connections to the server. True indicates everything is connecteda and active. False indicates disconnected or not yet connected streams.


      gameName

      private java.lang.String gameName
      name of the game


      sessionName

      private java.lang.String sessionName
      name of this session


      currentTime

      private double currentTime
      the global clock time in seconds. Starts at zero.

      Constructor Detail

      GameConnections

      public GameConnections(java.lang.String gameName,
                             java.lang.String sessionName,
                             int players)
      constructs a server without blocking.

      Parameters:
      gameName - the name of the game being played
      sessionName - the name of the current session for a given game
      players - the number of clients connecting
      Method Detail

      isFull

      public boolean isFull()
      determines if all connections are alive

      Returns:
      true if all connections are active, false otherw