OSDN Git Service

Refactoring. Pretty output.
[shogi-server/shogi-server.git] / shogi-server
index be06e55..0cde3d2 100755 (executable)
@@ -17,6 +17,8 @@
 ## along with this program; if not, write to the Free Software
 ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
 ## along with this program; if not, write to the Free Software
 ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
+module ShogiServer # for a namespace
+
 Max_Write_Queue_Size = 1000
 Max_Identifier_Length = 32
 Default_Timeout = 60            # for single socket operation
 Max_Write_Queue_Size = 1000
 Max_Identifier_Length = 32
 Default_Timeout = 60            # for single socket operation
@@ -31,9 +33,6 @@ Release = "$Name$".split[1].sub(/\A[^\d]*/, '').gsub(/_/, '.')
 Release.concat("-") if (Release == "")
 Revision = "$Revision$".gsub(/[^\.\d]/, '')
 
 Release.concat("-") if (Release == "")
 Revision = "$Revision$".gsub(/[^\.\d]/, '')
 
-STDOUT.sync = true
-STDERR.sync = true
-
 require 'getoptlong'
 require 'thread'
 require 'timeout'
 require 'getoptlong'
 require 'thread'
 require 'timeout'
@@ -42,8 +41,6 @@ require 'yaml'
 require 'yaml/store'
 require 'digest/md5'
 
 require 'yaml/store'
 require 'digest/md5'
 
-TCPSocket.do_not_reverse_lookup = true
-Thread.abort_on_exception = true
 
 
 class TCPSocket
 
 
 class TCPSocket
@@ -156,70 +153,9 @@ class League
 end
 
 
 end
 
 
-class BasicPlayer
-  # Idetifier of the player in the rating system
-  attr_accessor :id
-
-  # Name of the player
-  attr_accessor :name
-  
-  # Password of the player, which does not include a trip
-  attr_accessor :password
-
-  # Score in the rating sysem
-  attr_accessor :rate
-
-  # Group in the rating system
-  attr_accessor :rating_group
-  
-  # Last timestamp when the rate was modified
-  attr_accessor :modified_at
-
-
-
-  def initialize
-    @name = nil
-    @password = nil
-  end
-
-  def modified_at
-    @modified_at || Time.now
-  end
-
-  def rate=(new_rate)
-    if @rate != new_rate
-      @rate = new_rate
-      @modified_at = Time.now
-    end
-  end
-
-  def rated?
-    @id != nil
-  end
-
-  def simple_id
-    if @trip
-      simple_name = @name.gsub(/@.*?$/, '')
-      "%s+%s" % [simple_name, @trip[0..8]]
-    else
-      @name
-    end
-  end
-
-  ##
-  # Parses str in the LOGIN command, sets up @id and @trip
-  #
-  def set_password(str)
-    if str && !str.empty?
-      @password = str.strip
-      @id   = "%s+%s" % [@name, Digest::MD5.hexdigest(@password)]
-    else
-      @id = @password = nil
-    end
-  end
-end
-
-
+######################################################
+# Processes the LOGIN command.
+#
 class Login
   def Login.good_login?(str)
     tokens = str.split
 class Login
   def Login.good_login?(str)
     tokens = str.split
@@ -257,7 +193,11 @@ class Login
     end
   end
 
     end
   end
 
-  attr_reader :player, :csa_1st_str
+  attr_reader :player
+  
+  # the first command that will be executed just after LOGIN.
+  # If it is nil, the default process will be started.
+  attr_reader :csa_1st_str
 
   def initialize(player, password)
     @player = player
 
   def initialize(player, password)
     @player = player
@@ -267,6 +207,7 @@ class Login
 
   def process
     @player.write_safe(sprintf("LOGIN:%s OK\n", @player.name))
 
   def process
     @player.write_safe(sprintf("LOGIN:%s OK\n", @player.name))
+    log_message(sprintf("user %s run in %s mode", @player.name, @player.protocol))
   end
 
   def incorrect_duplicated_player(str)
   end
 
   def incorrect_duplicated_player(str)
