OpenLayers OpenLayers

Changeset 2821

Show
Ignore:
Timestamp:
03/19/07 20:00:17 (2 years ago)
Author:
sderle
Message:

Make WMS server selection deterministic, by using a real number hashing algorithm on the parameter string to select the server URL. Also, patch TMS since it doesn't use getFullParameterString(). Passes all tests. Fixes #410.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/openlayers/lib/OpenLayers/Layer/HTTPRequest.js

    r1907 r2821  
    9191    }, 
    9292     
     93    /** 
     94     * @param {String} paramString 
     95     * @param {Array(String)} urls 
     96     */ 
     97    selectUrl: function(paramString, urls) { 
     98        var product = 1; 
     99        for (var i = 0; i < paramString.length; i++) {  
     100            product *= paramString.charCodeAt(i) * (Math.sqrt(5) - 1) / Math.sqrt(3); 
     101            product -= Math.floor(product);  
     102        } 
     103        return urls[Math.floor(product * urls.length)]; 
     104    }, 
    93105 
    94106    /** combine url with layer's params and these newParams.  
     
    110122        var url = (altUrl == null) ? this.url : altUrl; 
    111123         
    112         // if url is not a string, it should be an array of strings,  
    113         //  in which case we will randomly select one of them in order 
    114         //  to evenly distribute requests to different urls. 
    115         if (typeof url == "object") { 
    116             url = url[Math.floor(Math.random()*url.length)]; 
    117         }    
    118         // requestString always starts with url 
    119         var requestString = url;         
    120  
    121124        // create a new params hashtable with all the layer params and the  
    122125        // new params together. then convert to string 
     
    132135        } 
    133136        var paramsString = OpenLayers.Util.getParameterString(allParams); 
     137         
     138        // if url is not a string, it should be an array of strings,  
     139        // in which case we will deterministically select one of them in  
     140        // order to evenly distribute requests to different urls. 
     141        if (url instanceof Array) { 
     142            url = this.selectUrl(paramsString, url); 
     143        }    
     144         
     145        // requestString always starts with url 
     146        var requestString = url;         
     147         
    134148        if (paramsString != "") { 
    135149            var lastServerChar = url.charAt(url.length - 1); 
  • trunk/openlayers/lib/OpenLayers/Layer/TMS.js

    r1927 r2821  
    7676        var y = (bounds.bottom - this.tileOrigin.lat) / (res * this.tileSize.h); 
    7777        var z = this.map.getZoom(); 
    78         return this.url + "1.0.0" + "/" + this.layername + "/" + z + "/" + x + "/" + y + "." + this.type;  
     78        var path = "1.0.0" + "/" + this.layername + "/" + z + "/" + x + "/" + y + "." + this.type;  
     79        var url = this.url; 
     80        if (url instanceof Array) { 
     81            url = this.selectUrl(path, url); 
     82        } 
     83        return url + path; 
    7984    }, 
    8085 
  • trunk/openlayers/tests/Layer/test_HTTPRequest.html

    r2803 r2821  
    9090                   format: 'image/png'}; 
    9191         
    92         t.plan( 9 ); 
     92        t.plan( 12 ); 
    9393 
    9494  // without ?         
     
    148148        str = layer.getFullRequestString(null, tUrl); 
    149149        t.eq(str, tUrl + '?' + OpenLayers.Util.getParameterString(tParams), "getFullRequestString() works for url sans ?"); 
    150  
     150   
     151  // single url object         
     152        tUrl = ["http://octo.metacarta.com/cgi-bin/mapserv"]; 
     153        layer = new OpenLayers.Layer.HTTPRequest(name, tUrl, tParams, null); 
     154        str = layer.getFullRequestString(); 
     155        t.eq(str, tUrl[0] + '?' + OpenLayers.Util.getParameterString(tParams), "getFullRequestString() works for list of one url"); 
     156   
     157  // two url object         
     158        tUrl = ["http://octo.metacarta.com/cgi-bin/mapserv","http://labs.metacarta.com/cgi-bin/mapserv"]; 
     159        layer = new OpenLayers.Layer.HTTPRequest(name, tUrl, tParams, null); 
     160        str = layer.getFullRequestString(); 
     161        t.eq(str, tUrl[0] + '?' + OpenLayers.Util.getParameterString(tParams), "getFullRequestString() works for list of two urls"); 
     162        str = layer.getFullRequestString({'a':'f'}); 
     163        t.eq(str, tUrl[1] + '?' + OpenLayers.Util.getParameterString(OpenLayers.Util.extend(tParams,{'a':'f'})), "getFullRequestString() works for list of two urls and is deterministic"); 
     164 
     165    } 
     166 
     167    function test_04_Layer_HTTPRequest_selectUrl (t) {  
     168        t.plan( 4 );  
     169 
     170        layer = new OpenLayers.Layer.HTTPRequest(name, url, params, options);  
     171 
     172        urls = ["wms1", "wms2", "wms3", "wms4"];  
     173        t.eq( layer.selectUrl("bbox=-180,0,0,90",  urls), "wms4", "selectUrl(-90,-180) returns 4" );  
     174        t.eq( layer.selectUrl("bbox=-180,-90,0,0", urls), "wms3", "selectUrl(90,-180) returns 3" );  
     175        t.eq( layer.selectUrl("bbox=0,90,180,0",   urls), "wms1", "selectUrl(-90,180) returns 1" );  
     176        t.eq( layer.selectUrl("bbox=0,0,180,90",   urls), "wms2", "selectUrl(90,180) returns 2" );  
    151177    } 
    152178 
  • trunk/openlayers/tests/Layer/test_TMS.html

    r2803 r2821  
    107107    function test_10_Layer_TMS_getURL(t) { 
    108108 
    109         t.plan(1); 
     109        t.plan(2); 
    110110         
    111111        var map = new OpenLayers.Map('map', options); 
     
    116116        var tileurl = layer.getURL(new OpenLayers.Bounds(3.515625,45,4.21875,45.703125)); 
    117117        t.eq(tileurl, "http://labs.metacarta.com/wms-c/Basic.py/1.0.0/basic/9/261/192.png", "Tile URL is correct"); 
     118 
     119        layer.url = ["http://tilecache1/", "http://tilecache2/", "http://tilecache3/"]; 
     120        tileurl = layer.getURL(new OpenLayers.Bounds(3.515625,45,4.21875,45.703125)); 
     121        t.eq(tileurl, "http://tilecache2/1.0.0/basic/9/261/192.png", "Tile URL is deterministic"); 
    118122    } 
    119123