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:

  • length u16, (not including the length itself); this is added by Lignite::MessageSender#send and stripped by Lignite::MessageSender#receive

  • msgid, u16

  • type, u8

and then a type-specific body. It is sent or received via MessageSender

Direct Known Subclasses

DirectReply, SystemReply

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Bytes

f32, hexdump, 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.



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

def initialize(type:, body:)
  @msgid = self.class.msgid
  @type = type
  @body = body
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



22
23
24
# File 'lib/lignite/message.rb', line 22

def body
  @body
end

#msgidObject (readonly)

Returns the value of attribute msgid.



22
23
24
# File 'lib/lignite/message.rb', line 22

def msgid
  @msgid
end

#typeObject (readonly)

Returns the value of attribute type.



22
23
24
# File 'lib/lignite/message.rb', line 22

def type
  @type
end

Class Method Details

.direct_command_no_reply(body) ⇒ Object



47
48
49
# File 'lib/lignite/message.rb', line 47

def self.direct_command_no_reply(body)
  new(type: 0x80, body: body)
end

.direct_command_with_reply(body) ⇒ Object



43
44
45
# File 'lib/lignite/message.rb', line 43

def self.direct_command_with_reply(body)
  new(type: 0x00, body: body)
end

.msgidObject



16
17
18
19
20
# File 'lib/lignite/message.rb', line 16

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



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/lignite/message.rb', line 52

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 "Unexpected reply type %x" % type
  end
end

.system_command_no_reply(body) ⇒ Object



39
40
41
# File 'lib/lignite/message.rb', line 39

def self.system_command_no_reply(body)
  new(type: 0x81, body: body)
end

.system_command_with_reply(body) ⇒ Object



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

def self.system_command_with_reply(body)
  new(type: 0x01, body: body)
end

Instance Method Details

#bytesObject

not including the length



31
32
33
# File 'lib/lignite/message.rb', line 31

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