diff --git a/docs/index.html b/docs/index.html index 19627e77e..eeb21d5f9 100755 --- a/docs/index.html +++ b/docs/index.html @@ -136,7 +136,7 @@ The session class has the following synopsis:
 class session: public boost::noncopyable
 {
-	session(int listen_port);
+	session(int listen_port, const std::string& fingerprint = std::string());
 
 	torrent_handle add_torrent(const torrent_info& t, const std::string& save_path);
 
@@ -153,6 +153,13 @@ want to save the files. The save_path will be prepended to the director
 structure in the torrent-file.
 

+

+fingerprint is a short string that will be used in the peer_id to +identify the client. If you want your fingerprint to be shorter than 4 +characters, you can terminate the string with a null. The default is an +empty string. +

+

How to parse a torrent file and create a torrent_info object is described below.

@@ -597,7 +604,6 @@ struct http_settings std::string user_agent; int tracker_timeout; int tracker_maximum_response_length; - char fingerprint[4]; };
@@ -617,13 +623,6 @@ uncompressed (given your limit is lower than 2 megs). Default limit is 1 megabyte.

-

-fingerprint is a short string that will be used in the peer_id to -identify the client. If you want your fingerprint to be shorter than 4 -characters, you can terminate the string with a null. The default is an -empty string. -

-

big_number

