Class: Yarn::Server
Instance Attribute Summary collapse
-
#host ⇒ Object
Returns the value of attribute host.
-
#port ⇒ Object
Returns the value of attribute port.
-
#socket ⇒ Object
Returns the value of attribute socket.
-
#socket_listener ⇒ Object
Returns the value of attribute socket_listener.
Instance Method Summary collapse
-
#initialize(app = nil, opts = {}) ⇒ Server
constructor
A new instance of Server.
- #start ⇒ Object
- #stop ⇒ Object
Methods included from Logging
#debug, #log, #output, #timestamp
Constructor Details
#initialize(app = nil, opts = {}) ⇒ Server
Returns a new instance of Server.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/yarn/server.rb', line 10 def initialize(app=nil,opts={}) # merge given options with default values = { output: $stdout, host: '127.0.0.1', port: 3000 }.merge(opts) @app = app @host,@port,$output = [:host], [:port], [:output] @socket = TCPServer.new(@host, @port) @handler = @app ? RackHandler.new(@app, ) : RequestHandler.new() log "Yarn started #{"w/ Rack " if opts[:rackup_file]}and accepting requests on #{@host}:#{@port}" end |
Instance Attribute Details
#host ⇒ Object
Returns the value of attribute host.
8 9 10 |
# File 'lib/yarn/server.rb', line 8 def host @host end |
#port ⇒ Object
Returns the value of attribute port.
8 9 10 |
# File 'lib/yarn/server.rb', line 8 def port @port end |
#socket ⇒ Object
Returns the value of attribute socket.
8 9 10 |
# File 'lib/yarn/server.rb', line 8 def socket @socket end |
#socket_listener ⇒ Object
Returns the value of attribute socket_listener.
8 9 10 |
# File 'lib/yarn/server.rb', line 8 def socket_listener @socket_listener end |
Instance Method Details
#start ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/yarn/server.rb', line 28 def start @socket_listener = Thread.new do loop do begin session = @socket.accept Thread.new { @handler.clone.run session } rescue Exception => e session.close log e. log e.backtrace end end end begin @socket_listener.join rescue Interrupt => e log "Caught interrupt, stopping..." ensure stop end end |
#stop ⇒ Object
51 52 53 54 55 56 57 |
# File 'lib/yarn/server.rb', line 51 def stop @socket.close if @socket @socket = nil @socket_listener.kill if @socket_listener log "Server stopped" end |