Class: Amber::Server
- Inherits:
-
Object
- Object
- Amber::Server
- Defined in:
- lib/amber/server.rb
Instance Attribute Summary collapse
-
#base ⇒ Object
Returns the value of attribute base.
-
#host ⇒ Object
Returns the value of attribute host.
-
#port ⇒ Object
Returns the value of attribute port.
Instance Method Summary collapse
- #communicate(socket) ⇒ Object
-
#initialize(port = 3001, host = '0.0.0.0') ⇒ Server
constructor
A new instance of Server.
- #prepare ⇒ Object
- #start ⇒ Object
- #transaction(socket) ⇒ Object
Constructor Details
#initialize(port = 3001, host = '0.0.0.0') ⇒ Server
Returns a new instance of Server.
4 5 6 7 8 9 10 |
# File 'lib/amber/server.rb', line 4 def initialize(port = 3001, host = '0.0.0.0') @port = port @host = host @server_socket @server_address @fd_set = [] end |
Instance Attribute Details
#base ⇒ Object
Returns the value of attribute base.
2 3 4 |
# File 'lib/amber/server.rb', line 2 def base @base end |
#host ⇒ Object
Returns the value of attribute host.
2 3 4 |
# File 'lib/amber/server.rb', line 2 def host @host end |
#port ⇒ Object
Returns the value of attribute port.
2 3 4 |
# File 'lib/amber/server.rb', line 2 def port @port end |
Instance Method Details
#communicate(socket) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/amber/server.rb', line 36 def communicate(socket) if socket == @server_socket begin client_socket, client_address = @server_socket.accept_nonblock @fd_set << client_socket rescue IO::WaitReadable, Errno::EINTR retry end else @fd_set.delete(socket) task = Fiber.new { self.transaction(socket) } task.resume end end |
#prepare ⇒ Object
12 13 14 15 16 17 18 19 20 21 |
# File 'lib/amber/server.rb', line 12 def prepare @server_socket = Socket.new(Socket::AF_INET, Socket::SOCK_STREAM, 0) @server_address = Socket.sockaddr_in(@port, @host) @server_socket.setsockopt(Socket::SOL_SOCKET, Socket::SO_REUSEADDR, 1) @server_socket.bind(@server_address) @server_socket.listen(5) @fd_set << @server_socket end |
#start ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/amber/server.rb', line 23 def start loop { fd_items = IO.select(@fd_set, [], [], 5) if fd_items.is_a?(Array) readable_items = fd_items.first readable_items.each do |socket| self.communicate(socket) end end } end |
#transaction(socket) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/amber/server.rb', line 54 def transaction(socket) http_request = Amber::Http::Request.new(socket) http_response = Amber::Http::Response.new(socket) if http_request.receive time = Time.new time_string = time.strftime("%Y-%m-%d %H:%M:%S") STDOUT.puts "#{time_string} #{http_request.method} #{http_request.url}" begin if (path = @base.route.find(http_request.url)) && path.can_call? if path.method == http_request.method result = path.callback.call http_request case result when Amber::View http_response.status = result.code.to_s http_response.header = result.header http_response.content_type = result.content.type http_response.body = result.content.body when String http_response.status = "200" http_response.body = result else http_response.status = "500" http_response.body = "<html><h1>Server Internal Error</h1></html>" end else http_response.status = "301" http_response.body = "<html><h1>Moved Permanently</h1></html>" end else http_response.status = "404" http_response.body = "<html><h1>Not Found</h1></html>" end rescue Exception => e STDOUT.puts e end else http_response.status = "400" http_response.body = "<html><h1>Bad Request</h1></html>" end http_response.send socket.close end |