From: Garthy D Date: 2013-03-16T09:57:25+09:00 Subject: Re: IO buffering problem Hi Andrew, On 16/03/13 09:04, Andrew Wagner wrote: > All, > I'm writing a chess program that has a simple text-based interface. It's > designed to be run by another program, which has a nice UI. Here's the > program as it stands now, whittled to the simplest possible use case: > > i = 0 > moves = ["a7a6","a6a5","a5a4","a4a3"] > $stdin.each do |command| > if command.start_with?("protover") > $stdout.puts "feature ping=0 setboard=1 ics=1 usermove=1" > elsif command.start_with?("usermove") > $stdout.puts "move #{moves[i]}" > i+=1 > end > $stdout.flush > end > > The "protover" thing is needed to tell the UI program how to initialize > the protocol, and then every time a move is made, the UI program sends > "usermove " to my program, which responds with "move ". > > If I run this by itself, I can happily enter a few 'usermove' commands > manually, and get any number of canned moves back. However, when I run > this under the UI, I get an Interrupt: > > bin/winboard.rb:3:in `each': Interrupt > from bin/winboard.rb:3:in `
' > > I know that the UI program is pretty reliable, so I'm missing something > in the way that I'm handling IO buffering or something. If you want the > nitty gritty details, you can see them at > http://www.gnu.org/software/xboard/engine-intf.html . I'm running on > MacOSX. Any suggestions? Is it possible that you aren't processing one of the commands from the program, and the program is terminating your script, perhaps after a timeout or incorrect response? I'm wondering what would show up if you added this: else $stderr.print "Unknown command: '#{command}'\n" just before the second-last line. Are you handling every incoming command as it expects? Incidentally, you could always turn those if statements into case/when, something like: case command when /^protover/ ... when /^usermove/ ... else $stderr.print "Unknown command: '#{command}'\n" end This might make it easier if there are a lot of commands you need to respond to. Cheers, Garth