Class: Ardtweeno::NodeManager

Inherits:
Object
  • Object
show all
Defined in:
lib/ardtweeno/nodemanager.rb

Overview

Ardtweeno::NodeManager class for the Ardtweeno Mesh Network

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ NodeManager

Ardtweeno::NodeManager#new constructor

  • Args :

    • ++ -> options HashLogger, :nodelist Array

  • Returns : -

  • Raises :



53
54
55
56
57
58
59
60
# File 'lib/ardtweeno/nodemanager.rb', line 53

def initialize(options={})
  @log = Ardtweeno.options[:log] ||= Logger.new(STDOUT)
  @log.level = Ardtweeno.options[:level] ||= Logger::DEBUG
  
  @watchlist = Array.new
  
  @nodeList = options[:nodelist] ||= Array.new 
end

Instance Attribute Details

#logObject

Returns the value of attribute log.



41
42
43
# File 'lib/ardtweeno/nodemanager.rb', line 41

def log
  @log
end

#nodeListObject

Returns the value of attribute nodeList.



41
42
43
# File 'lib/ardtweeno/nodemanager.rb', line 41

def nodeList
  @nodeList
end

#watchlistObject

Returns the value of attribute watchlist.



41
42
43
# File 'lib/ardtweeno/nodemanager.rb', line 41

def watchlist
  @watchlist
end

#zonesObject

Returns the value of attribute zones.



41
42
43
# File 'lib/ardtweeno/nodemanager.rb', line 41

def zones
  @zones
end

Instance Method Details

#addNode(node) ⇒ Object

Ardtweeno::NodeManager#addNode adds an Ardtweeno::Node to the managed node list

  • Args :

    • ++ -> Ardtweeno::Node

  • Returns :

    • true || false

  • Raises :

    • Ardtweeno::NotANode



91
92
93
94
95
96
97
98
99
100
101
# File 'lib/ardtweeno/nodemanager.rb', line 91

def addNode(node)
  if node.class == Ardtweeno::Node
    @log.debug "Size of nodeList before addition: #{@nodeList.size}"
    @nodeList << node
    @log.debug "Size of nodeList after addition: #{@nodeList.size}"
    
    return true
  else
    raise Ardtweeno::NotANode, "Error Ardtweeno::NodeManager#addNode expects an Ardtweeno::Node as a parameter"
  end
end

#addWatch(params) ⇒ Object

Ardtweeno::NodeManager#addWatch adds a node to the watchlist

  • Args :

    • ++ -> String node, Hash watch { String :node, String :notifyURL,

      String :method, String :timeouts }
      
  • Returns : -

  • Raises :

    • Ardtweeno::NotInNodeList



220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/ardtweeno/nodemanager.rb', line 220

def addWatch(params)
  begin
    node = search({:node=>params[:node]})
    @log.debug "Found Node: " + node.inspect
    
    if watched?(params[:node])
      raise Ardtweeno::AlreadyWatched
    end
    
    watch = { :node=>params[:node], 
              :notifyURL=> params[:notifyURL],
              :method=>params[:method], 
              :timeout=>params[:timeout] 
            }
    
    @log.debug "Adding watch: " + watch.inspect
    
    @watchlist << watch
  
  rescue Ardtweeno::AlreadyWatched => e
    raise e
  rescue Ardtweeno::NotInNodeList => e
    raise e
  rescue Exception => e
    raise e
  end
end

#flushObject

Ardtweeno::NodeManager#flush empties the packetqueue inside each Ardtweeno:Node

  • Args :

    • ++ ->

  • Returns : -

  • Raises : -



73
74
75
76
77
# File 'lib/ardtweeno/nodemanager.rb', line 73

def flush()
  @nodeList.each do |i|
    i.packetqueue = Array.new
  end
end

#pushNotification(node) ⇒ Object

Ardtweeno::NodeManager#pushNotification pushes a notification to the node watcher

  • Args :

    • ++ -> String node

  • Returns : -

  • Raises : -



309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
# File 'lib/ardtweeno/nodemanager.rb', line 309

def pushNotification(node)
  
  @log.debug "Traversing watchlist"
  @watchlist.each do |i|
    
    @log.debug "Comparing " + i[:node] + " to " + node
            
    if i[:node] == node
      @log.debug "Ensuring the timeout cooloff period is respected"
      now = Time.now.to_i
      
      if i.has_key?(:last_push)
        timeout = i[:last_push] + i[:timeout].to_i
        
        if timeout > now
          @log.debug "We are within the cooloff period, ignoring the push notification"
          return 
        end
      end          
      
      @log.debug "Associated watch found, checking for method " + i[:method]
      if i[:method] == "POST"
        @log.debug "HTTP POST method executing"
        Typhoeus::Request.post(i[:notifyURL], 
                                 :body=> { :title=>"Push notification",
                                           :content=>"#{i[:node]}",
                                           :code=>""})
      elsif i[:method] == "GET"
        @log.debug "HTTP GET method executing" 
        Typhoeus::Request.get(i[:notifyURL], 
                                :body=> { :title=>"Push notification",
                                          :content=>"#{i[:node]}",
                                          :code=>""})
      end
      
      i[:last_push] = now

    end
  end
  
