BomberStudentClient.java 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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.net.InetAddress;
  8. import java.util.ArrayList;
  9. import org.json.JSONObject;
  10. import rsx.tcp.TcpClient;
  11. import rsx.udp.Broadcast;
  12. /**
  13. * Connexion à un server BomberStudent
  14. *
  15. * @author Arthur Brandao
  16. */
  17. public class BomberStudentClient {
  18. /**
  19. * Port pour le boradcast UDP
  20. */
  21. protected int portUdp;
  22. /**
  23. * Port pour la socket TCP
  24. */
  25. protected int portTcp;
  26. /**
  27. * Class pour broadcast
  28. */
  29. protected Broadcast finder;
  30. /**
  31. * Class pour le client TCP
  32. */
  33. protected TcpClient socket;
  34. /* --- Constructeurs --- */
  35. /**
  36. * Creation d'un client BomberStudent
  37. * Le port 18624 est utilisé pour le broadcast udp
  38. * Le port 18642 est utilisé pour la socket TCP
  39. */
  40. public BomberStudentClient() {
  41. this.portUdp = 18624;
  42. this.portTcp = 18642;
  43. this.finder = new Broadcast(this.portUdp);
  44. }
  45. /**
  46. * Creation d'un client BomberStudent
  47. * @param portUdp Le port pour le broadcast UDP
  48. * @param portTcp Le port pour la socket TCP
  49. */
  50. public BomberStudentClient(int portUdp, int portTcp) {
  51. this.portUdp = portUdp;
  52. this.portTcp = portTcp;
  53. this.finder = new Broadcast(this.portUdp);
  54. }
  55. /* --- Methodes --- */
  56. /**
  57. * Cherche un serveur par broadcast
  58. * @return Le nombre de serveur trouvé
  59. */
  60. public int findServer() {
  61. this.finder.search("i'm a bomberstudent server");
  62. return this.finder.getServers().size();
  63. }
  64. /**
  65. * Selectionne un serveur pour s'y connecter
  66. * @param index Le numero du serveur dans la liste
  67. * @return Reussite
  68. */
  69. public boolean selectServer(int index) {
  70. ArrayList<InetAddress> serv = this.finder.getServers();
  71. System.out.println(serv.size());
  72. if (!(index >= 0 && index < serv.size())) {
  73. System.err.println("Index invalide");
  74. return false;
  75. }
  76. this.socket = new TcpClient(serv.get(index), this.portTcp);
  77. return this.socket.connect();
  78. }
  79. /**
  80. * Envoi requete sur le serveur
  81. * @param method Methode d'envoi (POST ou GET)
  82. * @param ressource Ressource demandée
  83. * @return Reussite
  84. */
  85. public boolean send(String method, String ressource) {
  86. return this.send(method, ressource, null);
  87. }
  88. /**
  89. * Envoi requete sur le serveur
  90. * @param method Methode d'envoi (POST ou GET)
  91. * @param ressource Ressource demandée
  92. * @param param Parametre JSON à envoyer au serveur
  93. * @return Reussite
  94. */
  95. public boolean send(String method, String ressource, JSONObject param) {
  96. //Verif que la methode existe
  97. method = method.toUpperCase();
  98. if (!method.equals("POST") && !method.equals("GET")) {
  99. return false;
  100. }
  101. //Creation json pour envoi
  102. JSONObject json = new JSONObject();
  103. json.put("methode", method);
  104. json.put("ressource", ressource);
  105. if (param != null) {
  106. json.put("param", param);
  107. }
  108. //Envoi
  109. this.socket.send(json.toString());
  110. return true;
  111. }
  112. /**
  113. * Attend une reponse du serveur
  114. * @return La reponse analysée
  115. */
  116. public BomberStudentReponse receive() {
  117. String msg = this.socket.receive();
  118. if (msg == null) {
  119. return new BomberStudentReponse();
  120. } else {
  121. return new BomberStudentReponse(msg);
  122. }
  123. }
  124. /**
  125. * Ferme la connexion
  126. * @return Reussite
  127. */
  128. public boolean close() {
  129. return this.socket.close();
  130. }
  131. }