lib/torkify/conductor.rb in torkify-0.0.1 vs lib/torkify/conductor.rb in torkify-0.0.2
- old
+ new
@@ -1,33 +1,44 @@
-require_relative 'events/event'
-require_relative 'event_parser'
+require_relative 'event/basic_event'
+require_relative 'event/parser'
+require_relative 'event/dispatcher'
+require_relative 'log/parser'
module Torkify
# Connect the socket reader and observers, and dispatch events.
class Conductor
- attr_accessor :observers
-
# Create with a set of observers.
def initialize(observers)
- @observers = observers
+ @dispatcher = Event::Dispatcher.new observers
end
+ def observers
+ @dispatcher.observers
+ end
+
+ def observers=(*args)
+ @dispatcher.send :observers=, *args
+ end
+
# Start reading from the reader, which is an IO-like object.
#
# Parse each line and dispatch it as an event object to all observers.
def start(reader)
- dispatch Event.new 'startup'
- parser = EventParser.new
- reader.each_line do |line|
- event = parser.parse line
- dispatch event
- end
- dispatch Event.new 'shutdown'
+ dispatch Event::BasicEvent.new 'startup'
+ parser = Event::Parser.new
+
+ while line = reader.gets
+ Torkify.logger.debug { "Read line: #{line}" }
+ events = parser.parse line
+ dispatch(*events)
+ end
+
+ dispatch Event::BasicEvent.new 'shutdown'
end
protected
- def dispatch(event)
- @observers.dispatch(event)
+ def dispatch(*events)
+ events.each { |event| @dispatcher.dispatch(event) }
end
end
end