@@ -278,6 +219,9 @@ class Login
   end
 end
 
   end
 end
 
+######################################################
+# Processes LOGIN for the CSA standard mode.
+#
 class LoginCSA < Login
   PROTOCOL = "CSA"
 
 class LoginCSA < Login
   PROTOCOL = "CSA"
 
@@ -303,11 +247,13 @@ class LoginCSA < Login
 
   def process
     super
 
   def process
     super
-    log_message(sprintf("user %s run in CSA mode", @player.name))
     @csa_1st_str = "%%GAME #{@gamename} *"
   end
 end
 
     @csa_1st_str = "%%GAME #{@gamename} *"
   end
 end
 
+######################################################
+# Processes LOGIN for the extented mode.
+#
 class Loginx1 < Login
   PROTOCOL = "x1"
 
 class Loginx1 < Login
   PROTOCOL = "x1"
 
@@ -322,12 +268,73 @@ class Loginx1 < Login
 
   def process
     super
 
   def process
     super
-    log_message(sprintf("user %s run in %s mode", @player.name, PROTOCOL))
     @player.write_safe(sprintf("##[LOGIN] +OK %s\n", PROTOCOL))
   end
 end
 
 
     @player.write_safe(sprintf("##[LOGIN] +OK %s\n", PROTOCOL))
   end
 end
 
 
+class BasicPlayer
+  # Idetifier of the player in the rating system
+  attr_accessor :id
+
+  # Name of the player
+  attr_accessor :name
+  
+  # Password of the player, which does not include a trip
+  attr_accessor :password
+
+  # Score in the rating sysem
+  attr_accessor :rate
+  
+  # Group in the rating system
+  attr_accessor :rating_group
+
+  # Last timestamp when the rate was modified
+  attr_accessor :modified_at
+
+  def initialize
+    @name = nil
+    @password = nil
+  end
+
+  def modified_at
+    @modified_at || Time.now
+  end
+
+  def rate=(new_rate)
+    if @rate != new_rate
+      @rate = new_rate
+      @modified_at = Time.now
+    end
+  end
+
+  def rated?
+    @id != nil
+  end
+
+  def simple_id
+    if @trip
+      simple_name = @name.gsub(/@.*?$/, '')
+      "%s+%s" % [simple_name, @trip[0..8]]
+    else
+      @name
+    end
+  end
+
+  ##
+  # Parses str in the LOGIN command, sets up @id and @trip
+  #
+  def set_password(str)
+    if str && !str.empty?
+      @password = str.strip
+      @id   = "%s+%s" % [@name, Digest::MD5.hexdigest(@password)]
+    else
+      @id = @password = nil
+    end
+  end
+end
+
+
 class Player < BasicPlayer
   def initialize(str, socket)
     super()
 class Player < BasicPlayer
   def initialize(str, socket)
     super()
@@ -1961,7 +1968,7 @@ def parse_command_line
   return options
 end
 
   return options
 end
 
-def  write_pid_file(file)
+def write_pid_file(file)
   open(file, "w") do |fh|
     fh.print Process::pid, "\n"
   end
   open(file, "w") do |fh|
     fh.print Process::pid, "\n"
   end
@@ -1986,6 +1993,7 @@ def mutex_watchdog(mutex, sec)
 end
 
 def main
 end
 
 def main
+
   $mutex = Mutex::new
   Thread::start do
     Thread.pass
   $mutex = Mutex::new
   Thread::start do
     Thread.pass
@@ -2064,8 +2072,16 @@ def main
     end
   end
 end
     end
   end
 end
+module_function :main
+
+end # module ShogiServer
 
 if ($0 == __FILE__)
 
 if ($0 == __FILE__)
-  LEAGUE = League::new
-  main
+  STDOUT.sync = true
+  STDERR.sync = true
+  TCPSocket.do_not_reverse_lookup = true
+  Thread.abort_on_exception = true
+  
+  LEAGUE = ShogiServer::League::new
+  ShogiServer::main
 end
 end