Class: CARPS::SessionManager

Inherits:
Object
  • Object
show all
Defined in:
lib/carps/service/session.rb

Overview

A session manager

It coordinates sessions between the game and the email system The idea is to prevent a game from receiving messages intended for another game.

Its methods are reentrant

Instance Method Summary collapse

Constructor Details

#initializeSessionManager

Returns a new instance of SessionManager.



31
32
33
34
35
# File 'lib/carps/service/session.rb', line 31

def initialize
   @session = ""
   @semaphore = Mutex.new
   @syms = (" ".."~").to_a
end

Instance Method Details

#belong?(message) ⇒ Boolean

Is this message appropriate for the current session

Returns:

  • (Boolean)


77
78
79
80
81
82
83
84
85
86
87
# File 'lib/carps/service/session.rb', line 77

def belong? message
   @semaphore.synchronize do
      if not message.session
         false
      elsif not @session.empty?
         message.session == @session
      else
         true
      end
   end
end

#generate(key) ⇒ Object

Generate a new session from a key



46
47
48
49
50
51
52
53
54
55
# File 'lib/carps/service/session.rb', line 46

def generate key
   t = Time.new
   # It will be unlikely that anyone will guess this session
   ra = randoms 1000
   new_session = ra + key + t.to_f.to_s
   @semaphore.synchronize do
      @session = new_session 
   end
   @session
end

#keyObject

Get the session key



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

def key
   @session
end

#noneObject

Remove the current session



70
71
72
73
74
# File 'lib/carps/service/session.rb', line 70

def none
   @semaphore.synchronize do
      @session = "" 
   end
end

#randoms(size) ⇒ Object

Generate a number of random symbols



38
39
40
41
42
43
# File 'lib/carps/service/session.rb', line 38

def randoms size
   ras = Array.new size do |forget|
      @syms[rand(95)]
   end
   ras.join
end

#session=(sess) ⇒ Object

Set the current session



58
59
60
61
62
# File 'lib/carps/service/session.rb', line 58

def session= sess
   @semaphore.synchronize do
      @session = sess
   end
end

#tag(message) ⇒ Object

Tag a string with a message



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

def tag message
   V.session(@session) + message
end