Class: JSCommander::Shell

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

Defined Under Namespace

Classes: ReadlineConsole, SimpleConsole

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(broker) ⇒ Shell

Returns a new instance of Shell.



79
80
81
82
83
84
85
86
87
# File 'lib/jscmd/shell.rb', line 79

def initialize(broker)
  @broker = broker
  @clients = nil
  @msg_lock = Mutex.new
  @wait_for_event = {}
  @interrupt_lock = Mutex.new
  @interrupt_ready_cv = ConditionVariable.new
  @interrupt_ready = false
end

Instance Attribute Details

#clientsObject (readonly)

Returns the value of attribute clients.



77
78
79
# File 'lib/jscmd/shell.rb', line 77

def clients
  @clients
end

Instance Method Details

#complete_property(word) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/jscmd/shell.rb', line 116

def complete_property(word)
  if word =~ /\.$/
    object_props($`).map{|name| word + name}
  elsif word =~ /\.([^.]+)$/
    prefix = $1
    parent = $`
    props = object_props(parent)
    props.select{|name|name[0...(prefix.size)] == prefix}.map{|name| "#{parent}.#{name}"}
  else
    props = object_props("this")
    prefix = word
    props.select{|name|name[0...(prefix.size)] == prefix}
  end
end

#consoleObject



72
73
74
75
# File 'lib/jscmd/shell.rb', line 72

def console
  @console ||= $stdin.tty? ? ReadlineConsole.new(self) : SimpleConsole.new
  # @console ||= SimpleConsole.new
end

#generate_idObject



89
90
91
# File 'lib/jscmd/shell.rb', line 89

def generate_id
  Array.new(16){rand(62)}.pack("C*").tr("\x00-\x3d", "A-Za-z0-9")
end

#object_props(object) ⇒ Object



106
107
108
109
110
111
112
113
114
# File 'lib/jscmd/shell.rb', line 106

def object_props(object)
  send_script(object, "properties") do |msg|
    if msg.attributes["type"] == "value" && msg.body
      msg.body.split(/,/)
    else
      []
    end
  end
end

#runObject



131
132
133
134
135
136
137
138
139
140
141
142
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/jscmd/shell.rb', line 131

def run
  console.show_banner
  
  main_thread = Thread.current
  
  @broker.subscribe("events") do |msg|
    id = msg.attributes["in-reply-to"]
    type = msg.attributes["type"]
    @clients = msg.attributes["clients"]
    if id
      @msg_lock.synchronize do
        handler = @wait_for_event.delete(id)
        if handler
          queue, proc = handler
          queue.push(proc.call(msg))
        end
      end
      msg = nil
    elsif "abort" == type
      @msg_lock.synchronize do
        handler =  @wait_for_event.values.first
        if handler
          queue, proc = handler
          queue.push(nil)
        end
      end
    end
    unless msg.nil?
      case type
      when "value", "error"
        puts msg.body
      when "abort"
        puts "aborted"
      when "connect"
        puts
      end
      @interrupt_lock.synchronize do
        @interrupt_ready_cv.wait(@interrupt_lock) until @interrupt_ready
        @interrupt_ready = false
      end
      if Process.methods.include?("kill")
        Process.kill "INT", $$  # interrupt readline
      else
        # JRuby
        main_thread.raise Interrupt
      end
    end
  end

  begin
    @interrupt_lock.synchronize do
      @interrupt_ready = true
      @interrupt_ready_cv.broadcast
    end
    loop do
      break unless line = console.readline
      if line.chomp == ''
        send_script(nil, "ping") {}
      else
        send_script(line, "eval") do |msg|
          puts msg.body
        end
      end
    end
    $stderr.puts "Exiting shell..."
  rescue Interrupt
    # $stderr.puts "int"
    retry
  rescue SystemExit
    return
  rescue Exception => e
    $stderr.puts "#{e.inspect} at:\n#{e.backtrace.join("\n")}"
  ensure
    begin
      console.close
    rescue Exception => e
      $stderr.puts "failed to close console: #{e.inspect}"
    end
  end
end

#send_script(line, type, &handler) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/jscmd/shell.rb', line 93

def send_script(line, type, &handler)
  queue = Queue.new
  @msg_lock.synchronize do
    id = generate_id
    @broker.send "commands", Message.new(line, 
                                         :type => type, 
                                         :id => id)
    # wait until it gets response
    @wait_for_event[id] = [queue, proc{|msg| yield msg}]
  end
  queue.pop
end