Class: Protocol::ZMTP::Codec::Command
- Inherits:
-
Object
- Object
- Protocol::ZMTP::Codec::Command
- Defined in:
- lib/protocol/zmtp/codec/command.rb
Overview
ZMTP command encode/decode.
Command frame body format:
1 byte: command name length
N bytes: command name
remaining: command data
READY command data = property list:
1 byte: property name length
N bytes: property name
4 bytes: property value length (big-endian)
N bytes: property value
Instance Attribute Summary collapse
-
#data ⇒ String
readonly
Command data (binary).
-
#name ⇒ String
readonly
Command name (e.g. “READY”, “SUBSCRIBE”).
Class Method Summary collapse
-
.cancel(prefix) ⇒ Command
Builds a CANCEL command (unsubscribe).
-
.decode_properties(data) ⇒ Hash{String => String}
Decodes a ZMTP property list from binary data.
-
.encode_properties(props) ⇒ String
Encodes a hash of properties into ZMTP property list format.
-
.from_body(body) ⇒ Command
Decodes a command from a frame body.
-
.join(group) ⇒ Command
Builds a JOIN command (RADIO/DISH group subscription).
-
.leave(group) ⇒ Command
Builds a LEAVE command (RADIO/DISH group unsubscription).
-
.ping(ttl: 0, context: EMPTY_BINARY) ⇒ Command
Builds a PING command.
-
.pong(context: EMPTY_BINARY) ⇒ Command
Builds a PONG command.
-
.ready(socket_type:, identity: "", metadata: nil) ⇒ Command
Builds a READY command with Socket-Type, Identity, and any extra properties supplied by upper layers (e.g. an extension that injects
X-QoSorX-Compression). -
.subscribe(prefix) ⇒ Command
Builds a SUBSCRIBE command.
Instance Method Summary collapse
-
#initialize(name, data = EMPTY_BINARY) ⇒ Command
constructor
A new instance of Command.
-
#ping_ttl_and_context ⇒ Array(Numeric, String)
Extracts TTL (in seconds) and context from a PING command’s data.
-
#properties ⇒ Hash{String => String}
Parses READY command data as a property list.
-
#to_body ⇒ String
Encodes as a command frame body.
-
#to_frame ⇒ Frame
Encodes as a complete command Frame.
Constructor Details
#initialize(name, data = EMPTY_BINARY) ⇒ Command
Returns a new instance of Command.
30 31 32 33 |
# File 'lib/protocol/zmtp/codec/command.rb', line 30 def initialize(name, data = EMPTY_BINARY) @name = name @data = data.encoding == Encoding::BINARY ? data : data.b end |
Instance Attribute Details
#data ⇒ String (readonly)
Returns command data (binary).
25 26 27 |
# File 'lib/protocol/zmtp/codec/command.rb', line 25 def data @data end |
#name ⇒ String (readonly)
Returns command name (e.g. “READY”, “SUBSCRIBE”).
21 22 23 |
# File 'lib/protocol/zmtp/codec/command.rb', line 21 def name @name end |
Class Method Details
.cancel(prefix) ⇒ Command
Builds a CANCEL command (unsubscribe).
99 100 101 |
# File 'lib/protocol/zmtp/codec/command.rb', line 99 def self.cancel(prefix) new("CANCEL", prefix.b) end |
.decode_properties(data) ⇒ Hash{String => String}
Decodes a ZMTP property list from binary data.
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
# File 'lib/protocol/zmtp/codec/command.rb', line 179 def self.decode_properties(data) result = {} offset = 0 while offset < data.bytesize raise Error, "property name truncated" if offset + 1 > data.bytesize name_len = data.getbyte(offset) offset += 1 raise Error, "property name truncated" if offset + name_len > data.bytesize name = data.byteslice(offset, name_len) offset += name_len raise Error, "property value length truncated" if offset + 4 > data.bytesize value_len = data.byteslice(offset, 4).unpack1("N") offset += 4 raise Error, "property value truncated" if offset + value_len > data.bytesize value = data.byteslice(offset, value_len) offset += value_len result[name] = value end result end |
.encode_properties(props) ⇒ String
Encodes a hash of properties into ZMTP property list format.
164 165 166 167 168 169 170 171 |
# File 'lib/protocol/zmtp/codec/command.rb', line 164 def self.encode_properties(props) parts = props.map do |name, value| name_bytes = name.b value_bytes = value.b name_bytes.bytesize.chr.b + name_bytes + [value_bytes.bytesize].pack("N") + value_bytes end parts.join end |
.from_body(body) ⇒ Command
Decodes a command from a frame body.
59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/protocol/zmtp/codec/command.rb', line 59 def self.from_body(body) body = body.b raise Error, "command body too short" if body.bytesize < 1 name_len = body.getbyte(0) raise Error, "command name truncated" if body.bytesize < 1 + name_len name = body.byteslice(1, name_len) data = body.byteslice(1 + name_len..) new(name, data) end |
.join(group) ⇒ Command
Builds a JOIN command (RADIO/DISH group subscription).
108 109 110 |
# File 'lib/protocol/zmtp/codec/command.rb', line 108 def self.join(group) new("JOIN", group.b) end |
.leave(group) ⇒ Command
Builds a LEAVE command (RADIO/DISH group unsubscription).
117 118 119 |
# File 'lib/protocol/zmtp/codec/command.rb', line 117 def self.leave(group) new("LEAVE", group.b) end |
.ping(ttl: 0, context: EMPTY_BINARY) ⇒ Command
Builds a PING command.
127 128 129 130 |
# File 'lib/protocol/zmtp/codec/command.rb', line 127 def self.ping(ttl: 0, context: EMPTY_BINARY) ttl_ds = (ttl * 10).to_i new("PING", [ttl_ds].pack("n") + (context.encoding == Encoding::BINARY ? context : context.b)) end |
.pong(context: EMPTY_BINARY) ⇒ Command
Builds a PONG command.
137 138 139 |
# File 'lib/protocol/zmtp/codec/command.rb', line 137 def self.pong(context: EMPTY_BINARY) new("PONG", context.encoding == Encoding::BINARY ? context : context.b) end |
.ready(socket_type:, identity: "", metadata: nil) ⇒ Command
Builds a READY command with Socket-Type, Identity, and any extra properties supplied by upper layers (e.g. an extension that injects X-QoS or X-Compression).
79 80 81 82 83 |
# File 'lib/protocol/zmtp/codec/command.rb', line 79 def self.ready(socket_type:, identity: "", metadata: nil) props = { "Socket-Type" => socket_type, "Identity" => identity } props.merge!() if && !.empty? new("READY", encode_properties(props)) end |
.subscribe(prefix) ⇒ Command
Builds a SUBSCRIBE command.
90 91 92 |
# File 'lib/protocol/zmtp/codec/command.rb', line 90 def self.subscribe(prefix) new("SUBSCRIBE", prefix.b) end |
Instance Method Details
#ping_ttl_and_context ⇒ Array(Numeric, String)
Extracts TTL (in seconds) and context from a PING command’s data.
145 146 147 148 149 |
# File 'lib/protocol/zmtp/codec/command.rb', line 145 def ping_ttl_and_context ttl_ds = @data.unpack1("n") context = @data.bytesize > 2 ? @data.byteslice(2..) : EMPTY_BINARY [ttl_ds / 10.0, context] end |
#properties ⇒ Hash{String => String}
Parses READY command data as a property list.
155 156 157 |
# File 'lib/protocol/zmtp/codec/command.rb', line 155 def properties self.class.decode_properties(@data) end |
#to_body ⇒ String
Encodes as a command frame body.
39 40 41 42 43 |
# File 'lib/protocol/zmtp/codec/command.rb', line 39 def to_body name_bytes = @name.encoding == Encoding::BINARY ? @name : @name.b buf = String.new(capacity: 1 + name_bytes.bytesize + @data.bytesize, encoding: Encoding::BINARY) buf << Frame::FLAG_BYTES[name_bytes.bytesize] << name_bytes << @data end |