Package client

Source Code of client.CommandExecutor

package client;

import game.dice.Dice;
import game.gamesheet.GameSheet;
import game.leaderboard.Leaderboard;
import general.statics.function.ErrorPumper;

import java.util.LinkedList;

import server.constants.CommandList;
import server.protocol.Message;
import server.protocol.PlayerList;
import client.frame.GameplayFrame;
import client.frame.StartupFrame;
/**
* The <code>CommandExecutor</code> class defines a subclass of <code>Thread</code> that waits on the connection's
* inputStack. Executes changes in the state of the client and orders the GUI to redraw if neccessary.
* Implements the producer-consumer pattern as the consumer (With an object of type <code>SocketListener</code> as the producer).
*
* @author Priidu Neemre
*/
public class CommandExecutor extends Thread{
 
  private Client client;
  private LinkedList<Object> inputStack;
  private boolean done = false;
 
 
  public CommandExecutor(Client client){
    this.client = client;
    inputStack = client.getConnection().getInputStack();
  }

  public void run(){
    while(!done){
      Object inObj = null;
      synchronized(inputStack){
        while(inputStack.isEmpty()){
          try {
            inputStack.wait();
          } catch (InterruptedException e) {
            ErrorPumper.errorMsg(ErrorPumper.ERR014_COMMANDEXECUTOR_SLEEP_DISRUPT, e);
          }
        }
        inObj = inputStack.removeLast();
        inputStack.notifyAll();
      }   
      decodeInput(inObj);
    }
  }
 
  /**
   * A method for identfying and acting accordingly to the object received from the inputStack of
   * the connection module.
   *
   * @param inObj  the object received from the inputStack of the connection module
   */
  public void decodeInput(Object inObj){
    if(inObj == null)client.getConnection().broadcast(CommandList.GOTNULLCMD);
   
    switch(client.getState()){
    case INITIATING_CONNECTION:
      if(inObj.getClass().getSimpleName().equals("Dice")){
        Dice initDice = (Dice)inObj;
        client.setDice(initDice);
      }
      if(inObj.getClass().getSimpleName().equals("GameSheet")){
        GameSheet initSheet = (GameSheet)inObj;
        client.setGameSheet(initSheet);
      }
      if(inObj.getClass().getSimpleName().equals("Leaderboard")){
        Leaderboard initBoard = (Leaderboard)inObj;
        client.setLeaderboard(initBoard);
      }
      if(inObj.getClass().getSimpleName().equals("PlayerList")){
        PlayerList refPlayers = (PlayerList)inObj;
        client.getJoinedList().refreshPlayerList(refPlayers);
        client.getCurrentFrame().updateGUIForeign();
      }
      if(inObj.getClass().getSimpleName().equals("Message")){
        Message inboundMsg = (Message)inObj;
        processMessageInput(inboundMsg);
      }
      //client.getConnection().sendRcvdConfirmation(inObj);    //TEMPORARILY DISABLED
      break;
    case GAME_IN_PROGRESS:
      if(inObj.getClass().getSimpleName().equals("Dice")){
        Dice refreshedDice = (Dice)inObj;
        client.getDice().refershDice(refreshedDice);
        client.getCurrentFrame().updateGUIForeign();
        //System.out.println("Hetket�ringute (" + client.getDice().getDiceValues() + ") omanik: " + client.getDice().getCurOwner());
      }
      if(inObj.getClass().getSimpleName().equals("GameSheet")){
        GameSheet refGameSheet = (GameSheet)inObj;
        client.getGameSheet().refreshGameSheet(refGameSheet);
        client.getCurrentFrame().updateGUIForeign();
      }
      if(inObj.getClass().getSimpleName().equals("Leaderboard")){
        Leaderboard refLeaderboard = (Leaderboard)inObj;
        client.getLeaderboard().refreshLeaderboard(refLeaderboard);
        client.getCurrentFrame().updateGUIForeign();
      }
      if(inObj.getClass().getSimpleName().equals("Message")){
        Message inboundMsg = (Message)inObj;
        processMessageInput(inboundMsg);
      }
      //client.getConnection().sendRcvdConfirmation(inObj);    //TEMPORARILY DISABLED
      break;
    default:
      break;
    }
  }
 
  /**
   * A method for identfying and acting accordingly to a message received from the inputStack of
   * the connection module.
   *
   * @param inboundMsg  the message received from the inputStack of the connection module
   */
  public void processMessageInput(Message inboundMsg){
    //System.out.println("Message contents was: " + inboundMsg.getContents());    //DEBUG INFORMATION
    String msgContents = inboundMsg.getContents().toUpperCase();
    if(msgContents.equals(CommandList.RESETCMD)){  //Reset the game turn countdown timer of the client
      client.getTurnTimer().resetCountdown();  
      client.getCurrentFrame().updateGUIForeign();
    }else if(msgContents.equals(CommandList.GAMEOVERCMD)){
      ((GameplayFrame)client.getCurrentFrame()).notifyGameFinished();
    }else if(msgContents.equals(CommandList.HIGHSCORECMD)){
      ((GameplayFrame)client.getCurrentFrame()).notifyNewHighScore();
    }else if(msgContents.equals(CommandList.NAMEINUSECMD)){
      ((StartupFrame)client.getCurrentFrame()).notifyNameInUse();
    }else{
      client.getCurrentFrame().writeToLogAreaForeign(inboundMsg.getContents() + "\n");
    }
  }
 
  //[START]GETTERS AND SETTERS
  public boolean getDone(){
    return done;
  }
  public void setDone(Boolean done){
    this.done = done;
  }
  //[END]GETTERS AND SETTERS
}
TOP

Related Classes of client.CommandExecutor

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.