class QuartzTorrent::Handler

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.

Attributes

reactor[RW]

Methods not meant to be overridden

Public Instance Methods

cancelTimer(timerInfo) click to toggle source

Cancel a timer scheduled with scheduleTimer.

# File lib/quartz_torrent/reactor.rb, line 80
def cancelTimer(timerInfo)
  return if ! timerInfo
  @reactor.cancelTimer(timerInfo) if @reactor
end
clientInit(metainfo) click to toggle source

Event handler. An IO object has been successfully initialized. For example, a connect call has completed

# File lib/quartz_torrent/reactor.rb, line 17
def clientInit(metainfo)
end
close(io = nil) click to toggle source

Close the current io. This is meant to be called from one of the event handler methods.

# File lib/quartz_torrent/reactor.rb, line 113
def close(io = nil)
  @reactor.close(io) if @reactor
end
connect(addr, port, metainfo, timeout = nil) click to toggle source

Create a TCP connection to the specified host and port. Associate the passed metainfo with the IO representing the connection.

# File lib/quartz_torrent/reactor.rb, line 86
def connect(addr, port, metainfo, timeout = nil)
  @reactor.connect(addr, port, metainfo, timeout) if @reactor
end
connectError(metainfo, details) click to toggle source

Event handler: an error occurred during connection, or connection timed out. @param metainfo The metainfo associated with the io, as passed to the connect method.

# File lib/quartz_torrent/reactor.rb, line 58
def connectError(metainfo, details)
end
currentIo() click to toggle source

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.

# File lib/quartz_torrent/reactor.rb, line 119
def currentIo
  result = nil
  result = @reactor.currentIo if @reactor
  result
end
error(metainfo, details) click to toggle source

Event handler: an error occurred during read or write. Connection errors are reported separately in connectError @param metainfo The metainfo associated with the io.

# File lib/quartz_torrent/reactor.rb, line 53
def error(metainfo, details)
end
findIoByMetainfo(metainfo) click to toggle source

Find an io by metainfo.

# File lib/quartz_torrent/reactor.rb, line 126
def findIoByMetainfo(metainfo)
  @reactor.findIoByMetainfo metainfo if metainfo && @reactor
end
read(len) click to toggle source

Read len bytes from the current io. This is meant to be called from one of the event handler methods.

# File lib/quartz_torrent/reactor.rb, line 96
def read(len)
  result = ''
  result = @reactor.read(len) if @reactor 
  result
end
recvData(metainfo) click to toggle source

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.

# File lib/quartz_torrent/reactor.rb, line 43
def recvData(metainfo)
end
scheduleTimer(duration, metainfo = nil, recurring = true, immed = false) click to toggle source

Schedule a timer. @param duration The duration of the timer in seconds @param metainfo The metainfo to associate with the timer @param recurring If true when the timer duration expires, the timer will be rescheduled. If false the timer

will not be rescheduled.

@param immed If true then the timer will expire immediately (the next pass through the event loop). If the timer

is also recurring it will then be rescheduled according to it's duratoin.
# File lib/quartz_torrent/reactor.rb, line 75
def scheduleTimer(duration, metainfo = nil, recurring = true, immed = false)
  @reactor.scheduleTimer(duration, metainfo, recurring, immed) if @reactor
end
serverInit(metainfo, addr, port) click to toggle source

Event handler. A peer has connected to the listening socket

# File lib/quartz_torrent/reactor.rb, line 21
def serverInit(metainfo, addr, port)
end
setMetaInfo(metainfo) click to toggle source

Set the metainfo for the current io. This is meant to be called from one of the event handler methods.

# File lib/quartz_torrent/reactor.rb, line 131
def setMetaInfo(metainfo)
  @reactor.setMetaInfo metainfo if @reactor
end
setReadRateLimit(rateLimit) click to toggle source

Set the max rate at which the current IO can be read. The parameter should be a RateLimit object.

# File lib/quartz_torrent/reactor.rb, line 136
def setReadRateLimit(rateLimit)
  @reactor.setReadRateLimit rateLimit if @reactor
end
setWriteRateLimit(rateLimit) click to toggle source

Set the max rate at which the current IO can be written to. The parameter should be a RateLimit object.

# File lib/quartz_torrent/reactor.rb, line 141
def setWriteRateLimit(rateLimit)
  @reactor.setWriteRateLimit rateLimit if @reactor
end
stopReactor() click to toggle source

Shutdown the reactor.

# File lib/quartz_torrent/reactor.rb, line 103
def stopReactor
  @reactor.stop if @reactor
end
stopped?() click to toggle source

Check if stop has been called on the reactor.

# File lib/quartz_torrent/reactor.rb, line 108
def stopped?
  @stopped
end
timerExpired(metainfo) click to toggle source

Event handler: a timer has expired. @param metainfo The metainfo associated with the timer, that was passed to scheduleTimer.

# File lib/quartz_torrent/reactor.rb, line 48
def timerExpired(metainfo)
end
userEvent(event) click to toggle source

Event handler: this is called for events added using addUserEvent to the reactor.

# File lib/quartz_torrent/reactor.rb, line 62
def userEvent(event)
end
write(data) click to toggle source

Write data to the current io.

# File lib/quartz_torrent/reactor.rb, line 91
def write(data)
  @reactor.write(data) if @reactor
end