OSDN Git Service

* No loger use Dependencies.rb in Rails.
[shogi-server/shogi-server.git] / shogi-server
index 84594b5..4b048bd 100755 (executable)
@@ -25,6 +25,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 +34,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
 
@@ -113,20 +115,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,7 +185,44 @@ def setup_watchdog_for_giant_lock
   end
 end
 
+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])
+
+    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
+  
+  #[ShogiServer::League::Floodgate, ShogiServer::Pairing].each do |klass|
+  #  Dependencies.unloadable klass
+  #end
 
   setup_watchdog_for_giant_lock
 
@@ -191,14 +231,14 @@ def main
     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"])
-  end
 
   LEAGUE.event = ARGV.shift
   port = ARGV.shift
@@ -219,12 +259,17 @@ def main
   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
+    if $options["pid-file"]
+      write_pid_file($options["pid-file"])
     end
-    config[:StopCallback] = Proc.new do
+    fg_thread = setup_floodgate
+  end
+
+  config[:StopCallback] = Proc.new do
+    if $options["pid-file"]
       FileUtils.rm(pid_file, :force => true)
     end
   end
@@ -234,12 +279,12 @@ def main
     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 +312,6 @@ def main
         $mutex.unlock
       end
   end
-  $logger.close
 end
 
 
@@ -277,6 +321,10 @@ if ($0 == __FILE__)
   TCPSocket.do_not_reverse_lookup = true
   Thread.abort_on_exception = $DEBUG ? true : false
 
+  begin
   LEAGUE = ShogiServer::League::new
-  main
+    main
+  rescue Exception => ex
+    log_error("main: #{ex.class}: #{ex.message}\n\t#{ex.backtrace[0]}")
+  end
 end