Class: Arpoon::Interface

Inherits:
Object
  • Object
show all
Defined in:
lib/arpoon/interface.rb

Defined Under Namespace

Classes: Handler

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, &block) ⇒ Interface

Returns a new instance of Interface.



38
39
40
41
42
43
# File 'lib/arpoon/interface.rb', line 38

def initialize (name, &block)
	@name   = name.to_s
	@events = Hash.new { |h, k| h[k] = [] }

	load &block if block
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(*args, &block) ⇒ Object



45
46
47
# File 'lib/arpoon/interface.rb', line 45

def method_missing (*args, &block)
	Arpoon.__send__ *args, &block
end

Instance Attribute Details

#captureObject (readonly)

Returns the value of attribute capture.



36
37
38
# File 'lib/arpoon/interface.rb', line 36

def capture
  @capture
end

#nameObject (readonly)

Returns the value of attribute name.



36
37
38
# File 'lib/arpoon/interface.rb', line 36

def name
  @name
end

Instance Method Details

#fire(name, *args) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/arpoon/interface.rb', line 80

def fire (name, *args)
	delete = []

	@events[name].each {|block|
		case block.call(*args)
		when :delete then delete << block
		when :stop   then break
		end
	}

	@events[name] -= delete

	self
end

#inspectObject



103
104
105
# File 'lib/arpoon/interface.rb', line 103

def inspect
	"#<#{self.class.name}: #{name}>"
end

#load(path = nil, &block) ⇒ Object



66
67
68
69
70
71
72
73
74
# File 'lib/arpoon/interface.rb', line 66

def load (path = nil, &block)
	if path
		instance_eval File.read(File.expand_path(path)), path, 1
	else
		instance_exec &block
	end

	self
end

#on(name = :anything, &block) ⇒ Object



76
77
78
# File 'lib/arpoon/interface.rb', line 76

def on (name = :anything, &block)
	@events[name] << block
end

#startObject



49
50
51
52
53
54
55
56
57
58
# File 'lib/arpoon/interface.rb', line 49

def start
	@capture = FFI::PCap::Live.new(device: @name.to_s, promisc: true, handler: FFI::PCap::Handler)
	@capture.nonblocking = true
	@capture.setfilter('arp')

	@handler = EM.watch @capture.selectable_fd, Handler, self
	@handler.notify_readable = true

	self
end

#stopObject



60
61
62
63
64
# File 'lib/arpoon/interface.rb', line 60

def stop
	@handler.detach

	self
end

#to_sObject



95
96
97
# File 'lib/arpoon/interface.rb', line 95

def to_s
	name
end

#to_symObject



99
100
101
# File 'lib/arpoon/interface.rb', line 99

def to_sym
	name.to_sym
end