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'
end

Instance Method Details

#any(&block) ⇒ Object



187
188
189
190
191
192
193
# File 'lib/arpoon.rb', line 187

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

	@any << block
end

#broadcast(*args) ⇒ Object



149
150
151
152
153
# File 'lib/arpoon.rb', line 149

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

#command(*args, &block) ⇒ Object



155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/arpoon.rb', line 155

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



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

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



145
146
147
# File 'lib/arpoon.rb', line 145

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

#interface(name, &block) ⇒ Object



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/arpoon.rb', line 170

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



131
132
133
134
135
136
137
138
139
# File 'lib/arpoon.rb', line 131

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
83
84
85
# 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.puts ''
	io.seek 0

	io.read.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



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

def reload_table!
	@table = Table.new
end

#routeObject



127
128
129
# File 'lib/arpoon.rb', line 127

def route
	Route.new
end

#startObject



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

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



105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/arpoon.rb', line 105

def stop
	return unless started?

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

	if @signature
		EM.stop_server @signature
	end

	@started = false
end

#tableObject



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

def table
	@table || reload_table!
end