1

I am trying to make an interactive telnet client for Ruby. The current library is extremely lacking so I have been trying to add onto it by creating an interactive gem that allows a user to streamline data in real time with telnet. To do this I need to use multithreading:

t1 accepts user input. Users must always have capacity to input data through entire application. Once user data is sent we will receive data back right away which will be caught with our block { |c| print c }. The problem is that we want data to be streamlined to us. In other words, right now we only get data that is sent back after we send something, we want data to be received up to a minute after we sent a command. We want data to constantly be flowing to us.

I made t2 for this purpose. t2 waits for data to be received and then displays it when its regex pattern is matched. The problem with t2 is, if data is never received then the user cannot enter information into t1.

t3 operates on t1 and t2. My Question is how can I organize my threads in such a way where the user can constantly type in console and submit commands, and simultaneously constantly receive information back from the server?

t1 = Thread.new {
  while true
    input = gets.chomp
    localhost.cmd(input) { |c| print c }
  end
}

t2 = Thread.new {
  puts localhost.waitfor("Prompt" => /[$%#>:?.|](\e\[0?m\s*)* *\z/)
}

t3 = Thread.new {
  t1.join
  t2.join
}

t3.join
chopper draw lion4
  • 12,401
  • 13
  • 53
  • 100
  • Your `t3` thread is useless. You may just call `t1.join` and `t2.join` directly. I think the `localhost.waitfor` method is blocking the whole localhost object, probably with a mutex or something. Can you provide more code? Like the source of `localhost.waitfor` and `localhost.cmd` ? –  Aug 10 '14 at 17:15

1 Answers1

0

The problem is that we want data to be streamlined to us. In other words, right now we only get data that is sent back after we send something,

require 'thread'

user_data = Queue.new

t1 = Thread.new do
  loop do
    print "Enter data: "
    line = gets.chomp

    if line == ""
      user_data << "END_OF_DATA"
      break
    else
      user_data << line
    end

  end
end.join

t2 = Thread.new do
  processed_data = []

  loop do
    line = user_data.shift
    break if line == "END_OF_DATA"
    processed_data << line
  end

  p processed_data
end.join

You might want to read this:

https://www.rfc-editor.org/rfc/rfc854

Community
  • 1
  • 1
7stud
  • 46,922
  • 14
  • 101
  • 127