Changeset 71

Show
Ignore:
Timestamp:
05/15/05 16:33:11 (4 years ago)
Author:
pragma
Message:

--

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/dsp/ConfigParser.d

    r67 r71  
    2626module dsp.ConfigParser; 
    2727 
    28 private import xml.XMLException; 
    29 private import xml.XMLAttributes; 
    30 private import xml.IXMLConsumerAdapter; 
    31 private import xml.XMLParser; 
    32 private import xml.ILexerStream; 
     28private import xml.utf8all; 
    3329 
    3430private import mango.http.utils.Dictionary; 
    3531private import mango.io.model.IConduit; 
     32private import mango.io.model.IBuffer; 
     33private import mango.io.Buffer; 
     34private import mango.io.Tokenizer; 
    3635 
    3736private import std.utf; 
     
    7473    uint state; 
    7574     
    76     public MutableDictionary parse(ILexerStream stream){ 
     75    static const uint DEFAULT_CAPACITY = 1024; 
     76     
     77    class CompleteBufferReader: Scanner{ 
     78        IBuffer buffer; 
     79     
     80        this(IBuffer buffer){ 
     81            this.buffer = buffer; 
     82        } 
     83         
     84        char[] read(){ 
     85            char[] data; 
     86         
     87            // delegate to read in everything that is passed in 
     88            int scan (char[] content) 
     89            { 
     90                    data ~= content; 
     91                    return IConduit.Eof; 
     92            } 
     93            super.next (buffer, &scan);          
     94            return(data); 
     95        }    
     96    } 
     97     
     98    public MutableDictionary parse(IConduit conduit){ 
     99        IBuffer buf = new Buffer(DEFAULT_CAPACITY); 
     100        buf.setConduit(conduit); 
     101         
     102        CompleteBufferReader reader = new CompleteBufferReader(buf); 
    77103        dictionary = new MutableDictionary(); 
    78         text = ""; 
    79         name = ""; 
    80         state = Root; 
    81          
    82         XMLParser parser = new XMLParser(); 
    83         parser.parse(this,stream); 
    84          
     104     
     105        XMLParser parser = new XMLParser();      
     106        parser.parse(this,charTConvert(reader.read())); 
    85107        return dictionary; 
    86     } 
    87          
     108    }   
     109            
    88110    void xmlStartElement(char[] name,XMLAttributes attribs,bit isEmpty){ 
    89111        switch(state){ 
     
    102124                this.name = name; 
    103125            }        
    104  
     126             
    105127            if("value" in attribs){ 
    106128                dictionary.put(name,attribs["value"]);   
     
    140162    } 
    141163     
    142     void xmlText(char[] text){ 
     164    void xmlText(char[] text,uint type){ 
    143165        switch(state){ 
    144166        case Root: 
  • trunk/dsp/DSPProcessor.d

    r67 r71  
    3333 
    3434 
    35 private import xml.IXMLConsumerAdapter; 
    36 private import xml.XMLAttributes; 
     35private import xml.utf8all; 
    3736 
    3837private import std.stdio; 
    3938private import std.string; 
    4039 
    41 class DSPProcessor : IXMLConsumerAdapter{ 
     40class DSPProcessor : SimpleConsumer{ 
    4241    ServletGenerator generator; 
    4342    TagLibrary tags; 
  • trunk/dsp/DSPTag.d

    r48 r71  
    2626module dsp.DSPTag; 
    2727 
    28 private import xml.XMLAttributes
     28private import xml.utf8all
    2929 
    3030private import dsp.ServletGenerator; 
  • trunk/dsp/ServletCompiler.d

    r68 r71  
    3030private import dsp.RequestConfig; 
    3131 
    32 private import xml.all; 
     32private import xml.utf8all; 
    3333 
    3434private import std.file; 
     
    5656     
    5757    private void ProcessDSPFile(RequestConfig cfg){ 
    58         char[] source = strip(toUTF8(cast(char[])std.file.read(cfg.dspSourceFilename))); 
     58        CharT[] source = charTConvert(cast(char[])std.file.read(cfg.dspSourceFilename)); 
    5959         
    60         SimpleStream stream = new SimpleStream(source); 
    61  
    62         parser.parse(processor, provider, stream); 
     60        parser.parse(processor, provider, source); 
    6361        std.file.write(cfg.dSourceFilename,cast(char[])processor.getServletSource(cfg.requestModule,cfg.ext)); 
    6462    } 
  • trunk/dsp/ServletGenerator.d

    r67 r71  
    135135                } 
    136136                else{ 
    137                     expression ~= " ~ " ~ thisPart
     137                    expression ~= " ~ " ~ DSPGrammar.toStringFunc ~ "(" ~ thisPart ~ ")"
    138138                }        
    139139            } 
  • trunk/dsp/TagLibrary.d

    r48 r71  
    2626module dsp.TagLibrary; 
    2727 
    28 private import xml.XMLAttributes
     28private import xml.utf8all
    2929 
    3030private import dsp.ServletGenerator; 
  • trunk/dsp/Util.d

    r48 r71  
    3838    int pos = 0; 
    3939     
     40    debug printf("split: %.*s\n",src); 
     41    debug printf("src: '%*.s'\n",src); 
    4042    while(lastPos < src.length){ 
    4143        pos = find(src[lastPos..$],delim); 
    42         pos += lastPos; 
    4344        if(pos == -1){ 
    4445            result ~= src[lastPos..$]; 
     
    4647        } 
    4748        else{ 
    48             result ~= src[lastPos..pos]; // get segment 
     49            pos += lastPos; 
     50            debug printf("last %d, pos: %d\n",lastPos,pos); 
     51            if(pos == 0) result ~= ""; 
     52            else result ~= src[lastPos..pos]; // get segment 
    4953            result ~= src[pos..pos + delim.length]; // keep the delimiter 
    5054            lastPos = pos + delim.length; // advance 
    5155        } 
    5256    } 
     57    printf("\n"); 
    5358    return result; 
    5459} 
  • trunk/dsp/servlet/DSPServlet.d

    r67 r71  
    3838private import dsp.RequestConfig; 
    3939 
    40 private import mango.cache.Payload; 
    41 private import mango.cache.QueuedCache; 
     40private import mango.cache.HashMap; 
    4241 
    4342private import mango.servlet.Servlet; 
     
    6059//TODO: use SystemFinalizer to catch libraries that are still loaded at shutdown 
    6160 
    62 class DSPLibrary : Payload
     61class DSPLibrary
    6362    protected long timeModified; 
    6463    protected Library lib; 
    6564    public ServiceHandle service; 
    6665    protected DependencyHandle dependencies; 
     66     
     67    public DSPLibrary next,last; 
     68    public char[] key; 
    6769 
    6870    protected void unload(){ 
    69         debug printf("unloading library\n"); 
    70         lib.unload(); 
    71         lib = null; 
    72         service = null; 
    73         dependencies = null; 
    74         timeModified = 0; 
     71        if(lib){ 
     72            debug printf("unloading library %.*s\n",lib.getPath()); 
     73            if(lib.isLoaded) lib.unload(); 
     74            lib = null; 
     75            service = null; 
     76            dependencies = null; 
     77            timeModified = 0; 
     78        } 
     79    } 
     80     
     81    public char[] getPath(){ 
     82        if(lib && lib.isLoaded) return lib.getPath(); 
     83        return "[not loaded]"; 
    7584    } 
    7685 
     
    91100                throw new DSPException("servlet library does not have a valid dependencies handle"); 
    92101            } 
     102             
     103            debug printf("library loaded: %.*s\n",path); 
    93104        } 
    94105        else{ 
     
    107118        // verify the date/time of the dependencies 
    108119        foreach(char[] dep; dependencies()){ 
    109             debug printf("[%.*s%.*s]\n",basePath,dep); 
     120            debug debug printf("[%.*s%.*s]\n",basePath,dep); 
    110121            if((new FileProxy(basePath ~ dep)).getModifiedTime() > timeModified){ 
    111                 debug printf("%.*s%.*s : servlet is out of date.\n",basePath,dep);               
    112                 unload(); // force unload 
     122                debug printf("%.*s%.*s : servlet is out of date.\n",basePath,dep);       
    113123                return true; 
    114124            } 
     
    118128         
    119129        return false; 
    120     } 
    121      
    122     void destroy (){ 
    123         printf("library: %.*s destroyed\n",lib.getPath); 
    124         unload(); 
    125130    } 
    126131     
     
    137142    } 
    138143} 
     144 
     145 
     146class DSPLibraryCache{ 
     147    HashMap map; 
     148    DSPLibrary mostPopular,leastPopular; // mp through last pointers to lp. lp through next opinters to mp. 
     149    uint freeCount; 
     150 
     151    this (uint capacity = 101, uint concurrency = 16){ 
     152        map = new HashMap (capacity, 0.75, concurrency); 
     153        mostPopular = null; 
     154        leastPopular = null; 
     155        freeCount = capacity; 
     156    } 
     157     
     158    protected void dropEntry(DSPLibrary lib){ 
     159        debug printf("dropEntry\n"); 
     160        if(lib.next) lib.next.last = lib.last; 
     161        if(lib.last) lib.last.next = lib.next; 
     162        if(lib is mostPopular){ 
     163            mostPopular = mostPopular.last; 
     164            if(mostPopular){ 
     165                mostPopular.last = null; 
     166            } 
     167        } 
     168        if(lib is leastPopular){ 
     169            leastPopular = leastPopular.next; 
     170            if(leastPopular){ 
     171                leastPopular.last = null; 
     172            } 
     173        } 
     174    } 
     175     
     176    protected void makePopular(DSPLibrary lib){ 
     177        debug printf("makePopular\n"); 
     178        dropEntry(lib); 
     179        if(mostPopular is null){ 
     180            mostPopular = lib; 
     181            leastPopular = lib; 
     182        } 
     183        else if(!(lib is mostPopular)){ 
     184            mostPopular.next = lib; 
     185            lib.last = mostPopular; 
     186            mostPopular = lib; 
     187        } 
     188    } 
     189     
     190    protected void dropLeastPopular(){ 
     191        debug printf("dropLeastPopular\n"); 
     192        if(leastPopular){ 
     193            leastPopular.unload(); 
     194            leastPopular = leastPopular.next; 
     195            if(leastPopular){ 
     196                leastPopular.last = null; 
     197            } 
     198        } 
     199    } 
     200     
     201    void put(char[] path,DSPLibrary lib){ 
     202        // place entry in the queue 
     203        if(lib){ 
     204            assert(!map.containsKey(path.dup));  
     205         
     206            lib.key = path.dup; // set the key for future use 
     207             
     208            if(freeCount == 0){ 
     209                //replace oldest cache entry and free the replaced entry 
     210                map.remove(leastPopular.key); 
     211                synchronized(this){ 
     212                    dropLeastPopular(); 
     213                    makePopular(lib); 
     214                } 
     215            } 
     216            else{ 
     217                synchronized(this) makePopular(lib); 
     218                freeCount--; 
     219            } 
     220             
     221            // place the entry in the map 
     222            map.put(lib.key,lib); 
     223        } 
     224         
     225        // remove the entry from the queue 
     226        else{ 
     227            Object obj = map.remove(path); 
     228            if(obj){ 
     229                DSPLibrary lib = cast(DSPLibrary)obj; 
     230                //remove this entry from the list 
     231                synchronized(this){ 
     232                    dropEntry(lib); 
     233                } 
     234                freeCount++; 
     235            } 
     236        } 
     237    } 
     238     
     239    DSPLibrary get(char[] path){ 
     240        Object obj = map.get(path); 
     241        if(obj){ 
     242            DSPLibrary lib = cast(DSPLibrary)obj; 
     243            synchronized(this) makePopular(lib); 
     244            return lib; 
     245        } 
     246        return null; 
     247    } 
     248     
     249    char[] toString(){ 
     250        char[] output = ""; 
     251        DSPLibrary current = leastPopular; 
     252        uint i; 
     253        while(current){ 
     254            output ~= "- " ~ current.getPath() ~ "\n";   
     255            current = current.next; 
     256            i++; 
     257            if(i == 100) return  "[possible infinite recursion on linked-list]\n"; 
     258        } 
     259        return output; 
     260    } 
     261} 
     262 
    139263 
    140264/*  
     
    154278class DSPServlet : Servlet{ 
    155279    private static Logger logger; 
    156     protected QueuedCache cache; 
     280    protected DSPLibraryCache cache; 
    157281 
    158282    static this (){ 
     
    170294        libraryPath ~= path.getName[1..$] ~ ".dll"; 
    171295         
    172         debug printf("attempting to recover: %.*s\n",libraryPath); 
     296        debug debug printf("attempting to recover: %.*s\n",libraryPath); 
    173297         
    174298        //TODO: check for file existence here or not? 
     
    178302         
    179303        // catch-all here as the library may not be in a usable condition 
    180         //TODO: eliminate the library from the cache if it exists? 
    181304        catch(Exception e){ 
    182305            return null; 
     
    218341 
    219342        // debug 
    220         debug printf("%.*s\n",cfg.toString()); 
     343        debug debug printf("%.*s\n",cfg.toString()); 
    221344 
    222345        // build the servlet library 
     
    248371            DSPLibrary lib = cast(DSPLibrary)cache.get(request.getPathInfo); 
    249372             
    250             if(lib){ 
    251                 printf("cache lib: %.*s = %.*s\n",request.getPathInfo,lib.toString()); 
    252             } 
    253             else{ 
    254                 printf("cache lib: %.*s = [not present]\n",request.getPathInfo); 
     373            debug{ 
     374                if(lib){ 
     375                    printf("cache lib: %.*s = %.*s\n",request.getPathInfo,lib.toString()); 
     376                } 
     377                else{ 
     378                    printf("cache lib: %.*s = [not present]\n",request.getPathInfo); 
     379                } 
    255380            } 
    256381             
    257382            // attempt to recover the lib from the file cache 
    258383            if(!lib){ 
    259                 cache.extract(request.getPathInfo); // kill the cache entry 
    260384                lib = recoverLibrary(context,path); 
    261                 if(lib){ 
    262                     cache.put(request.getPathInfo,lib); 
    263                 } 
     385                cache.put(request.getPathInfo,lib); // update the cache with new or empty lib 
    264386            } 
    265387             
    266388            // rebuild the library if recovery fails or is out of date 
    267             if(!lib || lib.isOutOfDate(context.getBasePath,path)){ 
     389            if(!lib){ 
    268390                lib = buildLibrary(context,path); 
    269                 cache.put(request.getPathInfo,lib); 
     391                cache.put(request.getPathInfo,lib); // update the cache 
     392            } 
     393            else if(lib.isOutOfDate(context.getBasePath,path)){ 
     394                lib.unload(); 
     395                cache.put(request.getPathInfo,null); // remove the old entry 
     396                lib = buildLibrary(context,path); 
     397                cache.put(request.getPathInfo,lib); // update the cache          
    270398            } 
    271399             
    272400            // run the servlet 
    273             lib.service(dspRequest,dspResponse); 
    274              
     401            if(lib){ 
     402                lib.service(dspRequest,dspResponse); 
     403            } 
     404            else{ 
     405                //TODO: report failure to build library 
     406            } 
     407             
     408            debug printf("Cache:\n%.*s",cache.toString()); 
    275409            //TODO: debug information 
    276410        } 
     
    308442        debug printf("(cache config) size: %d threads: %d\n",size,threads); 
    309443         
    310         cache = new QueuedCache(size,threads);         
     444        cache = new DSPLibraryCache(size,threads);         
    311445    }    
    312446} 
  • trunk/dsp/servlet/DSPServletProvider.d

    r67 r71  
    2525 
    2626module dsp.servlet.DSPServletProvider; 
    27  
    28 private import xml.ConduitStream; 
    2927 
    3028private import dsp.ConfigParser; 
     
    5755        // load configuration 
    5856        if(fc){ 
    59             ConduitStream cs = new ConduitStream(fc.getPath.getName,fc); 
    6057            ConfigParser configParser = new ConfigParser(); 
    61             configuration = configParser.parse(cs); 
     58            configuration = configParser.parse(fc); 
    6259        } 
    6360    } 
  • trunk/dsp/tags/Code.d

    r48 r71  
    2626module dsp.tags.Code; 
    2727 
    28 private import xml.XMLAttributes
     28private import xml.utf8all
    2929 
    3030private import dsp.ServletGenerator; 
  • trunk/dsp/tags/InCode.d

    r48 r71  
    2626module dsp.tags.InCode; 
    2727 
    28 private import xml.XMLAttributes
     28private import xml.utf8all
    2929 
    3030private import dsp.ServletGenerator; 
  • trunk/dsp/tags/OutCode.d

    r48 r71  
    2626module dsp.tags.OutCode; 
    2727 
    28 private import xml.XMLAttributes
     28private import xml.utf8all
    2929 
    3030private import dsp.ServletGenerator;