TD2: Make the client send and receive object message.
[TD_SR.git] / TD2 / client / Main.java
index cc47bb7bd499c54bf3822ccc7fbaea975795df27..08300b7139c8f0b0236905b11561d8a19822862c 100644 (file)
@@ -4,49 +4,102 @@ import java.io.*;
 public class Main {
 
        /**
-        * Main for testing ClientSimplifie
+        * 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);
+                               }
+                       }
                }
        }
 
+       /**
+        * main for text based message broadcasting
+        */
+       public static void main2() {
+               SocketClient client = null;
+               Thread thS = null;
+               Thread thR = null;
 
-       public static void main (String[] args) {
-               SocketClient client = new SocketClient();
-
-               // 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();
+               }
+       }
+
+       public static void main(String[] args) {
+               SocketClient client = null;
+               Thread thS = null;
+               Thread thR = null;
 
+               try {
+                       client = new SocketClient();
+                       thS = new Thread(new ThreadClientoSend(client));
+                       thR = new Thread(new ThreadClientoReceive(client));
+                       thS.setName("thoS");
+                       thS.start();
+                       thR.setName("thoR");
+                       thR.start();
+               }
+               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();
+               }
        }
 
 }