Class: NexusSW::LXD::Transport::Mixins::Rest::WSController
- Inherits:
-
Object
- Object
- NexusSW::LXD::Transport::Mixins::Rest::WSController
- Defined in:
- lib/nexussw/lxd/transport/mixins/rest.rb
Instance Attribute Summary collapse
-
#callback ⇒ Object
Returns the value of attribute callback.
-
#waitlist ⇒ Object
readonly
Returns the value of attribute waitlist.
Instance Method Summary collapse
- #alive? ⇒ Boolean
- #exit ⇒ Object
-
#initialize(ws_options, baseurl, endpoints, &block) ⇒ WSController
constructor
A new instance of WSController.
- #join ⇒ Object
- #signal(signum) ⇒ Object
- #window_resize(width, height) ⇒ Object
Constructor Details
#initialize(ws_options, baseurl, endpoints, &block) ⇒ WSController
Returns a new instance of WSController.
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/nexussw/lxd/transport/mixins/rest.rb', line 143 def initialize(, baseurl, endpoints, &block) @waitlist = {} @callback = block if block_given? waitlist[:control] = NIO::WebSocket.connect(baseurl + endpoints[:control], ) 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'], ) 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'], ) 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'], ) 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 |
Instance Attribute Details
#callback ⇒ Object
Returns the value of attribute callback.
184 185 186 |
# File 'lib/nexussw/lxd/transport/mixins/rest.rb', line 184 def callback @callback end |
#waitlist ⇒ Object (readonly)
Returns the value of attribute waitlist.
183 184 185 |
# File 'lib/nexussw/lxd/transport/mixins/rest.rb', line 183 def waitlist @waitlist end |
Instance Method Details
#alive? ⇒ Boolean
186 187 188 |
# File 'lib/nexussw/lxd/transport/mixins/rest.rb', line 186 def alive? !@closed end |
#exit ⇒ Object
190 191 192 193 194 |
# File 'lib/nexussw/lxd/transport/mixins/rest.rb', line 190 def exit waitlist.each do |_fd, driver| driver.close end end |
#join ⇒ Object
196 197 198 199 200 201 202 203 204 205 206 |
# File 'lib/nexussw/lxd/transport/mixins/rest.rb', line 196 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) ⇒ Object
212 213 214 |
# File 'lib/nexussw/lxd/transport/mixins/rest.rb', line 212 def signal(signum) send_control_msg "signal", signum end |
#window_resize(width, height) ⇒ Object
208 209 210 |
# File 'lib/nexussw/lxd/transport/mixins/rest.rb', line 208 def window_resize(width, height) send_control_msg "window-resize", width: width.to_s, height: height.to_s end |