torrenter 0.0.2 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 126aceaca6caddcec9814075900faa9f8e5ce0e0
4
- data.tar.gz: 537b792fc0b1640c9e6683acc9157e2140b5390f
3
+ metadata.gz: f2ef8e3760de9d724d24826b929e2d40bcbea4be
4
+ data.tar.gz: 58aa8774b9ca139ec06d6c44751b48745f339147
5
5
  SHA512:
6
- metadata.gz: 9c8dc7755108cb35a6f9c99a8ed5f415c84a12e0f5e002f9a5b58d08aa2dff43b0b89952b26be964cfad7ee34683cf532cac1e64e9704fe05c37e0132fe49311
7
- data.tar.gz: 07eae93f45408462558f941efc24be0fd3779a1982c481f819e470fa0b1bb100c0f4133a0c6167331ffc15462ac3a6235a1452204ad4a6e5fae1171d9f14a544
6
+ metadata.gz: ce174384bf065513a2b487098c7430d195280c87d1ed0c9a3b1205b768b0effe4339c419710a7c99477780b6a6be12e4bafac6d41c4950393124082ade4002e6
7
+ data.tar.gz: 8985f1f8769bccb328fefbb122f7d024537c9958fdd6d07e89041935291c89f63f61fc487c8dc8237ee15751fb0789441b497ad435131b133fb2f64850bea53e
@@ -9,4 +9,5 @@ module Torrenter
9
9
  PIECE = "\a"
10
10
  CHOKE = "\x00"
11
11
  PROTOCOL = "\x13BitTorrent protocol\x00\x00\x00\x00\x00\x00\x00\x00"
12
+ EXCEPTIONS = [Errno::EADDRNOTAVAIL, Errno::ECONNREFUSED, Errno::EPIPE, Errno::ECONNRESET]
12
13
  end
@@ -12,35 +12,37 @@ module Torrenter
12
12
  Timeout::timeout(2) { @socket.sendmsg_nonblock(msg) }
13
13
  rescue Timeout::Error
14
14
  ''
15
- rescue Errno::EADDRNOTAVAIL
16
- ''
17
- rescue Errno::ECONNREFUSED
18
- ''
19
- rescue Errno::EPIPE
15
+ rescue *EXCEPTIONS
16
+ @status = false
20
17
  ''
18
+ # ''
19
+ # rescue Errno::ECONNREFUSED
20
+ # @status = false
21
+ # ''
22
+ # rescue Errno::EPIPE
23
+ # @status = false
24
+ # ''
21
25
  end
22
26
  end
23
27
 
24
28
  def recv_data(bytes=BLOCK, opts={})
25
29
  begin
26
- if opts[:peek]
27
- Timeout::timeout(2) { @socket.recv_nonblock(4, Socket::MSG_PEEK) }
28
- else
29
- Timeout::timeout(2) { buffer << @socket.recv_nonblock(bytes) }
30
- end
30
+ Timeout::timeout(2) { buffer << @socket.recv_nonblock(bytes) }
31
31
  rescue Timeout::Error
32
32
  ''
33
- rescue Errno::EADDRNOTAVAIL
34
- ''
35
- rescue Errno::ECONNREFUSED
36
- ''
37
- rescue Errno::ECONNRESET
33
+ rescue *EXCEPTIONS
34
+ @status = false
38
35
  ''
36
+ # ''
37
+ # rescue Errno::ECONNREFUSED
38
+ # ''
39
+ # rescue Errno::ECONNRESET
40
+ # ''
39
41
  rescue IO::EAGAINWaitReadable
40
42
  ''
41
- rescue Errno::ETIMEDOUT
42
- @status = false
43
- @buffer = ''
43
+ # rescue Errno::ETIMEDOUT
44
+ # @status = false
45
+ # @buffer = ''
44
46
  end
45
47
  end
46
48
 
@@ -61,8 +63,10 @@ module Torrenter
61
63
  def parse_bitfield
62
64
  len = msg_len
63
65
  buffer.slice!(0)
64
- @piece_index = buffer.slice!(0...len).unpack("B#{sha_list.size}")
65
- .first.split('')
66
+ @piece_index = buffer.slice!(0...len)
67
+ .unpack("B#{sha_list.size}")
68
+ .first
69
+ .split('')
66
70
  .map { |bit| bit == '1' ? :available : :unavailable }
67
71
  end
68
72
 
@@ -97,8 +101,10 @@ module Torrenter
97
101
  @buffer.slice!(0..3).unpack("N*").first - 1
98
102
  end
99
103
 
104
+ # needs optimization. It evaluates the master index lazily with
105
+ # no thought as to the 'rare pieces'
106
+
100
107
  def evaluate_index(master)
101
- @requesting = 0
102
108
  if @piece_index.any? { |chunk| chunk == :available }
103
109
  @index = @piece_index.index(:available)
104
110
  if master[@index] == :free
@@ -214,7 +220,6 @@ module Torrenter
214
220
  end
215
221
 
216
222
  def request_message(bytes=BLOCK)
217
- @requesting += 1
218
223
  send_data(pack(13) + "\x06" + pack(@index) + pack(@offset) + pack(bytes))
219
224
  end
220
225
 
@@ -4,7 +4,7 @@ module Torrenter
4
4
  include Torrenter
5
5
  # for non outside vars use the @ and remove them from
