Class: Fix::Engine::MessageBuffer

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Methods included from Logger

#log, log, 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

#clientObject

Returns the value of attribute client.



14
15
16
# File 'lib/fix/engine/message_buffer.rb', line 14

def client
  @client
end

#fieldsObject

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

Parameters:

  • data (String)

    The received FIX message bits, as they come



28
29
30
31
# File 'lib/fix/engine/message_buffer.rb', line 28

def add_data(data)
  msg_buf << data.chomp
  parse_messages
end

#append(fld) ⇒ Object

Append a single FIX field to the message

Parameters:

  • fld (String)

    A FIX formatted field, such as “35=0x01”



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

Returns:

  • (Boolean)

    Whether the message is complete



51
52
53
# File 'lib/fix/engine/message_buffer.rb', line 51

def complete?
  (@fields.count > 0) && (@fields.last[0] == 10)
end

#debugString

Returns a human-friendly string of the currently handled data

Returns:

  • (String)

    The parsed fields and the temporary buffer



84
85
86
# File 'lib/fix/engine/message_buffer.rb', line 84

def debug
  "#{to_s('|')}#{@msg_buf}"
end

#msg_bufObject

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 parse_messages(&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

Parameters:

  • sep (String) (defaults to: "\x01")

    The separator

Returns:

  • (String)

    The fields joined by the 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