Class: HTTPServer + + + +
+-
+
- Inherits: +
-
+ Object
+
+
-
+
- Object + +
- HTTPServer + +
+
-
+
- Defined in: +
- lib/tcp_server.rb +
Overview
Main class for managing and talking between sub-classes during request-response cycles. Utalizes the request, route and response classes.
+ + ++ Instance Method Summary + collapse +
+ +-
+
+
-
+
+
+ #initialize(port, router) ⇒ HTTPServer
+
+
+
+
+
+
+ constructor
+
+
+
+
+
+
+
+
+ ++ +
Initializes the HTTPServer object.
+
+
+
+ -
+
+
+ #start ⇒ Object
+
+
+
+
+
+
+
+
+
+
+
+
+
+ ++ +
Starts the TCP server and begins reading for requests on the designated port.
+
+
+
+
Constructor Details
+ ++ + #initialize(port, router) ⇒ HTTPServer + + + + + +
Initializes the HTTPServer object
+ + +
+ + + +13 +14 +15 +16+ |
+
+ # File 'lib/tcp_server.rb', line 13 + +def initialize(port, router) + @port = port + @router = router +end+ |
+
Instance Method Details
+ + ++ + #start ⇒ Object + + + + + +
Starts the TCP server and begins reading for requests on the designated port. When a request comes in it turns it into a request object using the request class, and sends that to the router. It then sends the matched information to the response class.
+ + +
+ + + +19 +20 +21 +22 +23 +24 +25 +26 +27 +28 +29 +30 +31 +32 +33 +34 +35 +36 +37 +38 +39 +40 +41+ |
+
+ # File 'lib/tcp_server.rb', line 19 + +def start + server = TCPServer.new(@port) + puts "Listening on #{@port}" + + while session = server.accept + data = "" + while line = session.gets and line !~ /^\s*$/ + data += line + end + puts "RECEIVED REQUEST" + puts "-" * 40 + puts data + puts "-" * 40 + + request = Request.new(data) + pp request + + routeReturn = @router.match_route(request) + + response = Response.new(routeReturn) + response.print(session) + end +end+ |
+