2003-10-23 01:00:57 +02:00
|
|
|
/*
|
|
|
|
|
|
|
|
Copyright (c) 2003, Arvid Norberg
|
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions
|
|
|
|
are met:
|
|
|
|
|
|
|
|
* Redistributions of source code must retain the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer.
|
|
|
|
* Redistributions in binary form must reproduce the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer in
|
|
|
|
the documentation and/or other materials provided with the distribution.
|
|
|
|
* Neither the name of the author nor the names of its
|
|
|
|
contributors may be used to endorse or promote products derived
|
|
|
|
from this software without specific prior written permission.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef TORRENT_TORRENT_HPP_INCLUDE
|
|
|
|
#define TORRENT_TORRENT_HPP_INCLUDE
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <vector>
|
|
|
|
#include <set>
|
|
|
|
#include <list>
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
#include <boost/limits.hpp>
|
|
|
|
#include <boost/filesystem/path.hpp>
|
2003-11-20 20:58:29 +01:00
|
|
|
#include <boost/date_time/posix_time/posix_time.hpp>
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2003-10-26 18:35:23 +01:00
|
|
|
#include "libtorrent/torrent_handle.hpp"
|
2003-10-23 01:00:57 +02:00
|
|
|
#include "libtorrent/entry.hpp"
|
|
|
|
#include "libtorrent/torrent_info.hpp"
|
|
|
|
#include "libtorrent/socket.hpp"
|
|
|
|
#include "libtorrent/policy.hpp"
|
|
|
|
#include "libtorrent/storage.hpp"
|
|
|
|
#include "libtorrent/url_handler.hpp"
|
2003-12-07 06:53:04 +01:00
|
|
|
#include "libtorrent/stat.hpp"
|
2003-12-22 08:14:35 +01:00
|
|
|
#include "libtorrent/alert.hpp"
|
2003-10-23 01:00:57 +02:00
|
|
|
|
|
|
|
namespace libtorrent
|
|
|
|
{
|
|
|
|
#ifndef NDEBUG
|
|
|
|
struct logger;
|
|
|
|
#endif
|
|
|
|
|
2003-12-22 08:14:35 +01:00
|
|
|
struct tracker_alert: alert
|
|
|
|
{
|
|
|
|
tracker_alert(const torrent_handle& h
|
|
|
|
, const std::string& msg)
|
|
|
|
: alert(alert::warning, msg)
|
|
|
|
, handle(h)
|
|
|
|
{}
|
|
|
|
|
|
|
|
virtual std::auto_ptr<alert> clone() const
|
|
|
|
{ return std::auto_ptr<alert>(new tracker_alert(*this)); }
|
|
|
|
|
|
|
|
torrent_handle handle;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct hash_failed_alert: alert
|
|
|
|
{
|
|
|
|
hash_failed_alert(
|
|
|
|
const torrent_handle& h
|
|
|
|
, int index
|
|
|
|
, const std::string& msg)
|
|
|
|
: alert(alert::info, msg)
|
|
|
|
, handle(h)
|
|
|
|
, piece_index(index)
|
|
|
|
{}
|
|
|
|
|
|
|
|
virtual std::auto_ptr<alert> clone() const
|
|
|
|
{ return std::auto_ptr<alert>(new hash_failed_alert(*this)); }
|
|
|
|
|
|
|
|
torrent_handle handle;
|
|
|
|
int piece_index;
|
|
|
|
};
|
|
|
|
|
2003-10-23 01:00:57 +02:00
|
|
|
namespace detail
|
|
|
|
{
|
|
|
|
struct session_impl;
|
|
|
|
}
|
|
|
|
|
|
|
|
// a torrent is a class that holds information
|
|
|
|
// for a specific download. It updates itself against
|
|
|
|
// the tracker
|
|
|
|
class torrent: public request_callback
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2003-12-07 06:53:04 +01:00
|
|
|
typedef entry::integer_type size_type;
|
|
|
|
|
|
|
|
torrent(
|
|
|
|
detail::session_impl& ses
|
|
|
|
, const torrent_info& torrent_file
|
|
|
|
, const boost::filesystem::path& save_path);
|
2003-12-14 06:56:12 +01:00
|
|
|
|
|
|
|
~torrent();
|
2003-10-23 01:00:57 +02:00
|
|
|
|
|
|
|
void abort() { m_abort = true; m_event = event_stopped; }
|
|
|
|
bool is_aborted() const { return m_abort; }
|
|
|
|
|
2003-12-01 06:01:40 +01:00
|
|
|
// is called every second by session.
|
|
|
|
void second_tick();
|
|
|
|
|
2003-10-27 16:43:33 +01:00
|
|
|
// returns true if it time for this torrent to make another
|
|
|
|
// tracker request
|
2004-01-12 21:31:27 +01:00
|
|
|
bool should_request() const
|
2003-10-23 01:00:57 +02:00
|
|
|
{
|
2003-11-26 15:11:25 +01:00
|
|
|
// boost::posix_time::time_duration d = m_next_request - boost::posix_time::second_clock::local_time();
|
|
|
|
// return d.is_negative();
|
|
|
|
return m_next_request < boost::posix_time::second_clock::local_time();
|
2003-10-23 01:00:57 +02:00
|
|
|
}
|
|
|
|
|
2004-01-08 18:03:04 +01:00
|
|
|
void force_tracker_request()
|
|
|
|
{
|
|
|
|
m_next_request = boost::posix_time::second_clock::local_time();
|
|
|
|
}
|
|
|
|
|
2003-10-23 01:00:57 +02:00
|
|
|
void print(std::ostream& os) const;
|
|
|
|
|
2004-01-08 18:03:04 +01:00
|
|
|
void check_files(
|
|
|
|
detail::piece_checker_data& data
|
|
|
|
, boost::mutex& mutex);
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2003-12-07 06:53:04 +01:00
|
|
|
stat statistics() const { return m_stat; }
|
|
|
|
size_type bytes_left() const;
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2003-10-31 05:02:51 +01:00
|
|
|
torrent_status status() const;
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2003-12-14 06:56:12 +01:00
|
|
|
peer_connection& connect_to_peer(
|
2003-12-01 06:01:40 +01:00
|
|
|
const address& a
|
|
|
|
, const peer_id& id);
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2004-01-08 18:03:04 +01:00
|
|
|
const torrent_info& torrent_file() const
|
2003-12-01 06:01:40 +01:00
|
|
|
{ return m_torrent_file; }
|
2003-10-23 01:00:57 +02:00
|
|
|
|
|
|
|
policy& get_policy() { return *m_policy; }
|
|
|
|
|
2003-12-07 06:53:04 +01:00
|
|
|
piece_manager& filesystem() { return m_storage; }
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2004-01-12 04:05:10 +01:00
|
|
|
void set_ratio(float ratio)
|
|
|
|
{ m_ratio = ratio; }
|
|
|
|
|
|
|
|
float ratio() const
|
|
|
|
{ return m_ratio; }
|
|
|
|
|
2003-10-23 01:00:57 +02:00
|
|
|
// --------------------------------------------
|
|
|
|
// PEER MANAGEMENT
|
|
|
|
|
|
|
|
// used by peer_connection to attach itself to a torrent
|
|
|
|
// since incoming connections don't know what torrent
|
|
|
|
// they're a part of until they have received an info_hash.
|
2003-12-01 06:01:40 +01:00
|
|
|
void attach_peer(peer_connection* p);
|
2003-10-23 01:00:57 +02:00
|
|
|
|
|
|
|
// this will remove the peer and make sure all
|
|
|
|
// the pieces it had have their reference counter
|
|
|
|
// decreased in the piece_picker
|
|
|
|
void remove_peer(peer_connection* p);
|
|
|
|
|
|
|
|
// the number of peers that belong to this torrent
|
|
|
|
int num_peers() const { return m_connections.size(); }
|
|
|
|
|
2003-12-01 06:01:40 +01:00
|
|
|
// returns true if this torrent has a connection
|
|
|
|
// to a peer with the given peer_id
|
|
|
|
bool has_peer(const peer_id& id) const;
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2003-11-05 00:27:06 +01:00
|
|
|
typedef std::vector<peer_connection*>::iterator peer_iterator;
|
|
|
|
typedef std::vector<peer_connection*>::const_iterator peer_const_iterator;
|
|
|
|
|
|
|
|
peer_const_iterator begin() const { return m_connections.begin(); }
|
|
|
|
peer_const_iterator end() const { return m_connections.end(); }
|
|
|
|
|
|
|
|
peer_iterator begin() { return m_connections.begin(); }
|
|
|
|
peer_iterator end() { return m_connections.end(); }
|
2003-10-23 01:00:57 +02:00
|
|
|
|
|
|
|
|
|
|
|
// --------------------------------------------
|
|
|
|
// TRACKER MANAGEMENT
|
|
|
|
|
|
|
|
// this is a callback called by the tracker_connection class
|
|
|
|
// when this torrent got a response from its tracker request
|
2003-12-22 08:14:35 +01:00
|
|
|
virtual void tracker_response(const entry& e);
|
|
|
|
virtual void tracker_request_timed_out();
|
|
|
|
virtual void tracker_request_error(const char* str);
|
2003-10-23 01:00:57 +02:00
|
|
|
|
|
|
|
// generates a request string for sending
|
|
|
|
// to the tracker
|
|
|
|
std::string generate_tracker_request(int port);
|
|
|
|
|
2003-11-05 00:27:06 +01:00
|
|
|
boost::posix_time::ptime next_announce() const
|
|
|
|
{ return m_next_request; }
|
2003-10-23 01:00:57 +02:00
|
|
|
|
|
|
|
// --------------------------------------------
|
|
|
|
// PIECE MANAGEMENT
|
|
|
|
|
|
|
|
// returns true if we have downloaded the given piece
|
2003-12-07 06:53:04 +01:00
|
|
|
bool have_piece(unsigned int index) const
|
|
|
|
{ return m_have_pieces[index]; }
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2003-12-17 04:40:13 +01:00
|
|
|
const std::vector<bool>& pieces() const
|
|
|
|
{ return m_have_pieces; }
|
|
|
|
|
2003-10-23 01:00:57 +02:00
|
|
|
// when we get a have- or bitfield- messages, this is called for every
|
|
|
|
// piece a peer has gained.
|
2003-12-18 04:30:41 +01:00
|
|
|
void peer_has(int index)
|
|
|
|
{ m_picker.inc_refcount(index); }
|
2003-10-23 01:00:57 +02:00
|
|
|
|
|
|
|
// when peer disconnects, this is called for every piece it had
|
|
|
|
void peer_lost(int index)
|
2003-12-18 04:30:41 +01:00
|
|
|
{ m_picker.dec_refcount(index); }
|
2003-10-23 01:00:57 +02:00
|
|
|
|
|
|
|
int block_size() const { return m_block_size; }
|
|
|
|
|
|
|
|
// this will tell all peers that we just got his piece
|
|
|
|
// and also let the piece picker know that we have this piece
|
|
|
|
// so it wont pick it for download
|
|
|
|
void announce_piece(int index);
|
|
|
|
|
|
|
|
void close_all_connections();
|
|
|
|
|
|
|
|
piece_picker& picker() { return m_picker; }
|
|
|
|
|
2003-12-07 06:53:04 +01:00
|
|
|
|
|
|
|
bool verify_piece(int piece_index);
|
|
|
|
|
2003-12-01 22:27:27 +01:00
|
|
|
// this is called from the peer_connection
|
|
|
|
// each time a piece has failed the hash
|
|
|
|
// test
|
|
|
|
void piece_failed(int index);
|
|
|
|
|
2003-12-07 06:53:04 +01:00
|
|
|
float priority() const
|
|
|
|
{ return m_priority; }
|
|
|
|
|
|
|
|
void set_priority(float p)
|
|
|
|
{
|
|
|
|
assert(p >= 0.f && p <= 0.f);
|
|
|
|
m_priority = p;
|
|
|
|
}
|
|
|
|
|
2003-12-09 19:09:34 +01:00
|
|
|
bool is_seed() const
|
|
|
|
{ return m_num_pieces == m_torrent_file.num_pieces(); }
|
|
|
|
|
2003-12-07 06:53:04 +01:00
|
|
|
boost::filesystem::path save_path() const
|
|
|
|
{ return m_storage.save_path(); }
|
|
|
|
|
2004-01-07 01:48:02 +01:00
|
|
|
alert_manager& alerts() const;
|
|
|
|
torrent_handle get_handle() const;
|
|
|
|
|
2003-10-23 01:00:57 +02:00
|
|
|
// DEBUG
|
|
|
|
#ifndef NDEBUG
|
|
|
|
logger* spawn_logger(const char* title);
|
|
|
|
#endif
|
|
|
|
|
2003-11-20 20:58:29 +01:00
|
|
|
#ifndef NDEBUG
|
|
|
|
virtual void debug_log(const std::string& line);
|
2003-12-14 06:56:12 +01:00
|
|
|
void check_invariant();
|
2003-11-20 20:58:29 +01:00
|
|
|
#endif
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2003-11-26 15:11:25 +01:00
|
|
|
private:
|
|
|
|
|
2003-10-23 01:00:57 +02:00
|
|
|
void try_next_tracker();
|
|
|
|
|
|
|
|
enum event_id
|
|
|
|
{
|
|
|
|
event_started = 0,
|
|
|
|
event_stopped,
|
|
|
|
event_completed,
|
|
|
|
event_none
|
|
|
|
};
|
|
|
|
|
|
|
|
// the size of a request block
|
|
|
|
// each piece is divided into these
|
|
|
|
// blocks when requested
|
|
|
|
int m_block_size;
|
|
|
|
|
2003-10-23 18:55:52 +02:00
|
|
|
// is set to true when the torrent has
|
|
|
|
// been aborted.
|
2003-10-23 01:00:57 +02:00
|
|
|
bool m_abort;
|
|
|
|
|
|
|
|
event_id m_event;
|
|
|
|
|
2003-10-23 18:55:52 +02:00
|
|
|
void parse_response(const entry& e, std::vector<peer>& peer_list);
|
2003-10-23 01:00:57 +02:00
|
|
|
|
|
|
|
torrent_info m_torrent_file;
|
|
|
|
|
2003-12-07 02:26:57 +01:00
|
|
|
piece_manager m_storage;
|
2003-10-23 01:00:57 +02:00
|
|
|
|
|
|
|
// the time of next tracker request
|
|
|
|
boost::posix_time::ptime m_next_request;
|
|
|
|
|
|
|
|
// -----------------------------
|
|
|
|
// DATA FROM TRACKER RESPONSE
|
|
|
|
|
|
|
|
// the number number of seconds between requests
|
|
|
|
// from the tracker
|
|
|
|
int m_duration;
|
|
|
|
|
|
|
|
std::vector<peer_connection*> m_connections;
|
|
|
|
|
2003-12-07 06:53:04 +01:00
|
|
|
// this is the upload and download statistics for the whole torrent.
|
|
|
|
// it's updated from all its peers once every second.
|
|
|
|
libtorrent::stat m_stat;
|
|
|
|
|
2003-10-23 01:00:57 +02:00
|
|
|
// -----------------------------
|
|
|
|
|
|
|
|
boost::shared_ptr<policy> m_policy;
|
|
|
|
|
2003-12-07 06:53:04 +01:00
|
|
|
// a back reference to the session
|
|
|
|
// this torrent belongs to.
|
|
|
|
detail::session_impl& m_ses;
|
2003-10-23 01:00:57 +02:00
|
|
|
|
|
|
|
piece_picker m_picker;
|
|
|
|
|
|
|
|
// this is an index into m_torrent_file.trackers()
|
|
|
|
int m_last_working_tracker;
|
|
|
|
int m_currently_trying_tracker;
|
|
|
|
|
2003-12-01 06:01:40 +01:00
|
|
|
// this is a counter that is increased every
|
|
|
|
// second, and when it reaches 10, the policy::pulse()
|
|
|
|
// is called and the time scaler is reset to 0.
|
|
|
|
int m_time_scaler;
|
2003-12-07 06:53:04 +01:00
|
|
|
|
|
|
|
// this is the priority of this torrent. It is used
|
|
|
|
// to weight the assigned upload bandwidth between peers
|
|
|
|
// it should be within the range [0, 1]
|
|
|
|
float m_priority;
|
|
|
|
|
|
|
|
// the bitmask that says which pieces we have
|
|
|
|
std::vector<bool> m_have_pieces;
|
|
|
|
|
|
|
|
// the number of pieces we have. The same as
|
|
|
|
// std::accumulate(m_have_pieces.begin(),
|
|
|
|
// m_have_pieces.end(), 0)
|
|
|
|
int m_num_pieces;
|
2003-12-22 08:14:35 +01:00
|
|
|
|
|
|
|
// is false by default and set to
|
|
|
|
// true when the first tracker reponse
|
|
|
|
// is received
|
|
|
|
bool m_got_tracker_response;
|
2004-01-12 04:05:10 +01:00
|
|
|
|
|
|
|
// the upload/download ratio that each peer
|
|
|
|
// tries to maintain.
|
|
|
|
// 0 is infinite
|
|
|
|
float m_ratio;
|
2003-10-23 01:00:57 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // TORRENT_TORRENT_HPP_INCLUDED
|