From 0176ab0010a6fa8fc91b26562bc70ab50c9237c6 Mon Sep 17 00:00:00 2001 From: Thibault Jouan Date: Fri, 17 Apr 2015 21:25:00 +0000 Subject: [PATCH] Prototype two workers: blocking and multiplexing --- lib/uh/wm.rb | 4 ++++ lib/uh/wm/workers.rb | 21 +++++++++++++++++++++ lib/uh/wm/workers/base.rb | 31 +++++++++++++++++++++++++++++++ lib/uh/wm/workers/blocking.rb | 15 +++++++++++++++ lib/uh/wm/workers/mux.rb | 18 ++++++++++++++++++ 5 files changed, 89 insertions(+) create mode 100644 lib/uh/wm/workers.rb create mode 100644 lib/uh/wm/workers/base.rb create mode 100644 lib/uh/wm/workers/blocking.rb create mode 100644 lib/uh/wm/workers/mux.rb diff --git a/lib/uh/wm.rb b/lib/uh/wm.rb index c3c6f0c..0614a84 100644 --- a/lib/uh/wm.rb +++ b/lib/uh/wm.rb @@ -12,6 +12,10 @@ require 'uh/wm/logger_formatter' require 'uh/wm/manager' require 'uh/wm/run_control' require 'uh/wm/runner' +require 'uh/wm/workers' +require 'uh/wm/workers/base' +require 'uh/wm/workers/blocking' +require 'uh/wm/workers/mux' module Uh module WM diff --git a/lib/uh/wm/workers.rb b/lib/uh/wm/workers.rb new file mode 100644 index 0000000..c065293 --- /dev/null +++ b/lib/uh/wm/workers.rb @@ -0,0 +1,21 @@ +module Uh + module WM + module Workers + FACTORIES = { + block: ->(options) { Blocking.new(options) }, + mux: ->(options) { Mux.new(options) } + }.freeze + + class << self + def types + FACTORIES.keys + end + + def build type, **options + (FACTORIES[type] or fail ArgumentError, "unknown worker: `#{type}'") + .call options + end + end + end + end +end diff --git a/lib/uh/wm/workers/base.rb b/lib/uh/wm/workers/base.rb new file mode 100644 index 0000000..2c2e63b --- /dev/null +++ b/lib/uh/wm/workers/base.rb @@ -0,0 +1,31 @@ +module Uh + module WM + module Workers + class Base + def initialize **options + @ios = [] + end + + def watch io + @ios << io + end + + def before_wait &block + if block_given? then @before_wait = block else @before_wait end + end + + def on_timeout &block + if block_given? then @on_timeout = block else @on_timeout end + end + + def on_read &block + if block_given? then @on_read = block else @on_read end + end + + def on_read_next &block + if block_given? then @on_read_next = block else @on_read_next end + end + end + end + end +end diff --git a/lib/uh/wm/workers/blocking.rb b/lib/uh/wm/workers/blocking.rb new file mode 100644 index 0000000..d6aefc1 --- /dev/null +++ b/lib/uh/wm/workers/blocking.rb @@ -0,0 +1,15 @@ +module Uh + module WM + module Workers + class Blocking < Base + def work_events + #until yield + # @on_events_read_bang.call + #end + #@on_events_read_bang.call until yield + @on_read_next.call + end + end + end + end +end diff --git a/lib/uh/wm/workers/mux.rb b/lib/uh/wm/workers/mux.rb new file mode 100644 index 0000000..e940f4e --- /dev/null +++ b/lib/uh/wm/workers/mux.rb @@ -0,0 +1,18 @@ +module Uh + module WM + module Workers + class Mux < Base + def initialize timeout: 1 + super + @timeout = timeout + end + + def work_events + @before_wait.call if @before_wait + if res = select(@ios, [], [], @timeout) then @on_read.call res + else @on_timeout.call if @on_timeout end + end + end + end + end +end