1 ## Copyright (C) 2007 Daigo Moriwaki <daigo at debian dot org>
3 ## This program is free software; you can redistribute it and/or modify
4 ## it under the terms of the GNU General Public License as published by
5 ## the Free Software Foundation; either version 2 of the License, or
6 ## (at your option) any later version.
8 ## This program is distributed in the hope that it will be useful,
9 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
10 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 ## GNU General Public License for more details.
13 ## You should have received a copy of the GNU General Public License
14 ## along with this program; if not, write to the Free Software
15 ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 CLIENT_PASSWORD = "web1235"
27 def initialize(config = {})
29 config[:host] ||= "localhost"
30 config[:port] ||= 4081
31 @socket = TCPSocket.open(config[:host], config[:port])
35 @socket.puts "LOGIN #{CLIENT_NAME} #{CLIENT_PASSWORD} x1"
47 @@mutex.synchronize do
54 @@mutex.synchronize do
83 class ListServlet < WEBrick::HTTPServlet::AbstractServlet
85 res.body = $client.list
86 res['Content-Type'] = "text/plain"
90 class WhoServlet < WEBrick::HTTPServlet::AbstractServlet
92 res.body = $client.who
93 res['Content-Type'] = "text/plain"
99 USAGE: #{$0} [--daemon dir] [--port port]
100 --daemon dir Run as a daemon. Log files are put in dir
101 --port port Listening port for HTTP server (default 4080)
105 def parse_command_line
107 parser = GetoptLong.new( ["--daemon", GetoptLong::REQUIRED_ARGUMENT],
108 ["--port", GetoptLong::REQUIRED_ARGUMENT])
111 parser.each_option do |name, arg|
113 options[name] = arg.dup
117 raise parser.error_message
120 dir = options["daemon"] || nil
121 if dir && ! File.exist?(dir)
124 options["dir"] = dir || File.dirname(__FILE__)
126 options["port"] ||= 4080
127 options["port"] = options["port"].to_i
134 $options = parse_command_line
136 $client = ShogiClient.new
139 http_log_file = File.join($options["dir"], "shogi-server-httpd.log")
140 http_access_log_file = File.join($options["dir"], "shogi-server-access.log")
142 http_config[:Port] = $options["port"]
143 http_config[:ServerType] = WEBrick::Daemon if $options["daemon"]
144 http_config[:Logger] = WEBrick::Log.new(http_log_file)
145 http_config[:AccessLog] =
146 [[ WEBrick::Log.new(http_access_log_file), WEBrick::AccessLog::COMMON_LOG_FORMAT ]]
149 server = WEBrick::HTTPServer.new(http_config)
150 ["INT", "TERM"].each {|signal| trap(signal){ server.shutdown; $client.logout } }
151 server.mount("/list", ListServlet)
152 server.mount("/who", WhoServlet)
157 TCPSocket.do_not_reverse_lookup = true