6
6
 
7
- attr_reader :socket, :peer, :sha_list, :piece_len, :info_hash, :status
7
+ attr_reader :socket, :peer, :sha_list, :piece_len, :info_hash, :status, :index
8
8
  attr_accessor :piece_index, :offset, :buffer, :block_map
9
9
 
10
10
 
@@ -32,7 +32,7 @@ module Torrenter
32
32
  def connect
33
33
  puts "\nConnecting to IP: #{peer[:ip]} PORT: #{peer[:port]}"
34
34
  begin
35
- Timeout::timeout(2) { @socket = TCPSocket.new(peer[:ip], peer[:port]) }
35
+ Timeout::timeout(5) { @socket = TCPSocket.new(peer[:ip], peer[:port]) }
36
36
  rescue Timeout::Error
37
37
  puts "Timed out."
38
38
  rescue Errno::EADDRNOTAVAIL
@@ -44,7 +44,6 @@ module Torrenter
44
44
  end
45
45
 
46
46
  if @socket
47
- emit_event
48
47
 
49
48
  @socket.write(handshake)
50
49
  @status = true
@@ -53,12 +52,6 @@ module Torrenter
53
52
  end
54
53
  end
55
54
 
56
- def emit_event
57
- # http = Net::HTTP.new("localhost", 9000)
58
-
59
- # http.post("/watcher", JSON.generate(peer))
60
- end
61
-
62
55
  def handshake
63
56
  "#{PROTOCOL}#{@info_hash}#{PEER_ID}"
64
57
  end
@@ -22,27 +22,29 @@ module Torrenter
22
22
  data = IO.read($data_dump, @piece_length, n * @piece_length) || ''
23
23
  @master_index[n] = :downloaded if Digest::SHA1.digest(data) == @sha_list[n]
24
24
  end
25
- $update = @master_index
25
+ $update = { index: indices }
26
26
  puts "#{@master_index.count(:downloaded)} pieces are downloaded already."
27
27
  end
28
28
 
29
29
  # sends the handshake messages.
30
30
 
31
- def message_reactor(opts={})
31
+ def message_reactor
32
32
  modify_index
33
33
  if !@master_index.all? { |index| index == :downloaded }
34
- @peers.each { |peer| peer.connect }
35
- puts "You are now connected to #{active_peers} peers."
34
+ @peers.each { |peer| peer.connect if active_peers.size < 4 }
35
+ puts "You are now connected to #{active_peers.size} peers."
36
36
  loop do
37
37
  break if @master_index.all? { |piece| piece == :downloaded }
38
38
  @peers.each do |peer|
39
+ $update = { index: indices, peer_count: peer_data }
40
+
39
41
  piece_count = @master_index.count(:downloaded)
40
42
  if peer.status
41
43
  peer.state(@master_index, @blocks) # unless peer.piece_index.all? { |piece| piece == :downloaded }
42
44
  if @master_index.count(:downloaded) > piece_count
43
- send_post
45
+
44
46
  system("clear")
45
- puts download_bar + "Downloading from #{active_peers} active peers"
47
+ puts download_bar + "Downloading from #{active_peers.size} active peers"
46
48
  end
47
49
  elsif Time.now.to_i % 60 == 0
48
50
  peer.connect
@@ -56,14 +58,36 @@ module Torrenter
56
58
  end
57
59
  end
58
60
 
59
- def send_post
60
- $update = @master_index
61
- # http = Net::HTTP.new("localhost", 4567)
62
- # http.post("/filer", JSON.generate({:index => @master_index}))
61
+ def indices
62
+ @master_index.map do |piece|
63
+ if piece == :free
64
+ 0
65
+ elsif piece == :downloaded
66
+ 1
67
+ else
68
+ get_status @master_index.index(piece)
69
+ end
70
+ end
71
+ end
72
+
73
+ def peer_data
74
+ active_peers.map { |peer| "ip: #{peer.peer[:ip]} port:#{peer.peer[:port]}" }.join("\n")
75
+ end
76
+
77
+ def get_status(i)
78
+ peer = @peers.find { |peer| peer.piece_index[i] == :downloading }
79
+ (peer.buffer.bytesize + peer.piece_size).fdiv(@piece_length)
63
80
  end
64
81
 
65
82
  def active_peers
66
- @peers.select { |peer| peer.status }.size
83
+ @peers.select { |peer| peer.index && peer.status }
84
+ end
85
+
86
+ def index_percentages
87
+ active_peers.map do |peer|
88
+ size = peer.buffer.bytesize + peer.piece_size
89
+ [peer.index, (size.fdiv @piece_length) * 100]
90
+ end
67
91
  end
68
92
 
69
93
  def download_bar
@@ -22,7 +22,9 @@ module Torrenter
22
22
 
23
23
  def message_relay
24
24
  @sender.send(connect_msg, 0)
25
+
25
26
  read_response
27
+
26
28
  if @response
27
29
  @connection_id = @response[-8..-1]
28
30
  @transaction_id = [rand(10000)].pack("I>")
@@ -1,3 +1,3 @@
1
1
  module Torrenter
2
- VERSION = '0.0.2'
2
+ VERSION = '0.0.3'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: torrenter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - wismer
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-04-23 00:00:00.000000000 Z
11
+ date: 2014-04-25 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: BitTorrent Client written in Ruby
14
14
  email: