From: Jérôme Benoit Date: Tue, 13 Mar 2018 20:44:44 +0000 (+0100) Subject: TD2: Fix all important bugs in the object passing server and client X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=da31e6daf7553c6b38ca83c428dd02a3aa8af853;p=TD_SR.git TD2: Fix all important bugs in the object passing server and client code. Signed-off-by: Jérôme Benoit --- diff --git a/TD2/client/Message.java b/TD2/client/Message.java index d5fb701..91082b2 100644 --- a/TD2/client/Message.java +++ b/TD2/client/Message.java @@ -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; } } diff --git a/TD2/client/SocketClient.java b/TD2/client/SocketClient.java index 814d68a..3c6d021 100644 --- a/TD2/client/SocketClient.java +++ b/TD2/client/SocketClient.java @@ -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() { diff --git a/TD2/client/ThreadClientoSend.java b/TD2/client/ThreadClientoSend.java index 2679180..53fdeb0 100644 --- a/TD2/client/ThreadClientoSend.java +++ b/TD2/client/ThreadClientoSend.java @@ -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); } diff --git a/TD2/server/BroadcastThreadService.java b/TD2/server/BroadcastThreadService.java index daf753f..cd971c2 100644 --- a/TD2/server/BroadcastThreadService.java +++ b/TD2/server/BroadcastThreadService.java @@ -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 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 index 0000000..e8a3622 --- /dev/null +++ b/TD2/server/BroadcastoThreadService.java @@ -0,0 +1,72 @@ +import java.net.*; +import java.io.*; +import java.util.*; + +public class BroadcastoThreadService implements Runnable { + + private Socket clientSocket; + private static ArrayList 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(); + } + + 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 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 iter = listoWriter.listIterator(); + while (iter.hasNext()) { + ObjectOutputStream cursorOOS = iter.next(); + cursorOOS.writeObject(oMsg); + cursorOOS.flush(); + } + } + +} diff --git a/TD2/server/Main.java b/TD2/server/Main.java index d46becc..9c3b547 100644 --- a/TD2/server/Main.java +++ b/TD2/server/Main.java @@ -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(); } } diff --git a/TD2/server/Makefile b/TD2/server/Makefile index a142b3d..7499447 100644 --- a/TD2/server/Makefile +++ b/TD2/server/Makefile @@ -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 index 0000000..91082b2 --- /dev/null +++ b/TD2/server/Message.java @@ -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; + } +}