From 0019e23b4f18c5817bc8c20b2c1bf47b154af613 Mon Sep 17 00:00:00 2001 From: Daniel Wallin Date: Sat, 29 Nov 2003 16:34:07 +0000 Subject: [PATCH] alerts --- include/libtorrent/alert.hpp | 166 +++++++++++++++++++++++++++++++++ include/libtorrent/session.hpp | 6 ++ src/alert.cpp | 84 +++++++++++++++++ src/session.cpp | 5 + src/storage.cpp | 2 +- 5 files changed, 262 insertions(+), 1 deletion(-) create mode 100755 include/libtorrent/alert.hpp create mode 100755 src/alert.cpp diff --git a/include/libtorrent/alert.hpp b/include/libtorrent/alert.hpp new file mode 100755 index 000000000..27cde95a0 --- /dev/null +++ b/include/libtorrent/alert.hpp @@ -0,0 +1,166 @@ +/* + +Copyright (c) 2003, Arvid Norberg, Daniel Wallin +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_ERROR_HPP_INCLUDED +#define TORRENT_ERROR_HPP_INCLUDED + +#include +#include +#include +#include +#include + +#include + +#include +#include +#include +#include + +namespace libtorrent { + + class alert + { + public: + enum severity_t { info, warning, critital, fatal }; + + alert(severity_t severity, const std::string& msg) + : m_msg(msg) + , m_severity(severity) + {} + + virtual ~alert() {} + + const std::string& msg() const + { + return m_msg; + } + + severity_t severity() const + { + return m_severity; + } + + virtual std::auto_ptr clone() const = 0; + + private: + std::string m_msg; + severity_t m_severity; + }; + + class alert_manager + { + public: + alert_manager(); + ~alert_manager(); + + void post_alert(const alert& alert_); + bool pending() const; + std::auto_ptr get(); + + void set_severity(alert::severity_t severity); + + private: + std::queue m_alerts; + alert::severity_t m_severity; + mutable boost::mutex m_mutex; + }; + + struct unhandled_alert : std::exception + { + unhandled_alert() {} + }; + + namespace detail { + + struct void_; + + template< + class Handler + , BOOST_PP_ENUM_PARAMS(5, class T) + > + void handle_alert_dispatch( + const std::auto_ptr& alert_ + , const Handler& handler + , const std::type_info& typeid_ + , BOOST_PP_ENUM_BINARY_PARAMS(5, T, *p)) + { + if (typeid_ == typeid(T0)) + handler(*static_cast(alert_.get())); + else + handle_alert_dispatch( + alert_ + , handler + , typeid_ + , BOOST_PP_ENUM_SHIFTED_PARAMS(5, p), (void_*)0 + ); + } + + template + void handle_alert_dispatch( + const std::auto_ptr& alert_ + , const Handler& handler + , const std::type_info& typeid_ + , BOOST_PP_ENUM_PARAMS(5, void_* BOOST_PP_INTERCEPT)) + { + throw unhandled_alert(); + } + + } // namespace detail + + template< + BOOST_PP_ENUM_PARAMS_WITH_A_DEFAULT(5, class T, detail::void_) + > + struct handle_alert + { + template + handle_alert( + const std::auto_ptr& alert_ + , const Handler& handler) + { + #define ALERT_POINTER_TYPE(z, n, text) (BOOST_PP_CAT(T, n)*)0 + + detail::handle_alert_dispatch( + alert_ + , handler + , typeid(*alert_) + , BOOST_PP_ENUM(5, ALERT_POINTER_TYPE, _) + ); + + #undef ALERT_POINTER_TYPE + } + }; + +} // namespace libtorrent + +#endif // TORRENT_ERROR_HPP_INCLUDED + diff --git a/include/libtorrent/session.hpp b/include/libtorrent/session.hpp index b67b5a0c4..86b661c08 100755 --- a/include/libtorrent/session.hpp +++ b/include/libtorrent/session.hpp @@ -55,6 +55,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/policy.hpp" #include "libtorrent/url_handler.hpp" #include "libtorrent/peer_info.hpp" +#include "libtorrent/alert.hpp" #include "libtorrent/debug.hpp" @@ -166,6 +167,9 @@ namespace libtorrent // unlimited int m_upload_rate; + // handles delayed alerts + alert_manager m_alerts; + #ifndef NDEBUG boost::shared_ptr create_log(std::string name); boost::shared_ptr m_logger; @@ -195,6 +199,8 @@ namespace libtorrent void set_http_settings(const http_settings& s); void set_upload_rate_limit(int bytes_per_second); + std::auto_ptr pop_alert(); + private: // data shared between the main thread diff --git a/src/alert.cpp b/src/alert.cpp new file mode 100755 index 000000000..0278714dd --- /dev/null +++ b/src/alert.cpp @@ -0,0 +1,84 @@ +/* + +Copyright (c) 2003, Arvid Norberg, Daniel Wallin +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/alert.hpp" + +namespace libtorrent { + + alert_manager::alert_manager() + : m_severity(alert::warning) + {} + + alert_manager::~alert_manager() + { + while (!m_alerts.empty()) + { + delete m_alerts.front(); + m_alerts.pop(); + } + } + + void alert_manager::post_alert(const alert& alert_) + { + boost::mutex::scoped_lock lock(m_mutex); + + if (m_severity <= alert_.severity()) + m_alerts.push(alert_.clone().release()); + } + + std::auto_ptr alert_manager::get() + { + boost::mutex::scoped_lock lock(m_mutex); + + assert(pending()); + + alert* result(m_alerts.front()); + m_alerts.pop(); + return std::auto_ptr(result); + } + + bool alert_manager::pending() const + { + boost::mutex::scoped_lock lock(m_mutex); + + return !m_alerts.empty(); + } + + void alert_manager::set_severity(alert::severity_t severity) + { + boost::mutex::scoped_lock lock(m_mutex); + + m_severity = severity; + } + +} // namespace libtorrent + diff --git a/src/session.cpp b/src/session.cpp index d4265e42e..79228cef6 100755 --- a/src/session.cpp +++ b/src/session.cpp @@ -663,6 +663,11 @@ namespace libtorrent } } + std::auto_ptr session::pop_alert() + { + return m_impl.m_alerts.get(); + } + // TODO: document // TODO: if the first 4 charachters are printable // maybe they should be considered a fingerprint? diff --git a/src/storage.cpp b/src/storage.cpp index fa5954ae2..f2a530e3e 100755 --- a/src/storage.cpp +++ b/src/storage.cpp @@ -1164,7 +1164,7 @@ void libtorrent::storage::initialize_pieces(torrent* t, { boost::mutex::scoped_lock lock(mutex); - data->progress += (float)current_piece / m_torrent_file->num_pieces(); + data->progress = (float)current_piece / m_torrent_file->num_pieces(); if (data->abort) return; }