Skip to content

Instantly share code, notes, and snippets.

@Prigin
Created May 27, 2020 20:43
Show Gist options
  • Save Prigin/862d393c6e717eaae7bc27ff5bddadf8 to your computer and use it in GitHub Desktop.
Save Prigin/862d393c6e717eaae7bc27ff5bddadf8 to your computer and use it in GitHub Desktop.
chat
require 'socket'
class Connection
attr_accessor :buff_in, :buff_out
def initialize(sock)
@sock = sock
@buff_in = Queue.new
@buff_out = Queue.new
@threads = [thread_rcv, thread_snd]
end
def thread_rcv
thr = Thread.new do
begin
loop { @buff_in << @sock.gets.chomp }
rescue StandardError
close
end
end
end
def thread_snd
thr = Thread.new do
begin
loop { @sock.puts @buff_out.deq }
rescue StandardError
close
end
end
end
def close
@threads.each(&:kill)
@sock.close
end
end
class Log
def initialize(line = 0, col = 0, line_len = 10, size = 6)
@line = line
@col = col
@buff = [''] * size
@line_len = line_len
end
def push(str)
str = str.chomp
loop do
@buff.shift
@buff.push(str.slice!(0, @line_len).ljust(@line_len))
break if str == ''
end
@buff.each_index do |i|
print "\033[#{@line + i};#{@col}H#{@buff[i]}"
end
end
end
class Line
def initialize(line = 0, col = 0)
@line = line
@col = col
@size = 0
end
def push(str)
print "\033[#{@line};#{@col}H#{str.ljust(@size)}"
@size = str.size
end
end
class List
def initialize(line = 0, col = 0)
@line = line
@col = col
@list = []
end
def push(obj)
flag = false
if obj.last == :delete
@list.delete(obj.first.chomp.ljust(13))
flag = true
elsif obj.last == :add
@list.push(obj.first.chomp.ljust(13))
end
refresh(flag)
end
def refresh(flag = false)
i = 0
@list.each do |u|
print "\e[#{@line + i};#{@col}H#{u}"
i += 1
end
print "\e[#{@line + i};#{@col}H#{' ' * 13}" if flag == true
end
end
require 'io/console'
require_relative 'scr_samples'
# screen configuration
class Screen
attr_reader :in
def initialize(units_hash)
print "\033[H\033[J"
@units_hash = units_hash
@in = Queue.new
@loop = Thread.new { loop { refresh } }
end
def refresh
hsh, str = @in.deq
@units_hash[hsh].push(str)
end
def stop
@loop.kill
print "\033[H\033[J"
end
end
# input handlig
class Input
attr_accessor :queue
def initialize(cmd_len: 30)
@cmd_len = cmd_len
@queue = Queue.new
end
# check command
def check_com(str)
str != '/close'
end
def key_read
buff = ''
tr = true
while tr
in_key = $stdin.noecho(&:getch)
if in_key == "\r"
tr = check_com(buff) # REDO!!!!!!!
@queue << [:input, buff] # REDO!!!!!!!
buff = ''
elsif in_key == "\b"
buff.chop!
elsif buff.size < @cmd_len
buff += in_key
end
@queue << [:type_in, buff] # REDO!!!!!!!
end
end
end
require_relative 'screen.rb'
require_relative 'network.rb'
conn = Connection.new(TCPSocket.new('localhost', 1337))
scr = Screen.new(cmd: Line.new(10, 0), log: Log.new, clock: Line.new(12, 0))
in_ = Input.new(cmd_len: 10)
def clock_tick
sleep 1
[:clock, Time.new.to_s]
end
master = Thread.new do
Thread.new { loop { scr.in << clock_tick } }
Thread.new do
loop { scr.in << [:log, conn.buff_in.deq] }
end
Thread.new do
loop do
buff = in_.queue.deq
case buff.first
when :type_in
scr.in << [:cmd, buff.last]
when :input
scr.in << [:log, buff.last]
conn.buff_out << buff.last
end
end
end
end
in_.key_read
require_relative 'screen.rb'
require_relative 'network.rb'
srv = TCPServer.new('localhost', 1337)
conn = Connection.new(srv.accept)
scr = Screen.new(cmd: Line.new(10, 0), log: Log.new, clock: Line.new(12, 0))
in_ = Input.new(cmd_len: 10)
def clock_tick
sleep 1
[:clock, Time.new.to_s]
end
master = Thread.new do
Thread.new { loop { scr.in << clock_tick } }
Thread.new do
loop { scr.in << [:log, conn.buff_in.deq] }
end
Thread.new do
loop do
buff = in_.queue.deq
case buff.first
when :type_in
scr.in << [:cmd, buff.last]
when :input
scr.in << [:log, buff.last]
conn.buff_out << buff.last
end
end
end
end
in_.key_read
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment