package pdp.scrabble.multiplayer.impl;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.Observable;
import java.util.Observer;
import pdp.scrabble.game.BoardCase;
import pdp.scrabble.game.BoardCaseState;
import pdp.scrabble.game.Letter;
import pdp.scrabble.ihm.MainFrame;
import pdp.scrabble.ihm.MainFrame_old;
import pdp.scrabble.ihm.MultiplayerPanel;
import pdp.scrabble.ihm.GameStatistics;
import pdp.scrabble.multiplayer.Client;
import pdp.scrabble.multiplayer.Server;
import pdp.scrabble.utility.Display;
import static pdp.scrabble.Language.getGameLang;
/**
*/
public class ClientImpl extends UnicastRemoteObject implements Client, Observer {
private static final long serialVersionUID = 1L;
/** Server reference. */
private Server server = null;
/** Main frame reference. */
private MainFrame mainFrame = null;
/** Multiplayer panel reference. */
private MultiplayerPanel multiplayerPanel = null;
/** Client name. */
private String name = null;
/** Open flag. */
private boolean isOpen;
/** Name existence flag. */
private boolean existName = false;
/** Player id. */
private int idPlayer = 0;
/** Thread check */
private Thread check = null;
/** Thread check server existence state. */
private boolean threadCheck = false;
/** Create a new client.
* @param mainFrame2 main frame reference.
* @param name player name.
* @throws RemoteException exception.
*/
public ClientImpl(MainFrame mainFrame2, String name) throws RemoteException {
super();
this.name = name;
this.mainFrame = mainFrame2;
if (this.mainFrame != null) {
this.multiplayerPanel = mainFrame2.getMultiplayerPanel();
}
}
public Server connect(
MainFrame_old mainFrame, String host, String port, String nick,
String language)
throws RemoteException, NotBoundException, MalformedURLException {
String url = "rmi://" + host + ":" + port + "/Scrabble";
// Look up for the Remote Object Server from RMI registry
this.server = (Server) Naming.lookup(url);
this.isOpen = this.server.isOpen();
this.existName = this.server.existName(nick);
if(!this.server.getLanguage().equals(language)){
showDialog("Server", "Server language is in",this.server.getLanguage());
this.server = null;
return this.server;
}
if (this.isOpen && !this.existName) {
this.server.connect(nick, this);
this.threadCheck = true;
this.check = new Thread() {
@Override
public void run() {
setName("Server alive check");
while (threadCheck) {
try {
Thread.sleep(TIMEOUT);
}
catch (InterruptedException e1) {
//Display.error("Sleep", "Error");
}
try {
server.ping();
}
catch (Exception e) {
try {
getSetPlayerTable(0, null, getGameLang("Disconnect"), -1);
showDialog("Server", "The server is out, "
+ "you must leave this party !");
server = null;
}
catch (RemoteException ex) {
Display.error("Disconnect", "Error RMI");
}
}
}
}
};
this.check.start();
}
return this.server;
}
public void disconnect(boolean creator)
throws RemoteException, MalformedURLException {
this.threadCheck = false;
if (this.server != null) {
if (creator) {
this.server.endServer();
}
this.server.disconnect(this.idPlayer);
}
}
public void launchGameMulti() throws RemoteException {
try {
this.multiplayerPanel.getAction().launchGameMulti();
}
catch (InterruptedException ex) {
Display.error("Client", "Error while starting game !");
}
}
public void update(Observable o, Object arg) {
BoardCase c = (BoardCase) arg;
try {
this.server.sendLetter(this, c.getLetter(), c.getV(), c.getH());
}
catch (RemoteException ex) {
}
}
public void getLetter(Letter letter, int v, int h) throws RemoteException {
BoardCase boardCase = this.mainFrame.getGameEnv().board().getCase(v, h);
boardCase.setLetter(letter);
boardCase.setState(BoardCaseState.OLD);
boardCase.setMultUsed(true);
this.mainFrame.repaint();
}
public void sendMessage(String name, String msg) throws RemoteException {
this.server.sendMessage(name, msg);
}
public void getMessage(String src, String msg) throws RemoteException {
if (this.multiplayerPanel != null) {
this.multiplayerPanel.updateMessageTchat(src, msg);
}
}
public String getName() throws RemoteException {
return this.name;
}
public void getTurn(String name) throws RemoteException {
this.mainFrame.getTurn(name);
}
public boolean isOpen() throws RemoteException {
return this.isOpen;
}
public boolean existName(String name) throws RemoteException {
return this.existName;
}
public void setIdClient(int id) throws RemoteException {
this.idPlayer = id;
}
public boolean ping() throws RemoteException {
return true;
}
public void getStats(int turn, String name, int points, String words)
throws RemoteException {
this.mainFrame.getOptionsPanel().getAction().addStat(
new GameStatistics(turn, name, points, words));
}
public void getAddPlayerTable(String name, String status, int score)
throws RemoteException {
if (this.multiplayerPanel != null) {
this.multiplayerPanel.addTable(name, status, score);
}
}
public void getRemovePlayerTable(int id) throws RemoteException {
if (this.multiplayerPanel != null) {
this.multiplayerPanel.removeTable(id);
}
}
public void getSetPlayerTable(int id, String name, String status, int score)
throws RemoteException {
if (this.multiplayerPanel != null) {
this.multiplayerPanel.setTable(id, name, status, score);
}
}
public void clearTable() throws RemoteException {
if (this.multiplayerPanel != null) {
this.multiplayerPanel.clearTable();
}
}
public void showDialog(String head, String message) throws RemoteException {
this.multiplayerPanel.showDialog(head, message);
}
public void showDialog(String head, String message, String add) throws RemoteException {
this.multiplayerPanel.showDialog(head, message,add);
}
public void updateButton() throws RemoteException {
if (this.multiplayerPanel != null) {
this.multiplayerPanel.getButton("Ready").setEnabled(true);
this.multiplayerPanel.getButton("Ready").setText(getGameLang("Ready"));
}
}
}