Last active
December 15, 2015 09:59
-
-
Save chrismo/5242948 to your computer and use it in GitHub Desktop.
parley IO question
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1.9.3-p385 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'curses' | |
require 'socket' | |
class OutputHandler | |
include Curses | |
def initialize | |
init_screen | |
crmode | |
end | |
def puts(s) | |
addstr(s) | |
refresh | |
end | |
def each_line | |
buffer = '' | |
loop do | |
buffer << getch | |
if buffer[-1..-1] == "\n" | |
yield buffer | |
buffer = '' | |
end | |
end | |
end | |
def clear_prior_line | |
setpos(0, 0) | |
clrtoeol | |
end | |
end | |
class InputReader | |
def initialize(output) | |
@output = output | |
@thread = Thread.new do | |
check_input | |
end | |
end | |
def check_input | |
@output.each_line do |line| | |
@output.clear_prior_line | |
process_input(line) | |
end | |
end | |
def process_input(input) | |
@output.puts "You Said: #{input}" | |
end | |
def join | |
@thread.join | |
end | |
end | |
class SocketReader | |
def initialize(output) | |
@output = output | |
@socket = TCPSocket.new '127.0.0.1', 3999 | |
@thread = Thread.new do | |
check_socket | |
end | |
end | |
def check_socket | |
while line = @socket.gets | |
# @output.puts line | |
end | |
end | |
def join | |
@thread.join | |
end | |
end | |
class SocketWriter | |
def initialize | |
Thread.new do | |
TCPServer.open('127.0.0.1', 3999) do |serv| | |
s = serv.accept | |
loop do | |
s.puts Time.now | |
sleep rand(3) | |
end | |
s.close | |
end | |
end | |
end | |
end | |
SocketWriter.new | |
sleep 0.5 # let the server get going before attaching to it | |
output = OutputHandler.new | |
input = InputReader.new(output) | |
socket = SocketReader.new(output) | |
input.join | |
socket.join |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2013-03-25 22:44:46 -0500 | |
2013-03-25 22:44:46 -0500 | |
2013-03-25 22:44:47 -0500 | |
2013-03-25 22:44:48 -0500 | |
2013-03-25 22:44:48 -0500 | |
this2013-03-25 22:44:50 -0500 | |
2013-03-25 22:44:50 -0500 | |
is | |
2013-03-25 22:44:51 -0500 | |
test | |
in2013-03-25 22:44:52 -0500 | |
2013-03-25 22:44:52 -0500 | |
put | |
2013-03-25 22:44:54 -0500 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment