rgradisc@859
|
1 |
/*
|
rgradisc@859
|
2 |
* Created on Aug 21, 2003
|
rgradisc@859
|
3 |
*/
|
rgradisc@859
|
4 |
package isac.bridge;
|
rgradisc@859
|
5 |
|
rgradisc@859
|
6 |
/**
|
rgradisc@859
|
7 |
* @author rgradisc
|
mlang@2452
|
8 |
*
|
mlang@2452
|
9 |
* Stores times of requests to sml-kernel. This is used for informing
|
mlang@2452
|
10 |
* Clients2KernelServer to kill sml-kernel if no response is coming back for a
|
mlang@2452
|
11 |
* certain amount of time, which means that the bridge_ has probably crashed or
|
mlang@2452
|
12 |
* is caught in an endless loop, and neeeds to be restarted.
|
rgradisc@859
|
13 |
*/
|
rgradisc@859
|
14 |
|
rgradisc@859
|
15 |
import java.util.Vector;
|
rgradisc@859
|
16 |
|
wneuper@1864
|
17 |
import org.apache.log4j.Logger;
|
wneuper@1864
|
18 |
|
rgradisc@859
|
19 |
public class TimeCheckerThread extends Thread {
|
rgradisc@859
|
20 |
|
mlang@2452
|
21 |
//Time to wait for response from the kernel to a request
|
mlang@2452
|
22 |
private long wait_millis_;
|
rgradisc@859
|
23 |
|
mlang@2452
|
24 |
//Stores the times when the waitTime for current request will run out
|
mlang@2452
|
25 |
private Vector time_out_times_;
|
mlang@2452
|
26 |
|
mlang@2452
|
27 |
//Reference to the bridgeMain object
|
mlang@2452
|
28 |
private BridgeMain bridge_;
|
mlang@2452
|
29 |
|
mlang@2544
|
30 |
static Logger logger_ = Logger.getLogger(TimeCheckerThread.class.getName());
|
mlang@2452
|
31 |
|
mlang@2452
|
32 |
/**
|
mlang@2452
|
33 |
* Constructor: instantiate a new TimeCheckerThread
|
mlang@2452
|
34 |
*
|
mlang@2452
|
35 |
* @param bridge_
|
mlang@2452
|
36 |
* BridgeMain object that belongs to this TimeCheckerThread
|
mlang@2452
|
37 |
* @param wait_millis_
|
mlang@2452
|
38 |
* Time to wait for response from kernel before restarting
|
mlang@2452
|
39 |
*/
|
mlang@2452
|
40 |
public TimeCheckerThread(BridgeMain bridge, long waitMillis) {
|
mlang@2544
|
41 |
logger_.warn("TimeCheckerThread: bridge_=" + bridge + ", wait_millis_"
|
mlang@2452
|
42 |
+ waitMillis);
|
mlang@2452
|
43 |
this.bridge_ = bridge;
|
mlang@2452
|
44 |
this.time_out_times_ = bridge.getTimeOutTimes();
|
mlang@2452
|
45 |
this.wait_millis_ = waitMillis;
|
mlang@2452
|
46 |
}
|
mlang@2452
|
47 |
|
mlang@2452
|
48 |
/**
|
mlang@2452
|
49 |
* Start this Thread
|
mlang@2452
|
50 |
*/
|
mlang@2452
|
51 |
public void run() {
|
mlang@2452
|
52 |
try {
|
mlang@2452
|
53 |
while (true) {
|
mlang@2452
|
54 |
if (!time_out_times_.isEmpty()) {
|
mlang@2452
|
55 |
long first = ((Long) time_out_times_.firstElement())
|
mlang@2452
|
56 |
.longValue();
|
mlang@2452
|
57 |
long timeOut = first + wait_millis_;
|
mlang@2452
|
58 |
long now = System.currentTimeMillis();
|
mlang@2452
|
59 |
if (now > timeOut) {
|
mlang@2452
|
60 |
// timeOut: no response from sml-kernel within TIME_OUT:
|
mlang@2452
|
61 |
// restart sml-kernel
|
mlang@2452
|
62 |
bridge_.restartSML();
|
mlang@2452
|
63 |
} else {
|
mlang@2452
|
64 |
// sleep until first time in vector would run out (plus
|
mlang@2452
|
65 |
// a little buffer)
|
mlang@2452
|
66 |
long sleepTime = timeOut - now + 20;
|
mlang@2452
|
67 |
bridge_
|
mlang@2452
|
68 |
.log(
|
mlang@2452
|
69 |
1,
|
mlang@2452
|
70 |
"TimeCheckerThread: "
|
mlang@2452
|
71 |
+ time_out_times_.size()
|
mlang@2452
|
72 |
+ " request(s) waiting for response, sleeping for "
|
mlang@2452
|
73 |
+ sleepTime + " millis");
|
mlang@2452
|
74 |
sleep(sleepTime);
|
mlang@2452
|
75 |
}
|
mlang@2452
|
76 |
} else {
|
mlang@2452
|
77 |
// TimesVector is empty: No pending requests
|
mlang@2452
|
78 |
bridge_.log(1,
|
mlang@2452
|
79 |
"TimeCheckerThread: no requests, sleeping for "
|
mlang@2452
|
80 |
+ wait_millis_ + " millis");
|
mlang@2452
|
81 |
sleep(wait_millis_);
|
mlang@2452
|
82 |
}
|
mlang@2452
|
83 |
}
|
mlang@2452
|
84 |
} catch (InterruptedException e) {
|
mlang@2452
|
85 |
bridge_.log(1, "TimeCheckerThread interrupted");
|
rgradisc@859
|
86 |
}
|
rgradisc@859
|
87 |
}
|
rgradisc@859
|
88 |
|
mlang@2452
|
89 |
} |