summaryrefslogtreecommitdiffstats
path: root/lib/blink.rb
blob: 74c03f2de723343fa0fc4ff663c5cef4439597c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#!/usr/local/bin/ruby -w

# $Id$

require 'singleton'
#require 'blink/component'
#require 'blink/interface'
#require 'blink/selector'
#require 'blink/type/service'
#require 'blink/type/file'
#require 'blink/type/symlink'

# XXX see the bottom of the file for further inclusions

PINK=""
GREEN=""
YELLOW=""
SLATE=""
ORANGE=""
BLUE=""
RESET=""

#------------------------------------------------------------
# the top-level module
#
# all this really does is dictate how the whole system behaves, through
# preferences for things like debugging
#
# it's also a place to find top-level commands like 'debug'
module Blink
    # the hash that determines how our system behaves
    @@config = Hash.new(false)


    msglevels = [:debug,:verbose,:notice,:warning,:error]

    # handle the different message levels
    msglevels.each { |level|
        define_method(level,proc { |args|
            Blink.message(level,args)
        })
        module_function level
        # default to enabling all notice levels except debug
        @@config[level] = true unless level == :debug
    }

	def Blink.message(level,*ary)
		msg = ary.join(" ")

		if @@config[level]
			Blink::Message.new(
				:level => level,
				:source => "Blink",
				:message => msg
			)
		end
	end

    # set up our configuration
	def Blink.init(args)
		args.each {|p,v|
			@@config[p] = v
		}
	end

    # just print any messages we get
    # we should later behave differently depending on the message
	def Blink.newmessage(msg)
		puts msg
	end

    # DISABLED

	# we've collected all data; now operate on it
#	def Blink.run
#		ops = Blink::Objects.genops()
#		ops.find_all { |op|
#			op.auto?()
#		}.each { |op|
#			Blink::Message.new(
#				:level => :debug,
#				:source => "Blink",
#				:message => "Running op %s" % op
#			)
#			op.check
#		}.find_all { |op|
#			puts "dirty? #{op}"
#			op.dirty?
#		}.collect { |op|
#			puts "%s is dirty; %s instead of %s" % [op, op.state, op.should]
#			op.fix
#		}.each { |event| # this might need to support lists someday...
#			#list.each { |event|
#				puts event
#				event.trigger
#			#}
#		}
#	end
#
#	def Blink.walk
#		root = Blink::Objects.root
#		root.check
#		if root.dirty?
#			Blink::Message.new(
#				:message => "someone's dirty",
#				:level => :notice,
#				:source => root
#			)
#			root.fix
#		end
#	end

	# configuration parameter access and stuff
	def Blink.[](param)
		return @@config[param]
	end

	# configuration parameter access and stuff
	def Blink.[]=(param,value)
		@@config[param] = value
	end

    # a simple class for creating callbacks
	class Event
		attr_reader :event, :object
		attr_writer :event, :object

		def initialize(args)
			@event = args[:event]
			@object = args[:object]

			if @event.nil? or @object.nil?
				raise "Event.new called incorrectly"
			end
		end

		def trigger
			@object.trigger(@event)
		end
	end

    # a class for storing state
    # not currently used
	class Storage
		include Singleton
		@@config = "/var/tmp/blinkstate"
		@@state = Hash.new
		@@splitchar = " "
		
		def initialize
			self.load
		end

		def Storage.load
			puts "loading state"
			return unless File.exists?(@@config)
			File.open(@@config) { |file|
				file.gets { |line|
					myclass, key, value = line.split(@@splitchar)

					unless defined? @@state[myclass]
						@@state[myclass] = Hash.new
					end

					@@state[myclass][key] = value
				}
			}
		end

		def Storage.state(myclass)
			unless defined? @@state[myclass]
				@@state[myclass] = Hash.new
			end
			return @@state[myclass]
		end

		def Storage.store
			File.open(@@config, File::CREAT|File::WRONLY, 0644) { |file|
				@@state.each { |key, value|
					file.puts([self.class,key,value].join(@@splitchar))
				}
			}
		end
	end

    #------------------------------------------------------------
    # provide feedback of various types to the user
    # modeled after syslog messages
    # each level of message prints in a different color
	class Message
		@@messages = Array.new
		@@levels = [ :debug, :verbose, :notice, :warning, :error ]
		@@colors = {
			:debug => SLATE,
			:verbose => ORANGE,
			:notice => PINK,
			:warning => GREEN,
			:error => YELLOW
		}

		attr_reader :level, :message
		attr_writer :level, :message

		def initialize(args)
			unless args.include?(:level) && args.include?(:message) &&
						args.include?(:source) 
				raise "Blink::Message called incorrectly"
			end

			if args[:level].class == String
				@level = args[:level].intern
			elsif args[:level].class == Symbol
				@level = args[:level]
			else
				raise "Level is not a string or symbol: #{args[:level].class}"
			end
			@message = args[:message]
			@source = args[:source]
			@time = Time.now
			# this should include the host name, and probly lots of other
			# stuff, at some point
			unless @@levels.include?(level)
				raise "Invalid message level #{level}"
			end

			@@messages.push(self)
			Blink.newmessage(self)
		end

		def to_s
			# this probably won't stay, but until this leaves the console,
			# i'm going to use coloring...
			#return "#{@time} #{@source} (#{@level}): #{@message}"
			#return @@colors[@level] + "%s %s (%s): %s" % [
			#	@time, @source, @level, @message
			#] + RESET
			return @@colors[@level] + "%s (%s): %s" % [
				@source, @level, @message
			] + RESET
		end
	end
    #------------------------------------------------------------

    #------------------------------------------------------------
    class Modification
        attr_accessor :object, :type, :from, :to

        def initialize(object)
            @object = object
            @type = object.class
            @from = object.is?
            @to = object.should?
        end
    end
    #------------------------------------------------------------
end

require 'blink/type'