Skip to content

Instantly share code, notes, and snippets.

@seki
Last active August 16, 2017 01:53
Show Gist options
  • Save seki/5285335 to your computer and use it in GitHub Desktop.
Save seki/5285335 to your computer and use it in GitHub Desktop.
LindaのWeb版の一提案。
# -*- coding; utf-8 -*-
require 'rinda/tuplespace'
require 'drb/drb'
require 'webrick'
require 'webrick/cgi'
class RichRinda < WEBrick::CGI
def initialize(ts, *args)
super(*args)
@ts = ts
end
def do_GET(req, res)
do_request(req, res)
end
def req_to_tuple(req, as_pattern)
hash = Hash[req.query.collect {|k, v|[ 'x-' + k, v == '' ? nil : v]}]
hash['query'] = as_pattern ? nil : req.query_string
hash
end
def do_rinda(req)
case req.path_info
when '/write'
@ts.write(req_to_tuple(req, false), 24 * 60 * 60)
{}
when '/take'
@ts.take(req_to_tuple(req, true), 600) rescue {}
when '/read'
@ts.read(req_to_tuple(req, true), 600) rescue {}
else
raise "invalid operation #{req.path_info}"
end
end
def do_request(req, res)
tuple = do_rinda(req)
res['content-type'] = 'application/x-www-form-urlencoded'
res.body = tuple['query']
rescue
end
end
if __FILE__ == $0
ts = Rinda::TupleSpace.new(60)
cgi = RichRinda.new(ts)
unless $DEBUG
Process.daemon
end
DRb.start_service('druby://localhost:50902', cgi)
DRb.thread.join
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment