-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTimer.java
80 lines (76 loc) · 1.9 KB
/
Timer.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/* Ivan Trendafilov 0837795 */
/**
* Sender4 - Timer thread - each packet is wrapped around a timer thread.
* The thread doesn't actually contain the packet, unless a timeout
* occurs, in which case it gets the timer from Sender4.
*/
public class Timer extends Thread
{
// debug from Sender4
public static int DEBUG = Sender4.DEBUG;
// Rate at which timer is checked
protected int m_rate = 1;
// Length of timeout
private int m_length = Sender4.timeout;
// Time elapsed
private int m_elapsed;
// thread /packet sequence number
private Short threadSeqNo;
private boolean running = true;
public Timer (short SeqNo) {
Thread.currentThread().setPriority(MIN_PRIORITY);
if(DEBUG > 1) System.out.println("Sending: "+SeqNo);
threadSeqNo = SeqNo;
m_elapsed = 0;
}
/** Performs timer specific code */
// Sleeps and checks if the packet has been received. If so, terminates.
// If not, times out & resends
public void run()
{
// Keep looping
loop:
while (running)
{
// Put the timer to sleep
try
{
Thread.sleep(m_rate);
}
catch (InterruptedException ioe)
{
continue;
}
// is the packet ACK-ed?
if(ACKThread.ackContains(threadSeqNo)) {
running = false;
break loop;
}
synchronized (this) {
// Increment time remaining
m_elapsed += m_rate;
// Check to see if the time has been exceeded
if (m_elapsed > m_length)
{
// Trigger a timeout and reset timer
synchronized (this) {
timeout();
m_elapsed = 0;
}
}
}
}
}
public void timeout() {
synchronized (this) {
try {
// re-send packet
Sender4.clientSocket.send(Sender4.packets.get(threadSeqNo));
} catch (Exception e) {
// in case by the time this reaches the timeout, the ACK is received and the clientSocket is closed.
System.exit(0);
}
if(DEBUG > 1) System.out.println("Resend "+threadSeqNo);
}
}
}