PageRenderTime 26ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/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
  1. /* Copyright 2010 Edouard Garnier de Labareyre
  2. *
  3. * This file is part of B@ggle.
  4. *
  5. * B@ggle is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * B@ggle is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with B@ggle. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. package boggleclient;
  19. import GUI.SolutionsFrame;
  20. import java.io.BufferedReader;
  21. import java.io.IOException;
  22. import java.io.InputStreamReader;
  23. import java.io.PrintWriter;
  24. import java.net.Socket;
  25. import java.net.UnknownHostException;
  26. import javax.swing.JOptionPane;
  27. /**
  28. *
  29. * @author edouard
  30. */
  31. public class Server extends Thread{
  32. private String server;
  33. private int port;
  34. private String name;
  35. private int avatar_index;
  36. private String password;
  37. private String grid;//tmp grid
  38. public Server(String server, int port, String name , int avatar_index,String password){
  39. this.server = server;
  40. this.port = port;
  41. this.name = name;
  42. this.avatar_index = avatar_index;
  43. this.password=password;
  44. }
  45. @Override
  46. public void run(){
  47. Main.socket = null;
  48. try {
  49. Main.socket = new Socket(server, port);
  50. Main.out = new PrintWriter(Main.socket.getOutputStream(), true);
  51. Main.in = new BufferedReader(new InputStreamReader(Main.socket.getInputStream()));
  52. } catch (UnknownHostException e) {
  53. JOptionPane.showMessageDialog(Main.connectionFrame, "Nom d'hôte inconnu.", "Erreur" , JOptionPane.WARNING_MESSAGE);
  54. Main.connectionFrame.enableAll();
  55. return;
  56. } catch (IOException e) {
  57. 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);
  58. Main.connectionFrame.enableAll();
  59. return;
  60. } catch (IllegalArgumentException e) {
  61. JOptionPane.showMessageDialog(Main.connectionFrame, "Numéro de port invalide.", "Erreur" , JOptionPane.WARNING_MESSAGE);
  62. Main.connectionFrame.enableAll();
  63. return;
  64. }
  65. Main.send(Key.NEW_PLAYER+":"+name.trim()+":"+avatar_index);
  66. if(password.length()>0){
  67. Main.send(password);
  68. }
  69. System.out.println("Connecté !");
  70. if(password.length()==0){
  71. Main.mainFrame.setVisible(true);
  72. Main.connectionFrame.setVisible(false);
  73. Main.mainFrame.word_field.grabFocus();
  74. }
  75. String packet;
  76. Key key;
  77. String message;
  78. try {
  79. while ((packet = Main.in.readLine()) != null ) {
  80. if(packet.contains(":")){
  81. try{
  82. key = Key.valueOf(packet.substring(0, packet.indexOf(':')));
  83. }catch(Exception e){
  84. key = null;
  85. }
  86. if(key!=null){
  87. message = packet.substring(packet.indexOf(':')+1).trim();
  88. switch (key){
  89. case NEW_PLAYER:
  90. newPlayer_hook();
  91. Main.mainFrame.addPlayer(message);
  92. break;
  93. case DISCONNECTED:
  94. Main.mainFrame.playersPane.removePlayer(message);
  95. break;
  96. case READY:
  97. Main.mainFrame.playersPane.isReady(message);
  98. break;
  99. case PLAYER_STATUS:
  100. Main.mainFrame.playersPane.statusChanged(message);
  101. break;
  102. case CHAT:
  103. if(Main.is_active && !Main.mainFrame.isActive()){//beep seulement si on a pas deja le focus
  104. java.awt.Toolkit.getDefaultToolkit().beep();
  105. }
  106. Main.mainFrame.chatPane.addMessage(message);
  107. break;
  108. case INFO:
  109. Main.mainFrame.chatPane.addServerInfo(message);
  110. break;
  111. case RESET:
  112. Main.mainFrame.playersPane.wantsToReset(message);
  113. break;
  114. case GRID:
  115. this.grid = message.trim();
  116. Main.mainFrame.time.setValue(100);
  117. break;
  118. case START:
  119. Main.grid=this.grid;
  120. Main.in_game=true;
  121. Main.mainFrame.platal.resetDesStatus();
  122. Main.mainFrame.refreshCenter();
  123. Main.mainFrame.playersPane.resetStatus();
  124. Main.mainFrame.playersPane.setResetIcon();
  125. Main.mainFrame.wordsPane.reset();
  126. if(Main.is_active && !Main.mainFrame.isActive()){//beep seulement si on a pas deja le focus
  127. java.awt.Toolkit.getDefaultToolkit().beep();
  128. Main.mainFrame.toFront();
  129. }
  130. break;
  131. case GOOD:
  132. Main.mainFrame.wordsPane.addWord(message, true);
  133. break;
  134. case BAD:
  135. Main.mainFrame.wordsPane.addWord(message, false);
  136. break;
  137. case NUMBER:
  138. Main.mainFrame.playersPane.setGauge(message);
  139. break;
  140. case TIMEOUT:
  141. Main.mainFrame.time.setValue(0);
  142. if(Main.is_active && !Main.mainFrame.isActive()){//beep seulement si on a pas deja le focus
  143. java.awt.Toolkit.getDefaultToolkit().beep();
  144. }
  145. Main.mainFrame.platal.resetDesStatus();
  146. Main.mainFrame.playersPane.setReadyIcon();
  147. Main.mainFrame.playersPane.resetStatus();
  148. Main.mainFrame.playersPane.resetGauge();
  149. Main.in_game=false;
  150. Main.solutionsFrame = new SolutionsFrame();
  151. break;
  152. case FOUND_ALONE:
  153. Main.solutionsFrame.addFoundAlone(message);
  154. break;
  155. case FOUND_OTHER:
  156. Main.solutionsFrame.addFoundOther(message);
  157. break;
  158. case SCORE:
  159. Main.solutionsFrame.setScore(message);
  160. break;
  161. case TOTAL_SCORE:
  162. Main.mainFrame.playersPane.setTotalScore(message);
  163. break;
  164. case END_OF_RESULTS:
  165. if(Main.is_active){
  166. Main.solutionsFrame.refresh();
  167. }
  168. break;
  169. case PROGRESS:
  170. Main.mainFrame.time.setValue(Integer.parseInt(message));
  171. Main.mainFrame.refreshCenter();
  172. break;
  173. case STATE:
  174. Main.mainFrame.playersPane.statusChanged(message);
  175. break;
  176. case SERVER_FULL:
  177. Main.mainFrame.disableAll();
  178. Main.mainFrame.setVisible(false);
  179. Main.connectionFrame.enableAll();
  180. Main.connectionFrame.setVisible(true);
  181. 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);
  182. break;
  183. case KICK:
  184. Main.send(Key.DISCONNECTED+":");
  185. JOptionPane.showMessageDialog(Main.mainFrame, "Deconnexion automatique du serveur", "B@ggle" , JOptionPane.WARNING_MESSAGE);
  186. break;
  187. case WRONG_PASSWORD:
  188. Main.connectionFrame.enableAll();
  189. JOptionPane.showMessageDialog(Main.mainFrame, "Password incorrect !", "Salon interdit" , JOptionPane.WARNING_MESSAGE);
  190. break;
  191. case GOOD_PASSWORD:
  192. Main.mainFrame.setVisible(true);
  193. Main.connectionFrame.setVisible(false);
  194. Main.mainFrame.word_field.grabFocus();
  195. break;
  196. default:
  197. //do nothing
  198. break;
  199. }
  200. }
  201. }
  202. }
  203. System.out.println("Déconnecté du serveur");
  204. Main.mainFrame.disableAll();
  205. }catch (IOException ex) {
  206. System.out.println("Déconnecté du serveur");
  207. }
  208. }
  209. public String getServerName(){
  210. if(server.length()==0) return "localhost";
  211. else return server;
  212. }
  213. public String getPlayerName(){
  214. return name;
  215. }
  216. public int getPort(){
  217. return port;
  218. }
  219. public int getPlayerAvatar(){
  220. return avatar_index;
  221. }
  222. private void newPlayer_hook() {
  223. int a = Main.mainFrame.playersPane.getNumberOfPlayers();
  224. if(a==1){//means we were alone
  225. if(!Main.mainFrame.hasFocus()){
  226. Main.mainFrame.toFront();
  227. }
  228. }
  229. }
  230. }