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.



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

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:



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

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)


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

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

#getString

Allocate a new unique token

Returns:

  • (String)


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

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

      tokens[t] = true
      return t
    end
  end

  raise 'unable to generate a unique token'
end

#register(token) ⇒ Object

Register an existing token in use



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

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

    tokens[token] = true
  end
end

#syncObject (protected)



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

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