Class: IORequest::Message
- Inherits:
-
Object
- Object
- IORequest::Message
- 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
- #data ⇒ Hash readonly
- #id ⇒ Utility::ExtendedID readonly
- #to ⇒ Utility::ExtendedID readonly
- #type ⇒ Symbol readonly
Class Method Summary collapse
Instance Method Summary collapse
-
#check_data ⇒ Object
Check data correctness.
-
#initialize(data, type: :request, id: nil, to: nil) ⇒ Message
constructor
Create new message.
- #request? ⇒ Boolean
- #response? ⇒ Boolean
-
#to_binary ⇒ String
Binary data to be passed over IO.
- #to_s ⇒ String
- #write_to(io_w) ⇒ Object
Methods included from Utility::WithID
Constructor Details
#initialize(data, type: :request, id: nil, to: nil) ⇒ Message
Create new message.
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
#data ⇒ Hash (readonly)
35 36 37 |
# File 'lib/io_request/message.rb', line 35 def data @data end |
#id ⇒ Utility::ExtendedID (readonly)
41 42 43 |
# File 'lib/io_request/message.rb', line 41 def id @id end |
#to ⇒ Utility::ExtendedID (readonly)
44 45 46 |
# File 'lib/io_request/message.rb', line 44 def to @to end |
#type ⇒ Symbol (readonly)
38 39 40 |
# File 'lib/io_request/message.rb', line 38 def type @type end |
Class Method Details
.read_from(io_r) ⇒ Message
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_data ⇒ Object
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
47 48 49 |
# File 'lib/io_request/message.rb', line 47 def request? @type == :request end |
#response? ⇒ Boolean
52 53 54 |
# File 'lib/io_request/message.rb', line 52 def response? @type == :response end |
#to_binary ⇒ String
Returns 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_s ⇒ 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
77 78 79 |
# File 'lib/io_request/message.rb', line 77 def write_to(io_w) io_w.write(to_binary) end |