Class: Wee::Session

Inherits:
Object show all
Defined in:
lib/wee/session.rb

Defined Under Namespace

Classes: AbortCallbackProcessing, MutexSerializer, Page, ThreadSerializer

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root_component, serializer = nil, page_cache_capacity = 20) ⇒ Session

Creates a new session.



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/wee/session.rb', line 95

def initialize(root_component, serializer=nil, page_cache_capacity=20)
  @root_component = root_component
  @page_cache = Wee::LRUCache.new(page_cache_capacity)
  @page_ids = Wee::IdGenerator::Sequential.new
  @current_page = nil

  @running = true

  @expire_after = 30*60
  @max_lifetime = nil
  @max_requests = nil

  @last_access = @creation_time = Time.now 
  @request_count = 0
  
  @serializer = serializer || MutexSerializer.new
end

Instance Attribute Details

#applicationObject

Points to the Wee::Application object this session belongs to.



65
66
67
# File 'lib/wee/session.rb', line 65

def application
  @application
end

#expire_afterObject

Expire the session after this number of seconds of inactivity. If this value is nil, the Session will never expire due to inactivity. (but still may expire for example due to max_lifetime).

Default: 1800 seconds (30 minutes)



74
75
76
# File 'lib/wee/session.rb', line 74

def expire_after
  @expire_after
end

#idObject

The (application-wide) unique id of this session.



60
61
62
# File 'lib/wee/session.rb', line 60

def id
  @id
end

#max_lifetimeObject

The maximum lifetime of this session in seconds. A value of nil means infinite lifetime.

Default: nil (infinite lifetime)



82
83
84
# File 'lib/wee/session.rb', line 82

def max_lifetime
  @max_lifetime
end

#max_requestsObject

The maximum number of requests this session is allowed to serve. A value of nil means no limitation.

Default: nil (infinite number of requests)



90
91
92
# File 'lib/wee/session.rb', line 90

def max_requests
  @max_requests
end

Class Method Details

.currentObject

Returns the current session (thread-local).



159
160
161
# File 'lib/wee/session.rb', line 159

def self.current
  Thread.current[:wee_session] || (raise "Not in session")
end

Instance Method Details

#alive?Boolean

Queries whether the session is still alive.

Returns:

  • (Boolean)


126
127
128
129
130
131
132
133
# File 'lib/wee/session.rb', line 126

def alive?
  now = Time.now
  return false if not @running
  return false if @expire_after and now - @last_access > @expire_after 
  return false if @max_lifetime and now - @creation_time > @max_lifetime 
  return false if @max_requests and @request_count >= @max_requests
  return true
end

#call(env) ⇒ Object

Handles a web request.



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/wee/session.rb', line 166

def call(env)
  if env['wee.session']
    # we are already serialized
    raise if env['wee.session'] != self
    begin
      Thread.current[:wee_session] = self
      @request_count += 1
      @last_access = Time.now
      awake
      response = handle(env)
      sleep
      return response
    ensure
      Thread.current[:wee_session] = nil
    end
  else
    env['wee.session'] = self
    @serializer.call(env)
  end
end

#dead?Boolean

Queries whether the session is dead.

Returns:

  • (Boolean)


138
139
140
# File 'lib/wee/session.rb', line 138

def dead?
  not alive?
end

#render_ajax_proc(block, component) ⇒ Object



252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
# File 'lib/wee/session.rb', line 252

def render_ajax_proc(block, component)
  proc {
    r = component.renderer_class.new
    r.session   = self
    r.request   = @request
    r.response  = Wee::Response.new
    r.document  = Wee::HtmlDocument.new
    r.callbacks = @page.callbacks
    r.current_component = component

    begin
      block.call(r)
    ensure
      r.close
    end

    r.response << r.document.to_s
    send_response(r.response)
  }
end

#send_response(response) ⇒ Object

Send a premature response



190
191
192
# File 'lib/wee/session.rb', line 190

def send_response(response)
  raise AbortCallbackProcessing.new(response)
end

#statisticsObject

Returns some statistics



145
146
147
148
149
150
151
152
153
154
# File 'lib/wee/session.rb', line 145

def statistics
  now = Time.now
  {
    :last_access => @last_access,        # The time when this session was last accessed
    :inactivity => now - @last_access,   # The number of seconds of inactivity
    :creation_time => @creation_time,    # The time at which this session was created 
    :lifetime =>  now - @creation_time,  # The lifetime of this session in seconds
    :request_count => @request_count     # The number of requests served by this session
  }
end

#terminateObject

Terminates the session.

This will usually not immediatly terminate the session from running, but further requests will not be answered.



119
120
121
# File 'lib/wee/session.rb', line 119

def terminate
  @running = false
end