Class: Emptyd::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/emptyd.rb

Constant Summary collapse

@@sessions =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options, &callback) ⇒ Session

Returns a new instance of Session.



250
251
252
253
254
255
256
257
258
259
260
261
262
263
# File 'lib/emptyd.rb', line 250

def initialize options, &callback
  keys = options[:keys]
  @interactive = !!options[:interactive]
  @logger = options[: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

#loggerObject (readonly)

Returns the value of attribute logger.



235
236
237
# File 'lib/emptyd.rb', line 235

def logger
  @logger
end

#queueObject (readonly)

Returns the value of attribute queue.



235
236
237
# File 'lib/emptyd.rb', line 235

def queue
  @queue
end

#uuidObject (readonly)

Returns the value of attribute uuid.



235
236
237
# File 'lib/emptyd.rb', line 235

def uuid
  @uuid
end

Class Method Details

.[](uuid) ⇒ Object



242
243
244
# File 'lib/emptyd.rb', line 242

def self.[](uuid)
  @@sessions[uuid] or raise KeyError, "no such session"
end

.idsObject



238
239
240
# File 'lib/emptyd.rb', line 238

def self.ids
  @@sessions.keys
end

Instance Method Details

#<<(data) ⇒ Object



316
317
318
319
320
321
322
# File 'lib/emptyd.rb', line 316

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



337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
# File 'lib/emptyd.rb', line 337

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

Returns:

  • (Boolean)


289
290
291
# File 'lib/emptyd.rb', line 289

def dead?
  @dead
end

#destroyObject



265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# File 'lib/emptyd.rb', line 265

def destroy
  @logger.debug "Destroying session #{@uuid}"
  p @running.map{|h,v| [h, v.class.name]}
  @running.each do |h,v| 
    if v.respond_to? :close
      p "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



281
282
283
# File 'lib/emptyd.rb', line 281

def destroy!
  @@sessions.delete @uuid
end

#done?Boolean

Returns:

  • (Boolean)


285
286
287
# File 'lib/emptyd.rb', line 285

def done?
  @running.empty?
end

#interactive?Boolean

Returns:

  • (Boolean)


246
247
248
# File 'lib/emptyd.rb', line 246

def interactive?
  @interactive
end

#run(cmd) ⇒ Object



293
294
295
296
297
298
299
300
301
# File 'lib/emptyd.rb', line 293

def run cmd
  dead = @connections.values.select(&:dead?)
  alive = @connections.values.reject(&:dead?)
  @queue.push [nil,:dead,dead.map(&:key)]
  alive.each { |h| @running[h.key] = true }
  alive.each do |h|
    h.run(cmd, self) { |h,e,c| callback h,e,c }
  end
end

#statusObject



303
304
305
306
307
308
309
310
311
312
313
314
# File 'lib/emptyd.rb', line 303

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



324
325
326
327
328
329
330
331
332
333
334
335
# File 'lib/emptyd.rb', line 324

def terminate key
  chan = @running[key]
  conn = @connections[key]
  chan.close if chan.respond_to? :close
  if conn
    callback conn, :close, "user"
    conn.unbind self 
  end
  @running.delete key
  @connections.delete key
  @terminated[key] = true
end