Class: OsCtld::SendReceive::Tokens
- Inherits:
-
Object
- Object
- OsCtld::SendReceive::Tokens
- Includes:
- Singleton
- Defined in:
- lib/osctld/send_receive/tokens.rb
Instance Attribute Summary collapse
-
#mutex ⇒ Object
readonly
protected
Returns the value of attribute mutex.
-
#tokens ⇒ Object
readonly
protected
Returns the value of attribute tokens.
Instance Method Summary collapse
- #find_container(token) ⇒ Container?
-
#free(token) ⇒ Object
Free allocated token.
-
#get ⇒ String
Allocate a new unique token.
-
#initialize ⇒ Tokens
constructor
A new instance of Tokens.
-
#register(token) ⇒ Object
Register an existing token in use.
- #sync(&block) ⇒ Object protected
Constructor Details
#initialize ⇒ Tokens
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
#mutex ⇒ Object (readonly, protected)
Returns the value of attribute mutex.
66 67 68 |
# File 'lib/osctld/send_receive/tokens.rb', line 66 def mutex @mutex end |
#tokens ⇒ Object (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?
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
51 52 53 |
# File 'lib/osctld/send_receive/tokens.rb', line 51 def free(token) sync { tokens.delete(token) } end |
#get ⇒ String
Allocate a new unique token
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 |