# Copyright: Copyright (c) 2004 Nicolas Despres. All rights reserved. # Author: Nicolas Despres . # License: Gnu General Public License. # $LastChangedBy: polrop $ # $Id: profile.rb 70 2004-12-07 17:44:50Z polrop $ require 'drb' require 'const_regexp' module Session class Profile attr_reader :data def initialize(usrname, usrdesc, uri, dispatcher, *hooks) @data = usrdesc.dup @data[:usrname] = usrname @data[:login_time] = Time.now @data[:last_request_time] = Time.now @data[:uri] = uri uri_chunks = Profile.parse_uri(uri) @data[:protocol], @data[:hostname], @data[:port] = *uri_chunks @data[:hooks] = hooks @data[:nb_requests] = 0 @data[:dispatcher] = dispatcher end def Profile.parse_uri(uri) return $1, $2, $3.to_i if uri =~ ConstRegexp::DRB_URI end def _dump(depth) data = {} @data.each do |key, val| begin data[key] = Marshal.dump(val) rescue TypeError data[key] = Marshal.dump(DRbObject.new(val)) end end Marshal.dump([self.class, data]) end def self._load(data) klass, data = Marshal.load(data) obj = klass.new(data[:usrname], {}, data[:uri], nil, nil) data.each do |attr, value| obj[attr] = Marshal.load(value) end obj end def [](key) @data[key] end def []=(key, value) @data[key] = value end def to_hash @data.dup end def each @data.each { |k, v| yield(k, v) } end def allowed_request?(request) return true if @data[:admin] @data[:allowed_requests].include?(request) end def add_request(request) unless @data[:allowed_requests].include?(request) @data[:allowed_requests] << request end end def allowed_hook?(hook) return true if @data[:admin] @data[:allowed_hooks].include?(hook) end def add_client(uri, client_class, *args) @data[:dispatcher].add(uri, @data[:usrname], @data[:passwd], client_class, *args) end def del_client(uri) @data[:dispatcher].del(uri) end end # class Profile end # class Session