TD1: Refactor the client side code.
[TD_SR.git] / TD2 / client / Main.java
index cc47bb7bd499c54bf3822ccc7fbaea975795df27..81a31fb2e99b07607d82e5e10065039b5c2f2249 100644 (file)
@@ -7,46 +7,66 @@ public class Main {
         * Main for testing ClientSimplifie
         */
        private static void main1() {
-               SocketClient client = new SocketClient();
+               ClientSimplifie client = null;
+               BufferedReader userInput = null;
 
                try {
-                       client.sendMsg("Line1");
-                       String msg = client.receiveMsg();
-                       System.out.println(msg);
+                       client = new ClientSimplifie();
+                       userInput = new BufferedReader(new InputStreamReader(System.in));
+                       boolean end = false;
+                       while (!end) {
+                               String line = userInput.readLine();
+                               if (line.equals("."))
+                                       end = true;
+                               client.sendMsg(line);
+                               String rline = client.receiveMsg();
+                               System.out.println(rline);
+                       }
                }
-               catch (Exception e) {
-                       System.err.println("Exception: " + e);
+               catch (IOException e) {
+                       System.err.println("IOException: " + e);
                }
                finally {
                        client.closeRWIO();
+                       if (userInput != null) {
+                               try {
+                                       userInput.close();
+                               }
+                               catch (IOException e) {
+                                       System.err.println("IOException: " + e);
+                               }
+                       }
                }
        }
 
-
        public static void main (String[] args) {
-               SocketClient client = new SocketClient();
+               SocketClient client = null;
+               Thread thS = null;
+               Thread thR = null;
 
-               // try {
-                       ThreadClientSend thCS = new ThreadClientSend(client);
-                       //FIXME: Implement a loop based on user input to set dynamically the message to sent.
-                       //for (int i = 0; i < 10; i++) {
-                       //      thCS.setMsg("Line" + i);
-                       //}
-                       thCS.setMsg("Line1");
-                       Thread thS = new Thread(thCS);
-                       Thread thR = new Thread(new ThreadClientReceive(client));
+               try {
+                       client = new SocketClient();
+                       thS = new Thread(new ThreadClientSend(client));
+                       thR = new Thread(new ThreadClientReceive(client));
                        thS.setName("thS");
                        thS.start();
                        thR.setName("thR");
                        thR.start();
-               // }
-               // catch (Exception e) {
-               //      System.err.println("Exception: " + e);
-               // }
-               // finally {
-               //      client.closeRWIO();
-               // }
-
+               }
+               catch (Exception e) {
+                       System.err.println("Exception: " + e);
+               }
+               finally {
+                       try {
+                               thS.join();
+                               thR.join();
+                       }
+                       catch (InterruptedException e) {
+                               System.err.println("InterruptedException: " + e);
+                               e.printStackTrace();
+                       }
+                       client.closeRWIO();
+               }
        }
 
 }