1 # A simple IPC server executing Ruby programs.
10 $stdout = StringIO.new
11 $stderr = StringIO.new
26 d = "dispatch_#{c.downcase}"
27 if respond_to?(d, true)
32 @out.puts("ERR 103 Unknown command\r\n")
37 def dispatch_cancel(c, r)
38 @out.puts("ERR 100 Not implemented\r\n")
41 def dispatch_bye(c, r)
42 @out.puts("ERR 100 Not implemented\r\n")
45 def dispatch_auth(c, r)
46 @out.puts("ERR 100 Not implemented\r\n")
49 def dispatch_reset(c, r)
50 @out.puts("ERR 100 Not implemented\r\n")
53 def dispatch_end(c, r)
57 def dispatch_help(c, r)
58 @out.puts("ERR 100 Not implemented\r\n")
61 def dispatch_quit(c, r)
62 @out.puts("ERR 100 Not implemented\r\n")
65 def dispatch_eval(c, r)
66 r = deq_data if r.empty?
69 while @thr.include?(name = @cnt.to_s)
72 @thr[name] = Thread.current
74 @out.puts("S name #{name}\r\n")
76 Thread.current[:rubyserv_name] = name
83 @out.puts("# output #{Thread.current[:rubyserv_name]} #{s}\r\n")
85 module_function :output
88 Thread.current[:rubyserv_error] = false
89 Thread.current[:rubyserv_response] = eval(r, env.module_eval{binding()})
91 Thread.current[:rubyserv_error] = true
92 Thread.current[:rubyserv_response] = e.to_s.sub(/\A.*?\n/, '')
94 @out.puts("# exit #{name}\r\n")
97 def dispatch_poll(c, r)
100 @out.puts("ERR 105 Parameter error: no such name \"#{r}\"\r\n")
102 @out.puts("S running #{r}\r\n")
105 if thr[:rubyserv_error]
106 @out.puts("S exited #{r}\r\n")
108 @out.puts("S finished #{r}\r\n")
110 if d = thr[:rubyserv_response]
117 def dispatch_exit(c, r)
120 @out.puts("ERR 105 Parameter error: no such name \"#{r}\"\r\n")
123 thr.kill if thr.alive?
129 s.gsub(/[%\r\n]/) {|m| '%%%02X' % m[0]}
133 s.gsub(/%([0-9A-Z][0-9A-Z])/) {[$1].pack('H*')}
139 len = [d.length, 998].min # 998 = 1000 - "D "
140 @out.puts("D #{d[0 ... len]}\r\n")