Class: NexusSW::LXD::Transport::Mixins::Rest::WSController

Inherits:
Object
  • Object
show all
Defined in:
lib/nexussw/lxd/transport/mixins/rest.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ws_options, baseurl, endpoints, &block) ⇒ WSController

Returns a new instance of WSController.



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/nexussw/lxd/transport/mixins/rest.rb', line 134

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
      waitlist.each { |_, v| v.close if v.respond_to? :close }
    end
    driver.on :close do # but on occasion I get a legit close
      waitlist.each { |_, v| v.close if v.respond_to? :close }
    end
  end
  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 if endpoints[:'2']
  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 if endpoints[:'1']
  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
end

Instance Attribute Details

#callbackObject

Returns the value of attribute callback.



166
167
168
# File 'lib/nexussw/lxd/transport/mixins/rest.rb', line 166

def callback
  @callback
end

#waitlistObject (readonly)

Returns the value of attribute waitlist.



165
166
167
# File 'lib/nexussw/lxd/transport/mixins/rest.rb', line 165

def waitlist
  @waitlist
end

Instance Method Details

#exitObject



168
169
170
171
172
# File 'lib/nexussw/lxd/transport/mixins/rest.rb', line 168

def exit
  waitlist.each do |_fd, driver|
    driver.close
  end
end

#joinObject



174
175
176
177
178
179
180
181
182
183
184
# File 'lib/nexussw/lxd/transport/mixins/rest.rb', line 174

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