module Angelo::Stash

utility class for stashing connected websockets in arbitrary contexts

Public Class Methods

new(server, context = :default) click to toggle source

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

<<(s) click to toggle source

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
[](context) click to toggle source

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
all_each() { |s| ... } click to toggle source

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
each() { |s| ... } click to toggle source

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
length() click to toggle source

return the number of websockets in this context (some are potentially disconnected)

# File lib/angelo/stash.rb, line 116
def length
  stash.length
end
peeraddr(s) click to toggle source

access the peeraddr info for a given websocket

# File lib/angelo/stash.rb, line 109
def peeraddr s
  peeraddrs[s]
end
peeraddrs() click to toggle source
# File lib/angelo/stash.rb, line 25
def peeraddrs; self.class.peeraddrs; end
reject!(&block) click to toggle source

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_socket(s) click to toggle source

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
stash() click to toggle source

access the underlying array of this context

# File lib/angelo/stash.rb, line 47
def stash
  stashes[@context]
end
stashes() click to toggle source
# File lib/angelo/stash.rb, line 24
def stashes; self.class.stashes; end