TD1: Refactor the client side code.
[TD_SR.git] / TD2 / client / ClientSimplifie.java
index d247a9383cc852cc6094e63953740b1b9ec09ddf..84fecb56e6b974b6385e1baacd12fd4417954341 100644 (file)
@@ -10,45 +10,50 @@ public class ClientSimplifie {
     public ClientSimplifie() {
         // établie une connexion au serveur par un appel
         // à connexionServeur()
-        connexionServeur("localhost", 5000);
+        attributesInit();
+        try {
+            connexionServeur("localhost", 5000);
+        }
+        catch (IOException e) {
+            System.err.println("IOException: " + e);
+            closeRWIO();
+        }
     }
 
     public ClientSimplifie(String adresseIPServeur, int portServeur) {
         // établie une connexion au serveur par un appel
         // à connexionServeur()
-        connexionServeur(adresseIPServeur, portServeur);
-    }
-
-    private void connexionServeur(String adresseIPServeur, int portServeur) {
-        // créer un objet socket lié au socket serveur et l'affecte à sock
-        // puis établie les chaînages de flot nécessaires
-        // pour l'envoi et la reception de messages
+        attributesInit();
         try {
-            sock = new Socket(adresseIPServeur, portServeur);
+            connexionServeur(adresseIPServeur, portServeur);
         }
         catch (IOException e) {
             System.err.println("IOException: " + e);
+            closeRWIO();
         }
+    }
+
+    private void connexionServeur(String adresseIPServeur, int portServeur) throws IOException {
+        // créer un objet socket lié au socket serveur et l'affecte à sock
+        // puis établie les chaînages de flot nécessaires
+        // pour l'envoi et la reception de messages
+        sock = new Socket(adresseIPServeur, portServeur);
         InputStream IStream = null;
-        try {
-            IStream = sock.getInputStream();
-        }
-        catch (IOException e) {
-            System.err.println("IOException: " + e);
-        }
+        IStream = sock.getInputStream();
         InputStreamReader IMesg = new InputStreamReader(IStream);
         lecture = new BufferedReader(IMesg);
 
         OutputStream OStream = null;
-        try {
-            OStream = sock.getOutputStream();
-        }
-        catch (IOException e) {
-            System.err.println("IOException: " + e);
-        }
+        OStream = sock.getOutputStream();
         ecriture = new PrintWriter(OStream);
     }
 
+    private void attributesInit() {
+        lecture = null;
+        ecriture  = null;
+        sock = null;
+    }
+
     /**
      * Send a message on the opened client socket
      * @param msg a string containing the message to send
@@ -62,15 +67,10 @@ public class ClientSimplifie {
      * Receive a message sent on the opened client socket
      * @return a string containing the received message
      */
-    public String receiveMsg() {
+    public String receiveMsg() throws IOException {
         String line = new String();
-        try {
-            //FIXME: read only the line before the ending newline
-            line = lecture.readLine();
-        }
-        catch (IOException e) {
-            System.err.println("IOException: " + e);
-        }
+        //FIXME: read only the line before the ending newline
+        line = lecture.readLine();
         return line;
     }
 
@@ -78,9 +78,13 @@ public class ClientSimplifie {
      * Close all opened I/O streams attached to this object instance
      */
     public void closeRWIO() {
-        ecriture.close();
         try {
-            lecture.close();
+            if (sock != null)
+                sock.close();
+            if (lecture != null)
+                lecture.close();
+            if (ecriture != null)
+                ecriture.close();
         }
         catch (IOException e) {
             System.err.println("IOException: " + e);