From: Jérôme Benoit Date: Tue, 13 Mar 2018 15:37:29 +0000 (+0100) Subject: TD2: Make the client send and receive object message. X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=c4aaaecaa30716d0bc52f859f799880e5a37b027;p=TD_SR.git TD2: Make the client send and receive object message. Signed-off-by: Jérôme Benoit --- diff --git a/TD2/client/ClientSimplifie.java b/TD2/client/ClientSimplifie.java index 5a2e048..ace3bd7 100644 --- a/TD2/client/ClientSimplifie.java +++ b/TD2/client/ClientSimplifie.java @@ -49,9 +49,9 @@ public class ClientSimplifie { } private void attributesInit() { + sock = null; lecture = null; ecriture = null; - sock = null; } /** diff --git a/TD2/client/Main.java b/TD2/client/Main.java index 00d8924..08300b7 100644 --- a/TD2/client/Main.java +++ b/TD2/client/Main.java @@ -39,7 +39,10 @@ public class Main { } } - public static void main (String[] args) { + /** + * main for text based message broadcasting + */ + public static void main2() { SocketClient client = null; Thread thS = null; Thread thR = null; @@ -69,4 +72,34 @@ public class Main { } } + 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(); + } + } + } diff --git a/TD2/client/Makefile b/TD2/client/Makefile index 5a7491f..bb2514c 100644 --- a/TD2/client/Makefile +++ b/TD2/client/Makefile @@ -46,10 +46,13 @@ JVM = java # NAME = Camilo Juan CLASSES = \ + Message.java \ ClientSimplifie.java \ SocketClient.java \ ThreadClientSend.java \ ThreadClientReceive.java \ + ThreadClientoSend.java \ + ThreadClientoReceive.java \ Main.java # diff --git a/TD2/client/Message.java b/TD2/client/Message.java new file mode 100644 index 0000000..d5fb701 --- /dev/null +++ b/TD2/client/Message.java @@ -0,0 +1,65 @@ +import java.io.Serializable; +import java.util.Calendar; + +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) { + emetteur = name; + texte = msg; + heure = Calendar.getInstance(); + } + + /** + * @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() { + return "<" + emetteur + ":" + heure + "> " + texte; + } +} diff --git a/TD2/client/SocketClient.java b/TD2/client/SocketClient.java index 8bc5d4f..814d68a 100644 --- a/TD2/client/SocketClient.java +++ b/TD2/client/SocketClient.java @@ -5,6 +5,8 @@ import java.util.*; public class SocketClient { BufferedReader lecture; // pour le flot d'entrée venant du serveur PrintWriter ecriture; // pour le flot de sortie vers le serveur + ObjectInput oLecture; + ObjectOutput oEcriture; Socket sock; // le socket client public SocketClient() { @@ -42,16 +44,20 @@ public class SocketClient { IStream = sock.getInputStream(); InputStreamReader IMesg = new InputStreamReader(IStream); lecture = new BufferedReader(IMesg); + oLecture = new ObjectInputStream(IStream); OutputStream OStream = null; OStream = sock.getOutputStream(); ecriture = new PrintWriter(OStream); + oEcriture = new ObjectOutputStream(OStream); } private void attributesInit() { + sock = null; lecture = null; ecriture = null; - sock = null; + oLecture = null; + oEcriture = null; } /** @@ -63,6 +69,15 @@ public class SocketClient { ecriture.flush(); } + /** + * Send an object message on the opened client socket + * @param msg a string containing the message to send + */ + public void sendoMsg(Message oMsg) throws IOException { + oEcriture.writeObject(oMsg); + oEcriture.flush(); + } + /** * Receive a message sent on the opened client socket * @return a string containing the received message @@ -74,6 +89,14 @@ public class SocketClient { return line; } + /** + * Receive an object message sent on the opened client socket + * @return a string containing the received message + */ + public Message receiveoMsg() throws IOException, ClassNotFoundException { + return (Message)oLecture.readObject(); + } + /** * Close all opened I/O streams attached to this object instance */ @@ -85,6 +108,11 @@ public class SocketClient { lecture.close(); if (ecriture != null) ecriture.close(); + if (oLecture != null) + oLecture.close(); + if (oEcriture != null) { + oEcriture.close(); + } } catch (IOException e) { System.err.println("IOException: " + e); diff --git a/TD2/client/ThreadClientReceive.java b/TD2/client/ThreadClientReceive.java index a3f28d8..8be380f 100644 --- a/TD2/client/ThreadClientReceive.java +++ b/TD2/client/ThreadClientReceive.java @@ -10,7 +10,7 @@ public class ThreadClientReceive implements Runnable { public void run() { try { boolean end = false; - //FIXME: Not exiting properly randomly from that loop! + //FIXME: not exiting properly randomly from that loop! while (!end) { String rline = client.receiveMsg(); if (rline.equals(".")) { diff --git a/TD2/client/ThreadClientoReceive.java b/TD2/client/ThreadClientoReceive.java new file mode 100644 index 0000000..cfad0c9 --- /dev/null +++ b/TD2/client/ThreadClientoReceive.java @@ -0,0 +1,31 @@ +import java.io.*; + +public class ThreadClientoReceive implements Runnable { + private SocketClient client; + + ThreadClientoReceive(SocketClient c) { + client = c; + } + + public void run() { + try { + boolean end = false; + //FIXME: not exiting properly randomly from that loop! + while (!end) { + Message roMsg = client.receiveoMsg(); + if (roMsg.getTexte().equals(".")) { + end = true; + } + System.out.println(Thread.currentThread().getName() + " a recu " + roMsg); + } + } + catch (IOException e) { + System.err.println("IOException: " + e); + e.printStackTrace(); + } + catch (ClassNotFoundException e) { + System.err.println("ClassNotFoundException: " + e); + e.printStackTrace(); + } + } +} diff --git a/TD2/client/ThreadClientoSend.java b/TD2/client/ThreadClientoSend.java new file mode 100644 index 0000000..2679180 --- /dev/null +++ b/TD2/client/ThreadClientoSend.java @@ -0,0 +1,40 @@ +import java.io.*; + +public class ThreadClientoSend implements Runnable { + private SocketClient client; + + ThreadClientoSend(SocketClient c) { + client = c; + } + + public void run() { + BufferedReader userInput = null; + try { + userInput = new BufferedReader(new InputStreamReader(System.in)); + boolean end = false; + while (!end) { + String line = userInput.readLine(); + if (line.equals(".")) { + end = true; + } + Message oMsg = new Message("Name", line); + client.sendoMsg(oMsg); + System.out.println(Thread.currentThread().getName() + " a envoye " + oMsg); + } + } + catch (IOException e) { + System.err.println("IOException: " + e); + e.printStackTrace(); + } + finally { + if (userInput != null) { + try { + userInput.close(); + } + catch (IOException e) { + System.err.println("IOException: " + e); + } + } + } + } +} diff --git a/TD2/server/Makefile b/TD2/server/Makefile index ca95faa..a142b3d 100644 --- a/TD2/server/Makefile +++ b/TD2/server/Makefile @@ -46,6 +46,7 @@ JVM = java # NAME = Camilo Juan CLASSES = \ + Message.java \ BroadcastThreadService.java \ Main.java