Class: Shell

Inherits:
Object show all
Defined in:
lib/gems/xmpp4r-0.4/data/doc/xmpp4r/examples/advanced/shellmgr/shellmgr.rb

Constant Summary collapse

PROMPT =
"_-=READY=-_"

Instance Method Summary collapse

Constructor Details

#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/gems/xmpp4r-0.4/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

Instance Method Details

#killObject



48
49
50
# File 'lib/gems/xmpp4r-0.4/data/doc/xmpp4r/examples/advanced/shellmgr/shellmgr.rb', line 48

def kill
  @child_pid.kill
end

#puts(str) ⇒ Object



44
45
46
# File 'lib/gems/xmpp4r-0.4/data/doc/xmpp4r/examples/advanced/shellmgr/shellmgr.rb', line 44

def puts(str)
  @parent_to_child_write.puts(str)
end