pong-bot bugfixes
This commit is contained in:
parent
9c8d04114c
commit
5bcd7260d3
4 changed files with 33 additions and 7 deletions
|
@ -25,16 +25,15 @@ class GGSNetwork
|
||||||
protected
|
protected
|
||||||
|
|
||||||
def connect(host, port)
|
def connect(host, port)
|
||||||
gs = TCPSocket.open(host, port)
|
@socket = TCPSocket.new(host, port)
|
||||||
@socket = gs.accept
|
read
|
||||||
Thread.start(@socket) { |s| read(s) }
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def write(message)
|
def write(message)
|
||||||
@socket.write(message)
|
@socket.write(message)
|
||||||
end
|
end
|
||||||
|
|
||||||
def read(s)
|
def read
|
||||||
loop do
|
loop do
|
||||||
headers = {}
|
headers = {}
|
||||||
size = 0
|
size = 0
|
||||||
|
@ -45,7 +44,7 @@ class GGSNetwork
|
||||||
headers[key] = value
|
headers[key] = value
|
||||||
end
|
end
|
||||||
|
|
||||||
if headers.contains?("Content-Size")
|
if headers.has_key?("Content-Size")
|
||||||
headers["Content-Size"].to_i.times do
|
headers["Content-Size"].to_i.times do
|
||||||
args << @socket.recv
|
args << @socket.recv
|
||||||
end
|
end
|
||||||
|
@ -56,7 +55,7 @@ class GGSNetwork
|
||||||
end
|
end
|
||||||
|
|
||||||
def receivedCommand(headers, data)
|
def receivedCommand(headers, data)
|
||||||
if headers.contains? "Client-Command"
|
if headers.has_key? "Client-Command"
|
||||||
command = headers["Client-Command"]
|
command = headers["Client-Command"]
|
||||||
case command
|
case command
|
||||||
when "hello"
|
when "hello"
|
||||||
|
|
2
games/Pong-bots/pong-bot.rb
Normal file → Executable file
2
games/Pong-bots/pong-bot.rb
Normal file → Executable file
|
@ -1,5 +1,7 @@
|
||||||
#!/usr/bin/env ruby -wKU
|
#!/usr/bin/env ruby -wKU
|
||||||
|
|
||||||
|
$: << "."
|
||||||
|
|
||||||
require 'ggs-network.rb'
|
require 'ggs-network.rb'
|
||||||
require 'ggs-delegate.rb'
|
require 'ggs-delegate.rb'
|
||||||
|
|
||||||
|
|
24
games/Pong-bots/test.rb
Normal file
24
games/Pong-bots/test.rb
Normal file
|
@ -0,0 +1,24 @@
|
||||||
|
#!/usr/bin/ruby
|
||||||
|
|
||||||
|
def func1
|
||||||
|
i=0
|
||||||
|
while i<=2
|
||||||
|
puts "func1 at: #{Time.now}"
|
||||||
|
sleep(2)
|
||||||
|
i=i+1
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def func2
|
||||||
|
j=0
|
||||||
|
while j<=2
|
||||||
|
puts "func2 at: #{Time.now}"
|
||||||
|
sleep(1)
|
||||||
|
j=j+1
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
puts "Started At #{Time.now}"
|
||||||
|
t1=Thread.new{func1()}
|
||||||
|
t2=Thread.new{func2()}
|
||||||
|
puts "End at #{Time.now}"
|
|
@ -65,6 +65,7 @@ expect_headers({char, $\n}, {Pid,_}, {Strings, Remains}) ->
|
||||||
[LastMessage|_] = Strings,
|
[LastMessage|_] = Strings,
|
||||||
case LastMessage of
|
case LastMessage of
|
||||||
"" -> % We have a data section.. Last line should thus be the content length.
|
"" -> % We have a data section.. Last line should thus be the content length.
|
||||||
|
% FIXME: the Content-Length doesn't have to be the last Header line
|
||||||
[LastMessage, SecondLast | Rest] = Strings,
|
[LastMessage, SecondLast | Rest] = Strings,
|
||||||
case re:split(SecondLast, ": ", [{return, list}]) of
|
case re:split(SecondLast, ": ", [{return, list}]) of
|
||||||
["Content-Length", X] ->
|
["Content-Length", X] ->
|
||||||
|
|
Reference in a new issue