package com.threerings.media.timer;

import com.samskivert.Log;
import com.samskivert.util.Interval;
import com.samskivert.util.RunQueue;

/**
 * Calibrates timing values from a subclass' implementation of current against those returned by
 * System.currentTimeMillis. If the subclass timer is moving 1.25 times faster or .75 times slower
 * than currentTimeMillis, hold it to the values from currentTimeMillis.
 */
public abstract class CalibratingTimer
    implements MediaTimer
{
    /**
     * Initializes this timer. Must be called before the timer is used.
     * 
     * @param milliDivider - value by which current() must be divided to get milliseconds
     * @param microDivider - value by which current() must be divided to get microseconds
     */
    protected void init (long milliDivider, long microDivider)
    {
        _milliDivider = milliDivider;
        _microDivider = microDivider;
        reset();
        Log.info("Using " + getClass() + " timer [mfreq=" + _milliDivider + ", ufreq="
            + _microDivider + ", start=" + _startStamp + "].");
        new Interval(RunQueue.AWT) {
            @Override public void expired () {
                calibrate();
            }}.schedule(5000, true);
    }

    // documentation inherited from interface
    public long getElapsedMicros ()
    {
        return elapsed() / _microDivider;
    }

    // documentation inherited from interface
    public long getElapsedMillis ()
    {
        return elapsed() / _milliDivider;
    }

    // documentation inherited from interface
    public void reset ()
    {
        _startStamp = _priorCurrent = current();
        _driftMilliStamp = System.currentTimeMillis();
        _driftTimerStamp = current();
    }

    /** Returns the difference between _startStamp and current() */
    protected long elapsed ()
    {
        long current = current();
        if (_driftRatio != 1.0) {
            long elapsed = current - _priorCurrent;
            _startStamp += (elapsed - (elapsed * _driftRatio));
            _priorCurrent = current;
        }
        return current - _startStamp;
    }

    /** Calculates the drift factor from the time elapsed from the last calibrate call. */
    protected void calibrate ()
    {
        double elapsedTimer = (current() - _driftTimerStamp);
        double elapsedMillis = System.currentTimeMillis() - _driftMilliStamp;
        double drift = elapsedMillis / (elapsedTimer / _milliDivider);
        if (drift > 1.25 || drift < 0.75) {
            Log.warning("Calibrating [drift=" + drift + "]");
            _driftRatio = drift;
        } else if (_driftRatio != 1.0) {
            // If we're within bounds now but we weren't before, reset _driftFactor and log it
            _driftRatio = 1.0;
            Log.warning("Calibrating [drift=" + drift + "]");
        }
        _driftMilliStamp = System.currentTimeMillis();
        _driftTimerStamp = current();
    }
    
    /** Return the current value for this timer. */
    public abstract long current ();

    /** current() value when the timer was started. */
    protected long _startStamp;
    
    /** current() value when elapsed was called last. */
    protected long _priorCurrent;

    /** Amount by which current() should be divided to get milliseconds. */
    protected long _milliDivider;

    /** Amount by which current() should be divided to get microseconds. */
    protected long _microDivider;

    /** currentTimeMillis() value from the last time we called calibrate. */
    protected long _driftMilliStamp = System.currentTimeMillis();

    /** current() value from the last time we called calibrate. */
    protected long _driftTimerStamp;

    /** Ratio of currentTimeMillis to timer millis. */
    protected double _driftRatio = 1.0;
}

@@ -1,8 +1,930 @@
+//%0A// $Id$%0A//%0A// Nenya library - tools for developing networked games%0A// Copyright (C) 2002-2007 Three Rings Design, Inc., All Rights Reserved%0A// http://www.threerings.net/code/nenya/%0A//%0A// This library is free software; you can redistribute it and/or modify it%0A// under the terms of the GNU Lesser General Public License as published%0A// by the Free Software Foundation; either version 2.1 of the License, or%0A// (at your option) any later version.%0A//%0A// This library is distributed in the hope that it will be useful,%0A// but WITHOUT ANY WARRANTY; without even the implied warranty of%0A// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU%0A// Lesser General Public License for more details.%0A//%0A// You should have received a copy of the GNU Lesser General Public%0A// License along with this library; if not, write to the Free Software%0A// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA%0A%0A
 package 

Let's have a header on that file.