TD2: Make the client send and receive object message.
authorJérôme Benoit <jerome.benoit@piment-noir.org>
Tue, 13 Mar 2018 15:37:29 +0000 (16:37 +0100)
committerJérôme Benoit <jerome.benoit@piment-noir.org>
Tue, 13 Mar 2018 15:37:29 +0000 (16:37 +0100)
Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
TD2/client/ClientSimplifie.java
TD2/client/Main.java
TD2/client/Makefile
TD2/client/Message.java [new file with mode: 0644]
TD2/client/SocketClient.java
TD2/client/ThreadClientReceive.java
TD2/client/ThreadClientoReceive.java [new file with mode: 0644]
TD2/client/ThreadClientoSend.java [new file with mode: 0644]
TD2/server/Makefile

index 5a2e0485f2282c0dcf347bbc05366e9fd21e8b6d..ace3bd7659961b9f1ee9f2944000636a0cd36ef7 100644 (file)
@@ -49,9 +49,9 @@ public class ClientSimplifie {
     }
 
     private void attributesInit() {
+        sock = null;
         lecture = null;
         ecriture  = null;
-        sock = null;
     }
 
     /**
index 00d8924007536f9abc71d99c88d3fccdccece996..08300b7139c8f0b0236905b11561d8a19822862c 100644 (file)
@@ -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();
+               }
+       }
+
 }
index 5a7491f47b8648d2952aed6b639bec37e630d026..bb2514c062cbcc38bd2a05b457736fb118ed7237 100644 (file)
@@ -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 (file)
index 0000000..d5fb701
--- /dev/null
@@ -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;  
+    }
+}
index 8bc5d4f7f81567253779c019e330908dca950a49..814d68a1ef3a62b74e72120a9d2e9b74bb7bb187 100644 (file)
@@ -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);
index a3f28d87f29a28a2f3701fbce7d501deb10522d9..8be380f42012c55e6787a82aa7d0d69eeeb049d6 100644 (file)
@@ -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 (file)
index 0000000..cfad0c9
--- /dev/null
@@ -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 (file)
index 0000000..2679180
--- /dev/null
@@ -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);
+                               }
+                       }
+        }
+    }
+}
index ca95faac14fef397b9061a1930f1165ffd7030f7..a142b3d2d21a8fe7722513cc2f6e0b78131cfbeb 100644 (file)
@@ -46,6 +46,7 @@ JVM = java
 # NAME = Camilo        Juan
 
 CLASSES = \
+               Message.java \
                BroadcastThreadService.java \
                Main.java