Class: Knjappserver::Httpserver
- Inherits:
-
Object
- Object
- Knjappserver::Httpserver
- Defined in:
- lib/include/class_httpserver.rb
Instance Attribute Summary collapse
-
#http_sessions ⇒ Object
readonly
Returns the value of attribute http_sessions.
-
#kas ⇒ Object
readonly
Returns the value of attribute kas.
-
#server ⇒ Object
readonly
Returns the value of attribute server.
-
#thread_accept ⇒ Object
readonly
Returns the value of attribute thread_accept.
-
#thread_restart ⇒ Object
readonly
Returns the value of attribute thread_restart.
-
#working_count ⇒ Object
Returns the value of attribute working_count.
Instance Method Summary collapse
- #count_add ⇒ Object
- #count_remove ⇒ Object
-
#initialize(kas) ⇒ Httpserver
constructor
A new instance of Httpserver.
- #spawn_httpsession(socket) ⇒ Object
- #start ⇒ Object
- #stop ⇒ Object
Constructor Details
#initialize(kas) ⇒ Httpserver
Returns a new instance of Httpserver.
5 6 7 8 9 |
# File 'lib/include/class_httpserver.rb', line 5 def initialize(kas) @kas = kas @debug = @kas.config[:debug] @mutex_count = Mutex.new end |
Instance Attribute Details
#http_sessions ⇒ Object (readonly)
Returns the value of attribute http_sessions.
3 4 5 |
# File 'lib/include/class_httpserver.rb', line 3 def http_sessions @http_sessions end |
#kas ⇒ Object (readonly)
Returns the value of attribute kas.
3 4 5 |
# File 'lib/include/class_httpserver.rb', line 3 def kas @kas end |
#server ⇒ Object (readonly)
Returns the value of attribute server.
3 4 5 |
# File 'lib/include/class_httpserver.rb', line 3 def server @server end |
#thread_accept ⇒ Object (readonly)
Returns the value of attribute thread_accept.
3 4 5 |
# File 'lib/include/class_httpserver.rb', line 3 def thread_accept @thread_accept end |
#thread_restart ⇒ Object (readonly)
Returns the value of attribute thread_restart.
3 4 5 |
# File 'lib/include/class_httpserver.rb', line 3 def thread_restart @thread_restart end |
#working_count ⇒ Object
Returns the value of attribute working_count.
2 3 4 |
# File 'lib/include/class_httpserver.rb', line 2 def working_count @working_count end |
Instance Method Details
#count_add ⇒ Object
112 113 114 115 116 |
# File 'lib/include/class_httpserver.rb', line 112 def count_add @mutex_count.synchronize do @working_count += 1 if @working_count end end |
#count_remove ⇒ Object
118 119 120 121 122 |
# File 'lib/include/class_httpserver.rb', line 118 def count_remove @mutex_count.synchronize do @working_count -= 1 if @working_count end end |
#spawn_httpsession(socket) ⇒ Object
108 109 110 |
# File 'lib/include/class_httpserver.rb', line 108 def spawn_httpsession(socket) @http_sessions << Knjappserver::Httpsession.new(self, socket) end |
#start ⇒ Object
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 |
# File 'lib/include/class_httpserver.rb', line 11 def start @http_sessions = [] @working_count = 0 raise "No host was given." if @kas and !@kas.config.has_key?(:host) raise "No port was given." if @kas and !@kas.config.has_key?(:port) @server = TCPServer.new(@kas.config[:host], @kas.config[:port]) @thread_accept = Thread.new do begin loop do if !@server or @server.closed? sleep 1 next end begin self.spawn_httpsession(@server.accept) STDOUT.print "Starting new HTTP-request.\n" if @debug rescue => e if @debug STDOUT.puts e.inspect STDOUT.puts e.backtrace STDOUT.print "\n" STDOUT.print "Could not accept HTTP-request - waiting 1 sec and then trying again.\n" end sleep 1 end end rescue => e STDOUT.print Knj::Errors.error_str(e) end end @thread_restart = Thread.new do begin loop do sleep 10 break if @kas.should_restart and @kas.should_restart_done if !@kas.should_restart and (!@server or @server.closed?) STDOUT.print "Socket does not exist or is closed - restarting HTTP-server!\n" @server = TCPServer.new(@kas.config[:host], @kas.config[:port]) STDOUT.print "Done.\n" end end rescue => e if @kas @kas.handle_error(e) else STDOUT.print Knj::Errors.error_str(e) end end end end |
#stop ⇒ Object
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 |
# File 'lib/include/class_httpserver.rb', line 68 def stop begin STDOUT.print "Stopping accept-thread.\n" if @debug @thread_accept.kill if @thread_accept and @thread_accept.alive? @thread_restart.kill if @thread_restart and @thread_restart.alive? rescue => e STDOUT.print "Could not stop threads.\n" if @debug STDOUT.puts e.inspect STDOUT.puts e.backtrace end STDOUT.print "Stopping all HTTP sessions.\n" if @debug if @http_sessions @http_sessions.each do |httpsession| httpsession.destruct end end begin STDOUT.print "Stopping TCPServer.\n" if @debug @server.close if @server and !@server.closed? STDOUT.print "TCPServer was closed.\n" if @debug rescue Timeout::Error raise "Could not close TCPserver.\n" rescue IOError => e if e. == "closed stream" #ignore - it should be closed. else raise e end end @http_sessions = nil @thread_accept = nil @thread_restart = nil @server = nil @working_count = nil @kas = nil end |