From 35141733fbb917a1e619b6671ae1030325bac2a0 Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Sat, 10 May 2014 20:20:57 +0000 Subject: [PATCH] remove some old unmaintained example code. make examples depend less on libtorrent internals --- examples/Jamfile | 1 - examples/Makefile.am | 4 - examples/client_test.cpp | 23 +++-- examples/dump_torrent.cpp | 103 ++------------------ examples/fragmentation_test.cpp | 152 ------------------------------ examples/upnp_test.cpp | 11 ++- examples/utp_test.cpp | 80 ---------------- include/libtorrent/hasher.hpp | 14 ++- include/libtorrent/lazy_entry.hpp | 4 +- src/time.cpp | 6 -- 10 files changed, 49 insertions(+), 349 deletions(-) delete mode 100644 examples/fragmentation_test.cpp delete mode 100644 examples/utp_test.cpp diff --git a/examples/Jamfile b/examples/Jamfile index adf4d5111..87f20e05a 100644 --- a/examples/Jamfile +++ b/examples/Jamfile @@ -22,7 +22,6 @@ exe simple_client : simple_client.cpp ; exe dump_torrent : dump_torrent.cpp ; exe make_torrent : make_torrent.cpp ; exe connection_tester : connection_tester.cpp ; -exe fragmentation_test : fragmentation_test.cpp ; exe rss_reader : rss_reader.cpp ; exe upnp_test : upnp_test.cpp ; diff --git a/examples/Makefile.am b/examples/Makefile.am index 98bf36b3a..09134cc09 100644 --- a/examples/Makefile.am +++ b/examples/Makefile.am @@ -1,7 +1,6 @@ example_programs = \ client_test \ dump_torrent \ - fragmentation_test\ make_torrent \ simple_client \ rss_reader \ @@ -33,9 +32,6 @@ connection_tester_SOURCES = connection_tester.cpp rss_reader_SOURCES = rss_reader.cpp #rss_reader_LDADD = $(top_builddir)/src/libtorrent-rasterbar.la -fragmentation_test_SOURCES = fragmentation_test.cpp -#fragmentation_test_LDADD = $(top_builddir)/src/libtorrent-rasterbar.la - upnp_test_SOURCES = upnp_test.cpp #upnp_test_LDADD = $(top_builddir)/src/libtorrent-rasterbar.la diff --git a/examples/client_test.cpp b/examples/client_test.cpp index 6d50865ee..05ddf6395 100644 --- a/examples/client_test.cpp +++ b/examples/client_test.cpp @@ -896,6 +896,15 @@ torrent_status const& get_active_torrent(std::vector cons return *filtered_handles[active_torrent]; } +char const* timestamp() +{ + time_t t = std::time(0); + tm* timeinfo = std::localtime(&t); + static char str[200]; + std::strftime(str, 200, "%b %d %X", timeinfo); + return str; +} + void print_alert(libtorrent::alert const* a, std::string& str) { using namespace libtorrent; @@ -911,7 +920,7 @@ void print_alert(libtorrent::alert const* a, std::string& str) } #endif str += "["; - str += time_now_string(); + str += timestamp(); str += "] "; str += a->message(); #ifdef ANSI_TERMINAL_COLORS @@ -919,7 +928,7 @@ void print_alert(libtorrent::alert const* a, std::string& str) #endif if (g_log_file) - fprintf(g_log_file, "[%s] %s\n", time_now_string(), a->message().c_str()); + fprintf(g_log_file, "[%s] %s\n", timestamp(), a->message().c_str()); } int save_file(std::string const& filename, std::vector& v) @@ -961,7 +970,7 @@ bool handle_alert(libtorrent::session& ses, libtorrent::alert* a { char msg[256]; snprintf(msg, sizeof(msg), "ERROR. could not load certificate %s: %s\n", cert.c_str(), ec.message().c_str()); - if (g_log_file) fprintf(g_log_file, "[%s] %s\n", time_now_string(), msg); + if (g_log_file) fprintf(g_log_file, "[%s] %s\n", timestamp(), msg); return true; } stat_file(priv, &st, ec); @@ -969,13 +978,13 @@ bool handle_alert(libtorrent::session& ses, libtorrent::alert* a { char msg[256]; snprintf(msg, sizeof(msg), "ERROR. could not load private key %s: %s\n", priv.c_str(), ec.message().c_str()); - if (g_log_file) fprintf(g_log_file, "[%s] %s\n", time_now_string(), msg); + if (g_log_file) fprintf(g_log_file, "[%s] %s\n", timestamp(), msg); return true; } char msg[256]; snprintf(msg, sizeof(msg), "loaded certificate %s and key %s\n", cert.c_str(), priv.c_str()); - if (g_log_file) fprintf(g_log_file, "[%s] %s\n", time_now_string(), msg); + if (g_log_file) fprintf(g_log_file, "[%s] %s\n", timestamp(), msg); h.set_ssl_certificate(cert, priv, "certificates/dhparams.pem", "1234"); h.resume(); @@ -1961,7 +1970,7 @@ int main(int argc, char* argv[]) // loop through the alert queue to see if anything has happened. std::deque alerts; ses.pop_alerts(&alerts); - std::string now = time_now_string(); + std::string now = timestamp(); for (std::deque::iterator i = alerts.begin() , end(alerts.end()); i != end; ++i) { @@ -2471,7 +2480,7 @@ int main(int argc, char* argv[]) std::deque alerts; ses.pop_alerts(&alerts); - std::string now = time_now_string(); + std::string now = timestamp(); for (std::deque::iterator i = alerts.begin() , end(alerts.end()); i != end; ++i) { diff --git a/examples/dump_torrent.cpp b/examples/dump_torrent.cpp index a70c3891f..adb3ca3da 100644 --- a/examples/dump_torrent.cpp +++ b/examples/dump_torrent.cpp @@ -149,93 +149,6 @@ int line_longer_than(libtorrent::lazy_entry const& e, int limit) return line_len; } -std::string print_entry(libtorrent::lazy_entry const& e, bool single_line = false, int indent = 0) -{ - using namespace libtorrent; - - char indent_str[200]; - memset(indent_str, ' ', 200); - indent_str[0] = ','; - indent_str[1] = '\n'; - indent_str[199] = 0; - if (indent < 197 && indent >= 0) indent_str[indent+2] = 0; - std::string ret; - switch (e.type()) - { - case lazy_entry::none_t: return "none"; - case lazy_entry::int_t: - { - char str[100]; - snprintf(str, sizeof(str), "%" PRId64, e.int_value()); - return str; - } - case lazy_entry::string_t: - { - bool printable = true; - char const* str = e.string_ptr(); - for (int i = 0; i < e.string_length(); ++i) - { - using namespace std; - if (is_print((unsigned char)str[i])) continue; - printable = false; - break; - } - ret += "'"; - if (printable) - { - ret += e.string_value(); - ret += "'"; - return ret; - } - for (int i = 0; i < e.string_length(); ++i) - { - char tmp[5]; - snprintf(tmp, sizeof(tmp), "%02x", (unsigned char)str[i]); - ret += tmp; - } - ret += "'"; - return ret; - } - case lazy_entry::list_t: - { - ret += '['; - bool one_liner = line_longer_than(e, 200) != -1 || single_line; - - if (!one_liner) ret += indent_str + 1; - for (int i = 0; i < e.list_size(); ++i) - { - if (i == 0 && one_liner) ret += " "; - ret += ::print_entry(*e.list_at(i), single_line, indent + 2); - if (i < e.list_size() - 1) ret += (one_liner?", ":indent_str); - else ret += (one_liner?" ":indent_str+1); - } - ret += "]"; - return ret; - } - case lazy_entry::dict_t: - { - ret += "{"; - bool one_liner = line_longer_than(e, 200) != -1 || single_line; - - if (!one_liner) ret += indent_str+1; - for (int i = 0; i < e.dict_size(); ++i) - { - if (i == 0 && one_liner) ret += " "; - std::pair ent = e.dict_at(i); - ret += "'"; - ret += ent.first; - ret += "': "; - ret += ::print_entry(*ent.second, single_line, indent + 2); - if (i < e.dict_size() - 1) ret += (one_liner?", ":indent_str); - else ret += (one_liner?" ":indent_str+1); - } - ret += "}"; - return ret; - } - } - return ret; -} - int main(int argc, char* argv[]) { using namespace libtorrent; @@ -252,15 +165,15 @@ int main(int argc, char* argv[]) if (argc > 2) item_limit = atoi(argv[2]); if (argc > 3) depth_limit = atoi(argv[3]); - int size = file_size(argv[1]); - if (size > 40 * 1000000) - { - fprintf(stderr, "file too big (%d), aborting\n", size); - return 1; - } - std::vector buf(size); + std::vector buf; error_code ec; int ret = load_file(argv[1], buf, ec, 40 * 1000000); + if (ret == -1) + { + fprintf(stderr, "file too big, aborting\n"); + return 1; + } + if (ret != 0) { fprintf(stderr, "failed to load file: %s\n", ec.message().c_str()); @@ -273,7 +186,7 @@ int main(int argc, char* argv[]) ret = lazy_bdecode(&buf[0], &buf[0] + buf.size(), e, ec, &pos , depth_limit, item_limit); - printf("\n\n----- raw info -----\n\n%s\n", ::print_entry(e).c_str()); + printf("\n\n----- raw info -----\n\n%s\n", print_entry(e).c_str()); if (ret != 0) { diff --git a/examples/fragmentation_test.cpp b/examples/fragmentation_test.cpp deleted file mode 100644 index 12abad848..000000000 --- a/examples/fragmentation_test.cpp +++ /dev/null @@ -1,152 +0,0 @@ -/* - -Copyright (c) 2010, 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. - -*/ - -#include "libtorrent/storage.hpp" -#include "libtorrent/file_pool.hpp" - -#include - -#include - -using namespace libtorrent; - -int main(int argc, char* argv[]) -{ - if (argc != 3 && argc != 2) - { - fprintf(stderr, "Usage: fragmentation_test torrent-file file-storage-path\n" - " fragmentation_test file\n\n"); - return 1; - } - - if (argc == 2) - { - error_code ec; - file f(argv[1], file::read_only, ec); - if (ec) - { - fprintf(stderr, "Error opening file %s: %s\n", argv[1], ec.message().c_str()); - return 1; - } - size_type off = f.phys_offset(0); - printf("physical offset of file %s: %" PRId64 "\n", argv[1], off); - return 0; - } - - error_code ec; - boost::intrusive_ptr ti(new torrent_info(argv[1], ec)); - - if (ec) - { - fprintf(stderr, "Error while loading torrent file: %s\n", ec.message().c_str()); - return 1; - } - - file_pool fp; - boost::shared_ptr st(default_storage_constructor(ti->files(), 0, argv[2], fp, std::vector())); - - // the first field is the piece index, the second - // one is the physical location of the piece on disk - std::vector > pieces; - - // make sure all the files are there -/* std::vector > files = get_filesizes(ti->files(), argv[2]); - for (int i = 0; i < ti->num_files(); ++i) - { - if (ti->file_at(i).size == files[i].first) continue; - fprintf(stderr, "Files for this torrent are missing or incomplete: %s was %" PRId64 " bytes, expected %" PRId64 " bytes\n" - , ti->files().file_path(ti->file_at(i)).c_str(), files[i].first, ti->file_at(i).size); - return 1; - } -*/ - bool warned = false; - for (int i = 0; i < ti->num_pieces(); ++i) - { - pieces.push_back(std::make_pair(i, st->physical_offset(i, 0))); - if (pieces.back().second == size_type(i) * ti->piece_length()) - { - if (!warned) - { - fprintf(stderr, "The files are incomplete\n"); - warned = true; - } - pieces.pop_back(); - } - } - - // this suggests that the OS doesn't support physical offset - // or that the file doesn't exist or is incomplete - if (pieces.empty()) - { - fprintf(stderr, "Your operating system or filesystem " - "does not appear to support querying physical disk offset\n"); - } - - FILE* f = fopen("fragmentation.log", "w+"); - if (f == 0) - { - fprintf(stderr, "error while opening log file: %s\n", strerror(errno)); - return 1; - } - - for (int i = 0; i < pieces.size(); ++i) - { - fprintf(f, "%d %" PRId64 "\n", pieces[i].first, pieces[i].second); - } - - fclose(f); - - f = fopen("fragmentation.gnuplot", "w+"); - if (f == 0) - { - fprintf(stderr, "error while opening gnuplot file: %s\n", strerror(errno)); - return 1; - } - - fprintf(f, - "set term png size 1200,800\n" - "set output \"fragmentation.png\"\n" - "set xrange [*:*]\n" - "set xlabel \"piece\"\n" - "set ylabel \"drive offset\"\n" - "set key box\n" - "set title \"fragmentation for '%s'\"\n" - "set tics nomirror\n" - "plot \"fragmentation.log\" using 1:2 with points lt rgb \"#e07070\" notitle axis x1y1\n" - , ti->name().c_str()); - - fclose(f); - - system("gnuplot fragmentation.gnuplot"); -} - - diff --git a/examples/upnp_test.cpp b/examples/upnp_test.cpp index 6bec75a2c..bc7843b44 100644 --- a/examples/upnp_test.cpp +++ b/examples/upnp_test.cpp @@ -34,6 +34,15 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/session.hpp" #include "libtorrent/alert_types.hpp" +char const* timestamp() +{ + time_t t = std::time(0); + tm* timeinfo = std::localtime(&t); + static char str[200]; + std::strftime(str, 200, "%b %d %X", timeinfo); + return str; +} + void print_alert(libtorrent::alert const* a) { using namespace libtorrent; @@ -47,7 +56,7 @@ void print_alert(libtorrent::alert const* a) printf("%s","\x1b[33m"); } - printf("[%s] %s\n", time_now_string(), a->message().c_str()); + printf("[%s] %s\n", timestamp(), a->message().c_str()); printf("%s", "\x1b[0m"); } diff --git a/examples/utp_test.cpp b/examples/utp_test.cpp deleted file mode 100644 index 1d1900bb3..000000000 --- a/examples/utp_test.cpp +++ /dev/null @@ -1,80 +0,0 @@ -/* - -Copyright (c) 2012, 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. - -*/ - -#include "libtorrent/error_code.hpp" -#include "libtorrent/session.hpp" -#include "libtorrent/socket_type.hpp" -#include "libtorrent/utp_socket_manager.hpp" -#include "libtorrent/utp_stream.hpp" - -using namespace libtorrent; - -void on_connect(error_code const& e) -{ -} - -void on_udp_receive(error_code const& e, udp::endpoint const& ep - , char const* buf, int size) -{ -} - -void on_utp_incoming(void* userdata - , boost::shared_ptr const& utp_sock) -{ -} - -int main(int argc, char* argv[]) -{ - //int rtt, rtt_var; - //int max_window, cur_window; - //int delay_factor, window_factor, scaled_gain; - - /*session s; - s.listen_on(std::make_pair(6881, 6889));*/ - - io_service ios; - connection_queue cc(ios); - udp_socket udp_sock(ios, boost::bind(&on_udp_receive, _1, _2, _3, _4), cc); - - void* userdata; - utp_socket_manager utp_sockets(udp_sock, boost::bind(&on_utp_incoming, _1, _2), userdata); - - /*error_code ec; - utp_stream sock(ios, cc); - sock.bind(udp::endpoint(address_v4::any(), 0), ec); - - tcp::endpoint ep(address_v4::from_string("239.192.152.143", ec), 6771); - - sock.async_connect(ep, boost::bind(on_connect, _1));*/ - - return 0; -} diff --git a/include/libtorrent/hasher.hpp b/include/libtorrent/hasher.hpp index 4eff38490..452998d70 100644 --- a/include/libtorrent/hasher.hpp +++ b/include/libtorrent/hasher.hpp @@ -74,7 +74,8 @@ namespace libtorrent namespace libtorrent { - + // this is a SHA-1 hash class. + // // You use it by first instantiating it, then call ``update()`` to feed it // with data. i.e. you don't have to keep the entire buffer of which you want to // create the hash in memory. You can feed the hasher parts of it at a time. When @@ -89,11 +90,14 @@ namespace libtorrent // // The sha1-algorithm used was implemented by Steve Reid and released as public domain. // For more info, see ``src/sha1.cpp``. - class TORRENT_EXTRA_EXPORT hasher + class TORRENT_EXPORT hasher { public: hasher(); + + // this is the same as default constructing followed by a call to + // ``update(data, len)``. hasher(const char* data, int len); #ifdef TORRENT_USE_GCRYPT @@ -101,10 +105,16 @@ namespace libtorrent hasher& operator=(hasher const& h); #endif + // append the following bytes to what is being hashed hasher& update(std::string const& data) { update(data.c_str(), data.size()); return *this; } hasher& update(const char* data, int len); + + // returns the SHA-1 digest of the buffers previously passed to + // update() and the hasher constructor. sha1_hash final(); + // restore the hasher state to be as if the hasher has just been + // default constructed. void reset(); #ifdef TORRENT_USE_GCRYPT diff --git a/include/libtorrent/lazy_entry.hpp b/include/libtorrent/lazy_entry.hpp index fa83e7ba3..2d8d24b6b 100644 --- a/include/libtorrent/lazy_entry.hpp +++ b/include/libtorrent/lazy_entry.hpp @@ -386,7 +386,9 @@ namespace libtorrent lazy_entry val; }; - TORRENT_EXTRA_EXPORT std::string print_entry(lazy_entry const& e + // print the bencoded structure in a human-readable format to a stting + // that's returned. + TORRENT_EXPORT std::string print_entry(lazy_entry const& e , bool single_line = false, int indent = 0); // get the ``error_category`` for bdecode errors diff --git a/src/time.cpp b/src/time.cpp index c8d535422..bea93856c 100644 --- a/src/time.cpp +++ b/src/time.cpp @@ -58,12 +58,6 @@ namespace libtorrent char const* time_now_string() { -// time_t t = std::time(0); -// tm* timeinfo = std::localtime(&t); -// static char str[200]; -// std::strftime(str, 200, "%b %d %X", timeinfo); -// return str; - static const ptime start = time_now_hires(); static char ret[200]; int t = total_milliseconds(time_now_hires() - start);