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
26
27
# File 'lib/nio/monitor.rb', line 10

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)


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

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



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

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

#closed?Boolean

Is this monitor closed?

Returns:

  • (Boolean)


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

def closed?
  @closed
end

#readable?Boolean

Is the IO object readable?

Returns:

  • (Boolean)


96
97
98
# File 'lib/nio/monitor.rb', line 96

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)


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

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)


101
102
103
# File 'lib/nio/monitor.rb', line 101

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