#
# on windows signals don't work.  least wise you cannot send, for instance,
# SIGABRT to another process.  rather, you can only signal yourself!  to get
# around this limitation a tiny drb service, fronting the Process object
# itself, can be setup to allow one process to signal another by making a drb
# call that actually causes the other process to signal *itself*.  you've
# really got to love ruby at times like these.
#
  require 'drb'

  mode = ARGV.shift || 'servant'
  uri = ARGV.shift
  signal = 'ABRT'

  start_server = lambda do
    trap(signal){ puts "#{ Process.pid } got #{ signal }..." }
    DRb.start_service 'druby://localhost:0', Process
    uri = DRb.uri
  end

  start_client = lambda do
    DRb.start_service 'druby://localhost:0'
    process = DRbObject.new nil, uri
  end

  case mode
    when /server/i
      puts "server.pid => #{ Process.pid }."
      uri = start_server.call
      puts DRb.uri
      loop do
        puts "not blocking..."
        sleep 1
      end

    when /client/i
      puts "client.pid => #{ Process.pid }."
      process = start_client.call
      process.kill signal, process.pid

    when /servant/i
      puts "server.pid => #{ Process.pid }."
      uri = start_server.call
      Thread.new do
        loop do
          puts "not blocking..."
          sleep 1
        end
      end
      program = __FILE__
      client_program = "ruby #{ program } client #{ uri }"
      4.times do
        system client_program
        sleep 1
      end
  end