Class: QuartzTorrent::ClassifiedPeers

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

Overview

This class is used to classift torrent peers by connection state.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(peers) ⇒ ClassifiedPeers

Pass a list of Peer objects for a specific torrent



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/quartz_torrent/classifiedpeers.rb', line 5

def initialize(peers)
  # Classify peers by state
  @disconnectedPeers = []
  @handshakingPeers = []
  @establishedPeers = []
  @interestedPeers = []
  @uninterestedPeers = []
  @chokedInterestedPeers = []
  @chokedUninterestedPeers = []
  @unchokedInterestedPeers = []
  @unchokedUninterestedPeers = []
  @requestablePeers = []

  peers.each do |peer|

    # If we come across ourself, ignore it.
    next if peer.isUs

    if peer.state == :disconnected
      @disconnectedPeers.push peer
    elsif peer.state == :handshaking
      @handshakingPeers.push peer
    elsif peer.state == :established
      @establishedPeers.push peer
      if peer.peerChoked
        if peer.peerInterested
          @chokedInterestedPeers.push peer
          @interestedPeers.push peer
        else
          @chokedUninterestedPeers.push peer
          @uninterestedPeers.push peer
        end
      else
        if peer.peerInterested
          @unchokedInterestedPeers.push peer
          @interestedPeers.push peer
        else
          @unchokedUninterestedPeers.push peer
          @uninterestedPeers.push peer
        end
      end

      if !peer.amChoked
        @requestablePeers.push peer
      end
    end
  end
end

Instance Attribute Details

#chokedInterestedPeersObject

Peers that we have an established connection to, and are choked but are interested.



66
67
68
# File 'lib/quartz_torrent/classifiedpeers.rb', line 66

def chokedInterestedPeers
  @chokedInterestedPeers
end

#chokedUninterestedPeersObject

Peers that we have an established connection to, and are choked and are not interested.



69
70
71
# File 'lib/quartz_torrent/classifiedpeers.rb', line 69

def chokedUninterestedPeers
  @chokedUninterestedPeers
end

#disconnectedPeersObject

Peers that are disconnected. Either they have never been connected to, or they were connected to and have been disconnected.



56
57
58
# File 'lib/quartz_torrent/classifiedpeers.rb', line 56

def disconnectedPeers
  @disconnectedPeers
end

#establishedPeersObject

Peers with an established connection. This is the union of chokedInterestedPeers, chokedUninterestedPeers, and unchokedPeers.



63
64
65
# File 'lib/quartz_torrent/classifiedpeers.rb', line 63

def establishedPeers
  @establishedPeers
end

#handshakingPeersObject

Peers still performing a handshake.



59
60
61
# File 'lib/quartz_torrent/classifiedpeers.rb', line 59

def handshakingPeers
  @handshakingPeers
end

#interestedPeersObject

Peers that we have an established connection to, and are interested



78
79
80
# File 'lib/quartz_torrent/classifiedpeers.rb', line 78

def interestedPeers
  @interestedPeers
end

#requestablePeersObject

Peers that we have an established connection to, that are not choking us, that we are interested in



84
85
86
# File 'lib/quartz_torrent/classifiedpeers.rb', line 84

def requestablePeers
  @requestablePeers
end

#unchokedInterestedPeersObject

Peers that we have an established connection to, and are not choked and are interested.



72
73
74
# File 'lib/quartz_torrent/classifiedpeers.rb', line 72

def unchokedInterestedPeers
  @unchokedInterestedPeers
end

#unchokedUninterestedPeersObject

Peers that we have an established connection to, and are not choked and are not interested.



75
76
77
# File 'lib/quartz_torrent/classifiedpeers.rb', line 75

def unchokedUninterestedPeers
  @unchokedUninterestedPeers
end

#uninterestedPeersObject

Peers that we have an established connection to, and are not interested



81
82
83
# File 'lib/quartz_torrent/classifiedpeers.rb', line 81

def uninterestedPeers
  @uninterestedPeers
end

Instance Method Details

#to_sObject



86
87
88
89
90
91
92
93
# File 'lib/quartz_torrent/classifiedpeers.rb', line 86

def to_s
  s = ""
  s << "  Choked and interested #{chokedInterestedPeers.inspect}"
  s << "  Choked and uninterested #{chokedUninterestedPeers.inspect}"
  s << "  Unchoked and interested #{unchokedInterestedPeers.inspect}"
  s << "  Unchoked and uninterested #{unchokedUninterestedPeers.inspect}"
  s  
end