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
12 13 14 15 16 17 18 19 20 |
# File 'lib/spockets/Watcher.rb', line 12 def initialize(args) raise MissingArgument.new(:sockets) unless args[:sockets] @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 @ic = @clean && @clean.is_a?(TrueClass) ? Iconv.new('UTF-8//IGNORE', 'UTF-8') : nil @stop = true end |
Instance Method Details
#clean? ⇒ Boolean
clean incoming strings
44 45 46 |
# File 'lib/spockets/Watcher.rb', line 44 def clean? @clean end |
#running? ⇒ Boolean
is the watcher running?
39 40 41 |
# File 'lib/spockets/Watcher.rb', line 39 def running? !@stop end |
#start ⇒ Object
start the watcher
23 24 25 26 |
# File 'lib/spockets/Watcher.rb', line 23 def start @stop = false @runner = Thread.new{watch} if @runner.nil? && @sockets.size > 0 end |
#stop ⇒ Object
stop the watcher
29 30 31 32 33 34 35 36 |
# File 'lib/spockets/Watcher.rb', line 29 def stop @stop = true @runner.join(0.1) @runner.raise Resync.new @runner.join(0.1) @runner.kill unless @runner.nil? || @runner.alive? @runner = nil end |
#sync ⇒ Object
Ensure all sockets are being listened to
49 50 51 52 |
# File 'lib/spockets/Watcher.rb', line 49 def sync raise NotRunning.new if @runner.nil? @runner.raise Resync.new end |