Class: NIO::Monitor

Inherits:
Object
  • Object
show all
Defined in:
lib/nio/monitor.rb,
ext/nio4r/monitor.c,
ext/nio4r/selector.c

Overview

Monitors watch IO objects for specific events

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io, interests, selector) ⇒ Object

Methods



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/nio/monitor.rb', line 10

def initialize(io, interests, selector)
  unless io.is_a?(IO)
    if IO.respond_to? :try_convert
      io = IO.try_convert(io)
    elsif io.respond_to? :to_io
      io = io.to_io
    end

    raise TypeError, "can't convert #{io.class} into IO" unless io.is_a? IO
  end

  @io        = io
  @interests = interests
  @selector  = selector
  @closed    = false
end

Instance Attribute Details

#interestsObject

Returns the value of attribute interests.



21
22
23
# File 'ext/nio4r/monitor.c', line 21

def interests
  @interests
end

#ioObject (readonly)

Returns the value of attribute io.



20
21
22
# File 'ext/nio4r/monitor.c', line 20

def io
  @io
end

#readinessObject

Returns the value of attribute readiness.



30
31
32
# File 'ext/nio4r/monitor.c', line 30

def readiness
  @readiness
end

#selectorObject (readonly)

Returns the value of attribute selector.



25
26
27
# File 'ext/nio4r/monitor.c', line 25

def selector
  @selector
end

#valueObject

Returns the value of attribute value.



28
29
30
# File 'ext/nio4r/monitor.c', line 28

def value
  @value
end

Instance Method Details

#add_interest(interest) ⇒ self

Add new interests to the existing interest set

Parameters:

  • interests (:r, :w, :rw)

    new I/O interests (read/write/readwrite)

Returns:

  • (self)


44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/nio/monitor.rb', line 44

def add_interest(interest)
  case interest
  when :r
    case @interests
    when :r  then @interests = :r
    when :w  then @interests = :rw
    when :rw then @interests = :rw
    when nil then @interests = :r
    end
  when :w
    case @interests
    when :r  then @interests = :rw
    when :w  then @interests = :w
    when :rw then @interests = :rw
    when nil then @interests = :w
    end
  when :rw
    @interests = :rw
  else raise ArgumentError, "bad interests: #{interest}"
  end
end

#close(deregister = true) ⇒ Object

Deactivate this monitor



110
111
112
113
# File 'lib/nio/monitor.rb', line 110

def close(deregister = true)
  @closed = true
  @selector.deregister(io) if deregister
end

#closed?Boolean

Is this monitor closed?

Returns:

  • (Boolean)


105
106
107
# File 'lib/nio/monitor.rb', line 105

def closed?
  @closed
end

#readable?Boolean

Is the IO object readable?

Returns:

  • (Boolean)


94
95
96
# File 'lib/nio/monitor.rb', line 94

def readable?
  readiness == :r || readiness == :rw
end

#remove_interest(interest) ⇒ self

Remove interests from the existing interest set

Parameters:

  • interests (:r, :w, :rw)

    I/O interests to remove (read/write/readwrite)

Returns:

  • (self)


71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/nio/monitor.rb', line 71

def remove_interest(interest)
  case interest
  when :r
    case @interests
    when :r  then @interests = nil
    when :w  then @interests = :w
    when :rw then @interests = :w
    when nil then @interests = nil
    end
  when :w
    case @interests
    when :r  then @interests = :r
    when :w  then @interests = nil
    when :rw then @interests = :r
    when nil then @interests = nil
    end
  when :rw
    @interests = nil
  else raise ArgumentError, "bad interests: #{interest}"
  end
end

#writable?Boolean Also known as: writeable?

Is the IO object writable?

Returns:

  • (Boolean)


99
100
101
# File 'lib/nio/monitor.rb', line 99

def writable?
  readiness == :w || readiness == :rw
end