Class: Arpoon

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/arpoon/version.rb,
lib/arpoon.rb,
lib/arpoon/route.rb,
lib/arpoon/table.rb,
lib/arpoon/client.rb,
lib/arpoon/packet.rb,
lib/arpoon/interface.rb,
lib/arpoon/controller.rb

Overview

          DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
                  Version 2, December 2004

          DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
 TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION

0. You just DO WHAT THE FUCK YOU WANT TO.

++

Defined Under Namespace

Classes: Client, Controller, Interface, Packet, Route, Table

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeArpoon

Returns a new instance of Arpoon.



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
# File 'lib/arpoon.rb', line 30

def initialize
	@commands    = {}
	@connections = []
	@interfaces  = {}
	@any         = []

	reload_table!

	any {
		on :packet do |packet|
			if packet.request?
				packet.interface.fire :request, packet, packet.interface
			else
				packet.interface.fire :reply, packet, packet.interface
			end

			if packet.destination.broadcast?
				packet.interface.fire :broadcast, packet, packet.interface
			end
		end
	}

	controller_at '/var/run/arpoon.ctl'
	logs_at       '/var/log/arpoon.log'
end

Instance Attribute Details

#interfacesObject (readonly)

Returns the value of attribute interfaces.



28
29
30
# File 'lib/arpoon.rb', line 28

def interfaces
  @interfaces
end

Class Method Details

.method_missing(*args, &block) ⇒ Object



24
25
26
# File 'lib/arpoon.rb', line 24

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

.versionObject



12
13
14
# File 'lib/arpoon/version.rb', line 12

def self.version
	'0.0.1.1'
end

Instance Method Details

#any(&block) ⇒ Object



184
185
186
187
188
189
190
# File 'lib/arpoon.rb', line 184

def any (&block)
	@interfaces.each_value {|interface|
		interface.load(&block)
	}

	@any << block
end

#broadcast(*args) ⇒ Object



146
147
148
149
150
# File 'lib/arpoon.rb', line 146

def broadcast (*args)
	@connections.each {|conn|
		conn.send_response(*args)
	}
end

#command(*args, &block) ⇒ Object



152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/arpoon.rb', line 152

def command (*args, &block)
	if block
		command = args.first.to_sym

		@commands[command] = block
	else
		controller = args.shift
		command    = args.shift

		controller.instance_exec *args, &@commands[command.to_sym]
	end
rescue Exception => e
	log e, "command: #{command}"
end

#connected(controller) ⇒ Object



138
139
140
# File 'lib/arpoon.rb', line 138

def connected (controller)
	@connections << controller
end

#controller_at(path) ⇒ Object



56
57
58
# File 'lib/arpoon.rb', line 56

def controller_at (path)
	@controller_at = File.expand_path(path)
end

#disconnected(controller) ⇒ Object



142
143
144
# File 'lib/arpoon.rb', line 142

def disconnected (controller)
	@connections.delete(controller)
end

#interface(name, &block) ⇒ Object



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/arpoon.rb', line 167

def interface (name, &block)
	unless @interfaces.member? name
		@interfaces[name] = Interface.new(name)

		@any.each {|block|
			@interfaces[name].load(&block)
		}

		EM.schedule {
			@interfaces[name].start if started?
		}
	end

	@interfaces[name].load(&block) if block
	@interfaces[name]
end

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



128
129
130
131
132
133
134
135
136
# File 'lib/arpoon.rb', line 128

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

	self
end

#log(what, group = nil) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/arpoon.rb', line 65

def log (what, group = nil)
	io = StringIO.new

	io.print "[#{Time.now}#{", #{group}" if group}] "

	if what.is_a? Exception
		io.puts "#{what.class.name}: #{what.message}"
		io.puts what.backtrace
	else
		io.puts what
	end

	io.string.tap {|text|
		$stderr.puts text

		File.open(@logs_at, 'a') { |f| f.print text }
	}
end

#logs_at(path) ⇒ Object



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

def logs_at (path)
	@logs_at = File.expand_path(path)
end

#reload_table!Object



120
121
122
# File 'lib/arpoon.rb', line 120

def reload_table!
	@table = Table.new
end

#routeObject



124
125
126
# File 'lib/arpoon.rb', line 124

def route
	Route.new
end

#startObject



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/arpoon.rb', line 84

def start
	return if started?

	@started = true

	@interfaces.each_value {|interface|
		interface.start
	}

	File.umask(0).tap {|old|
		begin
			@signature = EM.start_server(@controller_at, Controller)
		ensure
			File.umask(old)
		end
	}
end

#started?Boolean

Returns:

  • (Boolean)


63
# File 'lib/arpoon.rb', line 63

def started?; @started; end

#stopObject



102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/arpoon.rb', line 102

def stop
	return unless started?

	@interfaces.each_value {|interface|
		interface.stop
	}

	if @signature
		EM.stop_server @signature
	end

	@started = false
end

#tableObject



116
117
118
# File 'lib/arpoon.rb', line 116

def table
	@table || reload_table!
end