Class: Knjappserver::Httpsession::Contentgroup

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

#chunkedObject

Returns the value of attribute chunked.



6
7
8
# File 'lib/include/class_httpsession_contentgroup.rb', line 6

def chunked
  @chunked
end

#cur_dataObject (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

#doneObject (readonly)

Returns the value of attribute done.



5
6
7
# File 'lib/include/class_httpsession_contentgroup.rb', line 5

def done
  @done
end

#socketObject

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

#initObject



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

#joinObject



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_doneObject



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_threadObject



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_threadObject



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

#resetObject



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_beginObject



70
71
72
73
74
75
76
# File 'lib/include/class_httpsession_contentgroup.rb', line 70

def write_begin
  begin
    @resp.write if @httpsession.meta["METHOD"] != "HEAD"
  rescue Errno::ECONNRESET, Errno::ENOTCONN, Errno::EPIPE
    #Ignore - the user probaly left.
  end
end

#write_forceObject



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_outputObject



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_socketObject



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