Class: HTTPkit::Connection::EventMachine
- Inherits:
-
EM::Connection
- Object
- EM::Connection
- HTTPkit::Connection::EventMachine
- Defined in:
- lib/httpkit/connection/eventmachine.rb
Instance Attribute Summary collapse
-
#closed ⇒ Object
readonly
Returns the value of attribute closed.
-
#on_message ⇒ Object
writeonly
Sets the attribute on_message.
Class Method Summary collapse
Instance Method Summary collapse
-
#close(reason = nil) ⇒ Object
def send_data(data) p [__id__, :send, data] # p data super end.
-
#initialize(callback = proc {}) ⇒ EventMachine
constructor
A new instance of EventMachine.
- #on_body(chunk) ⇒ Object
- #on_headers_complete(_) ⇒ Object
- #on_message_complete ⇒ Object
- #receive_data(data) ⇒ Object
- #serialize(message) ⇒ Object
- #unbind(reason = nil) ⇒ Object
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
#closed ⇒ Object (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
17 18 19 |
# File 'lib/httpkit/connection/eventmachine.rb', line 17 def (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_complete ⇒ Object
68 69 70 |
# File 'lib/httpkit/connection/eventmachine.rb', line 68 def @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() serializer = Serializer.new(, 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 |