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 ⇒ Object protected
Constructor Details
#initialize ⇒ Tokens
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
#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?
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
50 51 52 |
# File 'lib/osctld/send_receive/tokens.rb', line 50 def free(token) sync { tokens.delete(token) } end |
#get ⇒ String
Allocate a new unique token
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 |
#sync ⇒ Object (protected)
68 69 70 |
# File 'lib/osctld/send_receive/tokens.rb', line 68 def sync(&) mutex.synchronize(&) end |