Annotation of parser3/src/main/pa_stylesheet_manager.C, revision 1.1

1.1     ! parser      1: /** @file
        !             2:        Parser: sql driver manager implementation.
        !             3: 
        !             4:        Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com)
        !             5:        Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf)
        !             6: 
        !             7:        $Id: pa_xslt_stylesheet_manager.C,v 1.4 2001/09/26 10:32:26 parser Exp $
        !             8: */
        !             9: #include "pa_config_includes.h"
        !            10: #ifdef XML
        !            11: 
        !            12: #include "pa_stylesheet_manager.h"
        !            13: #include "pa_stylesheet_connection.h"
        !            14: #include "ltdl.h"
        !            15: #include "pa_exception.h"
        !            16: #include "pa_common.h"
        !            17: #include "pa_threads.h"
        !            18: #include "pa_stack.h"
        !            19: 
        !            20: // globals
        !            21: 
        !            22: Stylesheet_manager *stylesheet_manager;
        !            23: 
        !            24: // consts
        !            25: 
        !            26: const int EXPIRE_UNUSED_CONNECTION_SECONDS=5*60;
        !            27: const int CHECK_EXPIRED_CONNECTION_SECONDS=EXPIRE_UNUSED_CONNECTION_SECONDS*2;
        !            28: 
        !            29: // helpers
        !            30: 
        !            31: static void expire_connection(Array::Item *value, void *info) {
        !            32:        Stylesheet_connection& connection=*static_cast<Stylesheet_connection *>(value);
        !            33:        time_t older_dies=reinterpret_cast<time_t>(info);
        !            34: 
        !            35:        if(connection.connected() && connection.expired(older_dies))
        !            36:                connection.disconnect();
        !            37: }
        !            38: static void expire_connections(const Hash::Key& key, Hash::Val *value, void *info) {
        !            39:        Stack& stack=*static_cast<Stack *>(value);
        !            40:        for(int i=0; i<=stack.top_index(); i++)
        !            41:                expire_connection(stack.get(i), info);
        !            42: }
        !            43: 
        !            44: // Stylesheet_manager
        !            45: 
        !            46: Stylesheet_manager::Stylesheet_manager(Pool& pool) : Pooled(pool),
        !            47:        connection_cache(pool),
        !            48:        prev_expiration_pass_time(0) {
        !            49: }
        !            50: Stylesheet_manager::~Stylesheet_manager() {
        !            51:        connection_cache.for_each(expire_connections, 
        !            52:                reinterpret_cast<void *>((time_t)0/*=in past=expire all*/));
        !            53: }
        !            54: 
        !            55: Stylesheet_connection& Stylesheet_manager::get_connection(const String& request_file_spec) {
        !            56:        Pool& pool=request_file_spec.pool(); // request pool                                                                                       
        !            57: 
        !            58:        // first trying to get cached stylesheet
        !            59:        Stylesheet_connection *result=get_connection_from_cache(request_file_spec);
        !            60:        if(!result) {
        !            61:                // then just construct it
        !            62: 
        !            63:                // make global_file_spec C-string on global pool
        !            64:                const char *request_file_spec_cstr=request_file_spec.cstr(String::UL_FILE_SPEC);
        !            65:                char *global_file_spec_cstr=(char *)malloc(strlen(request_file_spec_cstr)+1);
        !            66:                strcpy(global_file_spec_cstr, request_file_spec_cstr);
        !            67:                // make global_file_spec string on global pool
        !            68:                String& global_file_spec=*new(this->pool()) String(this->pool(), global_file_spec_cstr);
        !            69: 
        !            70:                result=new(this->pool()) Stylesheet_connection(this->pool(), global_file_spec);
        !            71:        }
        !            72:        // associate with services[request]  (deassociates at close)
        !            73:        result->set_services(&pool); 
        !            74:        // return it
        !            75:        return *result;
        !            76: }
        !            77: 
        !            78: void Stylesheet_manager::close_connection(const String& file_spec, 
        !            79:                                                                                  Stylesheet_connection& connection) {
        !            80:        // deassociate from services[request]
        !            81:        connection.set_services(0);
        !            82:        put_connection_to_cache(file_spec, connection);
        !            83: }
        !            84: 
        !            85: 
        !            86: // stylesheet cache
        !            87: /// @todo get rid of memory spending Stack [zeros deep inside got accumulated]
        !            88: Stylesheet_connection *Stylesheet_manager::get_connection_from_cache(const String& file_spec) { 
        !            89:        SYNCHRONIZED;
        !            90: 
        !            91:        maybe_expire_connection_cache();
        !            92: 
        !            93:        if(Stack *connections=static_cast<Stack *>(connection_cache.get(file_spec)))
        !            94:                while(connections->top_index()>=0) { // there are cached stylesheets to that 'file_spec'
        !            95:                        Stylesheet_connection *result=static_cast<Stylesheet_connection *>(connections->pop());
        !            96:                        if(result->connected()) // not expired?
        !            97:                                return result;
        !            98:                }
        !            99: 
        !           100:        return 0;
        !           101: }
        !           102: 
        !           103: void Stylesheet_manager::put_connection_to_cache(const String& file_spec, 
        !           104:                                                                                                 Stylesheet_connection& connection) { 
        !           105:        SYNCHRONIZED;
        !           106: 
        !           107:        Stack *connections=static_cast<Stack *>(connection_cache.get(file_spec));
        !           108:        if(!connections) { // there are no cached stylesheets to that 'file_spec' yet?
        !           109:                connections=NEW Stack(pool()); // NOTE: never freed up!
        !           110:                connection_cache.put(file_spec, connections);
        !           111:        }       
        !           112:        connections->push(&connection);
        !           113: }
        !           114: 
        !           115: void Stylesheet_manager::maybe_expire_connection_cache() {
        !           116:        time_t now=time(0);
        !           117: 
        !           118:        if(prev_expiration_pass_time<now-CHECK_EXPIRED_CONNECTION_SECONDS) {
        !           119:                connection_cache.for_each(expire_connections, 
        !           120:                        reinterpret_cast<void *>(now-EXPIRE_UNUSED_CONNECTION_SECONDS));
        !           121: 
        !           122:                prev_expiration_pass_time=now;
        !           123:        }
        !           124: }
        !           125: #endif

E-mail: