-
Notifications
You must be signed in to change notification settings - Fork 0
/
webserver
executable file
·180 lines (154 loc) · 4.35 KB
/
webserver
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#!/usr/bin/ruby
## Copyright (C) 2007 Daigo Moriwaki <daigo at debian dot org>
##
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
require 'getoptlong'
require 'timeout'
require 'thread'
require 'webrick'
require 'socket'
require 'fileutils'
def write_pid_file(file)
open(file, "w") do |fh|
fh.puts "#{$$}"
end
end
class ShogiClient
CLIENT_NAME = "web"
CLIENT_PASSWORD = "web1235"
def initialize(config = {})
@@mutex = Mutex.new
config[:host] ||= "localhost"
config[:port] ||= 4081
@socket = TCPSocket.open(config[:host], config[:port])
end
def login
@socket.puts "LOGIN #{CLIENT_NAME} #{CLIENT_PASSWORD} x1"
gets_ok do |s|
/OK x1/ =~ s
end
end
def logout
@socket.puts "LOGOUT"
gets_ok
end
def who
@@mutex.synchronize do
@socket.puts "%%WHO"
gets_ok
end
end
def list
@@mutex.synchronize do
@socket.puts "%%LIST"
gets_ok
end
end
private
def gets_ok
buf = ""
timeout(5) do
loop do
s = @socket.gets
break unless s
buf << s
if block_given?
break if yield(s)
else
break if /OK$/ =~ s
end
end
end
return buf
rescue Timeout::Error
return buf
end
end
class ListServlet < WEBrick::HTTPServlet::AbstractServlet
def do_GET(req, res)
res.body = $client.list
res['Content-Type'] = "text/plain"
end
end
class WhoServlet < WEBrick::HTTPServlet::AbstractServlet
def do_GET(req, res)
res.body = $client.who
res['Content-Type'] = "text/plain"
end
end
def usage
$stderr.puts <<-EOF
USAGE: #{$0} [--daemon dir] [--port port] [--pid-file file]
--daemon dir Run as a daemon. Log files are put in dir
--port port Listening port for HTTP server (default 4080)
--pid-file file Write the process id to the file
EOF
end
def parse_command_line
options = Hash::new
parser = GetoptLong.new( ["--daemon", GetoptLong::REQUIRED_ARGUMENT],
["--port", GetoptLong::REQUIRED_ARGUMENT],
["--pid-file", GetoptLong::REQUIRED_ARGUMENT])
parser.quiet = true
begin
parser.each_option do |name, arg|
name.sub!(/^--/, '')
options[name] = arg.dup
end
rescue
usage
raise parser.error_message
end
dir = options["daemon"]
dir = File.expand_path(dir) if dir
if dir && ! File.exist?(dir)
FileUtils.mkdir(dir)
end
options["dir"] = dir || File.dirname(__FILE__)
options["port"] ||= 4080
options["port"] = options["port"].to_i
options["pid-file"] = File.expand_path(options["pid-file"]) if options["pid-file"]
return options
end
def main
$options = parse_command_line
$client = ShogiClient.new
$client.login
http_log_file = File.join($options["dir"], "shogi-server-httpd.log")
http_access_log_file = File.join($options["dir"], "shogi-server-access.log")
http_config = {}
http_config[:Port] = $options["port"]
http_config[:ServerType] = WEBrick::Daemon if $options["daemon"]
http_config[:Logger] = WEBrick::Log.new(http_log_file)
http_config[:AccessLog] =
[[ WEBrick::Log.new(http_access_log_file), WEBrick::AccessLog::COMMON_LOG_FORMAT ]]
if $options["pid-file"]
http_config[:StartCallback] = Proc.new do
write_pid_file($options["pid-file"])
end
http_config[:StopCallback] = Proc.new do
FileUtils.rm($options["pid-file"], :force => true)
end
end
server = WEBrick::HTTPServer.new(http_config)
["INT", "TERM"].each {|signal| trap(signal){ server.shutdown; $client.logout } }
server.mount("/list", ListServlet)
server.mount("/who", WhoServlet)
server.start
end
if __FILE__ == $0
TCPSocket.do_not_reverse_lookup = true
main
end