Cool!
Will this deny clients who try to connect and don't have sufficient privileges?
> # TODO: Wait for mutex or find a better way to wait for the process to be ready.
> sleep(0.5)
I wonder if a better way to wait was found. Half a second delay is
way to slow. The xiki command using unix pipes completes in under .03
seconds.
Have you experimented with having smalle sleep times? I'm guessing it
might start hogging the cpu if you make the sleep time down in that
range?
Anyone know of a Windows equivalent to the Daemons gem? It lets you
start, stop, restart and get the status of a process.
--Craig
On Wed, Mar 13, 2013 at 4:55 AM, Werner Beroux <
notifi...@github.com> wrote:
>
> Code:
>
> # gem install win32-pipe
> # gem install win32-mutex
> require 'win32/mutex'
> require 'rbconfig'
> require 'win32/pipe'
> include Win32
>
> $stdout.sync = true
>
> THIS_FILE = File.expand_path(__FILE__)
> RUBY = File.join(Config::CONFIG['bindir'], Config::CONFIG['ruby_install_name'])
>
> def child_main(source)
> puts "[child] Started \"#{source}\""
> # Single instance (could also use a temporary file but this is a better way on Windows)
> Win32::Mutex.new(false, 'some_unique_id') do |mutex|
>
> while true do
> pipe_server = Pipe::Server.new("foo_pipe")
> pipe_server.connect
> data = pipe_server.read
> puts "[child] Got #{data} from client"
> pipe_server.close
> end
>
> mutex.release
> end
> puts "[child] Terminated"
> end
>
> if $PROGRAM_NAME == __FILE__
> cmd = %Q<#{RUBY} -r#{THIS_FILE} -e 'child_main("ip")'>
> begin
> Win32::Mutex.open('some_unique_id') do |mutex|
> mutex.close
> end
> puts "[parent] Process already running"
> rescue #Win32::Mutex::Error => e
> puts "[parent] Starting process"
> Process.spawn(cmd);
> end
>
> # TODO: Wait for mutex or find a better way to wait for the process to be ready.
> sleep(0.5)
>
> pipe_client = Pipe::Client.new("foo_pipe")
> pipe_client.write("Hello World")
> pipe_client.close
> end
>
> Execution:
>
> $ ruby test.rb
> [parent] Starting process
> [child] Started "ip"
> [child] Got ["Hello World"] from client
>
> $ ruby test.rb
> [parent] Process already running
> [child] Got ["Hello World"] from client