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
);
41 public void doService(Socket clientSocket
, ArrayList
<ObjectOutputStream
> sharedList
) throws IOException
, ClassNotFoundException
{
42 ObjectInputStream OReader
= new ObjectInputStream(clientSocket
.getInputStream());
43 ObjectOutputStream OWriter
= new ObjectOutputStream(clientSocket
.getOutputStream());
44 sharedList
.add(OWriter
);
47 Message roMsg
= (Message
)OReader
.readObject();
48 if (roMsg
.getTexte().equals(".")) {
49 end
= true; // le thread de service doit terminer
50 break; // do not broadcast the dot that will close all clients threads
54 sharedList
.remove(OWriter
);
59 System
.out
.println("Fin du thread repondant au client, port "
60 + clientSocket
.getPort());
63 private void broadcastoMsg(Message oMsg
) throws IOException
, ClassNotFoundException
{
64 ListIterator
<ObjectOutputStream
> iter
= listoWriter
.listIterator();
65 while (iter
.hasNext()) {
66 ObjectOutputStream cursorOOS
= iter
.next();
67 cursorOOS
.writeObject(oMsg
);