Sha256: f409434ea2a27d602e71909570b52af13bd954a550567023f47a1ac56e1a17c7

Contents?: true

Size: 958 Bytes

Versions: 1

Compression:

Stored size: 958 Bytes

Contents

module Terminus
  class Server
    
    def initialize(options = {})
      @options = options
    end
    
    def execute(script, &callback)
      messenger.publish('/terminus/commands', :command => script)
    end
    
    def running?
      not @server.nil?
    end
    
    def run!
      return if running?
      handler = Rack::Handler.get('thin')
      ensure_reactor_running!
      handler.run(app, :Port => @options[:port]) { |s| @server = s }
    end
    
    def stop!
      return unless running?
      @server.stop!
      @server = nil
    end
    
  private
    
    def app
      return @app if defined?(@app)
      frontend = Application.new
      @app = Faye::RackAdapter.new(frontend, :mount => FAYE_MOUNT, :timeout => 15)
    end
    
    def messenger
      app.get_client
    end
    
    def ensure_reactor_running!
      Thread.new { EM.run unless EM.reactor_running? }
      while not EM.reactor_running?; end
    end
    
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
terminus-0.1.0 lib/terminus/server.rb