001    package javax.realtime;
002    
003    /** Create a scope in a <code>RealtimeThread</code> for which
004     *  <code>interrupt()</code> will be called at the expiration
005     *  of a timer. This timer will begin measuring time at some
006     *  point between the time <code>doInterruptible()</code> is
007     *  invoked and the time the <code>run()</code> method of the
008     *  <code>Interruptible</code> object is invoked. Each call of
009     *  <code>doInterruptible()</code> on an instance of <code>Timed</code>
010     *  will restart the timer for the amount of time given in the
011     *  constructor or the most recent invocation of <code>resetTime()</code>.
012     *  All memory use of <code>Timed</code> occurs during construction
013     *  or the first invocation of <code>doInterruptible()</code>.
014     *  Subsequent invokes of <code>doInterruptible()</code> do not
015     *  allocate memory.
016     *  <p>
017     *  Usage: <code>new Timed(T).doInterruptible(interruptible);</code>
018     */
019    public class Timed extends AsynchronouslyInterruptedException {
020    
021        HighResolutionTime timeout;
022    
023        /** Create an instance of <code>Timed</code> with a timer set
024         *  to timeout. If the time is in the past the
025         *  <code>AsynchronouslyInterruptedException</code> mechanism
026         *  is immediately activated.
027         *
028         *  @param time The interval of time between the invocation of
029         *              <code>doInterruptible()</code> and when
030         *              <code>interrupt()</code> is called on
031         *              <code>currentRealtimeThread()</code>. If null the
032         *              <code>java.lang.IllegalArgumentException</code> is thrown.
033         *  @throws java.lang.IllegalArgumentException
034         */
035        public Timed(HighResolutionTime time)
036            throws IllegalArgumentException {
037            timeout = time;
038            if (time instanceof AbsoluteTime) {
039                AbsoluteTime a_time = (AbsoluteTime)time;
040                AbsoluteTime at = new AbsoluteTime();
041                Clock.getRealtimeClock().getTime(at);
042                if ((a_time.getMilliseconds() * 1000000 + a_time.getNanoseconds()) <
043                    (at.getMilliseconds() * 1000000 + a_time.getNanoseconds())) {
044                    // DO SOMETHING...
045                }
046            }
047            else {   // time should be instance of RelativeTime
048                RelativeTime r_time = (RelativeTime)time;
049                if ((r_time.getMilliseconds() * 1000000 + r_time.getNanoseconds()) < 0) {
050                    // DO SOMETHING...
051                }
052            }
053        }
054    
055        /** Execute a timeout method. Starts the timer and executes the
056         *  <code>run()</code> method of the given <code>Interruptible</code> object.
057         *
058         *  @param logic Implements an <code>Interruptible run()</code> method. If
059         *               null nothing happens.
060         */
061        public boolean doInterruptible(Interruptible logic) {
062            // TODO
063    
064            return false;
065        }
066    
067        /** To reschedule the timeout for the next invocation of
068         *  <code>doInterruptible()</code>.
069         *
070         *  @param time This can be an absolute time or a relative time. If null
071         *              the timeout is not changed.
072         */
073        public void resetTime(HighResolutionTime time) {
074            timeout = time;
075        }
076    }