Sha256: 09c17af4f6f08335d74e5343822c0f7eceac1fd5d98dddf909411dfe642963ba

Contents?: true

Size: 1.58 KB

Versions: 3

Compression:

Stored size: 1.58 KB

Contents

module Termvana
  class CommandProcessor < EventMachine::Connection
    include EventMachine::Deferrable
    attr_accessor :environment, :command
    # https://gist.github.com/535644
    #
    def self.register(command_klass)
      @command_list ||=[]
      @command_list << command_klass
    end

    def self.parse(environment, request)
      @command_list ||= []
      command_klass = @command_list.detect{|klass| klass.match?(request)}
      if command_klass
        command_klass.new(environment, request)
      else
        # Simple case - popen and set a timeout
        lambda do 
          process = EM.popen(environment.runnable(request), self, environment)
          process.callback do |data|
            environment.send_message Termvana::Response.new(:message => data)
          end
          process.errback do |data|
            environment.send_message Termvana::Response.new(:message => "Command timed out.", :type => :error)
          end
          process.timeout(10)
        end
      end
    end

    def initialize(environment)
      @environment = environment
      # @command = command
      environment.setup
    end

    # In the command processor, we are sending/recieving data over an
    # I/O pipe to a command
    def prepare_and_run
      # send_data "cd #{environment.cwd}\n"
      # send_data command.full_command
      # send_data "\n"
      # send_data "exit\n"
    end


    def receive_data data
      environment.send_message(Response.new(:message => data))
    end

    def unbind
      cancel_timeout
      # puts "exit status: #{get_status.exitstatus}"
    end
    
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
termvana-0.2.0.beta3 lib/termvana/app/command_processor.rb
termvana-0.2.0.beta2 lib/termvana/app/command_processor.rb
termvana-0.2.0.beta1 lib/termvana/app/command_processor.rb