TD2: Fix all important bugs in the object passing server and client
[TD_SR.git] / TD2 / server / BroadcastoThreadService.java
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();
+               }
+       }
+
+}