class NexusSW::LXD::Transport::Mixins::Rest::WSController

Attributes

callback[RW]
waitlist[R]

Public Class Methods

new(ws_options, baseurl, endpoints, &block) click to toggle source
# File lib/nexussw/lxd/transport/mixins/rest.rb, line 145
def initialize(ws_options, baseurl, endpoints, &block)
  @waitlist = {}
  @callback = block if block_given?
  waitlist[:control] = NIO::WebSocket.connect(baseurl + endpoints[:control], ws_options) do |driver|
    driver.on :io_error do # usually I get an EOF
      @closed = true
      # waitlist.each { |_, v| v.close if v.respond_to? :close }
      waitlist[:'0'].close
    end
    driver.on :close do # but on occasion I get a legit close
      @closed = true
      # waitlist.each { |_, v| v.close if v.respond_to? :close }
      waitlist[:'0'].close
    end
  end
  if endpoints[:'2']
    waitlist[:'2'] = NIO::WebSocket.connect(baseurl + endpoints[:'2'], ws_options) do |driver|
      driver.on :message do |ev|
        data = ev.data.is_a?(String) ? ev.data : ev.data.pack("U*")
        callback.call nil, data
      end
    end
  end
  if endpoints[:'1']
    waitlist[:'1'] = NIO::WebSocket.connect(baseurl + endpoints[:'1'], ws_options) do |driver|
      driver.on :message do |ev|
        data = ev.data.is_a?(String) ? ev.data : ev.data.pack("U*")
        callback.call data
      end
    end
  end
  waitlist[:'0'] = NIO::WebSocket.connect(baseurl + endpoints[:'0'], ws_options) do |driver|
    driver.on :message do |ev|
      data = ev.data.is_a?(String) ? ev.data : ev.data.pack("U*")
      callback.call data
    end
  end
  @closed = false
end

Public Instance Methods

alive?() click to toggle source
# File lib/nexussw/lxd/transport/mixins/rest.rb, line 188
def alive?
  !@closed
end
exit() click to toggle source
# File lib/nexussw/lxd/transport/mixins/rest.rb, line 192
def exit
  waitlist.each do |_fd, driver|
    driver.close
  end
end
join() click to toggle source
# File lib/nexussw/lxd/transport/mixins/rest.rb, line 198
def join
  loop do
    allclosed = true
    waitlist.each do |_fd, driver|
      allclosed = false unless driver.state == :closed
    end
    break if allclosed
    Thread.pass
    sleep 0.1
  end
end
signal(signum) click to toggle source
# File lib/nexussw/lxd/transport/mixins/rest.rb, line 214
def signal(signum)
  send_control_msg "signal", signum
end
window_resize(width, height) click to toggle source
# File lib/nexussw/lxd/transport/mixins/rest.rb, line 210
def window_resize(width, height)
  send_control_msg "window-resize", width: width.to_s, height: height.to_s
end

Private Instance Methods

send_control_msg(message, val) click to toggle source
# File lib/nexussw/lxd/transport/mixins/rest.rb, line 220
def send_control_msg(message, val)
  msg = {}.tap do |retval|
    retval["command"] = message
    case message
    when "window-resize" then retval["args"] = val
    when "signal" then retval["signal"] = val.to_i
    end
  end.to_json

  waitlist[:control].binary msg
end