summaryrefslogtreecommitdiff
path: root/lib/drb/extservm.rb
diff options
context:
space:
mode:
Diffstat (limited to 'lib/drb/extservm.rb')
-rw-r--r--lib/drb/extservm.rb94
1 files changed, 94 insertions, 0 deletions
diff --git a/lib/drb/extservm.rb b/lib/drb/extservm.rb
new file mode 100644
index 00000000000..50009dbae2f
--- /dev/null
+++ b/lib/drb/extservm.rb
@@ -0,0 +1,94 @@
+=begin
+ external service manager
+ Copyright (c) 2000 Masatoshi SEKI
+=end
+
+require 'drb/drb'
+require 'thread'
+
+module DRb
+ class ExtServManager
+ include DRbUndumped
+
+ @@command = {}
+
+ def self.command
+ @@command
+ end
+
+ def self.command=(cmd)
+ @@command = cmd
+ end
+
+ def initialize
+ @servers = {}
+ @waiting = []
+ @queue = Queue.new
+ @thread = invoke_thread
+ end
+ attr_reader :server
+
+ def service(name)
+ while true
+ server = nil
+ Thread.exclusive do
+ server = @servers[name] if @servers[name]
+ end
+ return server if server && server.alive?
+ invoke_service(name)
+ end
+ end
+
+ def regist(name, ro)
+ ary = nil
+ Thread.exclusive do
+ @servers[name] = ro
+ ary = @waiting
+ @waiting = []
+ end
+ ary.each do |th|
+ begin
+ th.run
+ rescue ThreadError
+ end
+ end
+ self
+ end
+
+ def unregist(name)
+ Thread.exclusive do
+ @servers.delete(name)
+ end
+ end
+
+ private
+ def invoke_thread
+ Thread.new do
+ while true
+ name = @queue.pop
+ invoke_service_command(name, @@command[name])
+ end
+ end
+ end
+
+ def invoke_service(name)
+ Thread.critical = true
+ @waiting.push Thread.current
+ @queue.push name
+ Thread.stop
+ end
+
+ def invoke_service_command(name, command)
+ raise "invalid command. name: #{name}" unless command
+ Thread.exclusive do
+ return if @servers.include?(name)
+ @servers[name] = false
+ end
+ if RUBY_PLATFORM =~ /mswin32/
+ system("cmd /c start /b #{command} #{DRb.uri} #{name}")
+ else
+ system("#{command} #{DRb.uri} #{name} &")
+ end
+ end
+ end
+end