Class: QuartzTorrent::PeerWireMessage

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

Overview

Represents a bittorrent peer protocol wire message (a non-handshake message). All messages other than handshake have a 4-byte length, 1-byte message id, and payload.

Constant Summary collapse

MessageKeepAlive =
-1
MessageChoke =
0
MessageUnchoke =
1
MessageInterested =
2
MessageUninterested =
3
MessageHave =
4
MessageBitfield =
5
MessageRequest =
6
MessagePiece =
7
MessageCancel =
8
MessageExtended =
20

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(messageId) ⇒ PeerWireMessage

Create a new PeerWireMessage with the specified message id.



91
92
93
# File 'lib/quartz_torrent/peermsg.rb', line 91

def initialize(messageId)
  @messageId = messageId
end

Instance Attribute Details

#messageIdObject (readonly)

Get the message id of this message (the message type from the peer wire protocol).



96
97
98
# File 'lib/quartz_torrent/peermsg.rb', line 96

def messageId
  @messageId
end

Instance Method Details

#lengthObject

Total message length



110
111
112
# File 'lib/quartz_torrent/peermsg.rb', line 110

def length
  payloadLength + 5
end

#payloadLengthObject

Subclasses must implement this method. It should return an integer.



105
106
107
# File 'lib/quartz_torrent/peermsg.rb', line 105

def payloadLength
  raise "Subclasses of PeerWireMessage must implement payloadLength but #{self.class} didn't"
end

#serializeTo(io) ⇒ Object

Serialize this message to the passed io



99
100
101
102
# File 'lib/quartz_torrent/peermsg.rb', line 99

def serializeTo(io)
  io.write [payloadLength+1].pack("N")
  io.write [@messageId].pack("C")
end

#to_sObject



119
120
121
# File 'lib/quartz_torrent/peermsg.rb', line 119

def to_s
  "#{self.class} message"
end

#unserialize(payload) ⇒ Object

Unserialize the message from the passed string.



115
116
117
# File 'lib/quartz_torrent/peermsg.rb', line 115

def unserialize(payload)
  raise "Subclasses of PeerWireMessage must implement unserialize but #{self.class} didn't"
end