Package clueless.controller.server

Source Code of clueless.controller.server.CluelessClientThread

package clueless.controller.server;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;

import clueless.controller.GameController;
import clueless.events.MessageEvent;
import clueless.messaging.Message;
import clueless.messaging.DisconnectMessage;
import clueless.messaging.SendClientPlayerMessage;
import clueless.model.Player;

/**
* Class that implements a thread that handles
* client communication with the server.
*
* @author Ts
*/
public class CluelessClientThread extends Thread {
  private Socket        clientSocket   = null;
  private ObjectInputStream input    = null;
  private ObjectOutputStream output  = null;
  private boolean      keepRunning   = true;
  private GameController  controller = null;
 
  /** Player associated with this client thread */
  private Player player;

  public CluelessClientThread(GameController controller, Socket socket, int clientNum){
    this.controller = controller;
    clientSocket = socket;
    try {
      output = new ObjectOutputStream(new DataOutputStream(clientSocket.getOutputStream()));
      input = new ObjectInputStream(new DataInputStream(clientSocket.getInputStream()));

    } catch (IOException e) {
      System.err.println("Error getting IO streams: "+e.getMessage());
      e.printStackTrace();
    }
    player = controller.getPlayerAtIndex(clientNum);
    send(new SendClientPlayerMessage(player));

  }

  @Override
  public void run(){

    /** Keep listening for messages from client associated with this thread  */
    while(keepRunning && clientSocket.isConnected()) {
     
      Message message = receive();
      if (message instanceof DisconnectMessage){
        keepRunning = false;
        disconnect();
      }
      /** Pass received messages to the controller */
      controller.fireMessageEvent(new MessageEvent(this, message));
    }
  }


  /**
   * Sends a message to client associated with this thread
   * @param message to send to client
   */
  public void send(Message message) {
    try {
      output.writeObject(message);
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }


  /**
   * Receives a message from client associated with this thread
   * @return message received from client
   */
  public Message receive() {
    Message message = null;
   
    try {
      input.read();
      message = (Message)input.readObject();
    } catch (IOException e) {     
    } catch (ClassNotFoundException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
 
    return message;
  }


  /**
   * Shuts down all sending and receiving and closes the socket.
   */
  public void disconnect(){
    keepRunning = false;
    try {
      input.close();
      output.close();
      clientSocket.close();
    } catch (IOException e) {
      System.err.println("Error closing IO streams: "+e.getMessage());
      e.printStackTrace();
    }
  }
 
  /**
   * Setter for Player object reference associated with this client thread
   * @param player
   */
  public void setPlayer(Player player)
  {
    this.player = player;
  }
 
  public Player getPlayer()
  {
    return this.player;
  }
}
TOP

Related Classes of clueless.controller.server.CluelessClientThread

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.