Class: Kafka::Message
- Inherits:
-
Object
- Object
- Kafka::Message
- Defined in:
- lib/kafka/message.rb
Overview
A message. The format of an N byte message is the following: 1 byte “magic” identifier to allow format changes 4 byte CRC32 of the payload N - 5 byte payload
Constant Summary collapse
- MAGIC_IDENTIFIER_DEFAULT =
0
Instance Attribute Summary collapse
-
#checksum ⇒ Object
Returns the value of attribute checksum.
-
#magic ⇒ Object
Returns the value of attribute magic.
-
#payload ⇒ Object
Returns the value of attribute payload.
Class Method Summary collapse
Instance Method Summary collapse
- #calculate_checksum ⇒ Object
-
#initialize(payload = nil, magic = MAGIC_IDENTIFIER_DEFAULT, checksum = nil) ⇒ Message
constructor
A new instance of Message.
- #valid? ⇒ Boolean
Constructor Details
#initialize(payload = nil, magic = MAGIC_IDENTIFIER_DEFAULT, checksum = nil) ⇒ Message
Returns a new instance of Message.
27 28 29 30 31 |
# File 'lib/kafka/message.rb', line 27 def initialize(payload = nil, magic = MAGIC_IDENTIFIER_DEFAULT, checksum = nil) self.magic = magic self.payload = payload self.checksum = checksum || self.calculate_checksum end |
Instance Attribute Details
#checksum ⇒ Object
Returns the value of attribute checksum.
25 26 27 |
# File 'lib/kafka/message.rb', line 25 def checksum @checksum end |
#magic ⇒ Object
Returns the value of attribute magic.
25 26 27 |
# File 'lib/kafka/message.rb', line 25 def magic @magic end |
#payload ⇒ Object
Returns the value of attribute payload.
25 26 27 |
# File 'lib/kafka/message.rb', line 25 def payload @payload end |
Class Method Details
.parse_from(binary) ⇒ Object
41 42 43 44 45 46 47 |
# File 'lib/kafka/message.rb', line 41 def self.parse_from(binary) size = binary[0, 4].unpack("N").shift.to_i magic = binary[4, 1].unpack("C").shift checksum = binary[5, 4].unpack("N").shift payload = binary[9, size] # 5 = 1 + 4 is Magic + Checksum return Kafka::Message.new(payload, magic, checksum) end |
Instance Method Details
#calculate_checksum ⇒ Object
33 34 35 |
# File 'lib/kafka/message.rb', line 33 def calculate_checksum Zlib.crc32(self.payload) end |
#valid? ⇒ Boolean
37 38 39 |
# File 'lib/kafka/message.rb', line 37 def valid? self.checksum == Zlib.crc32(self.payload) end |