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

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

E-mail: