Class: TinyDTLS::SessionManager

Inherits:
Object
  • Object
show all
Defined in:
lib/tinydtls/session_manager.rb

Overview

This class is used to manage established tinydtls sessions. It stores instances of the TinyDTLS::Session class.

While memory allocated for sessions is automatically freed by tinydtls, if it receive an alert from the peer associated with that session, memory isn’t freed if the peer doesn’t send an alert. Therefore this class starts a background thread automatically freeing memory associated with sessions which haven’t been used since a specified duration.

Constant Summary collapse

DEFAULT_TIMEOUT =

Default timeout for the cleanup thread in seconds.

(5 * 60).freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ctx, timeout = DEFAULT_TIMEOUT) ⇒ SessionManager

Creates a new instance of this class. A tinydtls context_t pointer is required to free sessions in the background thread.



19
20
21
22
23
24
25
# File 'lib/tinydtls/session_manager.rb', line 19

def initialize(ctx, timeout = DEFAULT_TIMEOUT)
  @store = {}
  @mutex = Mutex.new
  @timeout = timeout

  start_thread(ctx)
end

Instance Attribute Details

#timeoutObject

Returns the value of attribute timeout.



15
16
17
# File 'lib/tinydtls/session_manager.rb', line 15

def timeout
  @timeout
end

Instance Method Details

#[](addrinfo, &f) ⇒ Object

Retrieve a session from the session manager.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/tinydtls/session_manager.rb', line 28

def [](addrinfo, &f)
  unless addrinfo.is_a? Addrinfo
    raise TypeError
  end

  key = addrinfo.getnameinfo
  if @store.has_key? key
    sess, _ = @store[key]
  else
    sess = Session.new(addrinfo)
    @store[key] = [sess, true]
  end

  @mutex.synchronize { f.call(sess) }
end

#destroy!Object

Frees all ressources associated with this class.



45
46
47
48
49
# File 'lib/tinydtls/session_manager.rb', line 45

def destroy!
  @mutex.lock
  @store.clear
  @thread.kill
end