-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathTimerPacket.java
More file actions
56 lines (49 loc) · 1.54 KB
/
Copy pathTimerPacket.java
File metadata and controls
56 lines (49 loc) · 1.54 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
import java.io.IOException;
import java.net.DatagramPacket;
import java.util.Timer;
import java.util.TimerTask;
public class TimerPacket {
private Packet packet;
private Timer timer;
private boolean ack;
private final Object lock;
TimerPacket(Packet packet) {
this.packet = packet;
ack = false;
lock = new Object();
}
class TimeoutTask extends TimerTask {
public void run() {
synchronized (lock) {
if (!ack) {
byte[] sendData = packet.getBytes();
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length,
SRSender.channelAddress, SRSender.port);
try {
SRSender.socket.send(sendPacket);
} catch (IOException e) {
System.out.println("IOException when TimerPacket sending packet");
}
System.out.println(String.format("PKT SEND DAT %s %s", packet.getLength(), packet.getSeqNum()));
timer.schedule(new TimeoutTask(), SRSender.timeout);
}
}
}
}
public Packet getPacket() {
return packet;
}
public boolean isAck() {
return ack;
}
public void startTimer() {
timer = new Timer();
timer.schedule(new TimeoutTask(), SRSender.timeout);
}
public void stopTimer() {
synchronized (lock) {
timer.cancel();
ack = true;
}
}
}