Method: Shell#initialize
- Defined in:
- lib/vendor/xmpp4r/data/doc/xmpp4r/examples/advanced/shellmgr/shellmgr.rb
#initialize(shell = "/bin/bash", delay = 1, &block) ⇒ Shell
Returns a new instance of Shell.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/vendor/xmpp4r/data/doc/xmpp4r/examples/advanced/shellmgr/shellmgr.rb', line 7 def initialize(shell = "/bin/bash", delay = 1, &block) @cb = block @shell = shell @delay = delay @parent_to_child_read, @parent_to_child_write = IO.pipe @child_to_parent_read, @child_to_parent_write = IO.pipe @child_pid = fork do @parent_to_child_write.close @child_to_parent_read.close $stdin.reopen(@parent_to_child_read) $stdout.reopen(@child_to_parent_write) $stderr.reopen(@child_to_parent_write) exec("/bin/bash") end buffer = "" semaphore = Mutex.new Thread.new do while true c = @child_to_parent_read.read(1) semaphore.synchronize { buffer += c } end end Thread.new do ch = "" while true do sleep @delay semaphore.synchronize { if buffer == ch and ch != "" @cb.call buffer buffer = "" end ch = buffer } end end end |