Class: Emptyd::Session
- Inherits:
-
Object
- Object
- Emptyd::Session
- Defined in:
- lib/emptyd.rb
Constant Summary collapse
- @@sessions =
{}
Instance Attribute Summary collapse
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#queue ⇒ Object
readonly
Returns the value of attribute queue.
-
#uuid ⇒ Object
readonly
Returns the value of attribute uuid.
Class Method Summary collapse
Instance Method Summary collapse
- #<<(data) ⇒ Object
- #callback(h, e, c = nil) ⇒ Object
- #dead? ⇒ Boolean
- #destroy ⇒ Object
- #destroy! ⇒ Object
- #done? ⇒ Boolean
-
#initialize(options, &callback) ⇒ Session
constructor
A new instance of Session.
- #interactive? ⇒ Boolean
- #run(cmd) ⇒ Object
- #status ⇒ Object
- #terminate(key) ⇒ Object
- #write(key, data) ⇒ Object
Constructor Details
#initialize(options, &callback) ⇒ Session
Returns a new instance of Session.
291 292 293 294 295 296 297 298 299 300 301 302 303 304 |
# File 'lib/emptyd.rb', line 291 def initialize , &callback keys = [:keys] @interactive = !![:interactive] @logger = [:logger] @uuid = SecureRandom.uuid @@sessions[@uuid] = self @keys = keys @connections = Hash[keys.map{|h| [h, Connection[h, @logger]]}] @connections.each_value{|h| h.bind self} @queue = EM::Queue.new @running = {} @dead = false @terminated = {} end |
Instance Attribute Details
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
276 277 278 |
# File 'lib/emptyd.rb', line 276 def logger @logger end |
#queue ⇒ Object (readonly)
Returns the value of attribute queue.
276 277 278 |
# File 'lib/emptyd.rb', line 276 def queue @queue end |
#uuid ⇒ Object (readonly)
Returns the value of attribute uuid.
276 277 278 |
# File 'lib/emptyd.rb', line 276 def uuid @uuid end |
Class Method Details
.[](uuid) ⇒ Object
283 284 285 |
# File 'lib/emptyd.rb', line 283 def self.[](uuid) @@sessions[uuid] or raise KeyError, "no such session" end |
.ids ⇒ Object
279 280 281 |
# File 'lib/emptyd.rb', line 279 def self.ids @@sessions.keys end |
Instance Method Details
#<<(data) ⇒ Object
366 367 368 369 370 371 372 |
# File 'lib/emptyd.rb', line 366 def << data @running.each do |k,v| if v.respond_to? :send_data v.send_data data end end end |
#callback(h, e, c = nil) ⇒ Object
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 |
# File 'lib/emptyd.rb', line 386 def callback(h,e,c=nil) EM.next_tick do @logger.debug [h.key,e,c.nil?] case e when :init @queue.push [h.key,:start,nil] @running[h.key] = c when :close, :error h.unbind self unless @dead or not @connections.include? h.key @connections.delete h.key @queue.push [h.key,:dead,c] if e == :error @queue.push [h.key,:done,nil] @running.delete h.key if done? @queue.push [nil,:done,nil] @logger.debug "run is done." else @logger.debug "#{@running.size} connections pending" end else @logger.error "Session#run: unexpected callback #{e}" end end end |
#dead? ⇒ Boolean
330 331 332 |
# File 'lib/emptyd.rb', line 330 def dead? @dead end |
#destroy ⇒ Object
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 |
# File 'lib/emptyd.rb', line 306 def destroy @logger.debug "Destroying session #{@uuid}" @logger.debug @running.map{|h,v| [h, v.class.name]} @running.each do |h,v| if v.respond_to? :close @logger.debug "Closing channel for #{h}" v.close end end @connections.each_value do |h| callback h, :close, "user" h.unbind self end @dead = true end |
#destroy! ⇒ Object
322 323 324 |
# File 'lib/emptyd.rb', line 322 def destroy! @@sessions.delete @uuid end |
#done? ⇒ Boolean
326 327 328 |
# File 'lib/emptyd.rb', line 326 def done? @running.empty? end |
#interactive? ⇒ Boolean
287 288 289 |
# File 'lib/emptyd.rb', line 287 def interactive? @interactive end |
#run(cmd) ⇒ Object
334 335 336 337 338 339 340 341 342 343 |
# File 'lib/emptyd.rb', line 334 def run cmd dead = @connections.values.select(&:dead?) alive = @connections.values.reject(&:dead?) @queue.push [nil,:dead,dead.map(&:key)] @queue.push [nil,:done,nil] if alive.empty? alive.each { |h| @running[h.key] = true } alive.each do |h| h.run(cmd, self) { |h,e,c| callback h,e,c } end end |
#status ⇒ Object
345 346 347 348 349 350 351 352 353 354 355 356 |
# File 'lib/emptyd.rb', line 345 def status { :children => Hash[@keys.map{|k| [k, @terminated[k] ? :terminated : @running[k] == true ? :pending : @running[k] ? :running : @connections[k] ? @connections[k].dead? ? :dead : :unknown : :done]}], :dead => @dead } end |
#terminate(key) ⇒ Object
374 375 376 377 378 379 380 381 382 383 384 |
# File 'lib/emptyd.rb', line 374 def terminate key return if @terminated[key] chan = @running.delete key conn = @connections.delete key chan.close if chan.respond_to? :close if conn callback conn, :close, "user" conn.unbind self end @terminated[key] = true end |
#write(key, data) ⇒ Object
358 359 360 361 362 363 364 |
# File 'lib/emptyd.rb', line 358 def write key, data v = @running[key] raise KeyError unless @keys.include?(key) if v.respond_to? :send_data v.send_data data end end |