Class: Spockets::Watcher

Inherits:
Object
  • Object
show all
Defined in:
lib/spockets/Watcher.rb

Instance Method Summary collapse

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

Raises:



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

Returns:

  • (Boolean)


44
45
46
# File 'lib/spockets/Watcher.rb', line 44

def clean?
    @clean
end

#running?Boolean

is the watcher running?

Returns:

  • (Boolean)


39
40
41
# File 'lib/spockets/Watcher.rb', line 39

def running?
    !@stop
end

#startObject

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

#stopObject

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

#syncObject

Ensure all sockets are being listened to

Raises:



49
50
51
52
# File 'lib/spockets/Watcher.rb', line 49

def sync
    raise NotRunning.new if @runner.nil?
    @runner.raise Resync.new
end