Module: Zerg::Support::EventMachine::ProtocolAdapter
- Defined in:
- lib/zerg_support/event_machine/protocol_adapter.rb
Overview
Adapts generic Protocol modules for easy inclusion in EventMachine connection classes / modules.
Defined Under Namespace
Classes: StateBase
Class Method Summary collapse
Class Method Details
.adapter_module(protocol_module, object_name = nil) ⇒ Object
7 8 9 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 |
# File 'lib/zerg_support/event_machine/protocol_adapter.rb', line 7 def self.adapter_module(protocol_module, object_name = nil) unless object_name object_name = /(^|:)([^:]*)Protocol$/. match(protocol_module.name)[2].split(/(?=[A-Z])/).join('_').downcase end state_class = Class.new StateBase state_class.send :include, protocol_module state_class.class_eval do # Called by the protocol when an entire object is available. define_method :"received_#{object_name}" do |object| @target.send :"receive_#{object_name}", object end end adapter = Module.new adapter.module_eval do # Called by Event Machine when TCP stream data is available. define_method :receive_data do |data| @zerg_protocol_adapter_state ||= state_class.new self @zerg_protocol_adapter_state.received_bytes data end # Called by the Event Machine user to send data. define_method :"send_#{object_name}" do |object| @zerg_protocol_adapter_state ||= state_class.new self @zerg_protocol_adapter_state.send :"send_#{object_name}", object end end adapter end |