KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > DEOS > Clock


1 //
2
// Copyright (C) 2005 United States Government as represented by the
3
// Administrator of the National Aeronautics and Space Administration
4
// (NASA). All Rights Reserved.
5
//
6
// This software is distributed under the NASA Open Source Agreement
7
// (NOSA), version 1.3. The NOSA has been approved by the Open Source
8
// Initiative. See the file NOSA-1.3-JPF at the top of the distribution
9
// directory tree for the complete NOSA document.
10
//
11
// THE SUBJECT SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY OF ANY
12
// KIND, EITHER EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT
13
// LIMITED TO, ANY WARRANTY THAT THE SUBJECT SOFTWARE WILL CONFORM TO
14
// SPECIFICATIONS, ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR
15
// A PARTICULAR PURPOSE, OR FREEDOM FROM INFRINGEMENT, ANY WARRANTY THAT
16
// THE SUBJECT SOFTWARE WILL BE ERROR FREE, OR ANY WARRANTY THAT
17
// DOCUMENTATION, IF PROVIDED, WILL CONFORM TO THE SUBJECT SOFTWARE.
18
//
19
package DEOS;
20
21 import gov.nasa.jpf.jvm.Verify;
22
23
24 /**
25  * DOCUMENT ME!
26  */

27 public class Clock {
28   public static int TIME_CONSTRAINT = Registry.numPeriods * 20 * 2; // numPeriods is usually 3
29
public static int NOINTERRUPTS = 0;
30   public static int TIMEOUT = 1;
31   public static int SYSTEMINTERRUPT = 2;
32   public static int NOTIMECHANGE = 3;
33   int currentTime = -20;
34   PeriodicClock clockToNotify; // may use a list in another version
35
NewTimer timerToNotify; // if there are more than two clocking devices
36
boolean eventDriven = false;
37
38   // For abstraction - need to keep track of current time before event fo
39

40   /**
41    * Main constructor
42    */

43   public Clock (PeriodicClock periodicIn, NewTimer timerIn) {
44     if (DEOS.abstraction) {
45       currentTime = -20;
46     } else {
47       currentTime = -1;
48     }
49
50     clockToNotify = periodicIn;
51     timerToNotify = timerIn;
52   }
53
54   /**
55    * To get the current time
56    * @return currentTime
57    */

58   public int getCurrentTime () {
59     return currentTime;
60   }
61
62   /**
63    * To set the timer
64    * @param timeIn time with which to set timer
65    */

66   public void setTimer (int timeIn) {
67     timerToNotify.setTimer(timeIn, currentTime);
68   }
69
70   /**
71    * Clock clears interrupts
72    */

73   public void clearInterrupts () {
74     clockToNotify.clearInterrupt();
75     timerToNotify.clearTimeOut();
76   }
77
78   /**
79    * Clock ticks
80    * @return int - NOINTERRUPTS or TIMEOUT or SYSTEMINTERRUPT
81    */

82   public int ticks () {
83     clearInterrupts();
84
85     int delta;
86
87     if (!DEOS.abstraction) {
88       delta = 1;
89     } else {
90       int timeToEOP = (clockToNotify.getTimeToEOP());
91       int timeOutTime = (timerToNotify.getStoppingTime());
92       int timeToTimeOut = (timeOutTime - currentTime);
93
94       //System.out.println("currentTime = " + currentTime + " timeToEOP = " + timeToEOP + " timeOutTime = " + timeOutTime + " timeToTimeOut = " + timeToTimeOut);
95
if (Verify.randomBool()) {
96         delta = 0;
97       } else {
98         if (timeToEOP <= timeToTimeOut) {
99           delta = timeToEOP;
100         } else {
101           delta = timeToTimeOut;
102         }
103       }
104     }
105
106     if (delta == 0) {
107       return NOTIMECHANGE;
108     } else {
109       if ((currentTime + delta) > TIME_CONSTRAINT) {
110         return NOTIMECHANGE;
111       }
112
113       currentTime = (currentTime + delta);
114       clockToNotify.clockTicks(currentTime);
115       timerToNotify.clockTicks(currentTime);
116
117       if (clockToNotify.isInterrupted()) {
118         timerToNotify.interruptTimer();
119
120         return SYSTEMINTERRUPT;
121       } else if (timerToNotify.isTimeOut()) {
122         return TIMEOUT;
123       } else {
124         return NOINTERRUPTS;
125       }
126     }
127   }
128
129   /**
130    * System interrupt or time out events
131    * @return the event that occurred
132    */

133
134   /* does not seem to be called anywhere?
135      public int interruptEvents() {
136        clearInterrupts();
137        int timeToEOP = (clockToNotify.getTimeToEOP());
138        int periodTime = (currentTime + timeToEOP);
139        int timeOutTime = (timerToNotify.getStoppingTime());
140        if (periodTime <= timeOutTime) {
141          currentTime = periodTime;
142          clockToNotify.clockTicks(currentTime);
143          timerToNotify.clockTicks(currentTime);
144          return SYSTEMINTERRUPT;
145        } else {
146          currentTime = timeOutTime;
147          clockToNotify.clockTicks(currentTime);
148          timerToNotify.clockTicks(currentTime);
149          return TIMEOUT;
150        }
151      }
152    */

153 }
Popular Tags