#!/usr/bin/env ruby require "torrent_file" require "singleton" class TorrentClient class TorrentPeer attr_accessor :ip, :port, :peer_id, :last_contact_time attr_accessor :active, :conact_interval def initialize(data_hash,conact_interval) @active = true @ip = hash["ip"] @port = hash["port"] @peer_id = hash["peer id"] @conact_interval = conact_interval end def active? @active end end class TorrentPeerManager include Singleton @@peers = Array.new @@segment_peer_map = Hash.new def get_peers_for_segment(segment) @@segment_peer_map[segment] end def end def add_peer(bencode,segment) segment_hash = Bencode.decode(bencode) peers = segment_hash["peers"].collect{|peer| TorrentPeer.new(peer,segment_hash["interval"]) } @@peers.concat(peers).uniq @@segment_peer_map[segment] = peers peers end end attr_accessor :file, :torrent_peer_manager CLIENT_ID = "RL" def initialize(file_location) @uuid = (1..20).map{ rand(10)}.join @file = TorrentFile.new(file_location) @trackers = (@file.announce_list.lshift(@file.announce)).uniq @torrent_peer_manager end def set_peers_for_segment(segement) for tracker in @tracker end end end if __FILE__ == $0 require 'test/unit' class TorrentClientTest < Test::Unit::TestCase def setup @test_file = File.join(File.dirname(__FILE__),"test.torrent") end def test_download end end end #get_peers_for_segment #download_segment