Class: Spockets::Watcher
- Inherits:
-
Object
- Object
- Spockets::Watcher
- Defined in:
- lib/spockets/Watcher.rb
Instance Method Summary collapse
-
#clean? ⇒ Boolean
clean incoming strings.
-
#initialize(args = {}) ⇒ Watcher
constructor
- :sockets
- socket list :clean
- clean UTF8 strings or provide block to run on every read string :pool
-
ActionPool to use Creates a new watcher for sockets.
-
#running? ⇒ Boolean
is the watcher running?.
-
#start ⇒ Object
start the watcher.
-
#stop ⇒ Object
stop the watcher.
-
#sync ⇒ Object
Ensure all sockets are being listened to.
Constructor Details
#initialize(args = {}) ⇒ Watcher
- :sockets
-
socket list
- :clean
-
clean UTF8 strings or provide block to run on every read string
- :pool
-
ActionPool to use
Creates a new watcher for sockets
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/spockets/Watcher.rb', line 10 def initialize(args={}) raise ArgumentError.new('Expecting argument hash') unless args.is_a?(Hash) raise ArgumentError.new('Missing required argument :sockets') unless args[:sockets] && args[:sockets].is_a?(Hash) @sockets = args[:sockets] @runner = nil @clean = args[:clean] && (args[:clean].is_a?(Proc) || args[:clean].is_a?(TrueClass)) ? args[:clean] : nil @pool = args[:pool] && args[:pool].is_a?(ActionPool::Pool) ? args[:pool] : ActionPool::Pool.new if(@clean.is_a?(TrueClass)) require 'iconv' @ic = Iconv.new('UTF-8//IGNORE', 'UTF-8') else @ic = nil end @stop = true end |
Instance Method Details
#clean? ⇒ Boolean
clean incoming strings
63 64 65 |
# File 'lib/spockets/Watcher.rb', line 63 def clean? @clean end |
#running? ⇒ Boolean
is the watcher running?
58 59 60 |
# File 'lib/spockets/Watcher.rb', line 58 def running? !@stop end |
#start ⇒ Object
start the watcher
27 28 29 30 31 32 33 34 35 36 |
# File 'lib/spockets/Watcher.rb', line 27 def start if(@sockets.size < 0) raise 'No sockets available for listening' elsif(!@runner.nil? && @runner.alive?) raise AlreadyRunning.new else @stop = false @runner = Thread.new{watch} end end |
#stop ⇒ Object
stop the watcher
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/spockets/Watcher.rb', line 39 def stop if(@runner.nil? && @stop) raise NotRunning.new elsif(@runner.nil? || !@runner.alive?) @stop = true @runner = nil else @stop = true if(@runner) @runner.raise Resync.new if @runner.alive? && @runner.stop? @runner.join(0.05) if @runner @runner.kill if @runner && @runner.alive? end @runner = nil end nil end |
#sync ⇒ Object
Ensure all sockets are being listened to
68 69 70 71 |
# File 'lib/spockets/Watcher.rb', line 68 def sync raise NotRunning.new if @runner.nil? @runner.raise Resync.new end |