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.


111
112
113
114
115
116
117
118
# File 'lib/rsmp/message.rb', line 111

def initialize attributes = {}
  @timestamp = Time.now   # this timestamp is for internal use, and does not 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


90
91
92
93
94
95
96
97
98
99
100
# File 'lib/rsmp/message.rb', line 90

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 = Alarm.new 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

.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


70
71
72
# File 'lib/rsmp/message.rb', line 70

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

.validate_message_type(attributes) ⇒ Object

Raises:


102
103
104
105
106
107
108
109
# File 'lib/rsmp/message.rb', line 102

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


78
79
80
81
82
83
84
85
86
87
88
# File 'lib/rsmp/message.rb', line 78

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


120
121
122
123
# File 'lib/rsmp/message.rb', line 120

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

#generate_jsonObject


145
146
147
# File 'lib/rsmp/message.rb', line 145

def generate_json
  @json = JSON.generate @attributes
end

#m_idObject


66
67
68
# File 'lib/rsmp/message.rb', line 66

def m_id
  @attributes["mId"]
end

#m_id_shortObject


74
75
76
# File 'lib/rsmp/message.rb', line 74

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

#typeObject


62
63
64
# File 'lib/rsmp/message.rb', line 62

def type
  @attributes["type"]
end

#valid?Boolean

Returns:

  • (Boolean)

141
142
143
# File 'lib/rsmp/message.rb', line 141

def valid?
  true
end

#validate(schemas) ⇒ Object


125
126
127
128
129
130
131
# File 'lib/rsmp/message.rb', line 125

def validate schemas
  errors = RSMP::Schemer.validate attributes, schemas
  if errors
    error_string = errors.compact.join(', ').strip
    raise SchemaError.new error_string
  end
end

#validate_idObject


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

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


133
134
135
# File 'lib/rsmp/message.rb', line 133

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