Class: HTTPkit::Connection::EventMachine

Inherits:
EM::Connection
  • Object
show all
Defined in:
lib/httpkit/connection/eventmachine.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(callback = proc {}) ⇒ EventMachine

Returns a new instance of EventMachine


19
20
21
22
23
# File 'lib/httpkit/connection/eventmachine.rb', line 19

def initialize(callback = proc {})
  @closed = Promise.new
  @parser = HTTP::Parser.new(self)
  callback.call(self)
end

Instance Attribute Details

#closedObject (readonly)

Returns the value of attribute closed


16
17
18
# File 'lib/httpkit/connection/eventmachine.rb', line 16

def closed
  @closed
end

#on_message=(value) ⇒ Object (writeonly)

Sets the attribute on_message

Parameters:

  • value

    the value to set the attribute on_message to.


17
18
19
# File 'lib/httpkit/connection/eventmachine.rb', line 17

def on_message=(value)
  @on_message = value
end

Class Method Details

.start_client(config, client_class) ⇒ Object


6
7
8
9
# File 'lib/httpkit/connection/eventmachine.rb', line 6

def self.start_client(config, client_class)
  connection = EM.connect(config[:address], config[:port], self)
  client_class.new(config, connection)
end

.start_server(config, server_class) ⇒ Object


11
12
13
14
# File 'lib/httpkit/connection/eventmachine.rb', line 11

def self.start_server(config, server_class)
  EM.start_server(config[:address], config[:port], self,
                  proc { |conn| server_class.new(config, conn) })
end

Instance Method Details

#close(reason = nil) ⇒ Object

def send_data(data)

p [__id__, :send, data]
# p data
super

end


46
47
48
49
# File 'lib/httpkit/connection/eventmachine.rb', line 46

def close(reason = nil)
  closed.reject(reason) if reason
  close_connection_after_writing
end

#on_body(chunk) ⇒ Object


64
65
66
# File 'lib/httpkit/connection/eventmachine.rb', line 64

def on_body(chunk)
  @message.body.write(chunk)
end

#on_headers_complete(_) ⇒ Object


59
60
61
62
# File 'lib/httpkit/connection/eventmachine.rb', line 59

def on_headers_complete(_)
  @message = Support::Message.build(@parser)
  @on_message.call(@message)
end

#on_message_completeObject


68
69
70
# File 'lib/httpkit/connection/eventmachine.rb', line 68

def on_message_complete
  @message.close
end

#receive_data(data) ⇒ Object


30
31
32
33
34
35
36
37
38
# File 'lib/httpkit/connection/eventmachine.rb', line 30

def receive_data(data)
  # p [__id__, :receive, data]
  # p data
  @parser << data
rescue => ex
  # message = ex.message.empty? ? ex.class : ex.message
  # puts [message, *ex.backtrace].join("\n\t")
  close(ex)
end

#serialize(message) ⇒ Object


25
26
27
28
# File 'lib/httpkit/connection/eventmachine.rb', line 25

def serialize(message)
  serializer = Serializer.new(message, method(:send_data))
  serializer.serialize
end

#unbind(reason = nil) ⇒ Object


51
52
53
54
55
56
57
# File 'lib/httpkit/connection/eventmachine.rb', line 51

def unbind(reason = nil)
  if reason
    closed.reject(reason)
  else
    closed.fulfill
  end
end