Browse Source

:sparkles: Class utilisation reseaux

Arthur Brandao 6 năm trước cách đây
mục cha
commit
93068144ca

+ 24 - 18
Client/rsx/BomberStudentReponse.java

@@ -1,35 +1,43 @@
 package rsx;
 
 import org.json.JSONObject;
+import org.json.JSONException;
 
 /**
  * Analyse reponse d'un serveur BomberStudent
+ *
  * @author Arthur Brandao
  */
 public class BomberStudentReponse {
-    
+
     protected boolean error = true;
     protected int statut;
     protected String message;
-    protected JSONObject reponse;
-    
+    protected JSONObject reponse = null;
+
     /* --- Constructeurs --- */
-    
-    public BomberStudentReponse(){
-        
+    public BomberStudentReponse() {
+
     }
-    
-    public BomberStudentReponse(String reponse){
+
+    public BomberStudentReponse(String reponse) {
         this.error = false;
         //Parse reponse
-        JSONObject json = new JSONObject(reponse);
-        this.statut = json.getInt("statut");
-        this.message = json.getString("message");
-        this.reponse = json.getJSONObject("param");
-    }  
-    
-    /* --- Getter/Setter --- */
+        try {
+            JSONObject json = new JSONObject(reponse);
+            this.statut = json.getInt("statut");
+            this.message = json.getString("message");
+            if(json.has("param")){
+                this.reponse = json.getJSONObject("param");
+            }
+            this.error = false;
+        } catch(JSONException ex){
+            System.err.println("La reponse n'est pas en JSON : " + ex.getMessage());
+        }
 
+    }
+
+    /* --- Getter/Setter --- */
     public boolean isError() {
         return error;
     }
@@ -61,7 +69,5 @@ public class BomberStudentReponse {
     public void setReponse(JSONObject reponse) {
         this.reponse = reponse;
     }
-    
-    
-    
+
 }

+ 1 - 1
Client/jsontest/JsonTest.java → Client/test/JsonTest.java

@@ -3,7 +3,7 @@
  * To change this template file, choose Tools | Templates
  * and open the template in the editor.
  */
-package jsontest;
+package test;
 
 import java.util.ArrayList;
 import org.json.*;

+ 4 - 4
Client/serverres/ServerRes.java → Client/test/ServerTest.java

@@ -1,4 +1,4 @@
-package serverres;
+package test;
 
 import java.net.UnknownHostException;
 import java.util.ArrayList;
@@ -10,7 +10,7 @@ import rsx.tcp.TcpClient;
  * Exemple appel vers le serveur
  * @author Arthur Brandao
  */
-public class ServerRes {
+public class ServerTest {
 
     /**
      * @param args the command line arguments
@@ -22,14 +22,14 @@ public class ServerRes {
         System.out.println(b.search("i'm a bomberstudent server"));
         System.out.println("Resultat : " + !b.getServers().isEmpty());
         //*/
-        //*
+        /*
         TcpClient cli = new TcpClient("127.0.0.1", 18642);
         cli.connect();
         cli.send("Bonjour toi le serveur comment que ça va ?");
         System.out.println(cli.receive());
         cli.close();
         //*/
-        /*
+        //*
         BomberStudentClient bsc = new BomberStudentClient();
         int a = bsc.findServer();
         System.out.println(a);

+ 1 - 1
Client/jsontest/Test.java → Client/test/Test.java

@@ -1,4 +1,4 @@
-package jsontest;
+package test;
 
 import org.json.JSONObject;