shogi-server source
Revisão | d36b20955a253bea5759010ecf0af259e1a1935a (tree) |
---|---|
Hora | 2011-12-12 22:10:09 |
Autor | Daigo Moriwaki <beatles@user...> |
Commiter | Daigo Moriwaki |
Improved robustness of test/benchmark.rb.
@@ -420,7 +420,10 @@ def main | ||
420 | 420 | client.setsockopt(Socket::SOL_SOCKET, Socket::SO_KEEPALIVE, true) |
421 | 421 | # Keepalive time can be set by /proc/sys/net/ipv4/tcp_keepalive_time |
422 | 422 | player, login = login_loop(client) # loop |
423 | - next unless player | |
423 | + unless player | |
424 | + log_error("Detected a timed out login attempt") | |
425 | + next | |
426 | + end | |
424 | 427 | |
425 | 428 | log_message(sprintf("user %s login", player.name)) |
426 | 429 | login.process |
@@ -1,14 +1,18 @@ | ||
1 | 1 | #!/usr/bin/ruby |
2 | 2 | |
3 | +require 'logger' | |
3 | 4 | require 'socket' |
4 | 5 | require 'thread' |
5 | 6 | |
7 | +$logger = nil | |
8 | + | |
6 | 9 | class BenchPlayer |
7 | 10 | def initialize(game_name, name, sente) |
8 | 11 | @game_name = game_name |
9 | 12 | @name = "%s_%s" % [game_name, name] |
10 | 13 | @turn_mark = sente ? "+" : "-" |
11 | 14 | @nmoves = 0 |
15 | + @socket = nil | |
12 | 16 | end |
13 | 17 | attr_reader :nmoves |
14 | 18 |
@@ -22,37 +26,42 @@ class BenchPlayer | ||
22 | 26 | |
23 | 27 | def reader |
24 | 28 | Thread.new do |
29 | + Thread.pass | |
25 | 30 | loop do |
26 | - if r = select([@socket], nil, nil, 10) | |
31 | + if r = select([@socket], nil, nil, 300) | |
27 | 32 | str = r[0].first.gets |
28 | 33 | if %r!^[\+\-]\d{4}\w{2},T\d+$! =~ str |
29 | 34 | @nmoves += 1 |
30 | 35 | end |
31 | - @message << str | |
36 | + @message << str if str | |
32 | 37 | else |
33 | - raise "timed out" | |
38 | + $logger.warn "Timed out: %s" % [@name] | |
34 | 39 | end |
35 | 40 | end |
41 | + $logger.error "Socket error: %s" % [@name] | |
36 | 42 | end |
37 | 43 | end |
38 | 44 | |
39 | 45 | def wait(reg) |
40 | 46 | loop do |
41 | 47 | break if reg =~ @message |
42 | - sleep 0.1 | |
48 | + #$logger.debug "WAIT %s: %s" % [reg, @message] | |
49 | + sleep 0.001 | |
50 | + #Thread.pass | |
43 | 51 | end |
44 | 52 | end |
45 | 53 | |
46 | 54 | def wait_nmoves(n) |
47 | 55 | loop do |
48 | 56 | break if @nmoves == n |
49 | - sleep 0.01 | |
57 | + sleep 0.001 | |
58 | + #Thread.pass | |
50 | 59 | end |
51 | 60 | end |
52 | 61 | |
53 | 62 | def login |
54 | 63 | @socket.puts "LOGIN #{@name} dummy x1" |
55 | - wait %r!^LOGIN! | |
64 | + wait %r!^##\[LOGIN\] \+OK! | |
56 | 65 | end |
57 | 66 | |
58 | 67 | def game |
@@ -94,12 +103,20 @@ class BenchGame | ||
94 | 103 | end |
95 | 104 | |
96 | 105 | def start |
106 | + $logger.info "Starting... %s" % [@game_name] | |
107 | + $logger.debug "Connecting... %s" % [@game_name] | |
97 | 108 | each_player {|player| player.connect} |
109 | + $logger.debug "Logging in... %s" % [@game_name] | |
98 | 110 | each_player {|player| player.login} |
111 | + $logger.debug "Sending GAME... %s" % [@game_name] | |
99 | 112 | each_player {|player| player.game} |
113 | + $logger.debug "Waiting... %s" % [@game_name] | |
100 | 114 | each_player {|player| player.wait %r!^END Game_Summary!} |
115 | + $logger.debug "Agreeing... %s" % [@game_name] | |
101 | 116 | each_player {|player| player.agree} |
117 | + $logger.debug "AGREE waiting... %s" % [@game_name] | |
102 | 118 | each_player {|player| player.wait %r!^START:!} |
119 | + $logger.info "Started %s" % [@game_name] | |
103 | 120 | turn = true # black |
104 | 121 | nmoves = 0 |
105 | 122 | @csa.each_line do |line| |
@@ -115,10 +132,15 @@ class BenchGame | ||
115 | 132 | turn = true |
116 | 133 | nmoves += 1 |
117 | 134 | when /^%TORYO/ |
135 | + $logger.debug "Waiting TORYO... %s" % [@game_name] | |
136 | + @p1.wait_nmoves nmoves | |
137 | + @p2.wait_nmoves nmoves | |
118 | 138 | turn ? @p1.toryo : @p2.toryo |
119 | 139 | end |
120 | 140 | end |
141 | + $logger.info "Logging out... %s" % [@game_name] | |
121 | 142 | each_player {|player| player.logout} |
143 | + $logger.info "Finished %s" % [@game_name] | |
122 | 144 | end |
123 | 145 | end |
124 | 146 |
@@ -127,11 +149,15 @@ if __FILE__ == $0 | ||
127 | 149 | filepath = ARGV.shift || File.join(File.dirname(__FILE__), "csa", "wdoor+floodgate-900-0+gps_normal+gps_l+20100507120007.csa") |
128 | 150 | csa = File.open(filepath){|f| f.read} |
129 | 151 | |
152 | + $logger = Logger.new(STDOUT) | |
153 | + $logger.level = $DEBUG ? Logger::DEBUG : Logger::INFO | |
154 | + | |
130 | 155 | nclients = ARGV.shift || 1 |
131 | 156 | nclients = nclients.to_i |
132 | 157 | threads = [] |
133 | 158 | nclients.times do |i| |
134 | 159 | threads << Thread.new do |
160 | + Thread.pass | |
135 | 161 | game = BenchGame.new("b#{i}", csa) |
136 | 162 | game.start |
137 | 163 | end |