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
);
23 catch (IOException e
) {
24 System
.err
.println("IOException : " + e
);
27 catch (ClassNotFoundException e
) {
28 System
.err
.println("ClassNotFoundException: " + e
);
33 if (this.clientSocket
!= null)
34 this.clientSocket
.close();
36 catch (IOException e
) {
37 System
.err
.println("IOException : " + e
);
44 * Be careful, this function must be thread-safe
45 * @param clientSocket [description]
46 * @param sharedList [description]
47 * @throws IOException [description]
48 * @throws ClassNotFoundException [description]
50 public void doService(Socket clientSocket
, ArrayList
<ObjectOutputStream
> sharedList
) throws IOException
, ClassNotFoundException
{
51 ObjectInputStream OReader
= new ObjectInputStream(clientSocket
.getInputStream());
52 ObjectOutputStream OWriter
= new ObjectOutputStream(clientSocket
.getOutputStream());
53 sharedList
.add(OWriter
);
56 Message roMsg
= (Message
)OReader
.readObject();
57 if (roMsg
.getTexte().equals(".")) {
58 end
= true; // le thread de service doit terminer
59 break; // do not broadcast the dot that will close all clients threads
63 sharedList
.remove(OWriter
);
68 System
.out
.println("Fin du thread repondant au client, port "
69 + clientSocket
.getPort());
72 private void broadcastoMsg(Message oMsg
) throws IOException
, ClassNotFoundException
{
73 ListIterator
<ObjectOutputStream
> iter
= listoWriter
.listIterator();
74 while (iter
.hasNext()) {
75 ObjectOutputStream cursorOOS
= iter
.next();
76 cursorOOS
.writeObject(oMsg
);