Changeset 656

Show
Ignore:
Timestamp:
2007-12-04 01:28:45 (1 year ago)
Author:
MaierMan
Message:

Introducing per server limits

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • sandbox/serverswitcher/chrome.manifest

    r614 r656  
    44 
    55overlay chrome://dta/content/dta/manager.xul chrome://dtassw/content/manager.xul 
     6overlay chrome://dta/content/preferences/prefs.xul chrome://dtassw/content/paneServerSwitcher.xul 
  • sandbox/serverswitcher/chrome/content/manager.js

    r616 r656  
    3636  
    3737const SSW_PREF_ENABLED = 'ssw.enabled'; 
     38const SSW_PREF_LIMITS  = 'extensions.dta.serverlimit.'; 
    3839 
    39 // isn't accurate, but is good enough for this use case 
    40 function _ssw_getHost(host) { 
    41     var m = host.match(/([^.]+\.(?:[\w]{2,4}|museum)?(?:\.\w{2})?)$/); 
    42     if (m) {  
    43         return m[1]; 
    44     } 
    45     return host; 
    46 
    47 function _ssw_adjustButton() { 
    48         if (Preferences.getDTA(SSW_PREF_ENABLED, true)) { 
    49                 $('toolssw').removeAttribute('off'); 
    50         } 
    51         else { 
    52                 $('toolssw').setAttribute('off', true); 
    53         } 
     40var SSW = { 
     41        load: function() { 
     42                makeObserver(this); 
     43                Preferences.addObserver('extensions.dta.', this); 
     44                                 
     45                this.adjustButton(); 
     46                this.loadLimits(); 
     47        }, 
     48        observe: function(subject, topic, prefName) { 
     49                if (prefName == SSW_PREF_ENABLED) { 
     50                        this._adjustButton(); 
     51                } 
     52                else if (prefName.substr(0, SSW_PREF_LIMITS.length) == SSW_PREF_LIMITS) { 
     53                        this.loadLimits(); 
     54                } 
     55        }, 
     56        adjustButton: function() { 
     57                if (Preferences.getDTA(SSW_PREF_ENABLED, true)) { 
     58                        $('toolssw').removeAttribute('off'); 
     59                } 
     60                else { 
     61                        $('toolssw').setAttribute('off', true); 
     62                } 
     63        }, 
     64        toggle: function() { 
     65                Preferences.setDTA(SSW_PREF_ENABLED, !Preferences.getDTA(SSW_PREF_ENABLED, true)); 
     66                this.adjustButton();     
     67        }, 
     68        _limits: {}, 
     69        loadLimits: function() { 
     70                this._limits = {}; 
     71                let c = {}; 
     72                let limits = Preferences._pref.getChildList(SSW_PREF_LIMITS, c); 
     73                 
     74                for (let i = 0; i < c.value; ++i) { 
     75                        let limit = limits[i].substr(SSW_PREF_LIMITS.length); 
     76                        try { 
     77                                let value = Preferences.get(limits[i], 0); 
     78                                if (!value) { 
     79                                        continue; 
     80                                } 
     81                                this._limits[limit] = value; 
     82                                Debug.dump("loaded limit: " + limit); 
     83                        } 
     84                        catch (ex) { 
     85                                Debug.dump("Failed to load: " + limit, ex); 
     86                        } 
     87                } 
     88        }, 
     89        // isn't accurate, but is good enough for this use case 
     90        getHost: function(host) { 
     91            var m = host.match(/([^.]+\.(?:[\w]{2,4}|museum)?(?:\.\w{2})?)$/); 
     92            if (m) {  
     93                return m[1]; 
     94            } 
     95            return host; 
     96        },       
     97        /*Dialog.*/startNext: function() { 
     98                try { 
     99                        if (!Preferences.getDTA(SSW_PREF_ENABLED, true)) { 
     100                                return SSW.origStartNext(); 
     101                        } 
     102                         
     103                        let rv = false; 
     104                        if (this._running.length >= Prefs.maxInProgress) { 
     105                                return rv; 
     106                        } 
     107                         
     108                        let cset = {}; 
     109                        // Count the running task 
     110                        Dialog._running.forEach( 
     111                                function(i) { 
     112                                        let host = i.d._ssw_host; 
     113                                        if (!(host in cset)) { 
     114                                                cset[host] = new SSWItem(host); 
     115                                        } 
     116                                        else { 
     117                                                ++cset[host].n; 
     118                                        } 
     119                                }, 
     120                                this 
     121                        ); 
     122                        // prepare the other tasks 
     123                        for (let d in Tree.all) { 
     124                                if (!d.is(QUEUED)) { 
     125                                        continue; 
     126                                } 
     127                                let host = d._ssw_host; 
     128                                if (host in cset) { 
     129                                        cset[host].downloads.push(d); 
     130                                } 
     131                                else { 
     132                                        cset[host] = new SSWItem(host); 
     133                                } 
     134                        } 
     135                         
     136                        for (d in SSW._generator(cset)) { 
     137                                if (this._running.length >= Prefs.maxInProgress) { 
     138                                        break; 
     139                                } 
     140                                rv = true; 
     141                                this.run(d); 
     142                        } 
     143                         
     144                        return rv; 
     145                } catch(ex){ 
     146                        Debug.dump("ssw_startNext():", ex); 
     147                } 
     148                return false;    
     149        }, 
     150        _generator: function(downloadSet) { 
     151                let sorted = []; 
     152                for (let s in downloadSet) { 
     153                        sorted.push(downloadSet[s]); 
     154                } 
     155                while (sorted.length) { 
     156                        sorted.sort(SSWItem.prototype.cmp); 
     157                        for (i in sorted) { 
     158                                let s = sorted[i]; 
     159                                let limit = 0; 
     160                                if (s.host in SSW._limits) { 
     161                                        limit = SSW._limits[s.host]; 
     162                                } 
     163                                if ((limit && s.n >= limit) || s.downloads.length == 0) { 
     164                                        sorted.splice(i, 1); 
     165                                        break; 
     166                                } 
     167                                yield s.downloads.shift(); 
     168                                ++s.n; 
     169                        } 
     170                } 
     171        },  
     172        origStartNext: Dialog.startNext 
    54173}; 
    55 function _ssw_toggle() { 
    56         Preferences.setDTA(SSW_PREF_ENABLED, !Preferences.getDTA(SSW_PREF_ENABLED, true)); 
    57         _ssw_adjustButton();     
    58 } 
    59174 
    60 function SSWItem() { 
     175function SSWItem(host) { 
     176        this.host = host; 
    61177        this.n = 1; 
    62         this.i = []; 
     178        this.downloads = []; 
    63179}; 
    64180SSWItem.prototype = { 
     
    78194); 
    79195 
    80 Dialog._ssw_startNext = Dialog.startNext; 
    81 Dialog.startNext = function Dssw_startNext() { 
    82         try { 
    83                 if (!Preferences.getDTA(SSW_PREF_ENABLED, true)) { 
    84                         return this._ssw_startNext(); 
    85                 } 
    86                  
    87                 let rv = false; 
    88                 if (this._running.length >= Prefs.maxInProgress) { 
    89                         return rv; 
    90                 } 
    91                  
    92                 // building the map 
    93                 let cset = {}; 
    94                 Dialog._running.forEach( 
    95                         function(i) { 
    96                                 let host = i.d._ssw_host; 
    97                                 if (!(host in cset)) { 
    98                                         cset[host] = new SSWItem(); 
    99                                 } 
    100                                 else { 
    101                                         ++cset[host].n; 
    102                                 } 
    103                         }, 
    104                         this 
    105                 ); 
    106                 for (let d in Tree.all) { 
    107                         if (this._running.length >= Prefs.maxInProgress) { 
    108                                 return rv; 
    109                         }                                
    110                         if (!d.is(QUEUED)) { 
    111                                 continue; 
    112                         } 
    113                         let host = d._ssw_host; 
    114                         if (host in cset) { 
    115                                 cset[host].i.push(d); 
    116                                 continue; 
    117                         } 
    118                         else { 
    119                                         cset[host] = new SSWItem(); 
    120                         } 
    121                         this.run(d); 
    122                          
    123                         rv = true; 
    124                 } 
    125                 var a = []; 
    126                 for (let i in cset) { 
    127                         if (!cset[i].i.length) { 
    128                                 continue; 
    129                         } 
    130                         a.push(cset[i]); 
    131                 } 
    132                 while (a.length && this._running.length < Prefs.maxInProgress) { 
    133                         a.sort(SSWItem.prototype.cmp); 
    134                         this.run(a[0].i.shift()); 
    135                         rv = true; 
    136                         if (!a[0].length) { 
    137                                 a.shift(); 
    138                                 continue; 
    139                         } 
    140                         ++a[0].n; 
    141                 } 
    142                 return rv; 
    143         } catch(ex){ 
    144                 Debug.dump("ssw_startNext():", ex); 
    145         } 
    146         return false; 
    147 
     196Dialog.startNext = SSW.startNext; 
    148197 
    149 addEventListener('load', _ssw_adjustButton, false); 
     198addEventListener('load', function() { return SSW.load(); }, false); 
  • sandbox/serverswitcher/chrome/content/manager.xul

    r615 r656  
    99        <script type="application/javascript;version=1.7" src="chrome://dtassw/content/manager.js" /> 
    1010        <toolbar id="tools"> 
    11                 <toolbarbutton label="&ssw.switch.label;" tooltiptext="&ssw.switch.tip;" id="toolssw" onclick="_ssw_toggle();" insertafter="toolmovebottom" /> 
     11                <toolbarbutton label="&ssw.switch.label;" tooltiptext="&ssw.switch.tip;" id="toolssw" onclick="SSW.toggle();" insertafter="toolmovebottom" /> 
    1212                <spacer class="visible" insertafter="toolmovebottom" />          
    1313        </toolbar>