OSDN Git Service

Added a new command line to specify floodgate_history.yaml file.
[shogi-server/shogi-server.git] / shogi-server
index 84594b5..07e42a6 100755 (executable)
@@ -18,6 +18,7 @@
 ## along with this program; if not, write to the Free Software
 ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
+TOP_DIR = File.expand_path(File.dirname(__FILE__))
 $:.unshift File.dirname(__FILE__)
 require 'shogi_server'
 
@@ -25,6 +26,8 @@ require 'shogi_server'
 # MAIN
 #
 
+ShogiServer.reload
+
 def gets_safe(socket, timeout=nil)
   if r = select([socket], nil, nil, timeout)
     return r[0].first.gets
@@ -32,7 +35,7 @@ def gets_safe(socket, timeout=nil)
     return :timeout
   end
 rescue Exception => ex
-  log_error("#{ex.class}: #{ex.message}\n\t#{ex.backtrace[0]}")
+  log_error("gets_safe: #{ex.class}: #{ex.message}\n\t#{ex.backtrace[0]}")
   return :exception
 end
 
@@ -55,6 +58,9 @@ OPTIONS
         --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
@@ -113,20 +119,21 @@ def write_pid_file(file)
 end
 
 def mutex_watchdog(mutex, sec)
+  sec = 1 if sec < 1
+  queue = []
   while true
-    begin
-      timeout(sec) do
-        begin
-          mutex.lock
-        ensure
-          mutex.unlock
-        end
+    if mutex.try_lock
+      queue.clear
+      mutex.unlock
+    else
+      queue.push(Object.new)
+      if queue.size > sec
+        # timeout
+        log_error("mutex watchdog timeout: %d sec" % [sec])
+        queue.clear
       end
-      sleep(sec)
-    rescue TimeoutError
-      log_error("mutex watchdog timeout")
-      exit(1)
     end
+    sleep(1)
   end
 end
 
@@ -182,23 +189,59 @@ def setup_watchdog_for_giant_lock
   end
 end
 
-def main
+def setup_floodgate
+  return Thread.start do 
+    Thread.pass
+    floodgate = ShogiServer::League::Floodgate.new(LEAGUE)
+    log_message("Flooddgate reloaded. The next match will start at %s." % 
+                [floodgate.next_time])
 
-  setup_watchdog_for_giant_lock
+    while (true)
+      begin
+        diff = floodgate.next_time - Time.now
+        if diff > 0
+          sleep(diff/2)
+          next
+        end
+        LEAGUE.reload
+        floodgate.match_game
+        floodgate.charge
+        next_time = floodgate.next_time
+        $mutex.synchronize do
+          log_message("Reloading source...")
+          ShogiServer.reload
+        end
+        floodgate = ShogiServer::League::Floodgate.new(LEAGUE, next_time)
+        log_message("Floodgate will start the next match at %s." % 
+                    [floodgate.next_time])
+      rescue Exception => ex 
+        # ignore errors
+        log_error("[in Floodgate's thread] #{ex} #{ex.backtrace}")
+      end
+    end
+  end
+end
 
+def main
+  
   $options = parse_command_line
   if (ARGV.length != 2)
     usage
     exit 2
   end
+  if $options["player-log-dir"]
+    $options["player-log-dir"] = File.expand_path($options["player-log-dir"])
+  end
   if $options["player-log-dir"] && 
-     !File.exists?($options["player-log-dir"])
+     !File.directory?($options["player-log-dir"])
     usage
     exit 3
   end
-  if $options["player-log-dir"]
-    $options["player-log-dir"] = File.expand_path($options["player-log-dir"])
+  if $options["pid-file"] 
+    $options["pid-file"] = File.expand_path($options["pid-file"])
   end
+  $options["floodgate-history"] ||= File.join(File.dirname(__FILE__), "floodgate_history.yaml")
+  $options["floodgate-history"] = File.expand_path($options["floodgate-history"])
 
   LEAGUE.event = ARGV.shift
   port = ARGV.shift
@@ -212,34 +255,40 @@ def main
   log_file = dir ? File.join(dir, "shogi-server.log") : STDOUT
   $logger = setup_logger(log_file)
 
-  LEAGUE.dir = dir || File.dirname(__FILE__)
-  LEAGUE.setup_players_database
+  LEAGUE.dir = dir || TOP_DIR
 
   config = {}
   config[:Port]       = port
   config[:ServerType] = WEBrick::Daemon if $options["daemon"]
   config[:Logger]     = $logger
-  if $options["pid-file"]
-    pid_file = File.expand_path($options["pid-file"])
-    config[:StartCallback] = Proc.new do
-      write_pid_file(pid_file)
+
+  fg_thread = nil
+
+  config[:StartCallback] = Proc.new do
+    srand
+    if $options["pid-file"]
+      write_pid_file($options["pid-file"])
     end
-    config[:StopCallback] = Proc.new do
-      FileUtils.rm(pid_file, :force => true)
+    setup_watchdog_for_giant_lock
+    LEAGUE.setup_players_database
+    fg_thread = setup_floodgate
+  end
+
+  config[:StopCallback] = Proc.new do
+    if $options["pid-file"]
+      FileUtils.rm($options["pid-file"], :force => true)
     end
   end
 
   server = WEBrick::GenericServer.new(config)
   ["INT", "TERM"].each do |signal| 
     trap(signal) do
-      LEAGUE.shutdown
       server.shutdown
+      fg_thread.kill if fg_thread
     end
   end
   trap("HUP") do
-    LEAGUE.shutdown
     Dependencies.clear
-    LEAGUE.restart
   end
   $stderr.puts("server started as a deamon [Revision: #{ShogiServer::Revision}]") if $options["daemon"] 
   log_message("server started [Revision: #{ShogiServer::Revision}]")
@@ -267,7 +316,6 @@ def main
         $mutex.unlock
       end
   end
-  $logger.close
 end
 
 
@@ -277,6 +325,10 @@ if ($0 == __FILE__)
   TCPSocket.do_not_reverse_lookup = true
   Thread.abort_on_exception = $DEBUG ? true : false
 
-  LEAGUE = ShogiServer::League::new
-  main
+  begin
+    LEAGUE = ShogiServer::League.new(TOP_DIR)
+    main
+  rescue Exception => ex
+    log_error("main: #{ex.class}: #{ex.message}\n\t#{ex.backtrace[0]}")
+  end
 end