Class: UWA::Server

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/uwa/server.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeServer

Returns a new instance of Server.



13
14
15
16
17
18
19
20
# File 'lib/uwa/server.rb', line 13

def initialize
	@host = '0.0.0.0'
	@port = 42080
	@debug = true
	@reload = true
	@widgets = []
	@plugins = UWA::Plugin.load
end

Instance Attribute Details

#debugObject

Returns the value of attribute debug.



11
12
13
# File 'lib/uwa/server.rb', line 11

def debug
  @debug
end

#hostObject

Returns the value of attribute host.



11
12
13
# File 'lib/uwa/server.rb', line 11

def host
  @host
end

#pluginsObject

Returns the value of attribute plugins.



11
12
13
# File 'lib/uwa/server.rb', line 11

def plugins
  @plugins
end

#portObject

Returns the value of attribute port.



11
12
13
# File 'lib/uwa/server.rb', line 11

def port
  @port
end

#reloadObject

Returns the value of attribute reload.



11
12
13
# File 'lib/uwa/server.rb', line 11

def reload
  @reload
end

Class Method Details

.method_missing(name, *args) ⇒ Object



7
8
9
# File 'lib/uwa/server.rb', line 7

def self.method_missing(name, *args)
	self.instance.send(name, *args)
end

Instance Method Details

#<<(config) ⇒ Object



22
23
24
25
26
27
28
29
30
# File 'lib/uwa/server.rb', line 22

def <<(config)
	begin
		@widgets << @plugins[config[:name]].merge(config)
	rescue
		log(:warning, "Unable to register #{config[:name].inspect} at #{config[:path].inspect}")
	else
		log(:info, "Register #{config[:name].inspect} at #{config[:path].inspect}")
	end
end

#log(level, msg) ⇒ Object



58
59
60
61
62
63
64
65
66
# File 'lib/uwa/server.rb', line 58

def log(level, msg)
	return unless @debug
	if msg.is_a? Array
		msg.each { |l| STDERR.puts "       " + l }
	else
		STDERR.puts "#{Time.now}: #{level.to_s.upcase}: #{msg}"
	end
	STDERR.flush
end

#runObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/uwa/server.rb', line 32

def run
	m = nil
	begin
		$mongrel_debug_client = @debug
		m = Mongrel::HttpServer.new(@host, @port) if m.nil?
		m.register('/', UWA::Proxy.new)
		@widgets.each do |widget|
			object = widget[:class].new
			object.script = widget[:script]
			object.css = widget[:css]
			widget[:params].to_a.each do |name, value|
				object.instance_variable_set(('@' + name.to_s).to_sym, value)
			end
			m.register(widget[:path], object)
		end
		m.run.join
	rescue
		m.graceful_shutdown unless m.nil?
		log :error , $!.message
		log :error, $!.backtrace
		log :error, "Retrying in 5 seconds..."
		sleep(5)
		retry
	end
end