Source: modules/serverping.js


/**
 * Keep the cache of the given report alive in the server.
 * @namespace
 */

/* global SACK */
/* global addParametersToAJAX */
/* global addPromptToAjax */
/* global amIOnline */
/* global buildBASEURLForPageFile */
/* global dataAsJson */
/* global dataHasJSONError */
/* global generator */
/* global handleErrorPage */
/* global menubar */

var keepServerCacheAlive = (function() {

    var _defaultPollInterval = 5 * 60 * 1000; // 5 minutes interval
    var _progressCheckInterval = 500;
    var _lastProgress = 0; //
    var _pollTimeout = null;

    /**
     * Run the ping command and poll the server
     */
    var poll = function() {

        var pageURL = buildBASEURLForPageFile( '0.js', true);
        var ajax = new SACK( pageURL );

        // Set all parameters
        addParametersToAJAX(ajax, {
            page: '0.js',
            cmd: 'status'
        });

        addPromptToAjax && addPromptToAjax(ajax);

        ajax.onCompletion = function() {
            /* check for error */
            var data = dataAsJson( ajax.response, ajax.xmlhttp );
            var status = ajax.responseStatus[0];
            var progress = 100;

            if (dataHasJSONError(data, ajax.xmlhttp)) {
                _lastProgress = null; // temporary reset
            } else
            if ( status < 200 || status >= 400 ) {
                handleErrorPage(data, status);
                _lastProgress = null; // temporary reset
            } else
            if ( !!data && !!data.state && data.state != 'NO_CACHE_ENTRY' ) { // This response will occur if the cache has been cleared.
                progress = data.progress;
                keepServerCacheAlive.startPolling();
            }

            setProgress( progress );
        };

        try {
            ajax.runAJAX();
        } catch( e ) {
            // don't care.
        }
    };

    var _progressIndicator = null;
    var setProgress = function( progress ) {

        _lastProgress = progress;

        if ( progress >= 100 ) {
            _progressIndicator && _progressIndicator.setProgress( progress );
            _progressIndicator && _progressIndicator.dissolve();
            _progressIndicator = null;
            return;
        }

        if ( !_progressIndicator ) {
            var _bar = new menubar();
			var root = window.jQuery && jQuery( _bar.helpButton ).parentsUntil('.navigationentry') ? null : _bar.helpButton; 
            var element = _bar.createSpacer( root );
            _progressIndicator = new generator.progressCircle( element );
        }

        _progressIndicator.setProgress( progress );
    };

    var pollingInterval = function() {
        var interval = _lastProgress != null && _lastProgress < 100 ? _progressCheckInterval : _defaultPollInterval;
        return interval;
    };

    return {
        /**
         * Start polling the server if we are in online mode.
         */
        startPolling: function( reset ) {

            if ( reset ) {
                _lastProgress = 0;
            }

            keepServerCacheAlive.stopPolling();
            amIOnline.check(function (isOnline) {
                if (isOnline ) {
                    _pollTimeout = window.setTimeout( poll, pollingInterval());
                }
            });
        },

        /**
         * Check if the report rendering already finished.
         */
        isReportFinished: function() {
            return !(_lastProgress != null && _lastProgress < 100);
        },

        /**
         * Stop polling the server
         */
        stopPolling: function() {
            if ( _pollTimeout != null ) {
                window.clearTimeout( _pollTimeout );
                _pollTimeout = null;
            }
        }
    };
})();