Module: MaxCube::Messages::Serializer
- Includes:
- Handler
- Included in:
- TCP::Serializer, UDP::Serializer
- Defined in:
- lib/maxcube/messages/serializer.rb
Overview
This module provides methods connected to message serializing only (i.e. direction client -> Cube).
Constant Summary
Constants included from Handler
Constants included from MaxCube::Messages
DAYS_OF_WEEK, DEVICE_MODE, DEVICE_TYPE
Instance Method Summary collapse
-
#serialize(*args, esize: 0, size: 0, count: 0) ⇒ String
Serializes input
args
into String, with optional implicit conversion from integer into binary string (using Handler::PACK_FORMAT). -
#serialize_hash_body(hash, serializer_type) ⇒ String
Serializes message body, i.e.
-
#write(*args, esize: 0, size: 0, count: 0) ⇒ Object
It serializes
args
with #serialize and writes it into internalIO
variable.
Methods included from Handler
#check_data_type, #check_hash, #check_hash_keys, #check_hash_msg_type, #check_hash_values, #check_msg, #check_msg_msg_type, #check_msg_type, #msg_type_hash_keys, #msg_type_hash_opt_keys, #valid_data_type, #valid_hash, #valid_hash_keys, #valid_hash_msg_type, #valid_hash_values, #valid_msg, #valid_msg_msg_type, #valid_msg_type
Instance Method Details
#serialize(*args, esize: 0, size: 0, count: 0) ⇒ String
Serializes input args
into String, with optional implicit conversion from integer into binary string (using Handler::PACK_FORMAT). In any case, String elements are serialized as they are.
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/maxcube/messages/serializer.rb', line 32 def serialize(*args, esize: 0, size: 0, count: 0) return args.join if size.zero? && esize.zero? count, subcount, subsize = serialize_bounds(args, esize: esize, size: size, count: count) str = '' args.reverse! count.times do str << args.pop while args.last.is_a?(String) substr = args.pop(subcount).pack(PACK_FORMAT[subsize]) substr = substr[1..-1] if subsize == 3 str << substr end str << args.pop until args.empty? str end |
#serialize_hash_body(hash, serializer_type) ⇒ String
Serializes message body, i.e. message head has been already serialized. It dynamically calls method corresponding to message and serializer type. If message type is not implemented yet, it is unclear how to serialize the hash
, so an exception is raised.
70 71 72 73 74 75 76 |
# File 'lib/maxcube/messages/serializer.rb', line 70 def serialize_hash_body(hash, serializer_type) method_str = "serialize_#{serializer_type}_#{@msg_type.downcase}" return send(method_str, hash) if respond_to?(method_str, true) raise InvalidMessageType .new(@msg_type, 'serialization of message type' \ ' is not implemented (yet)') end |
#write(*args, esize: 0, size: 0, count: 0) ⇒ Object
It serializes args
with #serialize and writes it into internal IO
variable.
54 55 56 |
# File 'lib/maxcube/messages/serializer.rb', line 54 def write(*args, esize: 0, size: 0, count: 0) @io.write(serialize(*args, esize: esize, size: size, count: count)) end |