Class: QuartzTorrent::Handler
- Inherits:
-
Object
- Object
- QuartzTorrent::Handler
- Defined in:
- lib/quartz_torrent/reactor.rb
Overview
Callers must subclass this class to use the reactor. The event handler methods should be overridden. For data-oriented event handler methods, the functions write and read are available to access the current io, as well as the method currentIo. Close can be called from event handlers to close the current io.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#reactor ⇒ Object
Methods not meant to be overridden.
Instance Method Summary collapse
-
#cancelTimer(timerInfo) ⇒ Object
Cancel a timer scheduled with scheduleTimer.
-
#clientInit(metainfo) ⇒ Object
Event handler.
-
#close(io = nil) ⇒ Object
Close the current io.
-
#connect(addr, port, metainfo, timeout = nil) ⇒ Object
Create a TCP connection to the specified host and port.
-
#connectError(metainfo, details) ⇒ Object
Event handler: an error occurred during connection, or connection timed out.
-
#currentIo ⇒ Object
Return the current IO object.
-
#error(metainfo, details) ⇒ Object
Event handler: an error occurred during read or write.
-
#findIoByMetainfo(metainfo) ⇒ Object
Find an io by metainfo.
-
#read(len) ⇒ Object
Read len bytes from the current io.
-
#recvData(metainfo) ⇒ Object
Event handler: The current io is ready for reading.
-
#scheduleTimer(duration, metainfo = nil, recurring = true, immed = false) ⇒ Object
Schedule a timer.
-
#serverInit(metainfo, addr, port) ⇒ Object
Event handler.
-
#setMetaInfo(metainfo) ⇒ Object
Set the metainfo for the current io.
-
#setReadRateLimit(rateLimit) ⇒ Object
Set the max rate at which the current IO can be read.
-
#setWriteRateLimit(rateLimit) ⇒ Object
Set the max rate at which the current IO can be written to.
-
#stopped? ⇒ Boolean
Check if stop has been called on the reactor.
-
#stopReactor ⇒ Object
Shutdown the reactor.
-
#timerExpired(metainfo) ⇒ Object
Event handler: a timer has expired.
-
#userEvent(event) ⇒ Object
Event handler: this is called for events added using addUserEvent to the reactor.
-
#write(data) ⇒ Object
Write data to the current io.
Instance Attribute Details
#reactor ⇒ Object
Methods not meant to be overridden
66 67 68 |
# File 'lib/quartz_torrent/reactor.rb', line 66 def reactor @reactor end |
Instance Method Details
#cancelTimer(timerInfo) ⇒ Object
Cancel a timer scheduled with scheduleTimer.
80 81 82 83 |
# File 'lib/quartz_torrent/reactor.rb', line 80 def cancelTimer(timerInfo) return if ! timerInfo @reactor.cancelTimer(timerInfo) if @reactor end |
#clientInit(metainfo) ⇒ Object
Event handler. An IO object has been successfully initialized. For example, a connect call has completed
17 18 |
# File 'lib/quartz_torrent/reactor.rb', line 17 def clientInit() end |
#close(io = nil) ⇒ Object
Close the current io. This is meant to be called from one of the event handler methods.
113 114 115 |
# File 'lib/quartz_torrent/reactor.rb', line 113 def close(io = nil) @reactor.close(io) if @reactor end |
#connect(addr, port, metainfo, timeout = nil) ⇒ Object
Create a TCP connection to the specified host and port. Associate the passed metainfo with the IO representing the connection.
86 87 88 |
# File 'lib/quartz_torrent/reactor.rb', line 86 def connect(addr, port, , timeout = nil) @reactor.connect(addr, port, , timeout) if @reactor end |
#connectError(metainfo, details) ⇒ Object
Event handler: an error occurred during connection, or connection timed out.
58 59 |
# File 'lib/quartz_torrent/reactor.rb', line 58 def connectError(, details) end |
#currentIo ⇒ Object
Return the current IO object. This is meant to be called from one of the event handler methods. The returned object is actually an IoFacade, a wrapper around the IO object.
119 120 121 122 123 |
# File 'lib/quartz_torrent/reactor.rb', line 119 def currentIo result = nil result = @reactor.currentIo if @reactor result end |
#error(metainfo, details) ⇒ Object
Event handler: an error occurred during read or write. Connection errors are reported separately in connectError
53 54 |
# File 'lib/quartz_torrent/reactor.rb', line 53 def error(, details) end |
#findIoByMetainfo(metainfo) ⇒ Object
Find an io by metainfo.
126 127 128 |
# File 'lib/quartz_torrent/reactor.rb', line 126 def findIoByMetainfo() @reactor.findIoByMetainfo if && @reactor end |
#read(len) ⇒ Object
Read len bytes from the current io. This is meant to be called from one of the event handler methods.
96 97 98 99 100 |
# File 'lib/quartz_torrent/reactor.rb', line 96 def read(len) result = '' result = @reactor.read(len) if @reactor result end |
#recvData(metainfo) ⇒ Object
Event handler: The current io is ready for reading. If you will write to the same io from both this handler and the timerExpired handler, you must make sure to perform all writing at once in this handler. If not then the writes from the timer handler may be interleaved.
For example if the recvData handler performs:
1. read 5 bytes
2. write 5 bytes
3. read 5 bytes
4. write 5 bytes
and the writes in 2 and 4 are meant to be one message (say mesage 2 is the length, and message 4 is the body)
then this can occur:
recvData reads 5 bytes, writes 5 bytes, tries to read 5 more bytes and is blocked
timerExpired writes 5 bytes
recvData continues; reads the 5 bytes and writes 5 bytes.
Now the timerExpired data was written interleaved.
43 44 |
# File 'lib/quartz_torrent/reactor.rb', line 43 def recvData() end |
#scheduleTimer(duration, metainfo = nil, recurring = true, immed = false) ⇒ Object
Schedule a timer.
75 76 77 |
# File 'lib/quartz_torrent/reactor.rb', line 75 def scheduleTimer(duration, = nil, recurring = true, immed = false) @reactor.scheduleTimer(duration, , recurring, immed) if @reactor end |
#serverInit(metainfo, addr, port) ⇒ Object
Event handler. A peer has connected to the listening socket
21 22 |
# File 'lib/quartz_torrent/reactor.rb', line 21 def serverInit(, addr, port) end |
#setMetaInfo(metainfo) ⇒ Object
Set the metainfo for the current io. This is meant to be called from one of the event handler methods.
131 132 133 |
# File 'lib/quartz_torrent/reactor.rb', line 131 def setMetaInfo() @reactor.setMetaInfo if @reactor end |
#setReadRateLimit(rateLimit) ⇒ Object
Set the max rate at which the current IO can be read. The parameter should be a RateLimit object.
136 137 138 |
# File 'lib/quartz_torrent/reactor.rb', line 136 def setReadRateLimit(rateLimit) @reactor.setReadRateLimit rateLimit if @reactor end |
#setWriteRateLimit(rateLimit) ⇒ Object
Set the max rate at which the current IO can be written to. The parameter should be a RateLimit object.
141 142 143 |
# File 'lib/quartz_torrent/reactor.rb', line 141 def setWriteRateLimit(rateLimit) @reactor.setWriteRateLimit rateLimit if @reactor end |
#stopped? ⇒ Boolean
Check if stop has been called on the reactor.
108 109 110 |
# File 'lib/quartz_torrent/reactor.rb', line 108 def stopped? @stopped end |
#stopReactor ⇒ Object
Shutdown the reactor.
103 104 105 |
# File 'lib/quartz_torrent/reactor.rb', line 103 def stopReactor @reactor.stop if @reactor end |
#timerExpired(metainfo) ⇒ Object
Event handler: a timer has expired.
48 49 |
# File 'lib/quartz_torrent/reactor.rb', line 48 def timerExpired() end |
#userEvent(event) ⇒ Object
Event handler: this is called for events added using addUserEvent to the reactor.
62 63 |
# File 'lib/quartz_torrent/reactor.rb', line 62 def userEvent(event) end |
#write(data) ⇒ Object
Write data to the current io.
91 92 93 |
# File 'lib/quartz_torrent/reactor.rb', line 91 def write(data) @reactor.write(data) if @reactor end |