Class: Hayabusa::Http_server
- Inherits:
-
Object
- Object
- Hayabusa::Http_server
- Defined in:
- lib/hayabusa_http_server.rb
Overview
This class opens a port to run the HTTP-server on. It then spawns “Hayabusa::Http_session” for each active connection.
Instance Attribute Summary collapse
-
#hb ⇒ Object
readonly
Returns the value of attribute hb.
-
#http_sessions ⇒ Object
readonly
Returns the value of attribute http_sessions.
-
#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_block ⇒ Object
Increases and decreases the ‘working_count’-variable to keep track of how many HTTP-sessions are currently processing pages (used for gentle stops).
-
#initialize(hb) ⇒ Http_server
constructor
A new instance of Http_server.
-
#spawn_httpsession(socket) ⇒ Object
Spawns a new HTTP-session with the given socket.
-
#start ⇒ Object
Opens a port with TCPServer and spins up a thread to accept connections.
-
#stop ⇒ Object
Gently stops the HTTP-server.
Constructor Details
#initialize(hb) ⇒ Http_server
Returns a new instance of Http_server.
8 9 10 11 12 |
# File 'lib/hayabusa_http_server.rb', line 8 def initialize(hb) @hb = hb @debug = @hb.config[:debug] @mutex_count = Mutex.new end |
Instance Attribute Details
#hb ⇒ Object (readonly)
Returns the value of attribute hb.
6 7 8 |
# File 'lib/hayabusa_http_server.rb', line 6 def hb @hb end |
#http_sessions ⇒ Object (readonly)
Returns the value of attribute http_sessions.
6 7 8 |
# File 'lib/hayabusa_http_server.rb', line 6 def http_sessions @http_sessions end |
#server ⇒ Object (readonly)
Returns the value of attribute server.
6 7 8 |
# File 'lib/hayabusa_http_server.rb', line 6 def server @server end |
#thread_accept ⇒ Object (readonly)
Returns the value of attribute thread_accept.
6 7 8 |
# File 'lib/hayabusa_http_server.rb', line 6 def thread_accept @thread_accept end |
#thread_restart ⇒ Object (readonly)
Returns the value of attribute thread_restart.
6 7 8 |
# File 'lib/hayabusa_http_server.rb', line 6 def thread_restart @thread_restart end |
#working_count ⇒ Object
Returns the value of attribute working_count.
5 6 7 |
# File 'lib/hayabusa_http_server.rb', line 5 def working_count @working_count end |
Instance Method Details
#count_block ⇒ Object
Increases and decreases the ‘working_count’-variable to keep track of how many HTTP-sessions are currently processing pages (used for gentle stops).
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/hayabusa_http_server.rb', line 96 def count_block begin added = false @mutex_count.synchronize do @working_count += 1 if @working_count != nil added = true end yield ensure @hb.served += 1 if @hb @mutex_count.synchronize do @working_count -= 1 if @working_count != nil and added end end end |
#spawn_httpsession(socket) ⇒ Object
Spawns a new HTTP-session with the given socket.
90 91 92 93 |
# File 'lib/hayabusa_http_server.rb', line 90 def spawn_httpsession(socket) @hb.log_puts("Starting new HTTP-session.") if @debug @http_sessions << Hayabusa::Http_session.new(self, socket) end |
#start ⇒ Object
Opens a port with TCPServer and spins up a thread to accept connections.
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 |
# File 'lib/hayabusa_http_server.rb', line 15 def start @http_sessions = [] @working_count = 0 raise "No host was given." if @hb and !@hb.config.has_key?(:host) raise "No port was given." if @hb and !@hb.config.has_key?(:port) @server = TCPServer.new(@hb.config[:host], @hb.config[:port]) @thread_accept = Thread.new do loop do begin if !@server or @server.closed? @hb.log_puts "Starting TCPServer." if @debug @server = TCPServer.new(@hb.config[:host], @hb.config[:port]) end @hb.log_puts "Trying to spawn new HTTP-session from socket-accept." if @debug self.spawn_httpsession(@server.accept) @hb.log_puts "Starting new HTTP-request." if @debug rescue Exception => e if @debug @hb.log_puts Knj::Errors.error_str(e) @hb.log_puts "Could not accept HTTP-request - waiting 1 sec and then trying again." end raise e if e.is_a?(SystemExit) or e.is_a?(Interrupt) sleep 1 end end end end |
#stop ⇒ Object
Gently stops the HTTP-server. Will wait for various HTTP-sessions to be finish with a page (but wont wait for them to disconnect in regards to keep-alive).
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 |
# File 'lib/hayabusa_http_server.rb', line 49 def stop while @working_count > 0 @hb.log_puts "Waiting until no HTTP-sessions are running." if @debug sleep 0.1 end @hb.log_puts "Stopping accept-thread." if @debug @thread_accept.kill if @thread_accept and @thread_accept.alive? @thread_restart.kill if @thread_restart and @thread_restart.alive? #@hb.log_puts "Stopping all HTTP sessions." if @debug #if @http_sessions # @http_sessions.each do |httpsession| # httpsession.destruct # end #end begin @hb.log_puts "Stopping TCPServer." if @debug @server.close if @server and !@server.closed? @hb.log_puts "TCPServer was closed." if @debug rescue Timeout::Error raise "Could not close TCPserver." 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 @hb = nil end |