ファイル情報

Rev. 2d5cda5e9dde331e37d58133b688a8b2c6295345
サイズ 4,503 バイト
日時 2017-09-03 14:40:39
作者 Daigo Moriwaki
ログメッセージ

Merge remote-tracking branch 'origin/master' into wdoor-stable

内容

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