Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<h3>EDIT</h3> <p>Your should consider <strong>integrating with <a href="http://anyterm.org/howitworks.html" rel="nofollow noreferrer">AnyTerm</a></strong>. You can then either expose AnyTerm directly e.g. via Apache <code>mod_proxy</code>, or have your Rails controller act as the reverse proxy (handling authentication/session validation, then playing back <code>controller.request</code> minus any cookies to <code>localhost:&lt;AnyTerm-daemon-port&gt;</code>, and sending back as a response whatever AnyTerm replies with.)</p> <pre><code>class ConsoleController &lt; ApplicationController # AnyTerm speaks via HTTP POST only def update # validate session ... # forward request to AnyTerm response = Net::HTTP.post_form(URI.parse('http://localhost:#{AnyTermPort}/', request.params)) headers['Content-Type'] = response['Content-Type'] render_text response.body, response.status end </code></pre> <p>Otherwise, you'd need to use <a href="http://ruby-doc.org/core/classes/IO.html#M002246" rel="nofollow noreferrer">IO::Select</a> or <a href="http://ruby-doc.org/core/classes/IO.html#M002267" rel="nofollow noreferrer">IO::read_noblock</a> to know when data is available to be read (from either network or subprocess) so you don't deadlock. See <a href="https://stackoverflow.com/questions/946738/detect-key-press-non-blocking-w-o-getc-gets-in-ruby">this</a> too. Also check that either your Rails is used in a multi-threaded environment <em>or</em> that your Ruby version is not affected by <a href="http://redmine.ruby-lang.org/issues/show/1993" rel="nofollow noreferrer">this IO::Select bug</a>.</p> <p>You can start with something along these lines:</p> <pre><code>status = POpen4::popen4("ping localhost") do |stdout, stderr, stdin, pid| puts "PID #{pid}" # our buffers stdout_lines="" stderr_lines="" begin loop do # check whether stdout, stderr or both are # ready to be read from without blocking IO.select([stdout,stderr]).flatten.compact.each { |io| # stdout, if ready, goes to stdout_lines stdout_lines += io.readpartial(1024) if io.fileno == stdout.fileno # stderr, if ready, goes to stdout_lines stderr_lines += io.readpartial(1024) if io.fileno == stderr.fileno } break if stdout.closed? &amp;&amp; stderr.closed? # if we acumulated any complete lines (\n-terminated) # in either stdout/err_lines, output them now stdout_lines.sub!(/.*\n/m) { puts $&amp; ; '' } stderr_lines.sub!(/.*\n/m) { puts $&amp; ; '' } end rescue EOFError puts "Done" end end </code></pre> <p>To also handle <code>stdin</code>, change to:</p> <pre><code> IO.select([stdout,stderr],[stdin]).flatten.compact.each { |io| # program ready to get stdin? do we have anything for it? if io.fileno == stdin.fileno &amp;&amp; &lt;got data from client?&gt; &lt;write a small chunk from client to stdin&gt; end # stdout, if ready, goes to stdout_lines </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload