Class: Fix::Engine::MessageBuffer
- Inherits:
-
Object
- Object
- Fix::Engine::MessageBuffer
- Includes:
- Logger
- Defined in:
- lib/fix/engine/message_buffer.rb
Overview
A FIX message to which fields get appended, once it is completed by a proper terminator it is handled
Instance Attribute Summary collapse
-
#client ⇒ Object
Returns the value of attribute client.
-
#fields ⇒ Object
Returns the value of attribute fields.
Instance Method Summary collapse
-
#add_data(data) ⇒ Object
Adds received bytes to the message buffer and attempt to process them.
-
#append(fld) ⇒ Object
Append a single FIX field to the message.
-
#complete? ⇒ Boolean
Returns true if the last field of the collection is a FIX checksum.
-
#debug ⇒ String
Returns a human-friendly string of the currently handled data.
-
#initialize(&block) ⇒ MessageBuffer
constructor
A new instance of MessageBuffer.
-
#msg_buf ⇒ Object
The data buffer string.
-
#parse_messages(&block) ⇒ Object
Attempts to parse fields from the message buffer, if the fields that get parsed complete the temporary message, it is processed.
-
#to_s(sep = "\x01") ⇒ String
Returns the current fields as a string joined by a given separator.
Methods included from Logger
Constructor Details
#initialize(&block) ⇒ MessageBuffer
Returns a new instance of MessageBuffer.
16 17 18 19 20 21 |
# File 'lib/fix/engine/message_buffer.rb', line 16 def initialize(&block) @fields = [] raise "A block accepting a FP::Message as single parameter must be provided" unless (block && (block.arity == 1)) @msg_handler = block end |
Instance Attribute Details
#client ⇒ Object
Returns the value of attribute client.
14 15 16 |
# File 'lib/fix/engine/message_buffer.rb', line 14 def client @client end |
#fields ⇒ Object
Returns the value of attribute fields.
14 15 16 |
# File 'lib/fix/engine/message_buffer.rb', line 14 def fields @fields end |
Instance Method Details
#add_data(data) ⇒ Object
Adds received bytes to the message buffer and attempt to process them
28 29 30 31 |
# File 'lib/fix/engine/message_buffer.rb', line 28 def add_data(data) msg_buf << data.chomp end |
#append(fld) ⇒ Object
Append a single FIX field to the message
38 39 40 41 42 43 44 |
# File 'lib/fix/engine/message_buffer.rb', line 38 def append(fld) raise "Cannot append to complete message" if complete? field = fld.split('=') field[0] = field[0].to_i field[1] = field[1].gsub(/\x01\Z/, '') @fields << field end |
#complete? ⇒ Boolean
Returns true if the last field of the collection is a FIX checksum
51 52 53 |
# File 'lib/fix/engine/message_buffer.rb', line 51 def complete? (@fields.count > 0) && (@fields.last[0] == 10) end |
#debug ⇒ String
Returns a human-friendly string of the currently handled data
84 85 86 |
# File 'lib/fix/engine/message_buffer.rb', line 84 def debug "#{to_s('|')}#{@msg_buf}" end |
#msg_buf ⇒ Object
The data buffer string
75 76 77 |
# File 'lib/fix/engine/message_buffer.rb', line 75 def msg_buf @msg_buf ||= '' end |
#parse_messages(&block) ⇒ Object
Attempts to parse fields from the message buffer, if the fields that get parsed complete the temporary message, it is processed
59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/fix/engine/message_buffer.rb', line 59 def (&block) while idx = msg_buf.index("\x01") field = msg_buf.slice!(0, idx + 1).gsub(/\x01\Z/, '') append(field) if complete? parsed = FP.parse(to_s) @fields = [] @msg_handler.call(parsed) end end end |
#to_s(sep = "\x01") ⇒ String
Returns the current fields as a string joined by a given separator
94 95 96 |
# File 'lib/fix/engine/message_buffer.rb', line 94 def to_s(sep = "\x01") fields.map { |f| f.join('=') }.join(sep) + sep end |