2

I'm trying to write an EventMachine server to run an nCurses application via telnet or ssh, using ruby 1.9.3's PTY and io/console modules:

require 'rubygems'
require 'socket'
require 'pty'
require 'io/console'
require 'eventmachine'

 module CursesServer
   def post_init
     puts "-- someone connected to the server!"
     @reader, @writer, @pid = PTY.spawn('ruby ./ncurses_app_to_run.rb')
     @reader.sync = true #No buffering stdout     
  end

   def receive_data(data)
     screen = @reader.read(<number_of_bytes>)
     puts "received: " + data # Log input on server side
     send_data screen
     close_connection if data =~ /quit/i
   end

   def unbind
     puts "-- someone disconnected from the server!"
  end
end

# Note that this will block current thread.
EventMachine.run {
  EventMachine.start_server "127.0.0.1", 8081, CursesServer
}

If I play with the value of the argument passed to @reader.read, I can get portions of the screen to display on the client side, however I don't know how to determine the actual size of the client's terminal, either initally or after the client resizes the terminal.

How can I synchronize the output with the size of the client's terminal? Is it possible with telnet/netcat or do I have to use a more robust protocol like ssh?

Thanks in advance.

4

1 回答 1

0

也许这可以帮助你。不过,我正在使用 EventMachine 和 ZeroMQ,但它似乎很容易适应。

def run_command command
    _start = Time.now
    begin
      PTY.spawn( command ) do |r, w, pid|
        r.sync = true
        begin
          r.each do |line|
            $announce_socket.send_msg "LOG:#{line}"
          end
       rescue => ex
        puts "ERROR: #{ex.message}"
       end
     end
    rescue PTY::ChildExited => e
      $announce_socket.send_msg "CMD_PREMATURE_EXIT:#{e.message}"
      puts "command exited prematurely, notified UI"
    end
    $announce_socket.send_msg "CMD_EXIT:#{Time.now - _start}"
    puts "done running commmand (took #{Time.now - _start} seconds)"
  end
end

和反应堆部分:

$context = EM::ZeroMQ::Context.new(1)

def cleanup(exit_code = 0)
  EM::stop()
  exit exit_code
end

trap('INT') do
  cleanup
end

EM.run do

  $announce_socket = $context.socket ZMQ::DEALER, EMDEALERHandler.new
  $announce_socket.identity = MY_ID
  $announce_socket.connect "tcp://#{host_ip}:7777"

  # announce presence
  $announce_socket.send_msg "UP"
end

在此之后,您只需要一个 ROUTER 套接字来推送数据。

于 2012-11-03T11:42:07.587 回答