Sha256: a740417f9557d710e6321667e213c22e28473fbf2d5bbec3db37ad1cce2afb42

Contents?: true

Size: 1.31 KB

Versions: 1

Compression:

Stored size: 1.31 KB

Contents

require 'logger'

require 'whirled_peas/frame'
require 'whirled_peas/ui'
require 'whirled_peas/version'

module WhirledPeas
  class Error < StandardError; end

  DEFAULT_HOST = 'localhost'
  DEFAULT_PORT = 8765
  DEFAULT_REFRESH_RATE = 30


  def self.start(driver, template_factory, log_level: Logger::INFO, refresh_rate: DEFAULT_REFRESH_RATE, host: DEFAULT_HOST, port: DEFAULT_PORT)
    logger = Logger.new(File.open('whirled_peas.log', 'a'))
    logger.level = log_level
    logger.formatter = proc do |severity, datetime, progname, msg|
      "[#{severity}] #{datetime.strftime('%Y-%m-%dT%H:%M:%S.%L')} (#{progname}) - #{msg}\n"
    end

    consumer = Frame::Consumer.new(template_factory, refresh_rate, logger)
    consumer_thread = Thread.new { consumer.start(host: host, port: port) }

    Frame::Producer.start(logger: logger, host: host, port: port) do |producer|
      begin
        driver.start(producer)
        producer.stop
      rescue => e
        logger.warn('MAIN') { "Driver exited with error, terminating producer..." }
        logger.error('MAIN') { e }
        logger.error('MAIN') { e.backtrace.join("\n") }
        producer.terminate
        raise
      end
    end

    consumer_thread.join
  end

  def self.template(&block)
    template = UI::Template.new
    yield template, template.settings
    template
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
whirled_peas-0.1.0 lib/whirled_peas.rb