/baggle_1.0_src/baggle_client_1.0_src/boggleclient/Server.java
Java | 240 lines | 208 code | 10 blank | 22 comment | 18 complexity | 6595e4362efbebb6503df088f6284d52 MD5 | raw file
Possible License(s): GPL-3.0
- /* Copyright 2010 Edouard Garnier de Labareyre
- *
- * This file is part of B@ggle.
- *
- * B@ggle is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * B@ggle is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with B@ggle. If not, see <http://www.gnu.org/licenses/>.
- */
- package boggleclient;
- import GUI.SolutionsFrame;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.PrintWriter;
- import java.net.Socket;
- import java.net.UnknownHostException;
- import javax.swing.JOptionPane;
- /**
- *
- * @author edouard
- */
- public class Server extends Thread{
- private String server;
- private int port;
- private String name;
- private int avatar_index;
- private String password;
- private String grid;//tmp grid
- public Server(String server, int port, String name , int avatar_index,String password){
- this.server = server;
- this.port = port;
- this.name = name;
- this.avatar_index = avatar_index;
- this.password=password;
- }
- @Override
- public void run(){
- Main.socket = null;
- try {
- Main.socket = new Socket(server, port);
- Main.out = new PrintWriter(Main.socket.getOutputStream(), true);
- Main.in = new BufferedReader(new InputStreamReader(Main.socket.getInputStream()));
- } catch (UnknownHostException e) {
- JOptionPane.showMessageDialog(Main.connectionFrame, "Nom d'hôte inconnu.", "Erreur" , JOptionPane.WARNING_MESSAGE);
- Main.connectionFrame.enableAll();
- return;
- } catch (IOException e) {
- JOptionPane.showMessageDialog(Main.connectionFrame, "Serveur injoignable sur ce port.\n\nĘtes vous bien connectés au reseau ?\nLe serveur est il bien lancé sur le port "+port+" ?\nLe port est il ouvert sur le firewall ?", "Erreur" , JOptionPane.WARNING_MESSAGE);
- Main.connectionFrame.enableAll();
- return;
- } catch (IllegalArgumentException e) {
- JOptionPane.showMessageDialog(Main.connectionFrame, "Numéro de port invalide.", "Erreur" , JOptionPane.WARNING_MESSAGE);
- Main.connectionFrame.enableAll();
- return;
- }
- Main.send(Key.NEW_PLAYER+":"+name.trim()+":"+avatar_index);
- if(password.length()>0){
- Main.send(password);
- }
- System.out.println("Connecté !");
- if(password.length()==0){
- Main.mainFrame.setVisible(true);
- Main.connectionFrame.setVisible(false);
- Main.mainFrame.word_field.grabFocus();
- }
- String packet;
- Key key;
- String message;
- try {
- while ((packet = Main.in.readLine()) != null ) {
- if(packet.contains(":")){
- try{
- key = Key.valueOf(packet.substring(0, packet.indexOf(':')));
- }catch(Exception e){
- key = null;
- }
- if(key!=null){
- message = packet.substring(packet.indexOf(':')+1).trim();
- switch (key){
- case NEW_PLAYER:
- newPlayer_hook();
- Main.mainFrame.addPlayer(message);
- break;
- case DISCONNECTED:
- Main.mainFrame.playersPane.removePlayer(message);
- break;
- case READY:
- Main.mainFrame.playersPane.isReady(message);
- break;
- case PLAYER_STATUS:
- Main.mainFrame.playersPane.statusChanged(message);
- break;
- case CHAT:
- if(Main.is_active && !Main.mainFrame.isActive()){//beep seulement si on a pas deja le focus
- java.awt.Toolkit.getDefaultToolkit().beep();
- }
- Main.mainFrame.chatPane.addMessage(message);
- break;
- case INFO:
- Main.mainFrame.chatPane.addServerInfo(message);
- break;
- case RESET:
- Main.mainFrame.playersPane.wantsToReset(message);
- break;
- case GRID:
- this.grid = message.trim();
- Main.mainFrame.time.setValue(100);
- break;
- case START:
- Main.grid=this.grid;
- Main.in_game=true;
- Main.mainFrame.platal.resetDesStatus();
- Main.mainFrame.refreshCenter();
- Main.mainFrame.playersPane.resetStatus();
- Main.mainFrame.playersPane.setResetIcon();
- Main.mainFrame.wordsPane.reset();
- if(Main.is_active && !Main.mainFrame.isActive()){//beep seulement si on a pas deja le focus
- java.awt.Toolkit.getDefaultToolkit().beep();
- Main.mainFrame.toFront();
- }
- break;
- case GOOD:
- Main.mainFrame.wordsPane.addWord(message, true);
- break;
- case BAD:
- Main.mainFrame.wordsPane.addWord(message, false);
- break;
- case NUMBER:
- Main.mainFrame.playersPane.setGauge(message);
- break;
- case TIMEOUT:
- Main.mainFrame.time.setValue(0);
- if(Main.is_active && !Main.mainFrame.isActive()){//beep seulement si on a pas deja le focus
- java.awt.Toolkit.getDefaultToolkit().beep();
- }
- Main.mainFrame.platal.resetDesStatus();
- Main.mainFrame.playersPane.setReadyIcon();
- Main.mainFrame.playersPane.resetStatus();
- Main.mainFrame.playersPane.resetGauge();
- Main.in_game=false;
- Main.solutionsFrame = new SolutionsFrame();
- break;
- case FOUND_ALONE:
- Main.solutionsFrame.addFoundAlone(message);
- break;
- case FOUND_OTHER:
- Main.solutionsFrame.addFoundOther(message);
- break;
- case SCORE:
- Main.solutionsFrame.setScore(message);
- break;
- case TOTAL_SCORE:
- Main.mainFrame.playersPane.setTotalScore(message);
- break;
- case END_OF_RESULTS:
- if(Main.is_active){
- Main.solutionsFrame.refresh();
- }
- break;
- case PROGRESS:
- Main.mainFrame.time.setValue(Integer.parseInt(message));
- Main.mainFrame.refreshCenter();
- break;
- case STATE:
- Main.mainFrame.playersPane.statusChanged(message);
- break;
- case SERVER_FULL:
- Main.mainFrame.disableAll();
- Main.mainFrame.setVisible(false);
- Main.connectionFrame.enableAll();
- Main.connectionFrame.setVisible(true);
- JOptionPane.showMessageDialog(Main.mainFrame, "Le serveur a atteint le nombre maximum de joueurs autorisés.\nEssayez un autre salon.", "Impossible de se connecter" , JOptionPane.WARNING_MESSAGE);
- break;
- case KICK:
- Main.send(Key.DISCONNECTED+":");
- JOptionPane.showMessageDialog(Main.mainFrame, "Deconnexion automatique du serveur", "B@ggle" , JOptionPane.WARNING_MESSAGE);
- break;
- case WRONG_PASSWORD:
- Main.connectionFrame.enableAll();
- JOptionPane.showMessageDialog(Main.mainFrame, "Password incorrect !", "Salon interdit" , JOptionPane.WARNING_MESSAGE);
- break;
- case GOOD_PASSWORD:
- Main.mainFrame.setVisible(true);
- Main.connectionFrame.setVisible(false);
- Main.mainFrame.word_field.grabFocus();
- break;
- default:
- //do nothing
- break;
- }
- }
- }
- }
- System.out.println("Déconnecté du serveur");
- Main.mainFrame.disableAll();
- }catch (IOException ex) {
- System.out.println("Déconnecté du serveur");
- }
- }
- public String getServerName(){
- if(server.length()==0) return "localhost";
- else return server;
- }
- public String getPlayerName(){
- return name;
- }
- public int getPort(){
- return port;
- }
- public int getPlayerAvatar(){
- return avatar_index;
- }
- private void newPlayer_hook() {
- int a = Main.mainFrame.playersPane.getNumberOfPlayers();
- if(a==1){//means we were alone
- if(!Main.mainFrame.hasFocus()){
- Main.mainFrame.toFront();
- }
- }
- }
- }