Class: OsCtld::SendReceive::Tokens

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/osctld/send_receive/tokens.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTokens

Returns a new instance of Tokens.



17
18
19
20
# File 'lib/osctld/send_receive/tokens.rb', line 17

def initialize
  @mutex = Mutex.new
  @tokens = {}
end

Instance Attribute Details

#mutexObject (readonly, protected)

Returns the value of attribute mutex.



66
67
68
# File 'lib/osctld/send_receive/tokens.rb', line 66

def mutex
  @mutex
end

#tokensObject (readonly, protected)

Returns the value of attribute tokens.



66
67
68
# File 'lib/osctld/send_receive/tokens.rb', line 66

def tokens
  @tokens
end

Instance Method Details

#find_container(token) ⇒ Container?

Parameters:

  • token (String)

Returns:



57
58
59
60
61
62
63
# File 'lib/osctld/send_receive/tokens.rb', line 57

def find_container(token)
  return unless tokens.has_key?(token)

  DB::Containers.get.detect do |ct|
    ct.send_log && ct.send_log.token == token
  end
end

#free(token) ⇒ Object

Free allocated token

Parameters:

  • token (String)


51
52
53
# File 'lib/osctld/send_receive/tokens.rb', line 51

def free(token)
  sync { tokens.delete(token) }
end

#getString

Allocate a new unique token

Returns:

  • (String)


24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/osctld/send_receive/tokens.rb', line 24

def get
  sync do
    10.times do
      t = SecureRandom.hex(20)
      next if tokens.has_key?(t)

      tokens[t] = true
      return t
    end
  end

  fail 'unable to generate a unique token'
end

#register(token) ⇒ Object

Register an existing token in use



39
40
41
42
43
44
45
46
47
# File 'lib/osctld/send_receive/tokens.rb', line 39

def register(token)
  sync do
    if tokens.has_key?(token)
      fail "token #{token} already in use"
    end

    tokens[token] = true
  end
end

#sync(&block) ⇒ Object (protected)



68
69
70
# File 'lib/osctld/send_receive/tokens.rb', line 68

def sync(&block)
  mutex.synchronize(&block)
end