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

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: 
1.2     ! paf         7:        $Id: pa_stylesheet_manager.C,v 1.1 2001/10/22 16:44:42 parser Exp $
1.1       parser      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"
1.2     ! paf        19: #include "pa_vhash.h"
1.1       parser     20: 
                     21: // globals
                     22: 
                     23: Stylesheet_manager *stylesheet_manager;
                     24: 
                     25: // consts
                     26: 
                     27: const int EXPIRE_UNUSED_CONNECTION_SECONDS=5*60;
                     28: const int CHECK_EXPIRED_CONNECTION_SECONDS=EXPIRE_UNUSED_CONNECTION_SECONDS*2;
                     29: 
                     30: // helpers
                     31: 
                     32: static void expire_connection(Array::Item *value, void *info) {
                     33:        Stylesheet_connection& connection=*static_cast<Stylesheet_connection *>(value);
                     34:        time_t older_dies=reinterpret_cast<time_t>(info);
                     35: 
                     36:        if(connection.connected() && connection.expired(older_dies))
                     37:                connection.disconnect();
                     38: }
                     39: static void expire_connections(const Hash::Key& key, Hash::Val *value, void *info) {
                     40:        Stack& stack=*static_cast<Stack *>(value);
                     41:        for(int i=0; i<=stack.top_index(); i++)
                     42:                expire_connection(stack.get(i), info);
                     43: }
                     44: 
                     45: // Stylesheet_manager
                     46: 
                     47: Stylesheet_manager::Stylesheet_manager(Pool& pool) : Pooled(pool),
                     48:        connection_cache(pool),
                     49:        prev_expiration_pass_time(0) {
                     50: }
                     51: Stylesheet_manager::~Stylesheet_manager() {
                     52:        connection_cache.for_each(expire_connections, 
                     53:                reinterpret_cast<void *>((time_t)0/*=in past=expire all*/));
                     54: }
                     55: 
                     56: Stylesheet_connection& Stylesheet_manager::get_connection(const String& request_file_spec) {
                     57:        Pool& pool=request_file_spec.pool(); // request pool                                                                                       
                     58: 
                     59:        // first trying to get cached stylesheet
                     60:        Stylesheet_connection *result=get_connection_from_cache(request_file_spec);
                     61:        if(!result) {
                     62:                // then just construct it
                     63: 
                     64:                // make global_file_spec C-string on global pool
                     65:                const char *request_file_spec_cstr=request_file_spec.cstr(String::UL_FILE_SPEC);
                     66:                char *global_file_spec_cstr=(char *)malloc(strlen(request_file_spec_cstr)+1);
                     67:                strcpy(global_file_spec_cstr, request_file_spec_cstr);
                     68:                // make global_file_spec string on global pool
                     69:                String& global_file_spec=*new(this->pool()) String(this->pool(), global_file_spec_cstr);
                     70: 
                     71:                result=new(this->pool()) Stylesheet_connection(this->pool(), global_file_spec);
                     72:        }
                     73:        // associate with services[request]  (deassociates at close)
                     74:        result->set_services(&pool); 
                     75:        // return it
                     76:        return *result;
                     77: }
                     78: 
                     79: void Stylesheet_manager::close_connection(const String& file_spec, 
                     80:                                                                                  Stylesheet_connection& connection) {
                     81:        // deassociate from services[request]
                     82:        connection.set_services(0);
                     83:        put_connection_to_cache(file_spec, connection);
                     84: }
                     85: 
                     86: 
                     87: // stylesheet cache
                     88: /// @todo get rid of memory spending Stack [zeros deep inside got accumulated]
                     89: Stylesheet_connection *Stylesheet_manager::get_connection_from_cache(const String& file_spec) { 
                     90:        SYNCHRONIZED;
                     91: 
                     92:        maybe_expire_connection_cache();
                     93: 
                     94:        if(Stack *connections=static_cast<Stack *>(connection_cache.get(file_spec)))
                     95:                while(connections->top_index()>=0) { // there are cached stylesheets to that 'file_spec'
                     96:                        Stylesheet_connection *result=static_cast<Stylesheet_connection *>(connections->pop());
                     97:                        if(result->connected()) // not expired?
                     98:                                return result;
                     99:                }
                    100: 
                    101:        return 0;
                    102: }
                    103: 
                    104: void Stylesheet_manager::put_connection_to_cache(const String& file_spec, 
                    105:                                                                                                 Stylesheet_connection& connection) { 
                    106:        SYNCHRONIZED;
                    107: 
                    108:        Stack *connections=static_cast<Stack *>(connection_cache.get(file_spec));
                    109:        if(!connections) { // there are no cached stylesheets to that 'file_spec' yet?
                    110:                connections=NEW Stack(pool()); // NOTE: never freed up!
                    111:                connection_cache.put(file_spec, connections);
                    112:        }       
                    113:        connections->push(&connection);
                    114: }
                    115: 
                    116: void Stylesheet_manager::maybe_expire_connection_cache() {
                    117:        time_t now=time(0);
                    118: 
                    119:        if(prev_expiration_pass_time<now-CHECK_EXPIRED_CONNECTION_SECONDS) {
                    120:                connection_cache.for_each(expire_connections, 
                    121:                        reinterpret_cast<void *>(now-EXPIRE_UNUSED_CONNECTION_SECONDS));
                    122: 
                    123:                prev_expiration_pass_time=now;
                    124:        }
                    125: }
1.2     ! paf       126: 
        !           127: Value& Stylesheet_manager::get_status(Pool& pool, const String *source) {
        !           128:        VHash& result=*new(pool) VHash(pool);
        !           129: /*     
        !           130:        // cache
        !           131:        {
        !           132:                Array& columns=*new(pool) Array(pool, 3);
        !           133:                columns+=new(pool) String(pool, "protocol");
        !           134:                columns+=new(pool) String(pool, "time");
        !           135:                columns+=new(pool) String(pool, "times");
        !           136:                Table& table=*new(pool) Table(pool, 0, &columns, connection_cache.size());
        !           137: 
        !           138:                connection_cache.for_each(add_connections_to_status_cache_table, &table);
        !           139: 
        !           140:                result.hash(source).put(*new(pool) String(pool, "cache"), new(pool) VTable(pool, &table));
        !           141:        }
        !           142: */
        !           143:        return result;
        !           144: }
        !           145: 
1.1       parser    146: #endif

E-mail: