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