Class: SDN::Message
- Inherits:
-
Object
show all
- Includes:
- Helpers
- Defined in:
- lib/sdn/message.rb,
lib/sdn/message/get.rb,
lib/sdn/message/set.rb,
lib/sdn/message/post.rb,
lib/sdn/message/control.rb,
lib/sdn/message/helpers.rb,
lib/sdn/message/ilt2/get.rb,
lib/sdn/message/ilt2/set.rb,
lib/sdn/message/ilt2/post.rb,
lib/sdn/message/ilt2/master_control.rb
Direct Known Subclasses
GetGroupAddr, GetMotorIP, GetNodeAddr, ILT2::GetMotorIP, ILT2::PostIRConfig, ILT2::PostLockStatus, ILT2::PostMotorIP, ILT2::PostMotorPosition, ILT2::SetLockStatus, ILT2::SetMotorIP, ILT2::SetMotorPosition, Lock, Move, MoveOf, MoveTo, Nack, PostGroupAddr, PostMotorDirection, PostMotorIP, PostMotorLimits, PostMotorPosition, PostMotorRollingSpeed, PostMotorStatus, PostNodeAddr, PostNodeAppVersion, PostNodeLabel, PostNodeSerialNumber, SetFactoryDefault, SetGroupAddr, SetMotorDirection, SetMotorIP, SetMotorLimits, SetMotorRollingSpeed, SetNetworkLock, SetNodeLabel, SimpleRequest, Stop, UnknownMessage
Defined Under Namespace
Modules: Helpers, ILT2
Classes: Ack, GetGroupAddr, GetMotorDirection, GetMotorIP, GetMotorLimits, GetMotorPosition, GetMotorRollingSpeed, GetMotorStatus, GetNetworkLock, GetNodeAddr, GetNodeAppVersion, GetNodeLabel, GetNodeSerialNumber, GetNodeStackVersion, Lock, Move, MoveOf, MoveTo, Nack, PostGroupAddr, PostMotorDirection, PostMotorIP, PostMotorLimits, PostMotorPosition, PostMotorRollingSpeed, PostMotorStatus, PostNetworkLock, PostNodeAddr, PostNodeAppVersion, PostNodeLabel, PostNodeSerialNumber, PostNodeStackVersion, SetFactoryDefault, SetGroupAddr, SetMotorDirection, SetMotorIP, SetMotorLimits, SetMotorRollingSpeed, SetNetworkLock, SetNodeLabel, SimpleRequest, Stop, UnknownMessage, Wink
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from Helpers
#checksum, #from_number, #from_string, #is_group_address?, #node_type_from_number, #node_type_to_number, #node_type_to_string, #parse_address, #print_address, #to_number, #to_string, #transform_param
Constructor Details
#initialize(node_type: nil, ack_requested: false, src: nil, dest: nil) ⇒ Message
83
84
85
86
87
88
89
90
91
92
|
# File 'lib/sdn/message.rb', line 83
def initialize(node_type: nil, ack_requested: false, src: nil, dest: nil)
@node_type = node_type || 0
@ack_requested = ack_requested
if src.nil? && !dest.nil? && is_group_address?(dest)
src = dest
dest = nil
end
@src = src || [0, 0, 1]
@dest = dest || [0, 0, 0]
end
|
Instance Attribute Details
#ack_requested ⇒ Object
Returns the value of attribute ack_requested.
81
82
83
|
# File 'lib/sdn/message.rb', line 81
def ack_requested
@ack_requested
end
|
#dest ⇒ Object
Returns the value of attribute dest.
81
82
83
|
# File 'lib/sdn/message.rb', line 81
def dest
@dest
end
|
#node_type ⇒ Object
Returns the value of attribute node_type.
81
82
83
|
# File 'lib/sdn/message.rb', line 81
def node_type
@node_type
end
|
#src ⇒ Object
Returns the value of attribute src.
81
82
83
|
# File 'lib/sdn/message.rb', line 81
def src
@src
end
|
Class Method Details
.expected_response?(message) ⇒ Boolean
14
15
16
17
18
19
20
|
# File 'lib/sdn/message.rb', line 14
def expected_response?(message)
if name =~ /::Get([A-Za-z]+)/
message.class.name == name.sub("::Get", "::Post")
else
message.is_a?(Ack) || message.is_a?(Nack)
end
end
|
.inherited(klass) ⇒ Object
8
9
10
11
12
|
# File 'lib/sdn/message.rb', line 8
def inherited(klass)
return Message.inherited(klass) unless self == Message
@message_map = nil
(@subclasses ||= []) << klass
end
|
.parse(data) ⇒ Object
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
# File 'lib/sdn/message.rb', line 22
def parse(data)
offset = -1
msg = length = ack_requested = message_class = nil
loop do
offset += 1
result = ILT2::MasterControl.parse(data)
return result if result
return [nil, 0] if data.length - offset < 11
msg = to_number(data[offset])
length = to_number(data[offset + 1])
ack_requested = length & 0x80 == 0x80
length &= 0x7f
next if length < 11 || length > 43
next if length > data.length - offset
message_class = message_map[msg] || UnknownMessage
calculated_sum = checksum(data.slice(offset, length - 2))
read_sum = data.slice(offset + length - 2, 2)
next unless read_sum == calculated_sum
break
end
node_type = node_type_from_number(to_number(data[offset + 2]))
src = transform_param(data.slice(offset + 3, 3))
dest = transform_param(data.slice(offset + 6, 3))
begin
result = message_class.new(node_type: node_type, ack_requested: ack_requested, src: src, dest: dest)
result.parse(data.slice(offset + 9, length - 11))
result.msg = msg if message_class == UnknownMessage
rescue ArgumentError => e
SDN.logger.warn "discarding illegal message of type #{message_class.name}: #{e}"
result = nil
end
[result, offset + length]
end
|
Instance Method Details
#==(other) ⇒ Object
107
108
109
|
# File 'lib/sdn/message.rb', line 107
def ==(other)
self.serialize == other.serialize
end
|
#class_inspect ⇒ Object
116
117
118
119
120
|
# File 'lib/sdn/message.rb', line 116
def class_inspect
ivars = instance_variables - [:@node_type, :@ack_requested, :@src, :@dest, :@params]
return if ivars.empty?
ivars.map { |iv| ", #{iv}=#{instance_variable_get(iv).inspect}" }.join
end
|
#inspect ⇒ Object
Also known as:
to_s
111
112
113
|
# File 'lib/sdn/message.rb', line 111
def inspect
"#<%s @node_type=%s, @ack_requested=%s, @src=%s, @dest=%s%s>" % [self.class.name, node_type_to_string(node_type), ack_requested, print_address(src), print_address(dest), class_inspect]
end
|
#parse(params) ⇒ Object
94
95
96
|
# File 'lib/sdn/message.rb', line 94
def parse(params)
raise MalformedMessage, "unrecognized params for #{self.class.name}: #{params.map { |b| '%02x' % b }}" if self.class.const_defined?(:PARAMS_LENGTH) && params.length != self.class.const_get(:PARAMS_LENGTH)
end
|
#serialize ⇒ Object
98
99
100
101
102
103
104
105
|
# File 'lib/sdn/message.rb', line 98
def serialize
result = transform_param(node_type_to_number(node_type)) + transform_param(src) + transform_param(dest) + params
length = result.length + 4
length |= 0x80 if ack_requested
result = transform_param(self.class.const_get(:MSG)) + transform_param(length) + result
result.concat(checksum(result))
result.pack("C*")
end
|