Class: QuartzTorrent::Handler

Inherits:
Object
  • Object
show all
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

PeerClientHandler

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#reactorObject

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(metainfo)
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, metainfo, timeout = nil)
  @reactor.connect(addr, port, metainfo, timeout) if @reactor
end

#connectError(metainfo, details) ⇒ Object

Event handler: an error occurred during connection, or connection timed out.

Parameters:

  • metainfo

    The metainfo associated with the io, as passed to the connect method.



58
59
# File 'lib/quartz_torrent/reactor.rb', line 58

def connectError(metainfo, details)
end

#currentIoObject

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

Parameters:

  • metainfo

    The metainfo associated with the io.



53
54
# File 'lib/quartz_torrent/reactor.rb', line 53

def error(metainfo, details)
end

#findIoByMetainfo(metainfo) ⇒ Object

Find an io by metainfo.



126
127
128
# File 'lib/quartz_torrent/reactor.rb', line 126

def findIoByMetainfo(metainfo)
  @reactor.findIoByMetainfo metainfo if metainfo && @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(metainfo)
end

#scheduleTimer(duration, metainfo = nil, recurring = true, immed = false) ⇒ Object

Schedule a timer.

Parameters:

  • duration

    The duration of the timer in seconds

  • metainfo (defaults to: nil)

    The metainfo to associate with the timer

  • recurring (defaults to: true)

    If true when the timer duration expires, the timer will be rescheduled. If false the timer will not be rescheduled.

  • immed (defaults to: false)

    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.



75
76
77
# 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) ⇒ Object

Event handler. A peer has connected to the listening socket



21
22
# File 'lib/quartz_torrent/reactor.rb', line 21

def serverInit(metainfo, 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(metainfo)
  @reactor.setMetaInfo metainfo 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.

Returns:

  • (Boolean)


108
109
110
# File 'lib/quartz_torrent/reactor.rb', line 108

def stopped?
  @stopped
end

#stopReactorObject

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.

Parameters:

  • metainfo

    The metainfo associated with the timer, that was passed to scheduleTimer.



48
49
# File 'lib/quartz_torrent/reactor.rb', line 48

def timerExpired(metainfo)
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