OSDN Git Service

Merge remote-tracking branch 'origin/master'
[shogi-server/shogi-server.git] / shogi_server.rb
1 ## $Id$
2
3 ## Copyright (C) 2004 NABEYA Kenichi (aka nanami@2ch)
4 ## Copyright (C) 2007-2012 Daigo Moriwaki (daigo at debian dot org)
5 ##
6 ## This program is free software; you can redistribute it and/or modify
7 ## it under the terms of the GNU General Public License as published by
8 ## the Free Software Foundation; either version 2 of the License, or
9 ## (at your option) any later version.
10 ##
11 ## This program is distributed in the hope that it will be useful,
12 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 ## GNU General Public License for more details.
15 ##
16 ## You should have received a copy of the GNU General Public License
17 ## along with this program; if not, write to the Free Software
18 ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19
20 require 'kconv'
21 require 'getoptlong'
22 require 'thread'
23 require 'timeout'
24 require 'socket'
25 require 'yaml'
26 require 'yaml/store'
27 require 'digest/md5'
28 require 'webrick'
29 require 'fileutils'
30 require 'logger'
31 require 'pathname'
32
33 require 'shogi_server/compatible'
34 require 'shogi_server/board'
35 require 'shogi_server/game'
36 require 'shogi_server/league'
37 require 'shogi_server/login'
38 require 'shogi_server/move'
39 require 'shogi_server/piece'
40 require 'shogi_server/player'
41 require 'shogi_server/timeout_queue'
42 require 'shogi_server/usi'
43 require 'shogi_server/util'
44 require 'shogi_server/command'
45 require 'shogi_server/buoy'
46
47 module ShogiServer # for a namespace
48
49 Default_Max_Identifier_Length = 32
50 Default_Timeout = 60            # for single socket operation
51 Default_Game_Name = "default-1500-0"
52 Default_Max_Moves = 256
53 Default_Least_Time_Per_Move = 0
54 One_Time = 10
55 Login_Time = 300                # time for LOGIN
56 Revision = "20180825"
57
58 RELOAD_FILES = ["shogi_server/league/floodgate.rb",
59                 "shogi_server/league/persistent.rb",
60                 "shogi_server/pairing.rb"]
61 BASE_DIR = File.expand_path(File.dirname(__FILE__))
62 STOP_FILE = Pathname.new(BASE_DIR).join("STOP")
63
64 def reload
65   RELOAD_FILES.each do |f|
66     load File.join(BASE_DIR, f)
67   end
68 end
69 module_function :reload
70
71 ##
72 # When the STOP file exists, starting a new game is not allowed.
73 def available?
74    return !STOP_FILE.exist?
75 end
76 module_function :available?
77
78 class Logger < ::Logger
79
80   def initialize(logdev, shift_age = 0, shift_size = 1048576)
81     super
82     class << @logdev
83       def shift_log_period(now)
84         age_file = age_file_name(now)
85         move_age_file_in_the_way(age_file)
86         unless Mkdir.mkdir_for(age_file)
87           @dev.write("[ERROR] Could not create a directory: %s\n" % [File.dirname(age_file)])
88           return true
89         end
90
91         @dev.close
92         rename_file(@filename, age_file)
93         @dev = create_logfile(@filename)
94         return true
95       end
96
97       def age_file_name(time)
98         if RUBY_VERSION >= "2.2.0"
99           postfix = previous_period_end(time, @shift_age).strftime("%Y%m%d")    # YYYYMMDD
100         else
101           postfix = previous_period_end(time).strftime("%Y%m%d")        # YYYYMMDD
102         end
103         age_file = File.join(
104                      File.dirname(@filename),
105                      postfix[0..3], # YYYY
106                      postfix[4..5], # MM
107                      postfix[6..7], # DD
108                      File.basename(@filename))
109         return age_file
110       end 
111
112       def age_file_exists?(age_file)
113         return FileTest.exist?(age_file)
114       end
115
116       def rename_file(old_file, new_file)
117         File.rename(old_file, new_file)
118       end
119
120       def move_age_file_in_the_way(age_file)
121         return unless age_file_exists?(age_file)
122         
123         now = Time.now
124         new_file = "%s.%s%06d"  % [age_file, now.strftime("%Y%m%d%H%M%S"), now.usec]
125         @dev.write("[WARN] An existing '#{age_file}' is beeing moved to '#{new_file}'\n")
126         rename_file(age_file, new_file)
127       end
128
129     end # @logdev
130   end
131
132 end # class Logger
133
134 class Formatter < ::Logger::Formatter
135   def initialize
136     super
137     @datetime_format = "%Y-%m-%dT%H:%M:%S"
138   end
139
140   def call(severity, time, progname, msg)
141     %!%s [%s] %s\n! % [format_datetime(time), severity, msg2str(msg)]
142   end
143 end
144
145 end # module ShogiServer