end

#removeNode(options = {}) ⇒ Object

Ardtweeno::NodeManager#removeNode removes an Ardtweeno::Node from the managed nodeList

  • Args :

    • ++ -> :key

  • Returns :

    • Ardtweeno::Node

  • Raises :

    • Ardtweeno::NotInNodeList



114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/ardtweeno/nodemanager.rb', line 114

def removeNode(options={})
  @log.debug "removeNode function called, searching for node in list"
  
  begin
    found = search(options)
    @nodeList.delete(found)
    
    return found
    
  rescue Ardtweeno::NotInNodeList
    return nil
  end
  
end

#removeWatch(node) ⇒ Object

Ardtweeno::NodeManager#removeWatch removes a node from the watchlist

  • Args :

    • ++ -> String node

  • Returns : -

  • Raises :

    • Ardtweeno::NotInNodeList



259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# File 'lib/ardtweeno/nodemanager.rb', line 259

def removeWatch(node)
  begin
    @log.debug "removeWatch called, querying the node manager for the correct node"
    @log.debug "Searching watchlist for node"
    @log.debug "Size of watchlist before: " + @watchlist.size.to_s
    
    @watchlist.each do |i|
      @log.debug "Comparing #{i[:node]} to #{node}"
      if i[:node] == node
        @log.debug "Node to be deleted has been found, deleting"
        @watchlist.delete(i)
      end
    end
    
    @log.debug "Size of watchlist after: " + @watchlist.size.to_s
    
  rescue Ardtweeno::NotInNodeList => e
    raise e
  end
end

#search(options = {}) ⇒ Object

Ardtweeno::NodeManager#search provides an interface to search the managed Ardtweeno::Node list takes a single parameter hash, expects :node or :key search strings corresponding with the Ardtweeno::Node you wish to search for, and returns this Node if found Raises Ardtweeno::NotInNodeList exception if entry not found in the list

  • Args :

    • ++ -> options HashString, :key String

  • Returns :

    • Ardtweeno::Node

  • Raises :

    • Ardtweeno::NotInNodeList



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/ardtweeno/nodemanager.rb', line 143

def search(options={})
  key = options[:key] ||= nil
  node = options[:node] ||= nil
  
  if key.nil? and node.nil? then 
    raise Ardtweeno::NotInNodeList, "Error key does not match any Ardtweeno::Node being maintained by this Ardtweeno::NodeManager"
  end
  
  unless key.nil? then 
    @log.debug "Searching the Nodelist for key: " + key
    
    @nodeList.each do |i|
      @log.debug "Comparing #{key} with #{i.key}"
      if i.key == key
        @log.debug "Match found, returning node"
        return i       
      end
    end
    
  end
  
  unless node.nil? then
    @log.debug "Searching the Nodelist for node: " + node
    
    @nodeList.each do |i|
      @log.debug "Comparing #{node} with #{i.node}"
      if i.node == node
        @log.debug "Match found, returning #{i.to_s}"
        return i       
      end
    end
  end
  
  
  @log.debug "Node not found!"
  # Raise NotInNodeList exception if list has been traversed and a corresponding node was
  # not found
  raise Ardtweeno::NotInNodeList, "Error key does not match any Ardtweeno::Node being maintained by this Ardtweeno::NodeManager"
end

#watched?(node) ⇒ Boolean

Ardtweeno::NodeManager#watched? checks if a node has been added to a watchlist

  • Args :

    • ++ -> Ardtweeno::Node node

  • Returns :

    • True || False

  • Raises : -

Returns:

  • (Boolean)


194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/ardtweeno/nodemanager.rb', line 194

def watched?(node)
  
  @watchlist.each do |i|
    
    unless node.nil? then @log.debug "Comparing " + i[:node] + " and " + node; end
    if i[:node] == node
      return true
    end
  end
  
  return false
  
end

#watchListObject

Ardtweeno::NodeManager#watchList returns the list of nodes being watched

  • Args :

    • ++ ->

  • Returns :

    • Array of Hash { String :node, String :notifyURL,

      String :method, String :timeouts }
      
  • Raises : -



293
294
295
# File 'lib/ardtweeno/nodemanager.rb', line 293

def watchList
  return @watchlist
end