Module: RSMP::SiteProxyWait

Included in:
SiteProxy
Defined in:
lib/rsmp/site_proxy_wait.rb

Instance Method Summary collapse

Instance Method Details

#collect_command_responses(parent_task, options, m_id) ⇒ Object


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
65
66
67
68
69
70
71
72
73
74
# File 'lib/rsmp/site_proxy_wait.rb', line 30

def collect_command_responses parent_task, options, m_id
  task.annotate "wait for command response"
  want = options[:command_list].clone
  result = {}
  messages = []
  collect(parent_task,options.merge({
    type: ['CommandResponse','MessageNotAck'],
    num: 1
  })) do |message|
    if message.is_a?(MessageNotAck)
      if message.attribute('oMId') == m_id
        # set result to an exception, but don't raise it.
        # this will be returned by the task and stored as the task result
        # when the parent task call wait() on the task, the exception
        # will be raised in the parent task, and caught by rspec.
        # rspec will then show the error and record the test as failed
        m_id_short = RSMP::Message.shorten_m_id m_id, 8
        result = RSMP::MessageRejected.new "Command request #{m_id_short} was rejected: #{message.attribute('rea')}"
        next true   # done, no more messages wanted
      else
        false
      end
    else
      add = false
      # look through querues
      want.each_with_index do |query,i|
        # look through items in message
        message.attributes['rvs'].each do |input|
          matching = command_match? query, input
          if matching == true
            result[query] = input
            add = true
          elsif matching == false
            result.delete query
          end
        end
      end
      messages << message if add
      result.size == want.size # any queries left to match?
    end
  end
  return result, messages
rescue Async::TimeoutError
  raise RSMP::TimeoutError.new "Did not receive correct command response to #{m_id} within #{options[:timeout]}s"
end

#collect_status_responses(task, options, m_id) ⇒ Object


26
27
28
# File 'lib/rsmp/site_proxy_wait.rb', line 26

def collect_status_responses task, options, m_id
  collect_status_updates_or_responses task, 'StatusResponse', options, m_id
end

#collect_status_updates(task, options, m_id) ⇒ Object


22
23
24
# File 'lib/rsmp/site_proxy_wait.rb', line 22

def collect_status_updates task, options, m_id
  collect_status_updates_or_responses task, 'StatusUpdate', options, m_id
end

#collect_status_updates_or_responses(task, type, options, m_id) ⇒ Object


76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/rsmp/site_proxy_wait.rb', line 76

def collect_status_updates_or_responses task, type, options, m_id
  want = options[:status_list].clone
  result = {}
  messages = []
  # wait for a status update
  collect(task,options.merge({
    type: [type,'MessageNotAck'],
    num: 1
  })) do |message|
    if message.is_a?(MessageNotAck)
      if message.attribute('oMId') == m_id
        # set result to an exception, but don't raise it.
        # this will be returned by the task and stored as the task result
        # when the parent task call wait() on the task, the exception
        # will be raised in the parent task, and caught by rspec.
        # rspec will then show the error and record the test as failed
        m_id_short = RSMP::Message.shorten_m_id m_id, 8
        result = RSMP::MessageRejected.new "Status request #{m_id_short} was rejected: #{message.attribute('rea')}"
        next true   # done, no more messages wanted
      end
      false
    else
      found = []
      add = false
      # look through querues
      want.each_with_index do |query,i|
        # look through status items in message
        message.attributes['sS'].each do |input|
          matching = status_match? query, input
          if matching == true
            result[query] = input
            add = true
          elsif matching == false
            result.delete query
          end
        end
      end
      messages << message if add
      result.size == want.size # any queries left to match?
    end
  end
  return result, messages
rescue Async::TimeoutError
  type_str = {'StatusUpdate'=>'update', 'StatusResponse'=>'response'}[type]
  raise RSMP::TimeoutError.new "Did not received correct status #{type_str} in reply to #{m_id} within #{options[:timeout]}s"
end

#command_match?(query, item) ⇒ Boolean

Returns:

  • (Boolean)

135
136
137
138
139
140
141
142
143
144
# File 'lib/rsmp/site_proxy_wait.rb', line 135

def command_match? query, item
  return nil if query['cCI'] && query['cCI'] != item['cCI']
  return nil if query['n'] && query['n'] != item['n']
  if query['v'].is_a? Regexp
    return false if query['v'] && item['v'] !~ query['v']
  else
    return false if query['v'] && item['v'] != query['v']
  end
  true
end

#status_match?(query, item) ⇒ Boolean

Returns:

  • (Boolean)

123
124
125
126
127
128
129
130
131
132
133
# File 'lib/rsmp/site_proxy_wait.rb', line 123

def status_match? query, item
  return nil if query['sCI'] && query['sCI'] != item['sCI']
  return nil if query['n'] && query['n'] != item['n']
  return false if query['q'] && query['q'] != item['q']
  if query['s'].is_a? Regexp
    return false if query['s'] && item['s'] !~ query['s']
  else
    return false if query['s'] && item['s'] != query['s']
  end
  true
end

#wait_for_aggregated_status(parent_task, options, m_id) ⇒ Object


146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/rsmp/site_proxy_wait.rb', line 146

def wait_for_aggregated_status parent_task, options, m_id
  collect(parent_task,options.merge({
    type: ['AggregatedStatus','MessageNotAck'],
    num: 1
  })) do |message|
    if message.is_a?(MessageNotAck)
      if message.attribute('oMId') == m_id
        # set result to an exception, but don't raise it.
        # this will be returned by the task and stored as the task result
        # when the parent task call wait() on the task, the exception
        # will be raised in the parent task, and caught by rspec.
        # rspec will then show the error and record the test as failed
        m_id_short = RSMP::Message.shorten_m_id m_id, 8
        result = RSMP::MessageRejected.new "Aggregated status request #{m_id_short} was rejected: #{message.attribute('rea')}"
        next true   # done, no more messages wanted
      else
        false
      end
    else
      true
    end
  end
end

#wait_for_alarm(parent_task, options = {}) ⇒ Object


5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/rsmp/site_proxy_wait.rb', line 5

def wait_for_alarm parent_task, options={}
  matching_alarm = nil
  message = collect(parent_task,options.merge(type: "Alarm", with_message: true, num: 1)) do |message|
    # TODO check components
    matching_alarm = nil
    alarm = message
    next if options[:aCId] && options[:aCId] != alarm.attribute("aCId")
    next if options[:aSp] && options[:aSp] != alarm.attribute("aSp")
    next if options[:aS] && options[:aS] != alarm.attribute("aS")
    matching_alarm = alarm
    break
  end
  if item
    { message: message, status: matching_alarm }
  end
end