Class: Knjappserver::Httpserver

Inherits:
Object
  • Object
show all
Defined in:
lib/include/class_httpserver.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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_sessionsObject (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

#kasObject (readonly)

Returns the value of attribute kas.



3
4
5
# File 'lib/include/class_httpserver.rb', line 3

def kas
  @kas
end

#serverObject (readonly)

Returns the value of attribute server.



3
4
5
# File 'lib/include/class_httpserver.rb', line 3

def server
  @server
end

#thread_acceptObject (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_restartObject (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_countObject

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_addObject



85
86
87
88
89
# File 'lib/include/class_httpserver.rb', line 85

def count_add
   @mutex_count.synchronize do
     @working_count += 1 if @working_count
   end
end

#count_removeObject



91
92
93
94
95
# File 'lib/include/class_httpserver.rb', line 91

def count_remove
   @mutex_count.synchronize do
     @working_count -= 1 if @working_count
   end
end

#spawn_httpsession(socket) ⇒ Object



81
82
83
# File 'lib/include/class_httpserver.rb', line 81

def spawn_httpsession(socket)
   @http_sessions << Knjappserver::Httpsession.new(self, socket)
end

#startObject



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
# 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
end

#stopObject



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
# File 'lib/include/class_httpserver.rb', line 47

def stop
   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?
   
   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.message == "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