Class: RSMP::Message

Inherits:
Object
  • Object
show all
Includes:
Inspect
Defined in:
lib/rsmp/message.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Inspect

#inspect, #inspector

Constructor Details

#initialize(attributes = {}) ⇒ Message

Returns a new instance of Message.



128
129
130
131
132
133
134
135
# File 'lib/rsmp/message.rb', line 128

def initialize attributes = {}
  @timestamp = Time.now   # this timestamp is for internal use, and does not use the clock
                          # in the node, which can be set by an rsmp supervisor

  @attributes = { "mType"=> "rSMsg" }.merge attributes

  ensure_message_id
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



8
9
10
# File 'lib/rsmp/message.rb', line 8

def attributes
  @attributes
end

#directionObject

Returns the value of attribute direction.



10
11
12
# File 'lib/rsmp/message.rb', line 10

def direction
  @direction
end

#jsonObject

Returns the value of attribute json.



10
11
12
# File 'lib/rsmp/message.rb', line 10

def json
  @json
end

#nowObject (readonly)

Returns the value of attribute now.



8
9
10
# File 'lib/rsmp/message.rb', line 8

def now
  @now
end

#outObject (readonly)

Returns the value of attribute out.



8
9
10
# File 'lib/rsmp/message.rb', line 8

def out
  @out
end

#timestampObject (readonly)

this is an internal timestamp recording when we receive/send



9
10
11
# File 'lib/rsmp/message.rb', line 9

def timestamp
  @timestamp
end

Class Method Details

.bin_to_chars(s) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
# File 'lib/rsmp/message.rb', line 107

def self.bin_to_chars(s)
  out = s.gsub(/[^[:print:]]/i, '.')
  max = 120
  if out.size <= max
    out
  else
    mid = " ... "
    length = (max-mid.size)/2 - 1
    "#{out[0..length]} ... #{out[-length-1..-1]}"
  end
end

.build(attributes, json) ⇒ Object



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
# File 'lib/rsmp/message.rb', line 23

def self.build attributes, json
  validate_message_type attributes
  case attributes["type"]
  when "MessageAck"
    message = MessageAck.new attributes
  when "MessageNotAck"
    message = MessageNotAck.new attributes
  when "Version"
    message = Version.new attributes
  when "AggregatedStatus"
    message = AggregatedStatus.new attributes
  when "AggregatedStatusRequest"
    message = AggregatedStatusRequest.new attributes
  when "Watchdog"
    message = Watchdog.new attributes
  when "Alarm"
    message = self.build_alarm attributes
  when "CommandRequest"
    message = CommandRequest.new attributes
  when "CommandResponse"
    message = CommandResponse.new attributes
  when "StatusRequest"
    message = StatusRequest.new attributes
  when "StatusResponse"
    message = StatusResponse.new attributes
  when "StatusSubscribe"
    message = StatusSubscribe.new attributes
  when "StatusUnsubscribe"
    message = StatusUnsubscribe.new attributes
  when "StatusUpdate"
    message = StatusUpdate.new attributes
  else
    message = Unknown.new attributes
  end
  message.json = json
  message.direction = :in
  message
end

.build_alarm(attributes) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/rsmp/message.rb', line 62

def self.build_alarm attributes
  case attributes["aSp"]
  when 'Issue'
    AlarmIssue.new attributes
  when 'Request'
    AlarmRequest.new attributes
  when 'Acknowledge'
    AlarmAcknowledged.new attributes
  when 'Suspend'
    AlarmSuspend.new attributes
  when 'Resume'
    AlarmResume.new attributes
  else
    Alarm.new attributes
  end
end

.make_m_idObject



12
13
14
# File 'lib/rsmp/message.rb', line 12

def self.make_m_id
  SecureRandom.uuid()
end

.parse_attributes(json) ⇒ Object



16
17
18
19
20
21
# File 'lib/rsmp/message.rb', line 16

def self.parse_attributes json
  raise ArgumentError unless json
  JSON.parse json
rescue JSON::ParserError
  raise InvalidPacket, bin_to_chars(json)
end

.shorten_m_id(m_id, length = 4) ⇒ Object



87
88
89
# File 'lib/rsmp/message.rb', line 87

def self.shorten_m_id m_id, length=4
  m_id[0..length-1]
end

.validate_message_type(attributes) ⇒ Object

Raises:



119
120
121
122
123
124
125
126
# File 'lib/rsmp/message.rb', line 119

def self.validate_message_type attributes
  raise MalformedMessage.new("JSON must be a Hash, got #{attributes.class} ") unless attributes.is_a?(Hash)
  raise MalformedMessage.new("'mType' is missing") unless attributes["mType"]
  raise MalformedMessage.new("'mType' must be a String, got #{attributes["mType"].class}") unless attributes["mType"].is_a? String
  raise MalformedMessage.new("'mType' must be 'rSMsg', got '#{attributes["mType"]}'") unless attributes["mType"] == "rSMsg"
  raise MalformedMessage.new("'type' is missing") unless attributes["type"]
  raise MalformedMessage.new("'type' must be a String, got #{attributes["type"].class}") unless attributes["type"].is_a? String
end

Instance Method Details

#attribute(key) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
# File 'lib/rsmp/message.rb', line 95

def attribute key
  unless @attributes.key? key # note that this is not the same as @attributes[key] when
    maybe = @attributes.find { |k,v| k.downcase == key.downcase }
    if maybe
      raise MissingAttribute.new "attribute '#{maybe.first}' should be named '#{key}'"
    else
      raise MissingAttribute.new "missing attribute '#{key}'"
    end
  end
  @attributes[key]
end

#ensure_message_idObject



137
138
139
140
# File 'lib/rsmp/message.rb', line 137

def ensure_message_id
  # if message id is empty, generate a new one
  @attributes["mId"] ||= Message.make_m_id
end

#generate_jsonObject



162
163
164
# File 'lib/rsmp/message.rb', line 162

def generate_json
  @json = JSON.generate @attributes
end

#m_idObject



83
84
85
# File 'lib/rsmp/message.rb', line 83

def m_id
  @attributes["mId"]
end

#m_id_shortObject



91
92
93
# File 'lib/rsmp/message.rb', line 91

def m_id_short
  Message.shorten_m_id @attributes["mId"]
end

#typeObject



79
80
81
# File 'lib/rsmp/message.rb', line 79

def type
  @attributes["type"]
end

#valid?Boolean

Returns:

  • (Boolean)


158
159
160
# File 'lib/rsmp/message.rb', line 158

def valid?
  true
end

#validate(schemas) ⇒ Object



142
143
144
145
146
147
148
# File 'lib/rsmp/message.rb', line 142

def validate schemas
  errors = RSMP::Schemer.validate attributes, schemas
  if errors
    error_string = errors.map {|item| item.reject {|e| e=='' } }.compact.join(', ').strip
    raise SchemaError.new error_string
  end
end

#validate_idObject



154
155
156
# File 'lib/rsmp/message.rb', line 154

def validate_id
   (@attributes["mId"] =~ /[a-f0-9]{8}-[a-f0-9]{4}-4[a-f0-9]{3}-[89ab][a-f0-9]{3}-[a-f0-9]{12}/i) != nil
end

#validate_typeObject



150
151
152
# File 'lib/rsmp/message.rb', line 150

def validate_type
  @attributes["mType"] == "rSMsg"
end