1 # vim: ft=tcl foldmarker=<<<,>>>
3 class tlc
::Sessionobj {
10 variable timeout
1200 set_timeout
;# the length of time, in seconds, for which this session is valid
11 variable delete_when_empty
1 ;# determines whether setting a var to "" removes it, like a web session
12 method getvar
{varname
}
13 method setvar
{varname value
}
14 method var_exists
{varname
}
22 method update_access
{}
23 method set_timeout
{{newval
""}}
24 method delete_var
{varname
}
28 body tlc
::Sessionobj::constructor {args
} { #<<<1
30 log notice
"connecting ip is: $client_ip"
31 set sessionkey
"${client_ip}[clock clicks]"
32 while {[info exists tlc
::sessions($sessionkey)]} {
33 set sessionkey
"${client_ip}[clock clicks]"
39 body tlc
::Sessionobj::getvar {varname
} { #<<<1
40 if {[info exists sessionvars
($varname)]} {
41 return $sessionvars($varname)
47 body tlc
::Sessionobj::setvar {varname value
} { #<<<1
48 if {$value=="" && $delete_when_empty} {
51 set sessionvars
($varname) $value
56 body tlc
::Sessionobj::var_exists {varname
} { #<<<1
57 return [info exists sessionvars
($varname)]
60 body tlc
::Sessionobj::update_access {} { #<<<1
62 set timerid
[after [expr {$timeout*1000}] [code
$this destructor
]]
63 # set last_access [clock seconds]
66 body tlc
::Sessionobj::set_timeout {{newval
""}} { #<<<1
68 if {$newval == ""} {set newval
$timeout}
69 if {[string is double
-strict $newval]} {
72 set num
[string range
[expr [string length
$newval]-1] end
]
73 if {[string is double
-strict $num]} {
74 switch -- [string range
$newval end end
] {
75 "h" {set timeout
[expr {$newval*3600000}]}
76 "m" {set timeout
[expr {$newval*60000}]}
77 "s" {set timeout
[expr {$num*1000}]}
79 log
error "Unable to set session lifetime to requested value of\"$newval\".\
80 Value not understood."
85 log
error "Unable to set session lifetime to requested value of\"$newval\". Value not understood."
92 body tlc
::Sessionobj::delete_var {varname
} { #<<<1
93 if {[info exists sessionvars
(varname
)]} {
94 foreach {idx val
} [array get sessionvars
] {
95 if {$idx != $varname} {
96 set new_vars
($idx) $val
99 array unset sessionvars
100 array set sessionvars
[array get new_vars
]
102 return 1 ;# although the function wasn't successful in the correct sense of the word, the described
103 # var doesn't exist -- the net result is the same as deleting it
107 body tlc
::Sessionobj::dump {} { #<<<1
108 return [array get sessionvars
]
111 body tlc
::Sessionobj::destructor {} { #<<<1
112 log notice
"Session object ($sessionkey) is dying due to timeout."