TD2: Fix all important bugs in the object passing server and client
authorJérôme Benoit <jerome.benoit@piment-noir.org>
Tue, 13 Mar 2018 20:44:44 +0000 (21:44 +0100)
committerJérôme Benoit <jerome.benoit@piment-noir.org>
Tue, 13 Mar 2018 20:44:44 +0000 (21:44 +0100)
code.

Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
TD2/client/Message.java
TD2/client/SocketClient.java
TD2/client/ThreadClientoSend.java
TD2/server/BroadcastThreadService.java
TD2/server/BroadcastoThreadService.java [new file with mode: 0644]
TD2/server/Main.java
TD2/server/Makefile
TD2/server/Message.java [new file with mode: 0644]

index d5fb701027f6880a0a6b9572add09f87520c9e6e..91082b2f046bca6953a23c2917f9fe34376e4111 100644 (file)
@@ -1,5 +1,6 @@
 import java.io.Serializable;
 import java.util.Calendar;
+import java.text.SimpleDateFormat;
 
 public class Message implements Serializable {
     // L'emeteur du message
@@ -11,10 +12,10 @@ public class Message implements Serializable {
 
     // Les méthodes
 
-    Message(String name, String msg) {
+    Message(String name, String msg, Calendar c) {
         emetteur = name;
         texte = msg;
-        heure = Calendar.getInstance();
+        heure = c;
     }
 
     /**
@@ -60,6 +61,7 @@ public class Message implements Serializable {
     }
 
     public String toString() {
-        return "<" + emetteur + ":" + heure + "> " + texte;  
+        SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
+        return "<" + emetteur + "|" + dateFormat.format(heure.getTime()) + "> " + texte;
     }
 }
index 814d68a1ef3a62b74e72120a9d2e9b74bb7bb187..3c6d021c605f3a22a7d9537d8f1bf830a28854f7 100644 (file)
@@ -40,16 +40,15 @@ public class SocketClient {
         // 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;
-        IStream = sock.getInputStream();
-        InputStreamReader IMesg = new InputStreamReader(IStream);
-        lecture = new BufferedReader(IMesg);
-        oLecture = new ObjectInputStream(IStream);
 
-        OutputStream OStream = null;
-        OStream = sock.getOutputStream();
+        OutputStream OStream = sock.getOutputStream();
         ecriture = new PrintWriter(OStream);
         oEcriture = new ObjectOutputStream(OStream);
+
+        InputStream IStream = sock.getInputStream();
+        InputStreamReader IMesg = new InputStreamReader(IStream);
+        lecture = new BufferedReader(IMesg);
+        oLecture = new ObjectInputStream(IStream);
     }
 
     private void attributesInit() {
index 267918041efd6b6f4aca10120d8529576df6484b..53fdeb07addee6917201d274057b4156a39dd0ab 100644 (file)
@@ -1,10 +1,32 @@
 import java.io.*;
+import java.util.Calendar;
+import java.util.Random;
 
 public class ThreadClientoSend implements Runnable {
     private SocketClient client;
+    //FIXME: use a random name by thread for now
+    //       should setable
+    private String randName;
 
     ThreadClientoSend(SocketClient c) {
         client = c;
+        randName = randomName();
+    }
+
+    private String randomName() {
+
+        int leftLimit = 97; // letter 'a'
+        int rightLimit = 122; // letter 'z'
+        int targetStringLength = 8;
+        Random random = new Random();
+        StringBuilder buffer = new StringBuilder(targetStringLength);
+        for (int i = 0; i < targetStringLength; i++) {
+            int randomLimitedInt = leftLimit + (int)(random.nextFloat() * (rightLimit - leftLimit + 1));
+            buffer.append((char)randomLimitedInt);
+        }
+        String generatedString = buffer.toString();
+
+        return generatedString;
     }
 
     public void run() {
@@ -17,7 +39,7 @@ public class ThreadClientoSend implements Runnable {
                                if (line.equals(".")) {
                     end = true;
                 }
-                Message oMsg = new Message("Name", line);
+                Message oMsg = new Message(randName, line, Calendar.getInstance());
                 client.sendoMsg(oMsg);
                 System.out.println(Thread.currentThread().getName() + " a envoye " + oMsg);
             }
index daf753f665ab16d9e798dcbc45428ce2b0732774..cd971c252356d34b4821c6e170ec81aa462d6c46 100644 (file)
@@ -34,6 +34,12 @@ public class BroadcastThreadService implements Runnable {
                }
        }
 
+       /**
+        * Be careful, this function must be thread-safe
+        * @param  clientSocket [description]
+        * @param  sharedList   [description]
+        * @throws IOException  [description]
+        */
        public void doService(Socket clientSocket, ArrayList<PrintWriter> sharedList) throws IOException {
                BufferedReader in;
                in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
@@ -44,7 +50,7 @@ public class BroadcastThreadService implements Runnable {
                        String theLine = in.readLine();
                        if (theLine.equals(".")) {
                                end = true; // le thread de service doit terminer
-                               break; // do not broadcast the dot that will close clients threads
+                               break; // do not broadcast the dot that will close all clients threads
                        }
                        broadcastMsg(theLine);
                }
diff --git a/TD2/server/BroadcastoThreadService.java b/TD2/server/BroadcastoThreadService.java
new file mode 100644 (file)
index 0000000..e8a3622
--- /dev/null
@@ -0,0 +1,72 @@
+import java.net.*;
+import java.io.*;
+import java.util.*;
+
+public class BroadcastoThreadService implements Runnable {
+
+       private Socket clientSocket;
+       private static ArrayList<ObjectOutputStream> listoWriter;
+
+       BroadcastoThreadService(Socket clientSocket) {
+               System.out.println("Creation d'un thread pour repondre a un client, port "
+                                                       + clientSocket.getPort());
+               this.clientSocket = clientSocket;
+               if (listoWriter == null)
+                       listoWriter = new ArrayList<ObjectOutputStream>();
+       }
+
+       public void run() {
+               try {
+                       doService(clientSocket, listoWriter);
+                       clientSocket.close();
+               } catch (IOException e) {
+                       System.err.println("IOException : " + e);
+                       e.printStackTrace();
+               }
+               catch (ClassNotFoundException e) {
+               System.err.println("ClassNotFoundException: " + e);
+                       e.printStackTrace();
+               }
+               finally {
+                       try {
+                               if (this.clientSocket != null)
+                                       this.clientSocket.close();
+                       } catch (IOException e) {
+                               System.err.println("IOException : " + e);
+                               e.printStackTrace();
+                       }
+               }
+       }
+
+       public void doService(Socket clientSocket, ArrayList<ObjectOutputStream> sharedList) throws IOException, ClassNotFoundException {
+               ObjectInputStream OReader = new ObjectInputStream(clientSocket.getInputStream());
+               ObjectOutputStream OWriter = new ObjectOutputStream(clientSocket.getOutputStream());
+               sharedList.add(OWriter);
+               boolean end = false;
+               while (!end) {
+                       Message roMsg = (Message)OReader.readObject();
+                       if (roMsg.getTexte().equals(".")) {
+                               end = true; // le thread de service doit terminer
+                               break; // do not broadcast the dot that will close all clients threads
+                       }
+                       broadcastoMsg(roMsg);
+               }
+               sharedList.remove(OWriter);
+               if (OReader != null)
+                       OReader.close();
+               if (OWriter != null)
+                       OWriter.close();
+               System.out.println("Fin du thread repondant au client, port "
+                                                       + clientSocket.getPort());
+       }
+
+       private void broadcastoMsg(Message oMsg) throws IOException, ClassNotFoundException {
+               ListIterator<ObjectOutputStream> iter = listoWriter.listIterator();
+               while (iter.hasNext()) {
+                       ObjectOutputStream cursorOOS = iter.next();
+                       cursorOOS.writeObject(oMsg);
+                       cursorOOS.flush();
+               }
+       }
+
+}
index d46becce8c45f9993b74a0022364485321d8e2d0..9c3b5471c015af6e29351aa51e3d36d46245a32f 100644 (file)
@@ -9,7 +9,8 @@ public class Main {
                        while (true) { // le dispatcher est le thread qui execute main()
                                Socket clientSocket = listenSocket.accept();
                                System.out.println("Connexion de :" + clientSocket.getInetAddress());
-                               Thread serviceThread = new Thread(new BroadcastThreadService(clientSocket));
+                               //Thread serviceThread = new Thread(new BroadcastThreadService(clientSocket));
+                               Thread serviceThread = new Thread(new BroadcastoThreadService(clientSocket));
                                serviceThread.start();
                        }
                }
index a142b3d2d21a8fe7722513cc2f6e0b78131cfbeb..7499447edba0aab17e7cbc00b441df1522286a13 100644 (file)
@@ -48,6 +48,7 @@ JVM = java
 CLASSES = \
                Message.java \
                BroadcastThreadService.java \
+               BroadcastoThreadService.java \
                Main.java
 
 #
diff --git a/TD2/server/Message.java b/TD2/server/Message.java
new file mode 100644 (file)
index 0000000..91082b2
--- /dev/null
@@ -0,0 +1,67 @@
+import java.io.Serializable;
+import java.util.Calendar;
+import java.text.SimpleDateFormat;
+
+public class Message implements Serializable {
+    // L'emeteur du message
+    private String emetteur ;
+    // Le contenu du message
+    private String texte ;
+    // Heure du message
+    private Calendar heure ;
+
+    // Les méthodes
+
+    Message(String name, String msg, Calendar c) {
+        emetteur = name;
+        texte = msg;
+        heure = c;
+    }
+
+    /**
+     * @param name the emetteur to set
+     */
+    public void setEmetteur(String name) {
+        emetteur = name;
+    }
+
+    /**
+     * @return the emetteur
+     */
+    public String getEmetteur() {
+       return emetteur;
+    }
+
+    /**
+     * @param texte the texte to set
+     */
+    public void setTexte(String texte) {
+       this.texte = texte;
+    }
+
+    /**
+     * @return the texte
+     */
+    public String getTexte() {
+       return texte;
+    }
+
+    /**
+     * @param heure the heure to set
+     */
+    public void setHeure(Calendar heure) {
+       this.heure = heure;
+    }
+
+    /**
+     * @return the heure
+     */
+    public Calendar getHeure() {
+       return heure;
+    }
+
+    public String toString() {
+        SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
+        return "<" + emetteur + "|" + dateFormat.format(heure.getTime()) + "> " + texte;
+    }
+}