|
@@ -13,73 +13,79 @@ import rsx.udp.Broadcast;
|
|
|
|
|
|
/**
|
|
|
* Connexion à un server BomberStudent
|
|
|
+ *
|
|
|
* @author Arthur Brandao
|
|
|
*/
|
|
|
public class BomberStudentClient {
|
|
|
-
|
|
|
+
|
|
|
protected int portUdp;
|
|
|
protected int portTcp;
|
|
|
protected Broadcast finder;
|
|
|
protected TcpClient socket;
|
|
|
-
|
|
|
+
|
|
|
/* --- Constructeurs --- */
|
|
|
-
|
|
|
- public BomberStudentClient(){
|
|
|
- this.portUdp = 28624;
|
|
|
- this.portTcp = 28642;
|
|
|
+ public BomberStudentClient() {
|
|
|
+ this.portUdp = 18624;
|
|
|
+ this.portTcp = 18642;
|
|
|
this.finder = new Broadcast(this.portUdp);
|
|
|
}
|
|
|
-
|
|
|
- public BomberStudentClient(int portUdp, int portTcp){
|
|
|
+
|
|
|
+ public BomberStudentClient(int portUdp, int portTcp) {
|
|
|
this.portUdp = portUdp;
|
|
|
this.portTcp = portTcp;
|
|
|
this.finder = new Broadcast(this.portUdp);
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
/* --- Methodes --- */
|
|
|
-
|
|
|
- public int findServer(){
|
|
|
+ public int findServer() {
|
|
|
this.finder.search("i'm a bomberstudent server");
|
|
|
return this.finder.getServers().size();
|
|
|
}
|
|
|
-
|
|
|
- public boolean selectServer(int index){
|
|
|
+
|
|
|
+ public boolean selectServer(int index) {
|
|
|
ArrayList<InetAddress> serv = this.finder.getServers();
|
|
|
- if(index <= 0 || index >= serv.size()){
|
|
|
+ System.out.println(serv.size());
|
|
|
+ if (!(index >= 0 && index < serv.size())) {
|
|
|
System.err.println("Index invalide");
|
|
|
return false;
|
|
|
}
|
|
|
- this.socket = new TcpClient(this.finder.getServers().get(index), this.portTcp);
|
|
|
+ this.socket = new TcpClient(serv.get(index), this.portTcp);
|
|
|
return this.socket.connect();
|
|
|
}
|
|
|
-
|
|
|
- public boolean send(String method, String ressource, JSONObject param){
|
|
|
+
|
|
|
+ public boolean send(String method, String ressource) {
|
|
|
+ return this.send(method, ressource, null);
|
|
|
+ }
|
|
|
+
|
|
|
+ public boolean send(String method, String ressource, JSONObject param) {
|
|
|
//Verif que la methode existe
|
|
|
method = method.toUpperCase();
|
|
|
- if(!method.equals("POST") && !method.equals("GET")){
|
|
|
+ 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);
|
|
|
- json.put("param", param);
|
|
|
+ if (param != null) {
|
|
|
+ json.put("param", param);
|
|
|
+ }
|
|
|
//Envoi
|
|
|
this.socket.send(json.toString());
|
|
|
return true;
|
|
|
}
|
|
|
-
|
|
|
- public BomberStudentReponse receive(){
|
|
|
+
|
|
|
+ public BomberStudentReponse receive() {
|
|
|
String msg = this.socket.receive();
|
|
|
- if(msg == null){
|
|
|
+ if (msg == null) {
|
|
|
return new BomberStudentReponse();
|
|
|
} else {
|
|
|
return new BomberStudentReponse(msg);
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
- public boolean close(){
|
|
|
+
|
|
|
+ public boolean close() {
|
|
|
return this.socket.close();
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
}
|