diff --git a/examples/client_test.cpp b/examples/client_test.cpp index 32587b098..8ea130474 100755 --- a/examples/client_test.cpp +++ b/examples/client_test.cpp @@ -59,13 +59,10 @@ int main(int argc, char* argv[]) // settings.proxy_password = "foobar"; settings.user_agent = "example"; - const char* fingerprint = "ex01"; - std::copy(fingerprint, fingerprint+4, settings.fingerprint); - try { std::vector handles; - session s(6881); + session s(6881, "ex-01"); s.set_http_settings(settings); for (int i = 0; i < argc-1; ++i) diff --git a/include/libtorrent/session.hpp b/include/libtorrent/session.hpp index 949cffa33..7ee890ef3 100755 --- a/include/libtorrent/session.hpp +++ b/include/libtorrent/session.hpp @@ -45,7 +45,7 @@ POSSIBILITY OF SUCH DAMAGE. */ -#ifndef TORRENT_SESION_HPP_INCLUDED +#ifndef TORRENT_SESSION_HPP_INCLUDED #define TORRENT_SESSION_HPP_INCLUDED #include @@ -59,6 +59,7 @@ POSSIBILITY OF SUCH DAMAGE. #include #include +#include "libtorrent/torrent_handle.hpp" #include "libtorrent/torrent.hpp" #include "libtorrent/entry.hpp" #include "libtorrent/torrent_info.hpp" @@ -115,10 +116,7 @@ namespace libtorrent { typedef std::map, boost::shared_ptr > connection_map; - session_impl() - : m_abort(false) - , m_tracker_manager(m_settings) - {} + session_impl(const std::string& fingerprint); // must be locked to access the data // in this struct @@ -191,42 +189,13 @@ namespace libtorrent std::string extract_fingerprint(const peer_id& p); - struct torrent_handle - { - friend class session; - - torrent_handle(): m_ses(0) {} - - float progress() const; - void get_peer_info(std::vector& v); - void abort(); - enum state_t - { - checking_files, - connecting_to_tracker, - downloading, - seeding - }; -// state_t state() const; - - private: - - torrent_handle(detail::session_impl* s, const sha1_hash& h) - : m_ses(s) - , m_info_hash(h) - {} - - detail::session_impl* m_ses; - sha1_hash m_info_hash; // should be replaced with a torrent*? - - }; - class session: public boost::noncopyable { public: - session(int listen_port) - : m_thread(detail::main_loop_thread(listen_port, &m_impl)) {} + session(int listen_port, const std::string& fingerprint = std::string()) + : m_impl(fingerprint) + , m_thread(detail::main_loop_thread(listen_port, &m_impl)) {} ~session(); diff --git a/include/libtorrent/torrent.hpp b/include/libtorrent/torrent.hpp index 3fd0e8861..c603457e9 100755 --- a/include/libtorrent/torrent.hpp +++ b/include/libtorrent/torrent.hpp @@ -43,6 +43,7 @@ POSSIBILITY OF SUCH DAMAGE. #include #include +#include "libtorrent/torrent_handle.hpp" #include "libtorrent/entry.hpp" #include "libtorrent/torrent_info.hpp" #include "libtorrent/socket.hpp" @@ -119,13 +120,7 @@ namespace libtorrent int bytes_uploaded() const { return m_bytes_uploaded; } int bytes_left() const { return m_storage.bytes_left(); } - // TODO: temporary implementation. Should count the actually - // verified pieces and should support the different states - // a torrent can be in. - float progress() const - { - return bytes_downloaded() / static_cast(m_torrent_file.total_size()); - } + std::pair status() const; void connect_to_peer(const address& a, const peer_id& id); diff --git a/include/libtorrent/torrent_handle.hpp b/include/libtorrent/torrent_handle.hpp new file mode 100755 index 000000000..30ba3600f --- /dev/null +++ b/include/libtorrent/torrent_handle.hpp @@ -0,0 +1,82 @@ +/* + +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_HANDLE_HPP_INCLUDED +#define TORRENT_TORRENT_HANDLE_HPP_INCLUDED + +#include + +#include "libtorrent/peer_id.hpp" +#include "libtorrent/peer_info.hpp" + + +namespace libtorrent +{ + namespace detail + { + struct session_impl; + } + + struct torrent_handle + { + friend class session; + + torrent_handle(): m_ses(0) {} + + void get_peer_info(std::vector& v); + void abort(); + enum state_t + { + invalid_handle, + checking_files, + connecting_to_tracker, + downloading, + seeding + }; + std::pair status() const; + + private: + + torrent_handle(detail::session_impl* s, const sha1_hash& h) + : m_ses(s) + , m_info_hash(h) + {} + + detail::session_impl* m_ses; + sha1_hash m_info_hash; // should be replaced with a torrent*? + + }; + + +} + +#endif // TORRENT_TORRENT_HANDLE_HPP_INCLUDED diff --git a/src/session.cpp b/src/session.cpp index 333796bc7..5e2ee3bfe 100755 --- a/src/session.cpp +++ b/src/session.cpp @@ -70,74 +70,53 @@ but also CPU-wise. This goal has a number of implications: to scale better when having many peer connections. * - - - - */ -namespace -{ - using namespace libtorrent; - - peer_id generate_peer_id(const http_settings& s) - { - peer_id ret; - std::srand(std::time(0)); -/* - std::fill(ret.begin(), ret.end(), 0); - std::copy(s.fingerprint, s.fingerprint+4, std::ostream_iterator(std::cout, " ")); - std::cout << "\n"; -*/ - // libtorrent's fingerprint - unsigned char fingerprint[] = "lt."; - const int len = 3-(s.fingerprint[0] == 0?1:0); - std::copy(fingerprint, fingerprint+len, ret.begin()); -// std::copy(ret.begin(), ret.end(), std::ostream_iterator(std::cout, " ")); -// std::cout << "\n"; - - // the client's fingerprint - const int len2 = std::find(s.fingerprint, s.fingerprint+sizeof(s.fingerprint), 0) - - s.fingerprint; -// std::cout << "len: " << len << "\n"; -// std::cout << "len2: " << len2 << "\n"; - std::copy(s.fingerprint, s.fingerprint+len2, ret.begin()+len); -// std::copy(ret.begin(), ret.end(), std::ostream_iterator(std::cout, " ")); -// std::cout << "\n"; - - // the zeros - std::fill(ret.begin()+len+len2, ret.begin()+len+len2+3, 0); -// std::copy(ret.begin(), ret.end(), std::ostream_iterator(std::cout, " ")); -// std::cout << "\n"; - - // the random number - for (unsigned char* i = ret.begin()+len+len2+3; - i != ret.end(); - ++i) - { - *i = rand(); - } -// std::copy(ret.begin(), ret.end(), std::ostream_iterator(std::cout, " ")); -// std::cout << "\n"; - return ret; - } - -} - namespace libtorrent { namespace detail { + session_impl::session_impl(const std::string& cl_fprint) + : m_abort(false) + , m_tracker_manager(m_settings) + { + + // ---- generate a peer id ---- + + std::srand(std::time(0)); + + // libtorrent's fingerprint + unsigned char fingerprint[] = "lt."; + + const int len2 = std::min(cl_fprint.length(), (long)7); + const int len1 = (len2 == 0?2:3); + const int len3 = 12 - len1 - len2; + + std::copy(fingerprint, fingerprint+len1, m_peer_id.begin()); + + // the client's fingerprint + std::copy(cl_fprint.begin(), cl_fprint.begin()+len2, m_peer_id.begin()+len1); + + // the zeros + std::fill(m_peer_id.begin()+len1+len2, m_peer_id.begin()+len1+len2+len3, 0); + assert(len1 + len2 + len3 == 12); + + // the random number + for (unsigned char* i = m_peer_id.begin()+len1+len2+len3; + i != m_peer_id.end(); + ++i) + { + *i = rand(); + } + } + + void session_impl::run(int listen_port) { #if defined(TORRENT_VERBOSE_LOGGING) m_logger = create_log("main session"); #endif - // TODO: don't generate peer id until we need it - // this way it's possible to set the fingerprint - m_peer_id = generate_peer_id(m_settings); - boost::shared_ptr listener(new socket(socket::tcp, false)); int max_port = listen_port + 9; @@ -509,15 +488,15 @@ namespace libtorrent m_thread.join(); } - - float torrent_handle::progress() const + std::pair torrent_handle::status() const { - if (m_ses == 0) return 0.f; + if (m_ses == 0) return std::make_pair(invalid_handle, 0.f); + boost::mutex::scoped_lock l(m_ses->m_mutex); std::map >::iterator i = m_ses->m_torrents.find(m_info_hash); - if (i == m_ses->m_torrents.end()) return 0.f; - return i->second->progress(); + if (i == m_ses->m_torrents.end()) return std::make_pair(invalid_handle, 0.f); + return i->second->status(); } void torrent_handle::get_peer_info(std::vector& v) diff --git a/src/torrent.cpp b/src/torrent.cpp index 8865157b5..b31a7d70c 100755 --- a/src/torrent.cpp +++ b/src/torrent.cpp @@ -41,6 +41,7 @@ POSSIBILITY OF SUCH DAMAGE. #include #include +#include "libtorrent/torrent_handle.hpp" #include "libtorrent/session.hpp" #include "libtorrent/torrent_info.hpp" #include "libtorrent/url_handler.hpp" @@ -400,5 +401,13 @@ namespace libtorrent } } + // TODO: temporary implementation. Should count the actually + // verified pieces and should support the different states + // a torrent can be in. + std::pair torrent::status() const + { + return std::make_pair(torrent_handle::downloading, 0.f); + } + }