Module: Rity::Connection

Defined in:
lib/rity/connection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#appObject

Returns the value of attribute app.



8
9
10
# File 'lib/rity/connection.rb', line 8

def app
  @app
end

#logObject

Returns the value of attribute log.



8
9
10
# File 'lib/rity/connection.rb', line 8

def log
  @log
end

Instance Method Details

#post_initObject



10
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
# File 'lib/rity/connection.rb', line 10

def post_init
  @parser = Hatetepe::Parser.new
  @builder = Hatetepe::Builder.new
  
  (previous = EM::DefaultDeferrable.new).succeed
  request = nil
  
  @parser.on_response { close_connection }
  
  @parser.on_request do |verb, uri|
    previous = request if request
    request = Request.new(@app, verb, uri)
  end
  
  @parser.on_header do |name, value|
    request.add_header name, value
  end
  
  @parser.on_headers_complete do
    prev, req = previous, request
    
    req.env["stream.start"] = proc do |response|
      EM::Synchrony.sync prev
      
      @builder.response response[0..1]
      req.env["stream.send"] = proc do |chunk|
        @builder.body chunk
      end
      
      req.env["stream.close"] = proc do
        @builder.complete
        req.succeed
        close_connection_after_writing if req == request
      end
    end
    
    req.process
  end
  
  @parser.on_body_chunk do |chunk|
    request.add_body_chunk chunk
  end
  
  @parser.on_complete do
    request.close_body
  end
  
  @builder.on_write do |data|
    send_data data
  end
end

#receive_data(data) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/rity/connection.rb', line 62

def receive_data(data)
  @parser << data
rescue Hatetepe::ParserError
  close_connection
rescue Exception => ex
  if log
    log.error ex.message
    log << ex.backtrace.map {|line| "  #{line}" }.join("\n") + "\n"
  end
  close_connection
end