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



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/nio/monitor.rb', line 17

def initialize(io, interests, selector)
  unless defined?(::OpenSSL) && io.is_a?(::OpenSSL::SSL::SSLSocket)
    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
  end

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

Instance Attribute Details

#interestsObject

Returns the value of attribute interests.



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

def interests
  @interests
end

#ioObject (readonly)

Returns the value of attribute io.



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

def io
  @io
end

#readinessObject

Returns the value of attribute readiness.



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

def readiness
  @readiness
end

#selectorObject (readonly)

Returns the value of attribute selector.



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

def selector
  @selector
end

#valueObject

Returns the value of attribute value.



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

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)


53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/nio/monitor.rb', line 53

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



119
120
121
122
# File 'lib/nio/monitor.rb', line 119

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

#closed?Boolean

Is this monitor closed?

Returns:

  • (Boolean)


114
115
116
# File 'lib/nio/monitor.rb', line 114

def closed?
  @closed
end

#readable?Boolean

Is the IO object readable?

Returns:

  • (Boolean)


103
104
105
# File 'lib/nio/monitor.rb', line 103

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)


80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/nio/monitor.rb', line 80

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)


108
109
110
# File 'lib/nio/monitor.rb', line 108

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