-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSendOutThread.java
59 lines (52 loc) · 1.33 KB
/
SendOutThread.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
59
import java.io.DataOutputStream;
import java.util.LinkedList;
import java.util.Timer;
public class SendOutThread extends Thread{
private LinkedList<AbstractMessage> outQueue;
private final DataOutputStream out;
private Timer time;
private final Peer peer;
private boolean is_running;
private CommonResource.keepAliveTask kAliveTask;
public SendOutThread(DataOutputStream dOut,Peer p){
this.out=dOut;
this.peer=p;
this.time = new Timer();
this.outQueue = new LinkedList<AbstractMessage>();
this.is_running = true;
}
public LinkedList<AbstractMessage> getOutQueue(){
return this.outQueue;
}
@Override
public void run(){
while(is_running){
kAliveTask= new CommonResource.keepAliveTask(peer);
time.schedule(kAliveTask, 100000);
synchronized (outQueue){
while(is_running && outQueue.isEmpty()){
try {
outQueue.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
try{
// we need to cancel the timer task
kAliveTask.cancel();
final AbstractMessage sendMess = outQueue.removeFirst();
AbstractMessage.encodeMessage(sendMess, out);
}
catch(Exception e){}
}
}
}
}
public void disconnect(){
this.kAliveTask.cancel();
this.time.cancel();
is_running = false;
synchronized (outQueue) {
outQueue.notifyAll();
}
}
}