lib/tamashii/agent/component.rb in tamashii-agent-0.3.0 vs lib/tamashii/agent/component.rb in tamashii-agent-0.3.1
- old
+ new
@@ -5,12 +5,16 @@
module Tamashii
module Agent
class Component
include Common::Loggable
- def initialize(master)
+ class LoadDeviceError < RuntimeError; end
+
+ def initialize(name, master, options = {})
+ @name = name
@master = master
+ @options = options
@event_queue = Queue.new
end
def send_event(event)
@event_queue.push(event)
@@ -28,11 +32,11 @@
end
ev
end
def restart_current_component_async
- @master.send_event(Event.new(Event::RESTART_COMPONENT, self.class))
+ @master.send_event(Event.new(Event::RESTART_COMPONENT, @name))
end
def process_event(event)
logger.debug "Got event: #{event.type}, #{event.body}"
end
@@ -45,11 +49,11 @@
def run!
run_worker_loop
end
def stop
- logger.info "Stopping component"
+ logger.info "Stopping component #{@name}"
stop_threads
clean_up
end
def stop_threads
@@ -70,9 +74,40 @@
if !handle_new_event
logger.error "Thread error. Worker loop terminated"
break
end
end
+ end
+
+ 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
+
+ def get_device_instance(device_name)
+ klass = Common.load_device_class(get_device_class_name(device_name))
+ klass.new(self, @options)
+ end
+
+ 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
+
+ def default_device_name
+ raise NotImplementedError
+ end
+
+ def get_device_class_name(device_name)
+ raise NotImplementedError
end
end
end
end