123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 |
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package rsx;
- import java.net.InetAddress;
- import java.util.ArrayList;
- import org.json.JSONObject;
- import rsx.tcp.TcpClient;
- import rsx.udp.Broadcast;
- /**
- * Connexion à un server BomberStudent
- *
- * @author Arthur Brandao
- */
- public class BomberStudentClient {
- /**
- * Port pour le boradcast UDP
- */
- protected int portUdp;
-
- /**
- * Port pour la socket TCP
- */
- protected int portTcp;
-
- /**
- * Class pour broadcast
- */
- protected Broadcast finder;
-
- /**
- * Class pour le client TCP
- */
- protected TcpClient socket;
- /* --- Constructeurs --- */
- /**
- * Creation d'un client BomberStudent
- * Le port 18624 est utilisé pour le broadcast udp
- * Le port 18642 est utilisé pour la socket TCP
- */
- public BomberStudentClient() {
- this.portUdp = 18624;
- this.portTcp = 18642;
- this.finder = new Broadcast(this.portUdp);
- }
- /**
- * Creation d'un client BomberStudent
- * @param portUdp Le port pour le broadcast UDP
- * @param portTcp Le port pour la socket TCP
- */
- public BomberStudentClient(int portUdp, int portTcp) {
- this.portUdp = portUdp;
- this.portTcp = portTcp;
- this.finder = new Broadcast(this.portUdp);
- }
- /* --- Methodes --- */
- /**
- * Cherche un serveur par broadcast
- * @return Le nombre de serveur trouvé
- */
- public int findServer() {
- this.finder.search("i'm a bomberstudent server");
- return this.finder.getServers().size();
- }
- /**
- * Selectionne un serveur pour s'y connecter
- * @param index Le numero du serveur dans la liste
- * @return Reussite
- */
- public boolean selectServer(int index) {
- ArrayList<InetAddress> serv = this.finder.getServers();
- System.out.println(serv.size());
- if (!(index >= 0 && index < serv.size())) {
- System.err.println("Index invalide");
- return false;
- }
- this.socket = new TcpClient(serv.get(index), this.portTcp);
- return this.socket.connect();
- }
- /**
- * Envoi requete sur le serveur
- * @param method Methode d'envoi (POST ou GET)
- * @param ressource Ressource demandée
- * @return Reussite
- */
- public boolean send(String method, String ressource) {
- return this.send(method, ressource, null);
- }
- /**
- * Envoi requete sur le serveur
- * @param method Methode d'envoi (POST ou GET)
- * @param ressource Ressource demandée
- * @param param Parametre JSON à envoyer au serveur
- * @return Reussite
- */
- public boolean send(String method, String ressource, JSONObject param) {
- //Verif que la methode existe
- method = method.toUpperCase();
- if (!method.equals("POST") && !method.equals("GET")) {
- return false;
- }
- //Creation json pour envoi
- JSONObject json = new JSONObject();
- json.put("methode", method);
- json.put("ressource", ressource);
- if (param != null) {
- json.put("param", param);
- }
- //Envoi
- this.socket.send(json.toString());
- return true;
- }
- /**
- * Attend une reponse du serveur
- * @return La reponse analysée
- */
- public BomberStudentReponse receive() {
- String msg = this.socket.receive();
- if (msg == null) {
- return new BomberStudentReponse();
- } else {
- return new BomberStudentReponse(msg);
- }
- }
- /**
- * Ferme la connexion
- * @return Reussite
- */
- public boolean close() {
- return this.socket.close();
- }
- }
|