Class: Lignite::Message
- Inherits:
-
Object
- Object
- Lignite::Message
- Includes:
- Bytes
- Defined in:
- lib/lignite/message.rb
Overview
A Message has 3 common parts:
-
length u16, (not including the length itself); this is added by Connection#send and stripped by Connection#receive
-
msgid, u16
-
type, u8
and then a type-specific body.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#body ⇒ Object
readonly
Returns the value of attribute body.
-
#msgid ⇒ Object
readonly
Returns the value of attribute msgid.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Class Method Summary collapse
- .direct_command_no_reply(body) ⇒ Object
- .direct_command_with_reply(body) ⇒ Object
- .msgid ⇒ Object
- .reply_from_bytes(bytes) ⇒ Object
- .reset_msgid ⇒ Object
- .system_command_no_reply(body) ⇒ Object
- .system_command_with_reply(body) ⇒ Object
Instance Method Summary collapse
-
#bytes ⇒ Object
not including the length.
-
#initialize(type:, body:) ⇒ Message
constructor
A new instance of Message.
Methods included from Bytes
bin_to_hex, f32, hex_to_bin, u16, u32, u8, unpack_f32, unpack_u16, unpack_u32, unpack_u8
Methods included from Logger
Constructor Details
#initialize(type:, body:) ⇒ Message
Returns a new instance of Message.
27 28 29 30 31 |
# File 'lib/lignite/message.rb', line 27 def initialize(type:, body:) @msgid = self.class.msgid @type = type @body = body end |
Instance Attribute Details
#body ⇒ Object (readonly)
Returns the value of attribute body.
25 26 27 |
# File 'lib/lignite/message.rb', line 25 def body @body end |
#msgid ⇒ Object (readonly)
Returns the value of attribute msgid.
25 26 27 |
# File 'lib/lignite/message.rb', line 25 def msgid @msgid end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
25 26 27 |
# File 'lib/lignite/message.rb', line 25 def type @type end |
Class Method Details
.direct_command_no_reply(body) ⇒ Object
50 51 52 |
# File 'lib/lignite/message.rb', line 50 def self.direct_command_no_reply(body) new(type: 0x80, body: body) end |
.direct_command_with_reply(body) ⇒ Object
46 47 48 |
# File 'lib/lignite/message.rb', line 46 def self.direct_command_with_reply(body) new(type: 0x00, body: body) end |
.msgid ⇒ Object
19 20 21 22 23 |
# File 'lib/lignite/message.rb', line 19 def self.msgid @msg_counter += 1 logger.debug "MSGID #{@msg_counter}" @msg_counter end |
.reply_from_bytes(bytes) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/lignite/message.rb', line 55 def self.reply_from_bytes(bytes) msgid = unpack_u16(bytes[0..1]) type = unpack_u8(bytes[2]) body = bytes[3..-1] case type when 0x03 # SYSTEM_REPLY SystemReply.new(msgid: msgid, error: false, body: body) when 0x05 # SYSTEM_REPLY_ERROR SystemReply.new(msgid: msgid, error: true, body: body) when 0x02 # DIRECT_REPLY DirectReply.new(msgid: msgid, error: false, body: body) when 0x04 # DIRECT_REPLY_ERROR DirectReply.new(msgid: msgid, error: true, body: body) else raise format("Unexpected reply type %x", type) end end |
.reset_msgid ⇒ Object
14 15 16 |
# File 'lib/lignite/message.rb', line 14 def self.reset_msgid @msg_counter = 0 end |
.system_command_no_reply(body) ⇒ Object
42 43 44 |
# File 'lib/lignite/message.rb', line 42 def self.system_command_no_reply(body) new(type: 0x81, body: body) end |
.system_command_with_reply(body) ⇒ Object
38 39 40 |
# File 'lib/lignite/message.rb', line 38 def self.system_command_with_reply(body) new(type: 0x01, body: body) end |
Instance Method Details
#bytes ⇒ Object
not including the length
34 35 36 |
# File 'lib/lignite/message.rb', line 34 def bytes u16(@msgid) + u8(@type) + @body end |