4 ## Copyright (C) 2004 NABEYA Kenichi (aka nanami@2ch)
5 ## Copyright (C) 2007-2008 Daigo Moriwaki (daigo at debian dot org)
7 ## This program is free software; you can redistribute it and/or modify
8 ## it under the terms of the GNU General Public License as published by
9 ## the Free Software Foundation; either version 2 of the License, or
10 ## (at your option) any later version.
12 ## This program is distributed in the hope that it will be useful,
13 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ## GNU General Public License for more details.
17 ## You should have received a copy of the GNU General Public License
18 ## along with this program; if not, write to the Free Software
19 ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 $:.unshift File.dirname(__FILE__)
22 require 'shogi_server'
24 #################################################
28 def gets_safe(socket, timeout=nil)
29 if r = select([socket], nil, nil, timeout)
30 return r[0].first.gets
34 rescue Exception => ex
35 log_error("#{ex.class}: #{ex.message}\n\t#{ex.backtrace[0]}")
42 shogi-server - server for CSA server protocol
45 shogi-server [OPTIONS] event_name port_number
48 server for CSA server protocol
52 specify filename for logging process ID
54 run as a daemon. Log files will be put in dir.
57 GPL versoin 2 or later
62 #{ShogiServer::Release}
65 #{ShogiServer::Revision}
87 def parse_command_line
89 parser = GetoptLong.new(
90 ["--daemon", GetoptLong::REQUIRED_ARGUMENT],
91 ["--pid-file", GetoptLong::REQUIRED_ARGUMENT])
94 parser.each_option do |name, arg|
96 options[name] = arg.dup
100 raise parser.error_message
105 def write_pid_file(file)
106 open(file, "w") do |fh|
111 def mutex_watchdog(mutex, sec)
123 log_error("mutex watchdog timeout")
129 def login_loop(client)
132 while r = select([client], nil, nil, ShogiServer::Login_Time) do
133 break unless str = r[0].first.gets
134 $mutex.lock # guards LEAGUE
138 if (ShogiServer::Login::good_login?(str))
139 player = ShogiServer::Player::new(str, client, eol)
140 login = ShogiServer::Login::factory(str, player)
141 if (current_player = LEAGUE.find(player.name))
142 if (current_player.password == player.password &&
143 current_player.status != "game")
144 log_message(sprintf("user %s login forcely", player.name))
147 login.incorrect_duplicated_player(str)
155 client.write("LOGIN:incorrect" + eol)
156 client.write("type 'LOGIN name password' or 'LOGIN name password x1'" + eol) if (str.split.length >= 4)
162 return [player, login]
165 def setup_logger(log_file)
166 logger = Logger.new(log_file, 'daily')
167 logger.formatter = ShogiServer::Formatter.new
168 logger.level = Logger::INFO
169 logger.datetime_format = "%Y-%m-%d %H:%M:%S"
173 def setup_watchdog_for_giant_lock
177 mutex_watchdog($mutex, 10)
183 setup_watchdog_for_giant_lock
185 $options = parse_command_line
186 if (ARGV.length != 2)
191 LEAGUE.event = ARGV.shift
194 dir = $options["daemon"]
195 dir = File.expand_path(dir) if dir
196 if dir && ! File.exist?(dir)
200 log_file = dir ? File.join(dir, "shogi-server.log") : STDOUT
201 $logger = setup_logger(log_file)
203 LEAGUE.dir = dir || File.dirname(__FILE__)
204 LEAGUE.setup_players_database
208 config[:ServerType] = WEBrick::Daemon if $options["daemon"]
209 config[:Logger] = $logger
210 if $options["pid-file"]
211 pid_file = File.expand_path($options["pid-file"])
212 config[:StartCallback] = Proc.new do
213 write_pid_file(pid_file)
215 config[:StopCallback] = Proc.new do
216 FileUtils.rm(pid_file, :force => true)
220 server = WEBrick::GenericServer.new(config)
221 ["INT", "TERM"].each do |signal|
232 $stderr.puts("server started as a deamon [Revision: #{ShogiServer::Revision}]") if $options["daemon"]
233 log_message("server started [Revision: #{ShogiServer::Revision}]")
235 server.start do |client|
236 # client.sync = true # this is already set in WEBrick
237 client.setsockopt(Socket::SOL_SOCKET, Socket::SO_KEEPALIVE, true)
238 # Keepalive time can be set by /proc/sys/net/ipv4/tcp_keepalive_time
239 player, login = login_loop(client) # loop
242 log_message(sprintf("user %s login", player.name))
244 player.run(login.csa_1st_str) # loop
248 player.game.kill(player)
250 player.finish # socket has been closed
251 LEAGUE.delete(player)
252 log_message(sprintf("user %s logout", player.name))
263 TCPSocket.do_not_reverse_lookup = true
264 Thread.abort_on_exception = $DEBUG ? true : false
266 LEAGUE = ShogiServer::League::new