* transcode.c (trans_open_i): check the result of rb_transcoding_open.
[ruby-svn.git] / lib / drb / extservm.rb
blobbe40aea9f5dc062f597a8b7b78ad7b08f2335fc2
1 =begin
2  external service manager
3         Copyright (c) 2000 Masatoshi SEKI 
4 =end
6 require 'drb/drb'
7 require 'thread'
8 require 'monitor'
10 module DRb
11   class ExtServManager
12     include DRbUndumped
13     include MonitorMixin
15     @@command = {}
17     def self.command
18       @@command
19     end
21     def self.command=(cmd)
22       @@command = cmd
23     end
24       
25     def initialize
26       super()
27       @cond = new_cond
28       @servers = {}
29       @waiting = []
30       @queue = Queue.new
31       @thread = invoke_thread
32       @uri = nil
33     end
34     attr_accessor :uri
36     def service(name)
37       synchronize do
38         while true
39           server = @servers[name]
40           return server if server && server.alive?
41           invoke_service(name)
42           @cond.wait
43         end
44       end
45     end
47     def regist(name, ro)
48       synchronize do
49         @servers[name] = ro
50         @cond.signal
51       end
52       self
53     end
54     
55     def unregist(name)
56       synchronize do
57         @servers.delete(name)
58       end
59     end
61     private
62     def invoke_thread
63       Thread.new do
64         while true
65           name = @queue.pop
66           invoke_service_command(name, @@command[name])
67         end
68       end
69     end
71     def invoke_service(name)
72       @queue.push(name)
73     end
75     def invoke_service_command(name, command)
76       raise "invalid command. name: #{name}" unless command
77       synchronize do
78         return if @servers.include?(name)
79         @servers[name] = false
80       end
81       uri = @uri || DRb.uri
82       if RUBY_PLATFORM =~ /mswin32/ && /NT/ =~ ENV["OS"]
83         system(%Q'cmd /c start "ruby" /b #{command} #{uri} #{name}')
84       else
85         system("#{command} #{uri} #{name} &")
86       end
87     end
88   end
89 end