tipgame.net
Class Client

java.lang.Object
  extended by tipgame.net.Client
All Implemented Interfaces:
java.util.Observer

public class Client
extends java.lang.Object
implements java.util.Observer

This is used to receive and transmit data for consistency. The client sends its information, buts uses all information from the server as the authoritative source. Clients send new mouse and keyboard information when it changes. Clients receive regular updates from the Server via GameConnection. It reads all available mouse and keyboard information and time advances to update the model. Once all information from the server has been read and the model updated, the screen is refreshed. Upon a client connecting, the client receives:

  1. id of the client as an int
  2. keyboard array
  3. mouse array
  4. message array
      When the server starts running, the client sends to the server the following when they change:
      1. keyboard
      2. mouse
      3. message
      At regular intervals, the client receives from the server:
      1. the current time as a double
      2. if any of the below have changed, they are all sent
        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 Client.ReaderThread
                reads repeatedly from the server and refreshes the screen.
       
      Field Summary
      private  FrameAdvancer frameAdvancer
                the frameAdvancer is used for updating the model and screen upon receiving server information
      private  java.lang.String game
                the name of the current game
      private  int id
                id of this client.
      private  java.io.ObjectInputStream in
                all connections inbound.
      private  boolean isPaused
                since server communication ceases during a puased game, this variable being set to true means the game is paused (not necessarily lost connections to server)
       Keyboard[] keyboard
                all of the keyboards of the computers connected
      private  Keyboard localKeyboard
                the Keyboard trapping local keyboard events
      private  java.lang.Object localMessage
                unused and untested
      private  Mouse localMouse
                the Mouse trapping local mouse events
      private  java.lang.String machine
                the domain the server is on
       java.lang.Object[] message
                current unused and untested
       Mouse[] mouse
                all of the mouses of the computers connected
      private  java.io.ObjectOutputStream out
                all connections outbound.
      private  int players
                the number of players
      private  javax.swing.Timer readTimer
                the timer used to poll for new server information
      private  boolean sendPause
                pause is toggled by settting sendPause to true
      private  java.lang.String session
                the name of the currently running session
       
      Constructor Summary
      Client(java.lang.String machine, java.lang.String game, java.lang.String session, int players)
                sets the domain, game and session name and connects to the server.
       
      Method Summary
       void connect()
                Connects to the server then goes into an infinite loop sending and receiving consistency information from and to the server.
      private  void connectToServer()
                tries to connect to an existing server.
       void disconnect()
                disconnects and terminates sending and receiving informations to and from the server
       int getID()
                gets the id of the current client.
       boolean isPaused()
                determines if the game is currently advancing
       void pauseToggle()
                if the game was paused, this method will set it running again.
      private  boolean read()
                reads in from the server: current time if the are changed since the last message received it also reads keyboard array mouse array message array
       void setFrameAdvancer(FrameAdvancer frameAdvancer)
                 
       void setLocalKeyboard(Keyboard localKeyboard)
                 
       void setLocalMessage(java.lang.Object localMessage)
                 
       void setLocalMouse(Mouse localMouse)
                 
      private  void tryToConnect()
                tries to connect to the designated machine and port
      private  void tryToConnect(Server server)
                starts piped input and output to the local server
       void update(java.util.Observable arg0, java.lang.Object arg1)
                this method is called when there is new information to send to the server.
      private  void write()
                If the game is paused, sends nothing unless it is unpause.
       
      Methods inherited from class java.lang.Object
      clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
       

      Field Detail

      id

      private int id
      id of this client. Ids start at zero and go to the number of clients - 1.


      keyboard

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


      mouse

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


      message

      public java.lang.Object[] message
      current unused and untested


      localKeyboard

      private Keyboard localKeyboard
      the Keyboard trapping local keyboard events


      localMouse

      private Mouse localMouse
      the Mouse trapping local mouse events


      localMessage

      private java.lang.Object localMessage
      unused and untested


      frameAdvancer

      private FrameAdvancer frameAdvancer
      the frameAdvancer is used for updating the model and screen upon receiving server information


      in

      private 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.


      readTimer

      private javax.swing.Timer readTimer
      the timer used to poll for new server information


      game

      private java.lang.String game
      the name of the current game


      session

      private java.lang.String session
      the name of the currently running session


      machine

      private java.lang.String machine
      the domain the server is on


      players

      private int players
      the number of players


      sendPause

      private boolean sendPause
      pause is toggled by settting sendPause to true


      isPaused

      private boolean isPaused
      since server communication ceases during a puased game, this variable being set to true means the game is paused (not necessarily lost connections to server)

      Constructor Detail

      Client

      public Client(java.lang.String machine,
                    java.lang.String game,
                    java.lang.String session,
                    int players)
      sets the domain, game and session name and connects to the server.

      Parameters:
      machine - the domain of the server
      game - the name of the game
      session - the name of the current session
      the - number of players to wait for (only used for the first person starting the session)
      Method Detail

      setLocalMessage

      public void setLocalMessage(java.lang.Object localMessage)
      Parameters:
      localMessage - The localMessage to set.

      setFrameAdvancer

      public void setFrameAdvancer(FrameAdvancer frameAdvancer)
      Parameters:
      frameAdvancer - The frameAdvancer to set.

      setLocalKeyboard

      public void setLocalKeyboard(Keyboard localKeyboard)
      Parameters:
      localKeyboard - The localKeyboard to set.

      setLocalMouse

      public void setLocalMouse(Mouse localMouse)
      Parameters:
      localMouse - The localMouse to set.

      tryToConnect

      private void tryToConnect()
                         throws java.lang.Exception
      tries to connect to the designated machine and port

      Throws:
      java.lang.Exception - if no server is running on the machine and port

      tryToConnect

      private void tryToConnect(Server server)
                         throws java.lang.Exception
      starts piped input and output to the local server

      Parameters:
      server - the local server
      Throws:
      java.lang.Exception - if the connection cannot be established

      connectToServer

      private void connectToServer()
      tries to connect to an existing server. If no server exists, a server is started on the local machine. If a one player game is started, pipes are set up to connect since outside connections will not be necessary. If a multiplayer game is started, then a server is started on the local machine which can accept outside connections.


      write

      private void write()
                  throws java.io.IOException
      If the game is paused, sends nothing unless it is unpause. If the game is not paused, writes this client's:
      1. keyboard
      2. mouse
      3. message

      Throws:
      java.io.IOException - if the connection to the server is disrupted

      pauseToggle

      public void pauseToggle()
      if the game was paused, this method will set it running again. If the game was not paused, this method will pause the game.


      read

      private boolean read()
                    throws java.io.IOException,
                           java.lang.ClassNotFoundException
      reads in from the server:
      1. current time
      2. if the are changed since the last message received it also reads
        1. keyboard array
        2. mouse array
        3. message array

      Throws:
      java.io.IOException - if the connection to the server is disupted
      java.lang.ClassNotFoundException - if there are class loading problems

      connect

      public void connect()
      Connects to the server then goes into an infinite loop sending and receiving consistency information from and to the server. See class comment for further details

      See Also:
      Runnable.run()

      disconnect

      public void disconnect()
      disconnects and terminates sending and receiving informations to and from the server


      isPaused

      public boolean isPaused()
      determines if the game is currently advancing

      Returns:
      true if the game has temporarily stopped advancing, false otherwise

      getID

      public int getID()
      gets the id of the current client. This id corresponds to the index of this client in the array of mouse and keyboard information received from the client.

      Returns:
      the id of the current client, also the same as the index of this client in the playerMouse and playerKeyboard arrays

      update

      public void update(java.util.Observable arg0,
                         java.lang.Object arg1)
      this method is called when there is new information to send to the server. It is called when new keyboard and/or mouse information is received and when the game is paused or resumed.

      Specified by:
      update in interface java.util.Observer
      Parameters:
      arg0 - not used
      arg1 - not used
      See Also:
      Observer.update(java.util.Observable, java.lang.Object)