Class: IORequest::Message

Inherits:
Object
  • Object
show all
Includes:
Utility::WithID
Defined in:
lib/io_request/message.rb

Overview

Single message. Either request or response.

Constant Summary collapse

TYPES =

Types of messages.

%i[request response].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Utility::WithID

#__with_id__extended_id

Constructor Details

#initialize(data, type: :request, id: nil, to: nil) ⇒ Message

Create new message.

Parameters:

  • data (Hash)
  • type (Symbol) (defaults to: :request)

    one of TYPES member.

  • id (Utility::ExtendedID, String, nil) (defaults to: nil)

    only should be filled if message is received from outside.

  • to (Utility::ExtendedID, String, nil) (defaults to: nil)

    if message is response, it should include integer of original request.



17
18
19
20
21
22
23
24
# File 'lib/io_request/message.rb', line 17

def initialize(data, type: :request, id: nil, to: nil)
  @data = data
  @type = type
  @id = id.nil? ? extended_id : Utility::ExtendedID.from(id)
  @to = to.nil? ? nil : Utility::ExtendedID.from(to)

  check_data
end

Instance Attribute Details

#dataHash (readonly)

Returns:

  • (Hash)


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

def data
  @data
end

#idUtility::ExtendedID (readonly)

Returns:



41
42
43
# File 'lib/io_request/message.rb', line 41

def id
  @id
end

#toUtility::ExtendedID (readonly)

Returns:



44
45
46
# File 'lib/io_request/message.rb', line 44

def to
  @to
end

#typeSymbol (readonly)

Returns:

  • (Symbol)


38
39
40
# File 'lib/io_request/message.rb', line 38

def type
  @type
end

Class Method Details

.read_from(io_r) ⇒ Message

Parameters:

  • io_r (:read)

Returns:

Raises:



83
84
85
86
87
88
89
90
91
92
93
# File 'lib/io_request/message.rb', line 83

def self.read_from(io_r)
  size = io_r.read(2)&.unpack1('S') || 0
  raise ZeroSizeMessageError if size.zero?

  json_string = io_r.read(size).unpack1("a#{size}")
  msg = JSON.parse(json_string, symbolize_names: true)
  Message.new(msg[:data],
              id: msg[:id],
              type: msg[:type].to_sym,
              to: msg[:to])
end

Instance Method Details

#check_dataObject

Check data correctness.



27
28
29
30
31
32
# File 'lib/io_request/message.rb', line 27

def check_data
  raise '@data is not a hash' unless @data.is_a? Hash
  raise 'incorrect @type' unless TYPES.include? @type
  raise 'incorrect @id' unless @id.is_a? Utility::ExtendedID
  raise '@to not specified for response' if response? && @to.nil?
end

#request?Boolean

Returns:

  • (Boolean)


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

def request?
  @type == :request
end

#response?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/io_request/message.rb', line 52

def response?
  @type == :response
end

#to_binaryString

Returns binary data to be passed over IO.

Returns:

  • (String)

    binary data to be passed over IO.



66
67
68
69
70
71
72
73
74
# File 'lib/io_request/message.rb', line 66

def to_binary
  json_string = JSON.generate({
                                id: @id.to_s,
                                type: @type.to_s,
                                to: @to.to_s,
                                data: @data
                              })
  [json_string.size, json_string].pack("Sa#{json_string.size}")
end

#to_sString

Returns:

  • (String)


57
58
59
60
61
62
63
# File 'lib/io_request/message.rb', line 57

def to_s
  if request?
    "Request##{@id}: #{data}"
  else
    "Response##{@id}: #{data} to ##{@to}"
  end
end

#write_to(io_w) ⇒ Object

Parameters:

  • io_w (:write)


77
78
79
# File 'lib/io_request/message.rb', line 77

def write_to(io_w)
  io_w.write(to_binary)
end