* Add test script
* Rename cont to windex * Add optparse options
This commit is contained in:
parent
880e85f12d
commit
1e8d916a36
3 changed files with 119 additions and 40 deletions
80
server.rb
80
server.rb
|
|
@ -1,41 +1,83 @@
|
|||
#!/usr/bin/env ruby
|
||||
|
||||
require 'socket'
|
||||
require 'optparse'
|
||||
|
||||
PORT = 6789
|
||||
ROWS = 24
|
||||
COLS = 40
|
||||
PUBLIC_HOST = '0.0.0.0'
|
||||
PRIVATE_HOST = '127.0.0.1'
|
||||
options = {
|
||||
:port => 7570,
|
||||
:host => PRIVATE_HOST,
|
||||
:rows => 24,
|
||||
:cols => 40,
|
||||
}
|
||||
OptionParser.new do |opt|
|
||||
opt.on('-pPORT', '--port PORT', Integer,
|
||||
'Port to serve on (defaults to 7570)') do |o|
|
||||
options[:port] = o
|
||||
end
|
||||
opt.on('-rROWS', '--rows ROWS', Integer,
|
||||
'The number of rows to have in the shared buffer') do |o|
|
||||
options[:rows] = o
|
||||
end
|
||||
opt.on('-cCOLS', '--cols COLS', Integer,
|
||||
'The number of columns to have in the shared buffer') do |o|
|
||||
options[:cols] = o
|
||||
end
|
||||
opt.on('-s', '--public', Boolean,
|
||||
'Serve publicly, rather than privately') do
|
||||
options[:host] = PUBLIC_HOST
|
||||
end
|
||||
opt.on('-S', '--private', Boolean,
|
||||
'Serve privately, rather than publicly (default)') do
|
||||
options[:host] = PRIVATE_HOST
|
||||
end
|
||||
opt.on('-h', '--help', 'Get help with this command') do
|
||||
puts opt
|
||||
exit
|
||||
end
|
||||
end.parse!(into: options)
|
||||
|
||||
HOST = options[:host]
|
||||
PORT = options[:port]
|
||||
|
||||
ROWS = options[:rows]
|
||||
COLS = options[:cols]
|
||||
VERSION = 0.01
|
||||
|
||||
server = TCPServer.new(PORT)
|
||||
server = TCPServer.new(HOST, PORT)
|
||||
sockets = [server]
|
||||
|
||||
puts "PORT: #{PORT}"
|
||||
puts "GRID SIZE: #{ROWS}r#{COLS}c"
|
||||
puts 'Listening...'
|
||||
|
||||
buffer = ' '*ROWS*COLS
|
||||
cont = 0
|
||||
pre = "#{ROWS}r#{COLS}c >>> "
|
||||
buffer = pre.ljust(ROWS*COLS)[0...ROWS*COLS]
|
||||
windex = pre.length # Wipe your buffer clean with your write index
|
||||
windex %= buffer.length
|
||||
|
||||
def new_buf_cont_out(buf, count, newtext)
|
||||
def new_buf_wi_out(buf, wi, newtext)
|
||||
buf = buf.dup
|
||||
out = ''
|
||||
nti = 0 # New Text Index
|
||||
while nti < newtext.length
|
||||
out << "#{VERSION}|"
|
||||
out << ((count/COLS)+1).to_s + '|'
|
||||
out << ((count%COLS)+1).to_s + '|'
|
||||
out << ((wi/COLS)+1).to_s + '|'
|
||||
out << ((wi%COLS)+1).to_s + '|'
|
||||
loop do
|
||||
out << newtext[nti]
|
||||
buf[count] = newtext[nti]
|
||||
count += 1
|
||||
buf[wi] = newtext[nti]
|
||||
wi += 1
|
||||
nti += 1
|
||||
break if nti >= newtext.length
|
||||
break if count%COLS == 0
|
||||
break if wi%COLS == 0
|
||||
end
|
||||
count = 0 if count >= (ROWS*COLS)
|
||||
wi = 0 if wi >= (ROWS*COLS)
|
||||
wi %= ROWS*COLS
|
||||
out << "\n"
|
||||
end
|
||||
return [buf, count, out]
|
||||
return [buf, wi, out]
|
||||
end
|
||||
|
||||
loop do
|
||||
|
|
@ -47,8 +89,10 @@ loop do
|
|||
client = server.accept
|
||||
sockets << client
|
||||
puts "Connection: #{client.peeraddr}"
|
||||
client.puts new_buf_cont_out(buffer,0,buffer)[2]
|
||||
client.puts new_buf_cont_out(buffer,cont-1,'')[2]
|
||||
client.puts new_buf_wi_out(buffer,0,buffer)[2]
|
||||
if buffer.length > 1
|
||||
client.puts new_buf_wi_out(buffer,windex-1,buffer[windex-1])[2]
|
||||
end
|
||||
next
|
||||
end
|
||||
begin
|
||||
|
|
@ -69,11 +113,11 @@ loop do
|
|||
sck.close
|
||||
sockets.delete(sck)
|
||||
end
|
||||
buffer, cont, out = new_buf_cont_out(buffer,cont,newtext)
|
||||
buffer, windex, out = new_buf_wi_out(buffer,windex,newtext)
|
||||
clients = sockets - [server]
|
||||
clients.each { |c| c.puts out }
|
||||
#if out != ''
|
||||
# puts "#{cont}: (#{(cont/COLS)+1},#{(cont%COLS)+1})"
|
||||
# puts "#{windex}: (#{(windex/COLS)+1},#{(windex%COLS)+1})"
|
||||
#end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue