module Angelo::Stash
utility class for stashing connected websockets in arbitrary contexts
Public Class Methods
create a new instance with a context, creating the array if needed
# File lib/angelo/stash.rb, line 29 def initialize server, context = :default raise ArgumentError.new "symbol required" unless Symbol === context @context, @server = context, server @mutex = Mutex.new stashes[@context] ||= [] end
Public Instance Methods
add a websocket to this context's stash, save peeraddr info, start server handle_websocket task to read from the socket and fire events as needed
# File lib/angelo/stash.rb, line 40 def << s peeraddrs[s] = s.peeraddr stashes[@context] << s end
utility method to create a new instance with a different context
# File lib/angelo/stash.rb, line 81 def [] context self.class.new @server, context end
iterate on every connected websocket in all contexts, mostly used for ping_websockets task
# File lib/angelo/stash.rb, line 88 def all_each all_stashed = @mutex.synchronize { stashes.values.flatten } all_stashed.each do |s| begin yield s rescue Reel::SocketError, IOError, SystemCallError => e debug "all - #{e.message}" remove_socket s stashes.values.each {|_s| _s.delete s} end end end
iterate on each connected websocket in this context, handling errors as needed
# File lib/angelo/stash.rb, line 54 def each &block stash_dup = @mutex.synchronize { stash.dup } stash_dup.each do |s| begin yield s rescue Reel::SocketError, IOError, SystemCallError => e debug "context: #{@context} - #{e.message}" remove_socket s end end nil end
return the number of websockets in this context (some are potentially disconnected)
# File lib/angelo/stash.rb, line 116 def length stash.length end
access the peeraddr info for a given websocket
# File lib/angelo/stash.rb, line 109 def peeraddr s peeraddrs[s] end
# File lib/angelo/stash.rb, line 25 def peeraddrs; self.class.peeraddrs; end
pass the given block to the underlying stashed array's reject! method
# File lib/angelo/stash.rb, line 103 def reject! &block stash.reject! &block end
remove a socket from the stash, warn user, drop peeraddr info
# File lib/angelo/stash.rb, line 69 def remove_socket s s.close unless s.closed? if stash.include? s warn "removing socket from context ':#{@context}' (#{peeraddrs[s][2]})" stash.delete s peeraddrs.delete s end end
access the underlying array of this context
# File lib/angelo/stash.rb, line 47 def stash stashes[@context] end
# File lib/angelo/stash.rb, line 24 def stashes; self.class.stashes; end