01: /*
02: * <copyright>
03: *
04: * Copyright 1997-2004 BBNT Solutions, LLC
05: * under sponsorship of the Defense Advanced Research Projects
06: * Agency (DARPA).
07: *
08: * You can redistribute this software and/or modify it under the
09: * terms of the Cougaar Open Source License as published on the
10: * Cougaar Open Source Website (www.cougaar.org).
11: *
12: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
13: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
14: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
15: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
16: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
17: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
18: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
22: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23: *
24: * </copyright>
25: */
26:
27: package org.cougaar.core.agent.service.alarm;
28:
29: /**
30: * A Timer for real time ("system", or "wall clock" time).
31: */
32: public class RealTimer extends Timer {
33: public RealTimer() {
34: }
35:
36: protected String getName() {
37: return "RealTimer";
38: }
39:
40: protected void report(Alarm alarm) {
41: long now = currentTimeMillis();
42: long at = alarm.getExpirationTime();
43: if ((at + EPSILON) < now) {
44: // if we're more then epsilon late, we'll warn
45: if (log.isInfoEnabled()) {
46: log.info("Alarm " + alarm + " is " + (now - at)
47: + "ms late");
48: }
49: }
50: super .report(alarm);
51: }
52:
53: /* ///////////////////////////////////////////////////////
54:
55: // point test
56:
57: public static void main(String args[]) {
58: // create a timer
59: Timer timer = new RealTimer();
60: timer.start();
61:
62: System.err.println("currentTimeMillis() = "+timer.currentTimeMillis());
63: // test running advance
64: timer.addAlarm(timer.createTestAlarm(5*1000));// 5 sec
65: timer.addAlarm(timer.createTestAlarm(10*1000)); // 10 sec
66: timer.addAlarm(timer.createTestAlarm(10*1000)); // 10 sec (again)
67: timer.addAlarm(timer.createTestAlarm(20*1000));
68: timer.addAlarm(timer.createTestAlarm(30*1000));
69: timer.addAlarm(timer.createTestAlarm(40*1000));
70: timer.addAlarm(timer.createTestAlarm(60*60*1000)); // 60 min
71: timer.sleep(120*1000); // wait 10 seconds
72: System.exit(0);
73: }
74:
75: public void sleep(long millis) {
76: try {
77: synchronized(this) {
78: this.wait(millis);
79: }
80: } catch (InterruptedException ie) {}
81: }
82:
83:
84: Alarm createTestAlarm(long delta) {
85: return new TestAlarm(delta);
86: }
87: private class TestAlarm implements Alarm {
88: long exp;
89: public TestAlarm(long delta) { this.exp = currentTimeMillis()+delta; }
90: public long getExpirationTime() {return exp;}
91: public void expire() { System.err.println("Alarm "+exp+" expired.");}
92: public String toString() { return "<"+exp+">";}
93: public boolean cancel() {} // doesn't support cancel
94: }
95: */
96: }
|