Class: Lignite::Message

Inherits:
Object
  • Object
show all
Extended by:
Bytes, Logger
Includes:
Bytes
Defined in:
lib/lignite/message.rb

Overview

A Message has 3 common parts:

and then a type-specific body.

Direct Known Subclasses

DirectReply, SystemReply

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

default_logger, 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

#bodyObject (readonly)

Returns the value of attribute body.



25
26
27
# File 'lib/lignite/message.rb', line 25

def body
  @body
end

#msgidObject (readonly)

Returns the value of attribute msgid.



25
26
27
# File 'lib/lignite/message.rb', line 25

def msgid
  @msgid
end

#typeObject (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

.msgidObject



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

Parameters:

  • bytes (ByteString)

    does not include the length field



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_msgidObject



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

#bytesObject

not including the length



34
35
36
# File 'lib/lignite/message.rb', line 34

def bytes
  u16(@msgid) + u8(@type) + @body
end