Class: Tamashii::Agent::Component
Defined Under Namespace
Classes: LoadDeviceError
Instance Method Summary
collapse
#logger, #progname
Constructor Details
#initialize(name, master, options = {}) ⇒ Component
Returns a new instance of Component.
12
13
14
15
16
17
|
# File 'lib/tamashii/agent/component.rb', line 12
def initialize(name, master, options = {})
@name = name
@master = master
@options = options
@event_queue = Queue.new
end
|
Instance Method Details
#check_new_event(non_block = false) ⇒ Object
23
24
25
26
27
|
# File 'lib/tamashii/agent/component.rb', line 23
def check_new_event(non_block = false)
@event_queue.pop(non_block)
rescue ThreadError => e
nil
end
|
#clean_up ⇒ Object
64
65
|
# File 'lib/tamashii/agent/component.rb', line 64
def clean_up
end
|
#default_device_name ⇒ Object
104
105
106
|
# File 'lib/tamashii/agent/component.rb', line 104
def default_device_name
raise NotImplementedError
end
|
#display_name ⇒ Object
112
113
114
|
# File 'lib/tamashii/agent/component.rb', line 112
def display_name
@name
end
|
#get_device_class_name(device_name) ⇒ Object
108
109
110
|
# File 'lib/tamashii/agent/component.rb', line 108
def get_device_class_name(device_name)
raise NotImplementedError
end
|
#get_device_instance(device_name) ⇒ Object
92
93
94
95
|
# File 'lib/tamashii/agent/component.rb', line 92
def get_device_instance(device_name)
klass = Common.load_device_class(get_device_class_name(device_name))
klass.new(self, @options)
end
|
#handle_new_event(non_block = false) ⇒ Object
29
30
31
32
33
34
|
# File 'lib/tamashii/agent/component.rb', line 29
def handle_new_event(non_block = false)
if ev = check_new_event(non_block)
process_event(ev)
end
ev
end
|
#initialize_device ⇒ Object
81
82
83
84
85
86
87
88
89
90
|
# File 'lib/tamashii/agent/component.rb', line 81
def initialize_device
device_name = @options[:device] || default_device_name
logger.info "Using device: #{device_name}"
get_device_instance(device_name)
rescue => e
logger.error "Error when loading device: #{e.message}"
e.backtrace.each {|msg| logger.error msg}
logger.error "Fallback to default: #{default_device_name}"
load_default_device
end
|
#load_default_device ⇒ Object
97
98
99
100
101
102
|
# File 'lib/tamashii/agent/component.rb', line 97
def load_default_device
logger.info "loading default device: #{default_device_name}"
get_device_instance(default_device_name)
rescue => e
raise LoadDeviceError, "Unable to load device: #{e.message}"
end
|
#process_event(event) ⇒ Object
40
41
42
|
# File 'lib/tamashii/agent/component.rb', line 40
def process_event(event)
logger.debug "Got event: #{event.type}, #{event.body}"
end
|
#restart_current_component_async ⇒ Object
36
37
38
|
# File 'lib/tamashii/agent/component.rb', line 36
def restart_current_component_async
@master.send_event(Event.new(Event::RESTART_COMPONENT, @name))
end
|
#run ⇒ Object
45
46
47
|
# File 'lib/tamashii/agent/component.rb', line 45
def run
@worker_thr = Thread.start { run_worker_loop }
end
|
#run! ⇒ Object
49
50
51
|
# File 'lib/tamashii/agent/component.rb', line 49
def run!
run_worker_loop
end
|
#run_worker_loop ⇒ Object
67
68
69
|
# File 'lib/tamashii/agent/component.rb', line 67
def run_worker_loop
worker_loop
end
|
#send_event(event) ⇒ Object
19
20
21
|
# File 'lib/tamashii/agent/component.rb', line 19
def send_event(event)
@event_queue.push(event)
end
|
#stop ⇒ Object
53
54
55
56
57
|
# File 'lib/tamashii/agent/component.rb', line 53
def stop
logger.info "Stopping component #{@name}"
stop_threads
clean_up
end
|
#stop_threads ⇒ Object
59
60
61
62
|
# File 'lib/tamashii/agent/component.rb', line 59
def stop_threads
@worker_thr.exit if @worker_thr
@worker_thr = nil
end
|
#worker_loop ⇒ Object
72
73
74
75
76
77
78
79
|
# File 'lib/tamashii/agent/component.rb', line 72
def worker_loop
loop do
if !handle_new_event
logger.error "Thread error. Worker loop terminated"
break
end
end
end
|