Class: N::App::SessionManager

Inherits:
SafeHash
  • Object
show all
Defined in:
lib/n/app/session.rb

Overview

SessionManager

This object manages Session Objects. Several utility methods are also provided.

WARNING:

This object is typically called in a distributed configuration. Avoid writting methods that accept or return big objects!

SOS: This object lives in the Cluster!

Instance Attribute Summary collapse

Attributes inherited from SafeHash

#sync

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from SafeHash

#[], #[]=, #clear, #delete, #keys, #size, #values

Constructor Details

#initializeSessionManager

Returns a new instance of SessionManager.



34
35
36
37
# File 'lib/n/app/session.rb', line 34

def initialize
	super
	@online = N::SafeHash.new
end

Instance Attribute Details

#onlineObject (readonly)

the collection of online users.



32
33
34
# File 'lib/n/app/session.rb', line 32

def online
  @online
end

Class Method Details

.garbage_collect!Object

garbage collect stale sessions

TODO:

  • add unit testing.



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/n/app/session.rb', line 79

def self.garbage_collect!
	# gmosx: the only way to get it to ruby with
	# druby, aaarghhh!! Rethink this though! perhaps some
	# fixes i made allow for recoding this.
	for key in $sessions.keys()
		begin
			session = $sessions[key]
				if session.stale?
				$log.debug "Session finalized: logging out idle user '#{session.user}'" if $DBG
				session.logout()
				$sessions.delete(key)
			end
		rescue Exception, StandardError => e
			$log.error "Session gc errror #$!"
		end
	end
end

Instance Method Details

#anonymous_countObject

Return the number of anonymous sessions.



41
42
43
# File 'lib/n/app/session.rb', line 41

def anonymous_count
	return self.size - @online.size
end

#login(user_oid, user_name) ⇒ Object

Login a named user. The caller may pass user.to_html instead of the user name. this method is distributed keep a lightweight signature. By using @online.keys i get access to the oids and in this way to the full objects.



51
52
53
# File 'lib/n/app/session.rb', line 51

def (user_oid, user_name)
	@online[user_oid] = user_name
end

#logout(user_oid) ⇒ Object

Logout a named user.



57
58
59
# File 'lib/n/app/session.rb', line 57

def logout(user_oid)
	@online.delete(user_oid)
end

#session_for_name(name) ⇒ Object

Returns the session for the given user.



69
70
71
# File 'lib/n/app/session.rb', line 69

def session_for_name(name)
	return values().find { |s| name == s.user.name }
end

#session_for_user(user) ⇒ Object

Returns the session for the given user.



63
64
65
# File 'lib/n/app/session.rb', line 63

def session_for_user(user)
	return values().find { |s| user.oid == s.user.oid }
end