From: Jérôme Benoit Date: Mon, 12 Mar 2018 21:50:03 +0000 (+0100) Subject: TD1: Refactor the client side code. X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=e018d1ec033513412d9b3628a7d6701a48725382;p=TD_SR.git TD1: Refactor the client side code. Signed-off-by: Jérôme Benoit --- diff --git a/TD1/exo3/Consommateur.java b/TD1/exo3/Consommateur.java index 06dd95d..a857e7e 100644 --- a/TD1/exo3/Consommateur.java +++ b/TD1/exo3/Consommateur.java @@ -22,7 +22,7 @@ public class Consommateur implements Runnable { Integer val; while (true) { val = (Integer)buffer.preleve(); - System.out.println (Thread.currentThread().getName() + " a preleve " + val); + System.out.println(Thread.currentThread().getName() + " a preleve " + val); try { Thread.sleep(ThreadLocalRandom.current().nextInt(1001)); } diff --git a/TD1/exo3/Main.java b/TD1/exo3/Main.java index 9b34b05..4aa4e1a 100644 --- a/TD1/exo3/Main.java +++ b/TD1/exo3/Main.java @@ -6,7 +6,7 @@ public class Main { public static void main (String[] args) { - //FIXME: Implement the args parsing to set this tree values dynamically + //FIXME: Implement the args parsing to set the three values dynamically final int BUFFER_SIZE = 1; final int PROD_NUMBER = 20; final int CONS_NUMBER = 20; diff --git a/TD1/exo3/Producteur.java b/TD1/exo3/Producteur.java index 0ad13c3..36b67a3 100644 --- a/TD1/exo3/Producteur.java +++ b/TD1/exo3/Producteur.java @@ -23,7 +23,7 @@ public class Producteur implements Runnable { public void run() { while (true) { buffer.depose(new Integer(val)); - System.out.println (Thread.currentThread().getName() + " a depose " + val); + System.out.println(Thread.currentThread().getName() + " a depose " + val); val++; try { Thread.sleep(ThreadLocalRandom.current().nextInt(101)); diff --git a/TD2/client/ClientSimplifie.java b/TD2/client/ClientSimplifie.java index d247a93..84fecb5 100644 --- a/TD2/client/ClientSimplifie.java +++ b/TD2/client/ClientSimplifie.java @@ -10,45 +10,50 @@ public class ClientSimplifie { public ClientSimplifie() { // établie une connexion au serveur par un appel // à connexionServeur() - connexionServeur("localhost", 5000); + attributesInit(); + try { + connexionServeur("localhost", 5000); + } + catch (IOException e) { + System.err.println("IOException: " + e); + closeRWIO(); + } } public ClientSimplifie(String adresseIPServeur, int portServeur) { // établie une connexion au serveur par un appel // à connexionServeur() - connexionServeur(adresseIPServeur, portServeur); - } - - private void connexionServeur(String adresseIPServeur, int portServeur) { - // créer un objet socket lié au socket serveur et l'affecte à sock - // puis établie les chaînages de flot nécessaires - // pour l'envoi et la reception de messages + attributesInit(); try { - sock = new Socket(adresseIPServeur, portServeur); + connexionServeur(adresseIPServeur, portServeur); } catch (IOException e) { System.err.println("IOException: " + e); + closeRWIO(); } + } + + private void connexionServeur(String adresseIPServeur, int portServeur) throws IOException { + // créer un objet socket lié au socket serveur et l'affecte à sock + // 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; - try { - IStream = sock.getInputStream(); - } - catch (IOException e) { - System.err.println("IOException: " + e); - } + IStream = sock.getInputStream(); InputStreamReader IMesg = new InputStreamReader(IStream); lecture = new BufferedReader(IMesg); OutputStream OStream = null; - try { - OStream = sock.getOutputStream(); - } - catch (IOException e) { - System.err.println("IOException: " + e); - } + OStream = sock.getOutputStream(); ecriture = new PrintWriter(OStream); } + private void attributesInit() { + lecture = null; + ecriture = null; + sock = null; + } + /** * Send a message on the opened client socket * @param msg a string containing the message to send @@ -62,15 +67,10 @@ public class ClientSimplifie { * Receive a message sent on the opened client socket * @return a string containing the received message */ - public String receiveMsg() { + public String receiveMsg() throws IOException { String line = new String(); - try { - //FIXME: read only the line before the ending newline - line = lecture.readLine(); - } - catch (IOException e) { - System.err.println("IOException: " + e); - } + //FIXME: read only the line before the ending newline + line = lecture.readLine(); return line; } @@ -78,9 +78,13 @@ public class ClientSimplifie { * Close all opened I/O streams attached to this object instance */ public void closeRWIO() { - ecriture.close(); try { - lecture.close(); + if (sock != null) + sock.close(); + if (lecture != null) + lecture.close(); + if (ecriture != null) + ecriture.close(); } catch (IOException e) { System.err.println("IOException: " + e); diff --git a/TD2/client/Main.java b/TD2/client/Main.java index cc47bb7..81a31fb 100644 --- a/TD2/client/Main.java +++ b/TD2/client/Main.java @@ -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(); + } } } diff --git a/TD2/client/SocketClient.java b/TD2/client/SocketClient.java index 48a93eb..b8e0c0b 100644 --- a/TD2/client/SocketClient.java +++ b/TD2/client/SocketClient.java @@ -10,77 +10,67 @@ public class SocketClient { public SocketClient() { // établie une connexion au serveur par un appel // à connexionServeur() - connexionServeur("localhost", 5000); + attributesInit(); + try { + connexionServeur("localhost", 5000); + } + catch (IOException e) { + System.err.println("IOException: " + e); + closeRWIO(); + } } public SocketClient(String adresseIPServeur, int portServeur) { // établie une connexion au serveur par un appel // à connexionServeur() - connexionServeur(adresseIPServeur, portServeur); - } - - private void connexionServeur(String adresseIPServeur, int portServeur) { - // créer un objet socket lié au socket serveur et l'affecte à sock - // puis établie les chaînages de flot nécessaires - // pour l'envoi et la reception de messages + attributesInit(); try { - sock = new Socket(adresseIPServeur, portServeur); + connexionServeur(adresseIPServeur, portServeur); } catch (IOException e) { System.err.println("IOException: " + e); + closeRWIO(); } + } + + private void connexionServeur(String adresseIPServeur, int portServeur) throws IOException { + // créer un objet socket lié au socket serveur et l'affecte à sock + // 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; - try { - IStream = sock.getInputStream(); - } - catch (IOException e) { - System.err.println("IOException: " + e); - } + IStream = sock.getInputStream(); InputStreamReader IMesg = new InputStreamReader(IStream); lecture = new BufferedReader(IMesg); OutputStream OStream = null; - try { - OStream = sock.getOutputStream(); - } - catch (IOException e) { - System.err.println("IOException: " + e); - } + OStream = sock.getOutputStream(); ecriture = new PrintWriter(OStream); } + private void attributesInit() { + lecture = null; + ecriture = null; + sock = null; + } + /** * Send a message on the opened client socket * @param msg a string containing the message to send */ - public synchronized void sendMsg(String msg) { - //NOTE: it's not really required with one socket writer thread. - while (msg.isEmpty()) { - try { - wait(); - } - catch (InterruptedException e) { - System.err.println("InterruptedException: " + e); - } - } + public void sendMsg(String msg) { ecriture.println(msg); ecriture.flush(); - notifyAll(); } /** * Receive a message sent on the opened client socket * @return a string containing the received message */ - public String receiveMsg() { + public String receiveMsg() throws IOException { String line = new String(); - try { - //FIXME: read only the line before the ending newline - line = lecture.readLine(); - } - catch (IOException e) { - System.err.println("IOException: " + e); - } + //FIXME: read only the line before the ending newline + line = lecture.readLine(); return line; } @@ -88,9 +78,13 @@ public class SocketClient { * Close all opened I/O streams attached to this object instance */ public void closeRWIO() { - ecriture.close(); try { - lecture.close(); + if (sock != null) + sock.close(); + if (lecture != null) + lecture.close(); + if (ecriture != null) + ecriture.close(); } catch (IOException e) { System.err.println("IOException: " + e); diff --git a/TD2/client/ThreadClientReceive.java b/TD2/client/ThreadClientReceive.java index 42f5d91..70f21b6 100644 --- a/TD2/client/ThreadClientReceive.java +++ b/TD2/client/ThreadClientReceive.java @@ -1,35 +1,27 @@ import java.util.concurrent.ThreadLocalRandom; +import java.io.*; public class ThreadClientReceive implements Runnable { private SocketClient client; - private String msg = new String(); ThreadClientReceive(SocketClient c) { client = c; } - /** - * @return the msg - */ - public String getMsg() { - return msg; - } - public void run() { - while (true) { - try { - msg = client.receiveMsg(); - System.out.println (Thread.currentThread().getName() + " a recu " + msg); - try { - Thread.sleep(ThreadLocalRandom.current().nextInt(101)); - } - catch (InterruptedException e) { - System.err.println("InterruptedException: " + e); - } - } - catch (Exception e) { - System.err.println("Exception: " + e); + try { + boolean end = false; + while (!end) { + String rline = client.receiveMsg(); + if (rline.equals(".")) { + end = true; + } + System.out.println(Thread.currentThread().getName() + " a recu " + rline); } } + catch (IOException e) { + System.err.println("IOException: " + e); + e.printStackTrace(); + } } } diff --git a/TD2/client/ThreadClientSend.java b/TD2/client/ThreadClientSend.java index 69cd51a..59ffbd0 100644 --- a/TD2/client/ThreadClientSend.java +++ b/TD2/client/ThreadClientSend.java @@ -1,36 +1,40 @@ import java.util.concurrent.ThreadLocalRandom; +import java.io.*; public class ThreadClientSend implements Runnable { private SocketClient client; - private String msg = new String(); ThreadClientSend(SocketClient c) { client = c; } - /** - * Message to sent setter - * @param m the text message to sent - */ - public void setMsg(String m) { - msg = m; - } - public void run() { - while (true) { - try { - client.sendMsg(msg); - System.out.println (Thread.currentThread().getName() + " a envoye " + msg); - try { - Thread.sleep(ThreadLocalRandom.current().nextInt(101)); - } - catch (InterruptedException e) { - System.err.println("InterruptedException: " + e); - } - } - catch (Exception e) { - System.err.println("Exception: " + e); + 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; + } + client.sendMsg(line); + System.out.println(Thread.currentThread().getName() + " a envoye " + line); } + } + 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); + } + } } } }