Class: RubyTorrent::BitTorrent

Inherits:
Object
  • Object
show all
Includes:
EventSource
Defined in:
lib/rubytorrent.rb

Overview

the top-level class for RubyTorrent.

Constant Summary collapse

@@server =
nil

Instance Method Summary collapse

Methods included from EventSource

append_features, #on_event, #relay_event, #send_event, #unregister_events

Constructor Details

#initialize(metainfo, *rest) ⇒ BitTorrent

hash arguments: host, port, dlratelim, ulratelim

Raises:

  • (ArgumentError)


37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/rubytorrent.rb', line 37

def initialize(metainfo, *rest)
  args, rest = RubyTorrent::get_args(rest, :host, :port, :dlratelim, :ulratelim, :http_proxy)
  out = rest.shift
  raise ArgumentError, "wrong number of arguments (expected 0/1, got #{rest.length})" unless rest.empty?

  case metainfo
  when MetaInfo
    @metainfo = metainfo
  when String
    @metainfo = MetaInfo.from_location(metainfo)
  when IO
    @metainfo = MetaInfo.from_stream(metainfo)
  else
    raise ArgumentError, "'metainfo' should be a String, IO or RubyTorrent::MetaInfo object"
  end

  case out
  when Package
    @package = out
  else
    @package = Package.new(@metainfo, out)
  end

  unless @@server
    @@server = RubyTorrent::Server.new(args[:host], args[:port], args[:http_proxy])
    @@server.start
  end

  @cont = @@server.add_torrent(@metainfo, @package, args[:dlratelim], args[:ulratelim])

  @cont.relay_event self, :trying_peer, :forgetting_peer, :added_peer,
                          :removed_peer, :received_block, :sent_block,
                          :have_piece, :discarded_piece, :tracker_connected,
                          :tracker_lost, :requested_block
  @package.relay_event self, :complete
end

Instance Method Details

#bytes_completedObject



80
# File 'lib/rubytorrent.rb', line 80

def bytes_completed; @package.bytes_completed; end

#complete?Boolean

Returns:

  • (Boolean)


79
# File 'lib/rubytorrent.rb', line 79

def complete?; @package.complete?; end

#dlamtObject



85
# File 'lib/rubytorrent.rb', line 85

def dlamt; @cont.dlamt; end

#dlrateObject



83
# File 'lib/rubytorrent.rb', line 83

def dlrate; @cont.dlrate; end

#ipObject



74
# File 'lib/rubytorrent.rb', line 74

def ip; @@server.ip; end

#num_active_peersObject



90
# File 'lib/rubytorrent.rb', line 90

def num_active_peers; @cont.num_peers; end

#num_piecesObject



87
# File 'lib/rubytorrent.rb', line 87

def num_pieces; @package.num_pieces; end

#num_possible_peersObject



89
# File 'lib/rubytorrent.rb', line 89

def num_possible_peers; (@cont.tracker ? @cont.tracker.peers.length : 0); end

#peer_infoObject



76
# File 'lib/rubytorrent.rb', line 76

def peer_info; @cont.peer_info; end

#percent_completedObject



81
# File 'lib/rubytorrent.rb', line 81

def percent_completed; @package.percent_completed; end

#pieces_completedObject



82
# File 'lib/rubytorrent.rb', line 82

def pieces_completed; @package.pieces_completed; end

#portObject



75
# File 'lib/rubytorrent.rb', line 75

def port; @@server.port; end

#shutdownObject



77
# File 'lib/rubytorrent.rb', line 77

def shutdown; @cont.shutdown; end

#shutdown_allObject



78
# File 'lib/rubytorrent.rb', line 78

def shutdown_all; @@server.shutdown; end

#total_bytesObject



91
# File 'lib/rubytorrent.rb', line 91

def total_bytes; @package.size; end

#trackerObject



88
# File 'lib/rubytorrent.rb', line 88

def tracker; (@cont.tracker ? @cont.tracker.url : nil); end

#ulamtObject



86
# File 'lib/rubytorrent.rb', line 86

def ulamt; @cont.ulamt; end

#ulrateObject



84
# File 'lib/rubytorrent.rb', line 84

def ulrate; @cont.ulrate; end