Class: Torrenter::Reactor
- Inherits:
-
Object
- Object
- Torrenter::Reactor
- Defined in:
- lib/torrenter/reactor.rb
Instance Method Summary collapse
- #active_peers ⇒ Object
- #data_remaining ⇒ Object
- #download_bar ⇒ Object
- #extract_peers ⇒ Object
- #finished? ⇒ Boolean
- #have ⇒ Object
- #index_percentages ⇒ Object
-
#initialize(trackers, piece_index) ⇒ Reactor
constructor
A new instance of Reactor.
- #message_reactor ⇒ Object
- #pieces(type) ⇒ Object
- #server_listen ⇒ Object
Constructor Details
#initialize(trackers, piece_index) ⇒ Reactor
Returns a new instance of Reactor.
4 5 6 7 |
# File 'lib/torrenter/reactor.rb', line 4 def initialize(trackers, piece_index) @trackers = trackers @piece_index = piece_index end |
Instance Method Details
#active_peers ⇒ Object
46 47 48 |
# File 'lib/torrenter/reactor.rb', line 46 def active_peers @peers.select { |peer| peer.peer_state } end |
#data_remaining ⇒ Object
42 43 44 |
# File 'lib/torrenter/reactor.rb', line 42 def data_remaining (total_file_size - @byte_counter).fdiv(1024).fdiv(1024).round(2) end |
#download_bar ⇒ Object
57 58 59 |
# File 'lib/torrenter/reactor.rb', line 57 def ("\u2588" * pieces(:downloaded)) + ("\u2593" * pieces(:downloading)) + (" " * pieces(:free)) + " %#{pieces(:downloaded)} downloaded " end |
#extract_peers ⇒ Object
9 10 11 |
# File 'lib/torrenter/reactor.rb', line 9 def extract_peers @peers = @trackers.map { |tracker| tracker.peer_list if tracker.connected? }.flatten.compact end |
#finished? ⇒ Boolean
38 39 40 |
# File 'lib/torrenter/reactor.rb', line 38 def finished? @piece_index.all? end |
#have ⇒ Object
34 35 36 |
# File 'lib/torrenter/reactor.rb', line 34 def have ->(index, peer) { @piece_index[index].add_peer(peer) } end |
#index_percentages ⇒ Object
50 51 52 53 54 55 |
# File 'lib/torrenter/reactor.rb', line 50 def index_percentages active_peers.map do |peer| size = peer.buffer.bytesize + peer.piece_size [peer.index, (size.fdiv @piece_length) * 100] end end |
#message_reactor ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/torrenter/reactor.rb', line 13 def @piece_index.verify_status @peers.each { |peer| peer.connect unless active_peers.size > 5 } puts "You are now connected to #{active_peers.size} peers." loop do break if finished? @peers.each do |peer| if peer.peer_state peer.connection_state(@piece_index, have) else peer.connect if Time.now.to_i % ((active_peers.size * 10) + 1) == 0 end end @piece_index.clean_peers $status = @piece_index.to_json end end |
#pieces(type) ⇒ Object
61 62 63 |
# File 'lib/torrenter/reactor.rb', line 61 def pieces(type) (@piece_index.count(type).fdiv(@piece_index.size) * 100).round end |
#server_listen ⇒ Object
31 32 |
# File 'lib/torrenter/reactor.rb', line 31 def server_listen end |