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

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.6     ! paf         7:        $Id: pa_stylesheet_manager.C,v 1.5 2001/11/08 11:04:13 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.6     ! paf        48: Stylesheet_manager::Stylesheet_manager(Pool& apool) : Cache_manager(apool),
1.3       paf        49:        connection_cache(apool),
1.1       parser     50:        prev_expiration_pass_time(0) {
1.5       paf        51:        }
1.1       parser     52: Stylesheet_manager::~Stylesheet_manager() {
                     53:        connection_cache.for_each(expire_connections, 
                     54:                reinterpret_cast<void *>((time_t)0/*=in past=expire all*/));
                     55: }
                     56: 
                     57: Stylesheet_connection& Stylesheet_manager::get_connection(const String& request_file_spec) {
                     58:        Pool& pool=request_file_spec.pool(); // request pool                                                                                       
                     59: 
                     60:        // first trying to get cached stylesheet
                     61:        Stylesheet_connection *result=get_connection_from_cache(request_file_spec);
                     62:        if(!result) {
                     63:                // then just construct it
                     64: 
                     65:                // make global_file_spec C-string on global pool
                     66:                const char *request_file_spec_cstr=request_file_spec.cstr(String::UL_FILE_SPEC);
                     67:                char *global_file_spec_cstr=(char *)malloc(strlen(request_file_spec_cstr)+1);
                     68:                strcpy(global_file_spec_cstr, request_file_spec_cstr);
                     69:                // make global_file_spec string on global pool
                     70:                String& global_file_spec=*new(this->pool()) String(this->pool(), global_file_spec_cstr);
                     71: 
                     72:                result=new(this->pool()) Stylesheet_connection(this->pool(), global_file_spec);
                     73:        }
                     74:        // associate with services[request]  (deassociates at close)
                     75:        result->set_services(&pool); 
                     76:        // return it
                     77:        return *result;
                     78: }
                     79: 
                     80: void Stylesheet_manager::close_connection(const String& file_spec, 
                     81:                                                                                  Stylesheet_connection& connection) {
                     82:        // deassociate from services[request]
                     83:        connection.set_services(0);
                     84:        put_connection_to_cache(file_spec, connection);
                     85: }
                     86: 
                     87: 
                     88: // stylesheet cache
                     89: /// @todo get rid of memory spending Stack [zeros deep inside got accumulated]
                     90: Stylesheet_connection *Stylesheet_manager::get_connection_from_cache(const String& file_spec) { 
                     91:        SYNCHRONIZED;
                     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: 
1.5       paf       115: void Stylesheet_manager::maybe_expire_cache() {
1.1       parser    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: }
1.2       paf       125: 
1.3       paf       126: static void add_connection_to_status_cache_table(Array::Item *value, void *info) {
                    127:        Stylesheet_connection& connection=*static_cast<Stylesheet_connection *>(value);
                    128:        Table& table=*static_cast<Table *>(info);
                    129: 
                    130:        if(connection.connected()) {
                    131:                Pool& pool=table.pool();
1.4       paf       132:                Array& row=*new(pool) Array(pool);
1.3       paf       133: 
1.4       paf       134:                // file
1.3       paf       135:                row+=&connection.file_spec();
1.4       paf       136:                // time
1.3       paf       137:                time_t time_stamp=connection.get_time_used();
                    138:                const char *unsafe_time_cstr=ctime(&time_stamp);
                    139:                int time_buf_size=strlen(unsafe_time_cstr);
                    140:                char *safe_time_buf=(char *)pool.malloc(time_buf_size);
                    141:                memcpy(safe_time_buf, unsafe_time_cstr, time_buf_size);
                    142:                row+=new(pool) String(pool, safe_time_buf, time_buf_size);
                    143: 
                    144:                table+=&row;
                    145:        }
                    146: }
                    147: static void add_connections_to_status_cache_table(const Hash::Key& key, Hash::Val *value, void *info) {
                    148:        Stack& stack=*static_cast<Stack *>(value);
                    149:        Array_iter iter(stack);
                    150:        for(int countdown=stack.top_index(); countdown-->=0; )
                    151:                add_connection_to_status_cache_table(iter.next(), info);
                    152: }
                    153: 
1.2       paf       154: Value& Stylesheet_manager::get_status(Pool& pool, const String *source) {
                    155:        VHash& result=*new(pool) VHash(pool);
1.3       paf       156: 
1.2       paf       157:        // cache
                    158:        {
1.4       paf       159:                Array& columns=*new(pool) Array(pool);
1.3       paf       160:                columns+=new(pool) String(pool, "file");
1.2       paf       161:                columns+=new(pool) String(pool, "time");
                    162:                Table& table=*new(pool) Table(pool, 0, &columns, connection_cache.size());
                    163: 
                    164:                connection_cache.for_each(add_connections_to_status_cache_table, &table);
                    165: 
                    166:                result.hash(source).put(*new(pool) String(pool, "cache"), new(pool) VTable(pool, &table));
                    167:        }
1.3       paf       168: 
1.2       paf       169:        return result;
                    170: }
                    171: 
1.1       parser    172: #endif

E-mail: