2004-02-13 21:28:02 -05:00
|
|
|
require 'webrick'
|
|
|
|
require 'drb/drb'
|
|
|
|
require 'drb/http0'
|
|
|
|
require 'thread'
|
|
|
|
|
|
|
|
module DRb
|
|
|
|
module HTTP0
|
2011-05-15 07:55:52 -04:00
|
|
|
|
2004-02-13 21:28:02 -05:00
|
|
|
def self.open_server(uri, config)
|
|
|
|
unless /^http:/ =~ uri
|
2013-07-20 09:41:02 -04:00
|
|
|
raise(DRbBadScheme, uri) unless uri =~ /^http:/
|
|
|
|
raise(DRbBadURI, 'can\'t parse uri:' + uri)
|
2004-02-13 21:28:02 -05:00
|
|
|
end
|
|
|
|
Server.new(uri, config)
|
|
|
|
end
|
|
|
|
|
|
|
|
class Callback < WEBrick::HTTPServlet::AbstractServlet
|
|
|
|
def initialize(config, drb)
|
2013-07-20 09:41:02 -04:00
|
|
|
@config = config
|
|
|
|
@drb = drb
|
|
|
|
@queue = Queue.new
|
2004-02-13 21:28:02 -05:00
|
|
|
end
|
2011-05-15 07:55:52 -04:00
|
|
|
|
2004-02-13 21:28:02 -05:00
|
|
|
def do_POST(req, res)
|
2013-07-20 09:41:02 -04:00
|
|
|
@req = req
|
|
|
|
@res = res
|
|
|
|
@drb.push(self)
|
|
|
|
@res.body = @queue.pop
|
|
|
|
@res['content-type'] = 'application/octet-stream;'
|
2004-02-13 21:28:02 -05:00
|
|
|
end
|
2011-05-15 07:55:52 -04:00
|
|
|
|
2004-02-13 21:28:02 -05:00
|
|
|
def req_body
|
2013-07-20 09:41:02 -04:00
|
|
|
@req.body
|
2004-02-13 21:28:02 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def reply(body)
|
2013-07-20 09:41:02 -04:00
|
|
|
@queue.push(body)
|
2004-02-13 21:28:02 -05:00
|
|
|
end
|
2011-05-15 07:55:52 -04:00
|
|
|
|
2004-02-13 21:28:02 -05:00
|
|
|
def close
|
2013-07-20 09:41:02 -04:00
|
|
|
@queue.push('')
|
2004-02-13 21:28:02 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class Server
|
|
|
|
def initialize(uri, config)
|
2013-07-20 09:41:02 -04:00
|
|
|
@uri = uri
|
|
|
|
@config = config
|
|
|
|
@queue = Queue.new
|
|
|
|
setup_webrick(uri)
|
2004-02-13 21:28:02 -05:00
|
|
|
end
|
|
|
|
attr_reader :uri
|
|
|
|
|
|
|
|
def close
|
2013-07-20 09:41:02 -04:00
|
|
|
@server.shutdown if @server
|
|
|
|
@server = nil
|
2004-02-13 21:28:02 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def push(callback)
|
2013-07-20 09:41:02 -04:00
|
|
|
@queue.push(callback)
|
2004-02-13 21:28:02 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def accept
|
2013-07-20 09:41:02 -04:00
|
|
|
client = @queue.pop
|
|
|
|
ServerSide.new(client, @config)
|
2004-02-13 21:28:02 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def setup_webrick(uri)
|
2013-07-20 09:41:02 -04:00
|
|
|
logger = WEBrick::Log::new($stderr, WEBrick::Log::FATAL)
|
|
|
|
u = URI.parse(uri)
|
|
|
|
s = WEBrick::HTTPServer.new(:Port => u.port,
|
|
|
|
:AddressFamily => Socket::AF_INET,
|
|
|
|
:BindAddress => u.host,
|
|
|
|
:Logger => logger,
|
|
|
|
:ServerType => Thread)
|
|
|
|
s.mount(u.path, Callback, self)
|
|
|
|
@server = s
|
|
|
|
s.start
|
2004-02-13 21:28:02 -05:00
|
|
|
end
|
|
|
|
end
|
2011-05-15 07:55:52 -04:00
|
|
|
|
2004-02-13 21:28:02 -05:00
|
|
|
class ServerSide
|
|
|
|
def initialize(callback, config)
|
2013-07-20 09:41:02 -04:00
|
|
|
@callback = callback
|
|
|
|
@config = config
|
|
|
|
@msg = DRbMessage.new(@config)
|
|
|
|
@req_stream = StrStream.new(@callback.req_body)
|
2004-02-13 21:28:02 -05:00
|
|
|
end
|
2011-05-15 07:55:52 -04:00
|
|
|
|
2004-02-13 21:28:02 -05:00
|
|
|
def close
|
2013-07-20 09:41:02 -04:00
|
|
|
@callback.close if @callback
|
|
|
|
@callback = nil
|
2004-02-13 21:28:02 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def alive?; false; end
|
|
|
|
|
|
|
|
def recv_request
|
2013-07-20 09:41:02 -04:00
|
|
|
begin
|
|
|
|
@msg.recv_request(@req_stream)
|
|
|
|
rescue
|
|
|
|
close
|
|
|
|
raise $!
|
|
|
|
end
|
2004-02-13 21:28:02 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def send_reply(succ, result)
|
2013-07-20 09:41:02 -04:00
|
|
|
begin
|
|
|
|
return unless @callback
|
|
|
|
stream = StrStream.new
|
|
|
|
@msg.send_reply(stream, succ, result)
|
|
|
|
@callback.reply(stream.buf)
|
|
|
|
rescue
|
|
|
|
close
|
|
|
|
raise $!
|
|
|
|
end
|
2004-02-13 21:28:02 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|