Report abuse

relauncher.rb (run this)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
require 'jruby/vm'
require 'socket'

# listen on 3000
server = TCPServer.new('localhost', 3000)

# prepare initial mtime and child vm
mtime = File.mtime('server.rb')
vm = JRuby::VM.spawn('server.rb')
vm << JRuby::VM_ID

while true
  # accept connections
  sock = server.accept

  # check mtime and reload VM if changed
  new_mtime = File.mtime('server.rb')
  if mtime != new_mtime
    mtime = new_mtime
    vm << 'quit'
    t = Time.now
    vm = JRuby::VM.spawn('server.rb')
    vm << JRuby::VM_ID
    puts "reloaded in #{Time.now - t} seconds"
  end

  # handle request in child vm
  request = sock.gets("\r\n\r\n")
  vm << request
  response = JRuby::VM.get_message
  sock.write(response)
  sock.close
end

server.rb (touch to cause reload on next request)

1
2
3
4
5
6
7
8
9
10
11
parent_id = JRuby::VM.get_message

header = "HTTP/1.1 200 OK\r\nDate: Mon, 01 Dec 2008 01:52:09 GMT\r\nContent-Type: text/html\r\nConnection: close\r\n"

while true
  request = JRuby::VM.get_message
  break if request == 'quit'
  # just dumping request back at the moment
  response = "#{header}Content-Length: #{11 + request.length}\r\n\r\n<pre>#{request}</pre>"
  JRuby::VM.send_message(parent_id, response)
end