3 ## Copyright (C) 2007 Daigo Moriwaki <daigo at debian dot org>
5 ## This program is free software; you can redistribute it and/or modify
6 ## it under the terms of the GNU General Public License as published by
7 ## the Free Software Foundation; either version 2 of the License, or
8 ## (at your option) any later version.
10 ## This program is distributed in the hope that it will be useful,
11 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ## GNU General Public License for more details.
15 ## You should have received a copy of the GNU General Public License
16 ## along with this program; if not, write to the Free Software
17 ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 def write_pid_file(file)
27 open(file, "w") do |fh|
34 CLIENT_PASSWORD = "web1235"
36 def initialize(config = {})
38 config[:host] ||= "localhost"
39 config[:port] ||= 4081
40 @socket = TCPSocket.open(config[:host], config[:port])
44 @socket.puts "LOGIN #{CLIENT_NAME} #{CLIENT_PASSWORD} x1"
56 @@mutex.synchronize do
63 @@mutex.synchronize do
92 class ListServlet < WEBrick::HTTPServlet::AbstractServlet
94 res.body = $client.list
95 res['Content-Type'] = "text/plain"
99 class WhoServlet < WEBrick::HTTPServlet::AbstractServlet
101 res.body = $client.who
102 res['Content-Type'] = "text/plain"
108 USAGE: #{$0} [--daemon dir] [--port port] [--pid-file file]
109 --daemon dir Run as a daemon. Log files are put in dir
110 --port port Listening port for HTTP server (default 4080)
111 --pid-file file Write the process id to the file
115 def parse_command_line
117 parser = GetoptLong.new( ["--daemon", GetoptLong::REQUIRED_ARGUMENT],
118 ["--port", GetoptLong::REQUIRED_ARGUMENT],
119 ["--pid-file", GetoptLong::REQUIRED_ARGUMENT])
122 parser.each_option do |name, arg|
124 options[name] = arg.dup
128 raise parser.error_message
131 dir = options["daemon"]
132 dir = File.expand_path(dir) if dir
133 if dir && ! File.exist?(dir)
136 options["dir"] = dir || File.dirname(__FILE__)
137 options["port"] ||= 4080
138 options["port"] = options["port"].to_i
139 options["pid-file"] = File.expand_path(options["pid-file"]) if options["pid-file"]
146 $options = parse_command_line
148 $client = ShogiClient.new
151 http_log_file = File.join($options["dir"], "shogi-server-httpd.log")
152 http_access_log_file = File.join($options["dir"], "shogi-server-access.log")
154 http_config[:Port] = $options["port"]
155 http_config[:ServerType] = WEBrick::Daemon if $options["daemon"]
156 http_config[:Logger] = WEBrick::Log.new(http_log_file)
157 http_config[:AccessLog] =
158 [[ WEBrick::Log.new(http_access_log_file), WEBrick::AccessLog::COMMON_LOG_FORMAT ]]
159 if $options["pid-file"]
160 http_config[:StartCallback] = Proc.new do
161 write_pid_file($options["pid-file"])
163 http_config[:StopCallback] = Proc.new do
164 FileUtils.rm($options["pid-file"], :force => true)
170 server = WEBrick::HTTPServer.new(http_config)
171 ["INT", "TERM"].each {|signal| trap(signal){ server.shutdown; $client.logout } }
172 server.mount("/list", ListServlet)
173 server.mount("/who", WhoServlet)
178 TCPSocket.do_not_reverse_lookup = true