Class: Knjappserver::Httpsession::Contentgroup
- Inherits:
-
Object
- Object
- Knjappserver::Httpsession::Contentgroup
- Defined in:
- lib/include/class_httpsession_contentgroup.rb
Overview
This class handels the adding of content and writing to socket. Since this can be done with multiple threads and multiple IO’s it can get complicated.
Constant Summary collapse
- NL =
"\r\n"
Instance Attribute Summary collapse
-
#chunked ⇒ Object
Returns the value of attribute chunked.
-
#cur_data ⇒ Object
readonly
Returns the value of attribute cur_data.
-
#done ⇒ Object
readonly
Returns the value of attribute done.
-
#socket ⇒ Object
Returns the value of attribute socket.
Instance Method Summary collapse
-
#force_content(newcont) ⇒ Object
Forces the content to be the input - nothing else can be added after calling this.
- #init ⇒ Object
-
#initialize(args = {}) ⇒ Contentgroup
constructor
A new instance of Contentgroup.
- #join ⇒ Object
- #mark_done ⇒ Object
- #new_io(obj = "") ⇒ Object
- #new_thread ⇒ Object
- #register_thread ⇒ Object
- #reset ⇒ Object
- #write(cont) ⇒ Object
- #write_begin ⇒ Object
- #write_force ⇒ Object
- #write_output ⇒ Object
- #write_to_socket ⇒ Object
Constructor Details
#initialize(args = {}) ⇒ Contentgroup
Returns a new instance of Contentgroup.
9 10 11 12 13 14 15 16 |
# File 'lib/include/class_httpsession_contentgroup.rb', line 9 def initialize(args = {}) @socket = args[:socket] @chunked = args[:chunked] @resp = args[:resp] @httpsession = args[:httpsession] @mutex = Mutex.new @debug = false end |
Instance Attribute Details
#chunked ⇒ Object
Returns the value of attribute chunked.
6 7 8 |
# File 'lib/include/class_httpsession_contentgroup.rb', line 6 def chunked @chunked end |
#cur_data ⇒ Object (readonly)
Returns the value of attribute cur_data.
5 6 7 |
# File 'lib/include/class_httpsession_contentgroup.rb', line 5 def cur_data @cur_data end |
#done ⇒ Object (readonly)
Returns the value of attribute done.
5 6 7 |
# File 'lib/include/class_httpsession_contentgroup.rb', line 5 def done @done end |
#socket ⇒ Object
Returns the value of attribute socket.
6 7 8 |
# File 'lib/include/class_httpsession_contentgroup.rb', line 6 def socket @socket end |
Instance Method Details
#force_content(newcont) ⇒ Object
Forces the content to be the input - nothing else can be added after calling this.
48 49 50 |
# File 'lib/include/class_httpsession_contentgroup.rb', line 48 def force_content(newcont) @ios = [{:str => newcont, :done => true}] end |
#init ⇒ Object
18 19 20 21 22 23 24 25 26 |
# File 'lib/include/class_httpsession_contentgroup.rb', line 18 def init @done = false @thread = nil @cur_data = { :str => "", :done => false } @ios = [@cur_data] end |
#join ⇒ Object
111 112 113 114 115 |
# File 'lib/include/class_httpsession_contentgroup.rb', line 111 def join return nil if @forced sleep 0.1 while !@thread @thread.join end |
#mark_done ⇒ Object
106 107 108 109 |
# File 'lib/include/class_httpsession_contentgroup.rb', line 106 def mark_done @cur_data[:done] = true @done = true end |
#new_io(obj = "") ⇒ Object
41 42 43 44 45 |
# File 'lib/include/class_httpsession_contentgroup.rb', line 41 def new_io(obj = "") @cur_data[:done] = true if @cur_data @cur_data = {:str => obj, :done => false} @ios << @cur_data end |
#new_thread ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/include/class_httpsession_contentgroup.rb', line 57 def new_thread cgroup = Knjappserver::Httpsession::Contentgroup.new(:socket => @socket, :chunked => @chunked) cgroup.init @mutex.synchronize do @ios << cgroup self.new_io end self.register_thread return cgroup end |
#register_thread ⇒ Object
52 53 54 55 |
# File 'lib/include/class_httpsession_contentgroup.rb', line 52 def register_thread Thread.current[:knjappserver] = {} if !Thread.current[:knjappserver] Thread.current[:knjappserver][:contentgroup] = self end |
#reset ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/include/class_httpsession_contentgroup.rb', line 28 def reset @ios = [] @done = false @thread = nil @forced = false @mutex.synchronize do self.new_io end self.register_thread end |
#write(cont) ⇒ Object
78 79 80 81 82 |
# File 'lib/include/class_httpsession_contentgroup.rb', line 78 def write(cont) @mutex.synchronize do @cur_data[:str] << cont end end |
#write_begin ⇒ Object
70 71 72 73 74 75 76 |
# File 'lib/include/class_httpsession_contentgroup.rb', line 70 def write_begin begin @resp.write if @httpsession.["METHOD"] != "HEAD" rescue Errno::ECONNRESET, Errno::ENOTCONN, Errno::EPIPE #Ignore - the user probaly left. end end |
#write_force ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/include/class_httpsession_contentgroup.rb', line 94 def write_force @mutex.synchronize do @forced = true if !@thread end if @thread @thread.join else self.write_begin end end |
#write_output ⇒ Object
84 85 86 87 88 89 90 91 92 |
# File 'lib/include/class_httpsession_contentgroup.rb', line 84 def write_output return nil if @thread @mutex.synchronize do @thread = Thread.new do self.write_begin end end end |
#write_to_socket ⇒ Object
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 |
# File 'lib/include/class_httpsession_contentgroup.rb', line 117 def write_to_socket count = 0 @ios.each do |data| if data.is_a?(Knjappserver::Httpsession::Contentgroup) data.write_to_socket elsif data.key?(:str) if data[:str].is_a?(Hash) and data[:str][:type] == :file File.open(data[:str][:path], "r") do |file| loop do begin buf = file.sysread(16384) rescue EOFError break end if @chunked @socket.write("#{buf.length.to_s(16)}#{NL}#{buf}#{NL}") else @socket.write(buf) end end end else loop do break if data[:done] and data[:str].size <= 0 sleep 0.1 while data[:str].size < 512 and !data[:done] str = nil @mutex.synchronize do str = data[:str].bytes data[:str] = "" end #512 could take a long time for big pages. 16384 seems to be an optimal number. str.each_slice(16384) do |slice| buf = slice.pack("C*") if @chunked @socket.write("#{buf.length.to_s(16)}#{NL}#{buf}#{NL}") else @socket.write(buf) end end end end else raise "Unknown object: '#{data.class.name}'." end end count += 1 end |