Created
August 5, 2012 20:15
-
-
Save Poincare/3266979 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'socket' | |
require 'celluloid' | |
class Query | |
attr_accessor :type, :url, :other | |
def initialize (query_string) | |
@type, @url, @other = query_string.split " " | |
end | |
end | |
class AnswerWorker | |
include Celluloid | |
def process_get | |
... | |
end | |
def start(client) | |
@client = client | |
client.puts | |
headers = "HTTP/1.1 200 OK\r\nDate: Tue, 14 Dec 2010 10:48:45 GMT\r\nServer: Ruby\r\nContent-Type: text/html; charset=iso-8859-1\r\n\r\n" | |
client.puts headers | |
loop { | |
query = Query.new client.readline | |
process_get if query.type == "GET" | |
} | |
end | |
end | |
class HTTPServer | |
include Celluloid | |
def initialize(port) | |
@port = port | |
end | |
def start | |
@server = TCPServer.new(@port) | |
client = nil | |
pool = AnswerWorker.pool(size: 50) | |
loop { | |
client = @server.accept | |
pool.start! client | |
} | |
end | |
end | |
hs = HTTPServer.new 1777 | |
hs.start |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment