-
Notifications
You must be signed in to change notification settings - Fork 0
/
OutputStreamPool.java
58 lines (50 loc) · 1.55 KB
/
OutputStreamPool.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
//FINI
import java.io.*;
import java.util.*;
public class OutputStreamPool {
Map<Integer, ObjectOutputStream> pool = null;
public OutputStreamPool() {
pool = new HashMap<Integer, ObjectOutputStream>();
}
/**
* Fonction ajouter un flux prend une id et un flux en paramètre
* les places dans une collection de type Map (association clé -> valeur)
* pour faire correspondre l'id joueur au flux
* @param id
* @param oos
*/
public synchronized void addStream(Integer id, ObjectOutputStream oos) {
pool.put(id, oos);
}
/**
* Enlève un flux en désignant la clé
* @param id
*/
public synchronized void removeStream(Integer id) {
pool.remove(id);
}
/**
* Envoie à tous le monde : le serveur utilise cette fonction pour poster
* à tous les clients en balayant toute la Map via la clé et en écrivant
* le même objet dans chaque flux.
* @param o
* @throws IOException
*/
public synchronized void sendToAll(Object o) throws IOException {
boolean err = false;
String msg = "Ne peut pas poster à : ";
for (Integer dest : pool.keySet()) {
try {
pool.get(dest).writeObject(o);
//pool.get(dest).flush();
}
catch(IOException e) {
err = true;
msg += dest;
}
}
if (err) {
throw new IOException(msg);
}
}
}