Class: Colossus::Engine::Memory

Inherits:
Object
  • Object
show all
Includes:
Observable
Defined in:
lib/colossus/engines/memory/memory.rb,
lib/colossus/engines/memory/client_session.rb,
lib/colossus/engines/memory/client_session_store.rb

Overview

The Memory Engine is a non-distributed, in process, non-threadsafe engine. Based on EventMachine in order to provide the ttl to disconnect clients.

Defined Under Namespace

Classes: ClientSession, ClientSessionStore

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ttl) ⇒ Memory

Returns a new instance of Memory.

Parameters:

  • ttl (Integer/Float)

    TTL in seconds



12
13
14
15
16
17
# File 'lib/colossus/engines/memory/memory.rb', line 12

def initialize(ttl)
  @client_sessions = Hash.new do |hash, key|
    hash[key] = Colossus::Engine::Memory::ClientSessionStore.new
  end
  @ttl     = ttl
end

Instance Attribute Details

#client_sessionsObject (readonly)

Returns the value of attribute client_sessions.



9
10
11
# File 'lib/colossus/engines/memory/memory.rb', line 9

def client_sessions
  @client_sessions
end

#ttlObject (readonly)

Returns the value of attribute ttl.



9
10
11
# File 'lib/colossus/engines/memory/memory.rb', line 9

def ttl
  @ttl
end

Instance Method Details

#delete(user_id) ⇒ Object



62
63
64
# File 'lib/colossus/engines/memory/memory.rb', line 62

def delete(user_id)
  client_sessions.delete(user_id)
end

#delete_expired_users(user_ids) ⇒ Object



93
94
95
96
97
98
# File 'lib/colossus/engines/memory/memory.rb', line 93

def delete_expired_users(user_ids)
  user_ids.each do |user_id|
    delete(user_id)
    user_changed(user_id, DISCONNECTED)
  end
end

#gc_ttlObject



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/colossus/engines/memory/memory.rb', line 77

def gc_ttl
  user_ids_to_delete = []

  client_sessions.each_pair do |user_id, session_store|
    session_store.sessions.delete_if do |session_id, session|
      (session.last_seen + ttl) < Time.now
    end

    if (session_store.last_seen + ttl) < Time.now
      user_ids_to_delete << user_id
    end
  end

  delete_expired_users(user_ids_to_delete)
end

#get(user_id) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/colossus/engines/memory/memory.rb', line 42

def get(user_id)
  if client_sessions.has_key?(user_id)
    { user_id => client_sessions[user_id].status }
  else
    { user_id => DISCONNECTED }
  end
end

#get_allObject



54
55
56
57
58
59
60
# File 'lib/colossus/engines/memory/memory.rb', line 54

def get_all
  statuses = {}
  client_sessions.each_pair do |user_id, session_store|
    statuses[user_id] = session_store.status
  end
  statuses
end

#get_multi(*user_ids) ⇒ Object



50
51
52
# File 'lib/colossus/engines/memory/memory.rb', line 50

def get_multi(*user_ids)
  user_ids.inject({}) { |memo, user_id| memo.merge!(get(user_id)) }
end

#new_periodic_ttlObject



72
73
74
75
# File 'lib/colossus/engines/memory/memory.rb', line 72

def new_periodic_ttl
  secs_ttl = Colossus.config.seconds_before_ttl_check
  @periodic_ttl = EM::Synchrony.add_periodic_timer(secs_ttl, &method(:gc_ttl))
end

#reset!Object



66
67
68
69
70
# File 'lib/colossus/engines/memory/memory.rb', line 66

def reset!
  @client_sessions = Hash.new do |hash, key|
    hash[key] = Colossus::Engine::Memory::ClientSessionStore.new
  end
end

#set(user_id, client_id, given_status) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/colossus/engines/memory/memory.rb', line 24

def set(user_id, client_id, given_status)
  if given_status == DISCONNECTED
    client_sessions[user_id].delete(client_id)
  else
    client_sessions[user_id][client_id] = given_status
  end

  if client_sessions[user_id].sessions.empty? ||
      client_sessions[user_id].status_changed?
    status = client_sessions[user_id].status
    delete(user_id) if status == DISCONNECTED
    user_changed(user_id, status)
    return true
  end

  false
end

#user_changed(user_id, status) ⇒ Object



19
20
21
22
# File 'lib/colossus/engines/memory/memory.rb', line 19

def user_changed(user_id, status)
  changed
  notify_observers(user_id, status)
end