OSDN Git Service

Not have milliseconds logged.
[shogi-server/shogi-server.git] / shogi-server
index 2d90ff8..771936d 100755 (executable)
 ## 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'
-require 'shogi_server/board' # not autoloaded
-require 'shogi_server/player'
-require 'shogi_server/game'
 
 #################################################
 # MAIN
 #
 
+ShogiServer.reload
+
 def gets_safe(socket, timeout=nil)
   if r = select([socket], nil, nil, timeout)
     return r[0].first.gets
@@ -187,7 +187,7 @@ def setup_watchdog_for_giant_lock
 end
 
 def setup_floodgate
-  Thread.start do 
+  return Thread.start do 
     Thread.pass
     floodgate = ShogiServer::League::Floodgate.new(LEAGUE)
     log_message("Flooddgate reloaded. The next match will start at %s." % 
@@ -195,18 +195,22 @@ def setup_floodgate
 
     while (true)
       begin
-        sleep(10)
-        next if Time.now < floodgate.next_time
+        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
-          Dependencies.clear
+          log_message("Reloading source...")
+          ShogiServer.reload
         end
         floodgate = ShogiServer::League::Floodgate.new(LEAGUE, next_time)
-        log_message("Flooddgate reloaded. The next match will start at %s." % 
-                [floodgate.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}")
@@ -217,12 +221,6 @@ end
 
 def main
   
-  [ShogiServer::League::Floodgate, ShogiServer::Pairing].each do |klass|
-    Dependencies.unloadable klass
-  end
-
-  setup_watchdog_for_giant_lock
-
   $options = parse_command_line
   if (ARGV.length != 2)
     usage
@@ -236,6 +234,9 @@ def main
     usage
     exit 3
   end
+  if $options["pid-file"] 
+    $options["pid-file"] = File.expand_path($options["pid-file"])
+  end
 
   LEAGUE.event = ARGV.shift
   port = ARGV.shift
@@ -249,39 +250,39 @@ 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
 
+  fg_thread = nil
+
   config[:StartCallback] = Proc.new do
     if $options["pid-file"]
       write_pid_file($options["pid-file"])
     end
-    setup_floodgate
+    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(pid_file, :force => true)
+      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
-      # TODO Shutdown Floodgate's thread
+      fg_thread.kill if fg_thread
     end
   end
   trap("HUP") do
-    LEAGUE.shutdown
     Dependencies.clear
-    # TODO Restart Floodgate's thread
   end
   $stderr.puts("server started as a deamon [Revision: #{ShogiServer::Revision}]") if $options["daemon"] 
   log_message("server started [Revision: #{ShogiServer::Revision}]")
@@ -319,7 +320,7 @@ if ($0 == __FILE__)
   Thread.abort_on_exception = $DEBUG ? true : false
 
   begin
-  LEAGUE = ShogiServer::League::new
+    LEAGUE = ShogiServer::League.new(TOP_DIR)
     main
   rescue Exception => ex
     log_error("main: #{ex.class}: #{ex.message}\n\t#{ex.backtrace[0]}")