-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSender.java
More file actions
62 lines (47 loc) · 1.33 KB
/
Sender.java
File metadata and controls
62 lines (47 loc) · 1.33 KB
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
59
60
61
62
package chat;
import java.io.PrintStream;
import java.net.Socket;
import java.util.Vector;
public class Sender implements Runnable {
private volatile boolean shutdown = false;
public void shutdownSenderThread() {
this.shutdown = true;
}
void sendMsg() {
Vector<Socket> found = new Vector<Socket>();
for (Socket client : HandleClients.clientStreams.keySet()) {
PrintStream out = HandleClients.clientStreams.get(client).getOutputStreamWriter();
for (String msg : HandleClients.message) {
out.println(msg);
}
if (out.checkError()) {
found.add(client);
}
}
HandleClients.message.clear();
}
public void run() {
//new Thread(new CheckClients()).start();
Vector<PrintStream> streams = new Vector<PrintStream>();
long mapState = 0;
while(!this.shutdown) {
if (mapState != HandleClients.clientStreams.hashCode()) {
for (StreamsWrapper wrapper : HandleClients.clientStreams.values()) {
if (!streams.contains(wrapper.getOutputStreamWriter())) {
streams.add(wrapper.getOutputStreamWriter());
}
}
mapState = HandleClients.clientStreams.hashCode();
}
if (HandleClients.message.isEmpty()) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}else {
sendMsg();
}
}
}
}