pong-bot bugfixes

This commit is contained in:
Jeena Paradies 2011-04-12 18:29:47 +02:00
parent 9c8d04114c
commit 5bcd7260d3
4 changed files with 33 additions and 7 deletions

View file

@ -25,16 +25,15 @@ class GGSNetwork
protected
def connect(host, port)
gs = TCPSocket.open(host, port)
@socket = gs.accept
Thread.start(@socket) { |s| read(s) }
@socket = TCPSocket.new(host, port)
read
end
def write(message)
@socket.write(message)
end
def read(s)
def read
loop do
headers = {}
size = 0
@ -45,7 +44,7 @@ class GGSNetwork
headers[key] = value
end
if headers.contains?("Content-Size")
if headers.has_key?("Content-Size")
headers["Content-Size"].to_i.times do
args << @socket.recv
end
@ -56,7 +55,7 @@ class GGSNetwork
end
def receivedCommand(headers, data)
if headers.contains? "Client-Command"
if headers.has_key? "Client-Command"
command = headers["Client-Command"]
case command
when "hello"
@ -71,7 +70,7 @@ class GGSNetwork
def makeMessage(serverOrGame, command, args)
message =<<MESSAGE
Token: #{@game_t oken}
Token: #{@game_token}
#{serverOrGame}-Command: #{command}
Content-Length: #{args.length}

2
games/Pong-bots/pong-bot.rb Normal file → Executable file
View file

@ -1,5 +1,7 @@
#!/usr/bin/env ruby -wKU
$: << "."
require 'ggs-network.rb'
require 'ggs-delegate.rb'

24
games/Pong-bots/test.rb Normal file
View 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}"

View file

@ -65,6 +65,7 @@ expect_headers({char, $\n}, {Pid,_}, {Strings, Remains}) ->
[LastMessage|_] = Strings,
case LastMessage of
"" -> % 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,
case re:split(SecondLast, ": ", [{return, list}]) of
["Content-Length", X] ->