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
27 CLIENT_PASSWORD = "web1235"
29 def initialize(config = {})
31 config[:host] ||= "localhost"
32 config[:port] ||= 4081
33 @socket = TCPSocket.open(config[:host], config[:port])
37 @socket.puts "LOGIN #{CLIENT_NAME} #{CLIENT_PASSWORD} x1"
49 @@mutex.synchronize do
56 @@mutex.synchronize do
85 class ListServlet < WEBrick::HTTPServlet::AbstractServlet
87 res.body = $client.list
88 res['Content-Type'] = "text/plain"
92 class WhoServlet < WEBrick::HTTPServlet::AbstractServlet
94 res.body = $client.who
95 res['Content-Type'] = "text/plain"
101 USAGE: #{$0} [--daemon dir] [--port port]
102 --daemon dir Run as a daemon. Log files are put in dir
103 --port port Listening port for HTTP server (default 4080)
107 def parse_command_line
109 parser = GetoptLong.new( ["--daemon", GetoptLong::REQUIRED_ARGUMENT],
110 ["--port", GetoptLong::REQUIRED_ARGUMENT])
113 parser.each_option do |name, arg|
115 options[name] = arg.dup
119 raise parser.error_message
122 dir = options["daemon"] || nil
123 if dir && ! File.exist?(dir)
126 options["dir"] = dir || File.dirname(__FILE__)
128 options["port"] ||= 4080
129 options["port"] = options["port"].to_i
136 $options = parse_command_line
138 $client = ShogiClient.new
141 http_log_file = File.join($options["dir"], "shogi-server-httpd.log")
142 http_access_log_file = File.join($options["dir"], "shogi-server-access.log")
144 http_config[:Port] = $options["port"]
145 http_config[:ServerType] = WEBrick::Daemon if $options["daemon"]
146 http_config[:Logger] = WEBrick::Log.new(http_log_file)
147 http_config[:AccessLog] =
148 [[ WEBrick::Log.new(http_access_log_file), WEBrick::AccessLog::COMMON_LOG_FORMAT ]]
151 server = WEBrick::HTTPServer.new(http_config)
152 ["INT", "TERM"].each {|signal| trap(signal){ server.shutdown; $client.logout } }
153 server.mount("/list", ListServlet)
154 server.mount("/who", WhoServlet)
159 TCPSocket.do_not_reverse_lookup = true