Skip to content

Commit 4bf5328

Browse files
committed
Work in progress support for gz-sim on Windows
Signed-off-by: Silvio Traversaro <silvio.traversaro@iit.it>
1 parent b97ccab commit 4bf5328

File tree

1 file changed

+60
-36
lines changed

1 file changed

+60
-36
lines changed

src/cmd/cmdsim.rb.in

+60-36
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,15 @@ COMMANDS = { 'sim' =>
198198
class Cmd
199199

200200
def killProcess(pid, name, timeout)
201-
Process.kill("-INT", pid)
201+
if RbConfig::CONFIG['host_os'] =~ /mswin|mingw|cygwin/
202+
# On Windows, the Ruby signal machinery does not properly work,
203+
# so let's just use system level commands, see
204+
# https://blog.simplificator.com/2016/01/18/how-to-kill-processes-on-windows-using-ruby/
205+
print("Calling Process.kill INT with pid #{pid}\n")
206+
system("taskkill /pid #{pid}")
207+
else
208+
Process.kill("-INT", pid)
209+
end
202210

203211
sleepSecs = 0.001
204212
iterations = (timeout / sleepSecs).to_i
@@ -220,7 +228,11 @@ class Cmd
220228

221229
if killedPid != pid
222230
puts "Escalating to SIGKILL on [#{name}]"
223-
Process.kill("-KILL", pid)
231+
if RbConfig::CONFIG['host_os'] =~ /mswin|mingw|cygwin/
232+
system("taskkill /f /pid #{pid}")
233+
else
234+
Process.kill("-KILL", pid)
235+
end
224236
end
225237
end
226238

@@ -380,6 +392,8 @@ class Cmd
380392
end # parse()
381393

382394
def execute(args)
395+
# The parse method modifies args
396+
original_args = args.clone()
383397
options = parse(args)
384398

385399
library_name_path = Pathname.new(LIBRARY_NAME)
@@ -527,41 +541,51 @@ See https://github.com/gazebosim/gz-sim/issues/44 for more info."
527541
exit(-1)
528542
end
529543

530-
if plugin.end_with? ".dll"
531-
puts "`ign gazebo` currently only works with the -s argument on Windows.
532-
See https://github.com/gazebosim/gz-sim/issues/168 for more info."
533-
exit(-1)
534-
end
535-
536-
serverPid = Process.fork do
537-
ENV['RMT_PORT'] = '1500'
538-
Process.setpgid(0, 0)
539-
Process.setproctitle('gz sim server')
540-
Importer.runServer(parsed,
541-
options['iterations'], options['run'], options['hz'],
542-
options['initial_sim_time'], options['levels'],
543-
options['network_role'], options['network_secondaries'],
544-
options['record'], options['record-path'],
545-
options['record-resources'], options['log-overwrite'],
546-
options['log-compress'], options['playback'],
547-
options['physics_engine'],
548-
options['render_engine_server'],
549-
options['render_engine_server_api_backend'],
550-
options['render_engine_gui'],
551-
options['render_engine_gui_api_backend'],
552-
options['file'], options['record-topics'].join(':'),
553-
options['wait_gui'],
554-
options['headless-rendering'], options['record-period'],
555-
options['seed'])
556-
end
544+
if not(plugin.end_with? ".dylib" or plugin.end_with? ".dll")
545+
# Not on Windows or macOS, Process.fork works fine
546+
serverPid = Process.fork do
547+
ENV['RMT_PORT'] = '1500'
548+
Process.setpgid(0, 0)
549+
Process.setproctitle('gz sim server')
550+
Importer.runServer(parsed,
551+
options['iterations'], options['run'], options['hz'],
552+
options['initial_sim_time'], options['levels'],
553+
options['network_role'], options['network_secondaries'],
554+
options['record'], options['record-path'],
555+
options['record-resources'], options['log-overwrite'],
556+
options['log-compress'], options['playback'],
557+
options['physics_engine'],
558+
options['render_engine_server'],
559+
options['render_engine_server_api_backend'],
560+
options['render_engine_gui'],
561+
options['render_engine_gui_api_backend'],
562+
options['file'], options['record-topics'].join(':'),
563+
options['wait_gui'],
564+
options['headless-rendering'], options['record-period'],
565+
options['seed'])
566+
end
557567

558-
guiPid = Process.fork do
559-
ENV['RMT_PORT'] = '1501'
560-
Process.setpgid(0, 0)
561-
Process.setproctitle('gz sim gui')
562-
Importer.runGui(options['gui_config'], options['file'],
563-
options['wait_gui'], options['render_engine_gui'],
564-
options['render_engine_gui_api_backend'])
568+
guiPid = Process.fork do
569+
ENV['RMT_PORT'] = '1501'
570+
Process.setpgid(0, 0)
571+
Process.setproctitle('gz sim gui')
572+
Importer.runGui(options['gui_config'], options['file'],
573+
options['wait_gui'], options['render_engine_gui'],
574+
options['render_engine_gui_api_backend'])
575+
end
576+
else
577+
server_args = original_args.clone()
578+
# Add -s after sim to spawn a server
579+
server_args.insert(1, "-s")
580+
# Prepend gz script location
581+
server_args.prepend($PROGRAM_NAME)
582+
serverPid = Process.spawn("ruby", *server_args)
583+
gui_args = original_args.clone()
584+
# Add -g after sim to spawn a server
585+
gui_args.insert(1, "-g")
586+
# Add gz script location
587+
gui_args.prepend($PROGRAM_NAME)
588+
guiPid = Process.spawn("ruby", *gui_args)
565589
end
566590

567591
Signal.trap("INT") {

0 commit comments

Comments
 (0)