5 public class BroadcastoThreadService
implements Runnable
{
7 private Socket clientSocket
;
8 private static ArrayList
<ObjectOutputStream
> listoWriter
;
10 BroadcastoThreadService(Socket clientSocket
) {
11 System
.out
.println("Creation d'un thread pour repondre a un client, port "
12 + clientSocket
.getPort());
13 this.clientSocket
= clientSocket
;
14 if (listoWriter
== null)
15 listoWriter
= new ArrayList
<ObjectOutputStream
>();
20 doService(clientSocket
, listoWriter
);
22 } catch (IOException e
) {
23 System
.err
.println("IOException : " + e
);
26 catch (ClassNotFoundException e
) {
27 System
.err
.println("ClassNotFoundException: " + e
);
32 if (this.clientSocket
!= null)
33 this.clientSocket
.close();
34 } catch (IOException e
) {
35 System
.err
.println("IOException : " + e
);
42 * Be careful, this function must be thread-safe
43 * @param clientSocket [description]
44 * @param sharedList [description]
45 * @throws IOException [description]
46 * @throws ClassNotFoundException [description]
48 public void doService(Socket clientSocket
, ArrayList
<ObjectOutputStream
> sharedList
) throws IOException
, ClassNotFoundException
{
49 ObjectInputStream OReader
= new ObjectInputStream(clientSocket
.getInputStream());
50 ObjectOutputStream OWriter
= new ObjectOutputStream(clientSocket
.getOutputStream());
51 sharedList
.add(OWriter
);
54 Message roMsg
= (Message
)OReader
.readObject();
55 if (roMsg
.getTexte().equals(".")) {
56 end
= true; // le thread de service doit terminer
57 break; // do not broadcast the dot that will close all clients threads
61 sharedList
.remove(OWriter
);
66 System
.out
.println("Fin du thread repondant au client, port "
67 + clientSocket
.getPort());
70 private void broadcastoMsg(Message oMsg
) throws IOException
, ClassNotFoundException
{
71 ListIterator
<ObjectOutputStream
> iter
= listoWriter
.listIterator();
72 while (iter
.hasNext()) {
73 ObjectOutputStream cursorOOS
= iter
.next();
74 cursorOOS
.writeObject(oMsg
);