Class: Select

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

Overview

IO-Management Fires if IO is ready a given block.

Select.open do |sel|
  sock = TCPSocket.new 'localhost', 8090
  buffer = ''

  sel.on_read serv do |sock|

buffer << sock.sysread

end

sel.on_write STDOUT do |sock|
  return  if buffer.empty?
  written = sock.syswrite buffer
  buffer = buffer[written..-1]
end

sel.on_error sock do |sock|

sel.close

  end
end

Defined Under Namespace

Classes: Buffer, Entry, Server, Socket

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(timeout = 30) ⇒ Select

Returns a new instance of Select


48
49
50
51
52
53
# File 'lib/select.rb', line 48

def initialize timeout = 30
	@read, @write, @error, @times = {}, {}, {}, []
	@read.default = @write.default = @error.default = proc{}
	@timeout, @tevent, @exit, @exit_on_empty = timeout, proc{}, false, true
	@pause = {read: {}, write: {}, error: {}}
end

Instance Attribute Details

#errorObject (readonly)

Returns the value of attribute error


26
27
28
# File 'lib/select.rb', line 26

def error
  @error
end

#exitObject

Returns the value of attribute exit


27
28
29
# File 'lib/select.rb', line 27

def exit
  @exit
end

#exit_on_emptyObject

Returns the value of attribute exit_on_empty


27
28
29
# File 'lib/select.rb', line 27

def exit_on_empty
  @exit_on_empty
end

#readObject (readonly)

Returns the value of attribute read


26
27
28
# File 'lib/select.rb', line 26

def read
  @read
end

#timesObject (readonly)

Returns the value of attribute times


172
173
174
# File 'lib/select.rb', line 172

def times
  @times
end

#writeObject (readonly)

Returns the value of attribute write


26
27
28
# File 'lib/select.rb', line 26

def write
  @write
end

Class Method Details

.new(*args, &exe) ⇒ Object Also known as: open

Creates a new Select-instance. If you use a block, Select will close all IOs on end.


37
38
39
40
41
42
43
44
# File 'lib/select.rb', line 37

def self.new *args, &exe
	r = super *args
	if exe
		yield r
		r.close
	else r
	end
end

Instance Method Details

#at(a, &e) ⇒ Object


198
199
200
201
202
# File 'lib/select.rb', line 198

def at a, &e
	a = Entry.new a, &e if e
	@times << a
	@times.sort!
end

#cronObject


173
174
175
176
177
178
179
# File 'lib/select.rb', line 173

def cron
	@times.each do |e|
		return if e > Time.now
		e.call
		@times.shift
	end
end

#del(io, type = nil) ⇒ Object Also known as: off

Removes object from notification. If type is read/write/error, only this, else all.


110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/select.rb', line 110

def del io, type = nil
	case type
	when  READ, :read   then @read.delete io
	when WRITE, :write  then @write.delete io
	when ERROR, :error  then @error.delete io
	when nil, :all
		@read.delete io
		@write.delete io
		@error.delete io
	else
		raise ArgumentError, "Unknown event-type: '#{type}'"
	end
end

#empty?Boolean

There are no events to wait?

Returns:

  • (Boolean)

30
31
32
# File 'lib/select.rb', line 30

def empty?
	@read.empty? && @write.empty? && @error.empty?
end

#error_del(io) ⇒ Object Also known as: off_error


130
# File 'lib/select.rb', line 130

def error_del( io)  @error.delete io  end

#error_set(io, &event) ⇒ Object Also known as: on_error


127
# File 'lib/select.rb', line 127

def error_set( io, &event)  self.set io, :error, &event  end

#one(io, type = :read, &exe) ⇒ Object

only once this will be fired


139
140
141
142
143
144
# File 'lib/select.rb', line 139

def one io, type = :read, &exe
	on io, type do |*args|
		off io, type
		yield *args
	end
end

#pause(io, type = :read) ⇒ Object


80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/select.rb', line 80

def pause io, type = :read
	case type
	when  READ, :read   then @pause[ :read][  io] = @read.delete io
	when WRITE, :write  then @pause[ :write][ io] = @write.delete io
	when ERROR, :error  then @pause[ :error][ io] = @error.delete io
	when nil, :all
		@pause[ :read][ io] = @read.delete io
		@pause[ :write][ io] = @write.delete io
		@pause[ :error][ io] = @error.delete io
	else
		raise ArgumentError, "Unknown event-type: '#{type}'"
	end
end

#read_del(io) ⇒ Object Also known as: off_read


128
# File 'lib/select.rb', line 128

def  read_del( io)   @read.delete io  end

#read_set(io, &event) ⇒ Object Also known as: on_read


125
# File 'lib/select.rb', line 125

def  read_set( io, &event)  self.set io,  :read, &event  end

#run(&e) ⇒ Object

Runs in a loop


157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/select.rb', line 157

def run &e
	if e
		until @exit || (@exit_on_empty && empty?)
			cron
			self.run_once 1
			e.call
		end
	else
		until @exit || (@exit_on_empty && empty?)
			cron
			self.run_once 1
		end
	end
end

#run_once(timeout = nil) ⇒ Object

Runs once. Every ready disposed to read/write or has an error, will be fired.


148
149
150
151
152
153
154
# File 'lib/select.rb', line 148

def run_once timeout = nil
	timeout ||= @timeout
	r, w, e = Kernel.select( @read.keys, @write.keys, @error.keys, timeout)
	r and r.each {|h|   @read[ h].call h, :read   }
	w and w.each {|h|  @write[ h].call h, :write  }
	e and e.each {|h|  @error[ h].call h, :error  }
end

#set(io, type = :read, &event) ⇒ Object Also known as: on


63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/select.rb', line 63

def set io, type = :read, &event
	#raise ArgumentError, "This io isn't supported: '#{io.inspect}'"  unless IO === io
	#raise ArgumentError, "Unexpected Event: '#{event.inspect}'"  unless Proc === event
	case type
	when  READ, :read   then  @read[ io] = event
	when WRITE, :write  then @write[ io] = event
	when ERROR, :error  then @error[ io] = event
	when nil, :all
		@read[ io] = event
		@write[ io] = event
		@error[ io] = event
	else
		raise ArgumentError, "Unknown event-type: '#{type}'"
	end
end

#timeout(timeout = nil, &event) ⇒ Object

Raises:

  • (ArgumentError)

55
56
57
58
59
60
61
# File 'lib/select.rb', line 55

def timeout timeout = nil, &event
	return @timeout  if timeout.nil?
	raise ArgumentError, "Numeric value expected, not: '#{timeout}'"  unless Numeric === timeout
	@timeout = timeout
	@tevent = event  if event
	timeout
end

#unpause(io, type = :read) ⇒ Object


94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/select.rb', line 94

def unpause io, type = :read
	case type
	when  READ, :read   then @read[  io] = @pause[ :read].delete io
	when WRITE, :write  then @write[ io] = @pause[ :write].delete io
	when ERROR, :error  then @error[ io] = @pause[ :error].delete io
	when nil, :all
		@read[  io] = @pause[ :read].delete io
		@write[ io] = @pause[ :write].delete io
		@error[ io] = @pause[ :error].delete io
	else
		raise ArgumentError, "Unknown event-type: '#{type}'"
	end
end

#write_del(io) ⇒ Object Also known as: off_write


129
# File 'lib/select.rb', line 129

def write_del( io)  @write.delete io  end

#write_set(io, &event) ⇒ Object Also known as: on_write


126
# File 'lib/select.rb', line 126

def write_set( io, &event)  self.set io, :write, &event  end