$topdir = nil
$league = nil
$logger = nil
+$config = nil
$:.unshift File.dirname(__FILE__)
require 'shogi_server'
+require 'shogi_server/config'
+require 'shogi_server/util'
require 'tempfile'
#################################################
ShogiServer.reload
+# Return
+# - a received string
+# - :timeout
+# - :exception
+# - nil when a socket is closed
+#
def gets_safe(socket, timeout=nil)
if r = select([socket], nil, nil, timeout)
return r[0].first.gets
--player-log-dir dir
log network messages for each player. Log files
will be put in the dir.
- --floodgate-history
- file name to record Floodgate game history
- default: './floodgate_history.yaml'
LICENSE
GPL versoin 2 or later
parser = GetoptLong.new(
["--daemon", GetoptLong::REQUIRED_ARGUMENT],
["--pid-file", GetoptLong::REQUIRED_ARGUMENT],
- ["--player-log-dir", GetoptLong::REQUIRED_ARGUMENT],
- ["--floodgate-history", GetoptLong::REQUIRED_ARGUMENT])
+ ["--player-log-dir", GetoptLong::REQUIRED_ARGUMENT])
parser.quiet = true
begin
parser.each_option do |name, arg|
if $options["pid-file"]
$options["pid-file"] = File.expand_path($options["pid-file"], $topdir)
- unless is_writable_file? $options["pid-file"]
+ unless ShogiServer::is_writable_file? $options["pid-file"]
usage
$stderr.puts "Can not create the pid file: %s" % [$options["pid-file"]]
exit 4
end
end
- $options["floodgate-history"] ||= File.join($topdir, "floodgate_history.yaml")
- $options["floodgate-history"] = File.expand_path($options["floodgate-history"], $topdir)
- unless is_writable_file? $options["floodgate-history"]
- usage
- $stderr.puts "Can not create the floodgate history file: %s" % [$options["floodgate-history"]]
- exit 6
- end
-end
-
-# See if the file is writable. The file will be created if it does not exist
-# yet.
-# Return true if the file is writable, otherwise false.
-#
-def is_writable_file?(file)
- if File.exist?(file)
- if FileTest.file?(file)
- return FileTest.writable_real?(file)
- else
- return false
- end
+ if $options["floodgate-history"]
+ $stderr.puts "WARNING: --floodgate-history has been deprecated."
+ $options["floodgate-history"] = nil
end
-
- begin
- open(file, "w") {|fh| }
- FileUtils.rm file
- rescue
- return false
- end
-
- return true
end
# See if a file can be created in the directory.
player = login = nil
while r = select([client], nil, nil, ShogiServer::Login_Time) do
- break unless str = r[0].first.gets
+ str = nil
+ begin
+ break unless str = r[0].first.gets
+ rescue Exception => ex
+ # It is posssible that the socket causes an error (ex. Errno::ECONNRESET)
+ log_error("login_loop: #{ex.class}: #{ex.message}\n\t#{ex.backtrace[0]}")
+ break
+ end
$mutex.lock # guards $league
begin
str =~ /([\r\n]*)$/
end
end
-def setup_floodgate
- return Thread.start do
+def floodgate_reload_log(leagues)
+ floodgate = leagues.min {|a,b| a.next_time <=> b.next_time}
+ diff = floodgate.next_time - Time.now
+ log_message("Floodgate reloaded. The next match will start at %s in %d seconds" %
+ [floodgate.next_time, diff])
+end
+
+def setup_floodgate(game_names)
+ return Thread.start(game_names) do |game_names|
Thread.pass
- floodgate = ShogiServer::League::Floodgate.new($league)
- log_message("Flooddgate reloaded. The next match will start at %s." %
- [floodgate.next_time])
+ leagues = game_names.collect do |game_name|
+ ShogiServer::League::Floodgate.new($league,
+ {:game_name => game_name})
+ end
+ leagues.delete_if do |floodgate|
+ ret = false
+ unless floodgate.next_time
+ log_error("Unsupported game name: %s" % floodgate.game_name)
+ ret = true
+ end
+ ret
+ end
+ if leagues.empty?
+ log_error("No valid Floodgate game names found")
+ return # exit from this thread
+ end
+ floodgate_reload_log(leagues)
while (true)
begin
+ floodgate = leagues.min {|a,b| a.next_time <=> b.next_time}
diff = floodgate.next_time - Time.now
if diff > 0
+ floodgate_reload_log(leagues) if $DEBUG
sleep(diff/2)
next
end
- $league.reload
- floodgate.match_game
- floodgate.charge
- next_time = floodgate.next_time
+ next_array = leagues.collect do |floodgate|
+ if (floodgate.next_time - Time.now) > 0
+ [floodgate.game_name, floodgate.next_time]
+ else
+ log_message("Starting Floodgate games...: %s" % [floodgate.game_name])
+ $league.reload
+ floodgate.match_game
+ floodgate.charge
+ [floodgate.game_name, floodgate.next_time] # next_time has been updated
+ end
+ end
$mutex.synchronize do
log_message("Reloading source...")
ShogiServer.reload
end
- floodgate = ShogiServer::League::Floodgate.new($league, next_time)
- log_message("Floodgate: The next match will start at %s." %
- [floodgate.next_time])
+ # Regenerate floodgate instances after ShogiServer.realod
+ leagues = next_array.collect do |game_name, next_time|
+ floodgate = ShogiServer::League::Floodgate.new($league,
+ {:game_name => game_name,
+ :next_time => next_time})
+ end
+ floodgate_reload_log(leagues)
rescue Exception => ex
# ignore errors
log_error("[in Floodgate's thread] #{ex} #{ex.backtrace}")
$options = parse_command_line
check_command_line
+ $config = ShogiServer::Config.new $options
$league = ShogiServer::League.new($topdir)
end
setup_watchdog_for_giant_lock
$league.setup_players_database
- fg_thread = setup_floodgate
+ fg_thread = setup_floodgate(["floodgate-900-0", "floodgate-3600-0"])
end
config[:StopCallback] = Proc.new do
fg_thread.kill if fg_thread
end
end
- trap("HUP") do
- Dependencies.clear
+ unless (RUBY_PLATFORM.downcase =~ /mswin|mingw|cygwin|bccwin/)
+ trap("HUP") do
+ Dependencies.clear
+ end
end
$stderr.puts("server started as a deamon [Revision: #{ShogiServer::Revision}]") if $options["daemon"]
log_message("server started [Revision: #{ShogiServer::Revision}]")