File: rubytorrent.rb

package info (click to toggle)
libtorrent-ruby 0.3-2
  • links: PTS
  • area: main
  • in suites: lenny, squeeze
  • size: 260 kB
  • ctags: 330
  • sloc: ruby: 2,751; makefile: 1
file content (94 lines) | stat: -rw-r--r-- 3,231 bytes parent folder | download | duplicates (3)
1
2
3
4
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
## rubytorrent.rb -- top-level RubyTorrent file.
## Copyright 2004 William Morgan.
##
## This file is part of RubyTorrent. RubyTorrent is free software;
## you can redistribute it and/or modify it under the terms of version
## 2 of the GNU General Public License as published by the Free
## Software Foundation.
##
## RubyTorrent is distributed in the hope that it will be useful, but
## WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
## General Public License (in the file COPYING) for more details.

require 'rubytorrent/util'
require 'rubytorrent/bencoding'
require 'rubytorrent/metainfo'
require 'rubytorrent/tracker'
require 'rubytorrent/package'
require 'rubytorrent/server'

require "socket"
Socket.do_not_reverse_lookup = true

module RubyTorrent
  VERSION = 0.3

## the top-level class for RubyTorrent.
class BitTorrent
  include EventSource
  event :trying_peer, :forgetting_peer, :added_peer, :removed_peer,
        :received_block, :sent_block, :have_piece, :discarded_piece, :complete,
        :tracker_connected, :tracker_lost, :requested_block

  @@server = nil

  ## hash arguments: host, port, dlratelim, ulratelim
  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

  def ip; @@server.ip; end
  def port; @@server.port; end
  def peer_info; @cont.peer_info; end
  def shutdown; @cont.shutdown; end
  def shutdown_all; @@server.shutdown; end
  def complete?; @package.complete?; end
  def bytes_completed; @package.bytes_completed; end
  def percent_completed; @package.percent_completed; end
  def pieces_completed; @package.pieces_completed; end
  def dlrate; @cont.dlrate; end
  def ulrate; @cont.ulrate; end
  def dlamt; @cont.dlamt; end
  def ulamt; @cont.ulamt; end
  def num_pieces; @package.num_pieces; end
  def tracker; (@cont.tracker ? @cont.tracker.url : nil); end
  def num_possible_peers; (@cont.tracker ? @cont.tracker.peers.length : 0); end
  def num_active_peers; @cont.num_peers; end
  def total_bytes; @package.size; end
end

end