Sha256: 229ed7404d9a3a0aca662b4ec4115a126dbe47aef4b6ca05be39f91eddb767bb

Contents?: true

Size: 1.18 KB

Versions: 5

Compression:

Stored size: 1.18 KB

Contents

class Knjappserver
  def initialize_cmdline
    @cmds = {}
    
    Thread.new do
      begin
        $stdin.each_line do |line|
          called = 0
          @cmds.each do |key, connects|
            data = {}
            
            if key.is_a?(Regexp)
              if line.match(key)
                connects.each do |conn|
                  called += 1
                  conn[:block].call(data)
                end
              end
            else
              raise "Unknown class for 'cmd_connect': '#{key.class.name}'."
            end
          end
          
          if called == 0
            print "Unknown command: '#{line.strip}'.\n"
          end
        end
      rescue => e
        self.handle_error(e)
      end
    end
    
    self.cmd_connect(/^\s*restart\s*$/i) do |data|
      print "Restart will begin shortly.\n"
      self.should_restart = true
    end
    
    self.cmd_connect(/^\s*stop\s*$/i) do |data|
      print "Stopping appserver.\n"
      self.stop
    end
  end
  
  #Connects a proc to a specific command in the command-line (key should be a regex).
  def cmd_connect(cmd, &block)
    @cmds[cmd] = [] if !@cmds.key?(cmd)
    @cmds[cmd] << {:block => block}
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
knjappserver-0.0.29 lib/include/class_knjappserver_cmdline.rb
knjappserver-0.0.28 lib/include/class_knjappserver_cmdline.rb
knjappserver-0.0.26 lib/include/class_knjappserver_cmdline.rb
knjappserver-0.0.25 lib/include/class_knjappserver_cmdline.rb
knjappserver-0.0.24 lib/include/class_knjappserver_cmdline.rb