BomberStudentClient.java 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package rsx;
  7. import java.io.IOException;
  8. import java.net.InetAddress;
  9. import java.util.ArrayList;
  10. import java.util.HashMap;
  11. import org.json.JSONException;
  12. import org.json.JSONObject;
  13. import rsx.tcp.TcpClient;
  14. import rsx.udp.Broadcast;
  15. /**
  16. * Connexion à un server BomberStudent
  17. *
  18. * @author Arthur Brandao
  19. */
  20. public class BomberStudentClient {
  21. /**
  22. * Port pour le boradcast UDP
  23. */
  24. protected int portUdp;
  25. /**
  26. * Port pour la socket TCP
  27. */
  28. protected int portTcp;
  29. /**
  30. * Class pour broadcast
  31. */
  32. protected Broadcast finder;
  33. /**
  34. * Class pour le client TCP
  35. */
  36. protected TcpClient socket;
  37. /**
  38. * Class de gestion des requetes serveurs
  39. */
  40. protected BomberStudentRequest request;
  41. /**
  42. * Liste des handlers pour gerer les requetes du serveur
  43. */
  44. protected HashMap<String, BomberStudentHandler> handlers = new HashMap<>();
  45. /* --- Constructeurs --- */
  46. /**
  47. * Creation d'un client BomberStudent
  48. * Le port 18624 est utilisé pour le broadcast udp
  49. * Le port 18642 est utilisé pour la socket TCP
  50. */
  51. public BomberStudentClient() {
  52. this.portUdp = 18624;
  53. this.portTcp = 18642;
  54. this.finder = new Broadcast(this.portUdp);
  55. }
  56. /**
  57. * Creation d'un client BomberStudent
  58. * @param portUdp Le port pour le broadcast UDP
  59. * @param portTcp Le port pour la socket TCP
  60. */
  61. public BomberStudentClient(int portUdp, int portTcp) {
  62. this.portUdp = portUdp;
  63. this.portTcp = portTcp;
  64. this.finder = new Broadcast(this.portUdp);
  65. }
  66. /* --- Methodes --- */
  67. /**
  68. * Ajoute un handler
  69. * @param ressource Le nom de la ressource associè au handler
  70. * @param handler Le handler
  71. */
  72. public void addHandler(String ressource, BomberStudentHandler handler){
  73. this.handlers.put(ressource, handler);
  74. }
  75. /**
  76. * Cherche un serveur par broadcast
  77. * @return Le nombre de serveur trouvé
  78. */
  79. public int findServer() {
  80. this.finder.search("i'm a bomberstudent server");
  81. return this.finder.getServers().size();
  82. }
  83. /**
  84. * Selectionne un serveur pour s'y connecter
  85. * @param index Le numero du serveur dans la liste
  86. * @return Reussite
  87. */
  88. public boolean selectServer(int index) {
  89. ArrayList<InetAddress> serv = this.finder.getServers();
  90. System.out.println(serv.size());
  91. if (!(index >= 0 && index < serv.size())) {
  92. System.err.println("Index invalide");
  93. return false;
  94. }
  95. //Lancement gestionnaire des requetes serveur
  96. try {
  97. this.request = new BomberStudentRequest(serv.get(index), this.portTcp + 1, handlers);
  98. this.request.start();
  99. } catch (IOException ex) {
  100. return false;
  101. }
  102. //Creation socket d'envoi
  103. this.socket = new TcpClient(serv.get(index), this.portTcp);
  104. return this.socket.connect();
  105. }
  106. /**
  107. * Envoi requete sur le serveur
  108. * @param method Methode d'envoi (POST ou GET)
  109. * @param ressource Ressource demandée
  110. * @return Reussite
  111. */
  112. public boolean send(String method, String ressource) {
  113. return this.send(method, ressource, null);
  114. }
  115. /**
  116. * Envoi requete sur le serveur
  117. * @param method Methode d'envoi (POST ou GET)
  118. * @param ressource Ressource demandée
  119. * @param param Parametre JSON à envoyer au serveur
  120. * @return Reussite
  121. */
  122. public boolean send(String method, String ressource, JSONObject param) {
  123. //Verif que la methode existe
  124. method = method.toUpperCase();
  125. if (!method.equals("POST") && !method.equals("GET")) {
  126. return false;
  127. }
  128. //Creation json pour envoi
  129. JSONObject json = new JSONObject();
  130. json.put("methode", method);
  131. json.put("ressource", ressource);
  132. if (param != null) {
  133. json.put("param", param);
  134. }
  135. //Envoi
  136. this.socket.send(json.toString());
  137. return true;
  138. }
  139. /**
  140. * Attend une reponse du serveur
  141. * @return La reponse analysée
  142. */
  143. public JSONObject receive() {
  144. String msg = this.socket.receive();
  145. try {
  146. return new JSONObject(msg);
  147. } catch(JSONException ex){
  148. System.err.println("La reponse n'est pas en JSON : " + ex.getMessage());
  149. return null;
  150. }
  151. }
  152. /**
  153. * Ferme la connexion
  154. * @return Reussite
  155. */
  156. public boolean close() {
  157. this.request.interrupt();
  158. //Verif que le thread est bien arréte
  159. try {
  160. Thread.sleep(3000);
  161. if(this.request.isAlive()){
  162. System.err.println("Imposible de tuer le thread de gestion des requetes");
  163. }
  164. } catch (InterruptedException ex) {
  165. }
  166. return this.socket.close();
  167. }
  168. }