Class: Nmunch::Report

Inherits:
Object
  • Object
show all
Defined in:
lib/nmunch/report.rb

Overview

Public: Nmunch report class

Keeps track of discovered nodes and publishes nodes to registered subscribers

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Report

Public: Initialize a new report

options - Hash of command line options



12
13
14
15
16
# File 'lib/nmunch/report.rb', line 12

def initialize(options)
  @options          = options
  @subscribers      = []
  @discovered_nodes = []
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



7
8
9
# File 'lib/nmunch/report.rb', line 7

def options
  @options
end

Instance Method Details

#publish_node(node) ⇒ Object

Public: Publish a new node to all registered subscribers

node - An instance of Nmunch::Node

Already discovered nodes or nodes with meta addresses (e.g. DHCP requests) will be ignored.

Returns nothing



38
39
40
41
42
43
# File 'lib/nmunch/report.rb', line 38

def publish_node(node)
  if !node.meta_address? && !@discovered_nodes.include?(node)
    @subscribers.each { |s| s.process(node) }
    @discovered_nodes << node
  end
end

#register_subscriber(subscriber) ⇒ Object

Public: Register a new subscriber

subscriber - An object that responds to #process(node)

All subscribers will be notified of new nodes and it is up to each subscriber to do something meaningful with the node.

Returns nothing



26
27
28
# File 'lib/nmunch/report.rb', line 26

def register_subscriber(subscriber)
  @subscribers << subscriber
end

#summaryObject

Public: Get a summary

Returns a simple summary telling how many live nodes were discovered TODO: Make the summary more detailed



49
50
51
# File 'lib/nmunch/report.rb', line 49

def summary
  " Discovered #{@discovered_nodes.count} live node(s)"
end