2003-11-29 17:34:07 +01:00
|
|
|
/*
|
|
|
|
|
2013-04-09 04:38:11 +02:00
|
|
|
Copyright (c) 2003-2013, Arvid Norberg, Daniel Wallin
|
2003-11-29 17:34:07 +01:00
|
|
|
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.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2004-01-25 19:18:36 +01:00
|
|
|
#ifndef TORRENT_ALERT_HPP_INCLUDED
|
|
|
|
#define TORRENT_ALERT_HPP_INCLUDED
|
2003-11-29 17:34:07 +01:00
|
|
|
|
|
|
|
#include <memory>
|
2010-07-14 06:16:38 +02:00
|
|
|
#include <deque>
|
2003-11-29 17:34:07 +01:00
|
|
|
#include <string>
|
|
|
|
|
2004-01-25 19:18:36 +01:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
#pragma warning(push, 1)
|
|
|
|
#endif
|
|
|
|
|
2003-11-29 17:34:07 +01:00
|
|
|
#include <boost/preprocessor/repetition/enum_params_with_a_default.hpp>
|
|
|
|
#include <boost/preprocessor/repetition/enum.hpp>
|
|
|
|
#include <boost/preprocessor/repetition/enum_params.hpp>
|
|
|
|
#include <boost/preprocessor/repetition/enum_shifted_params.hpp>
|
2010-03-20 23:22:18 +01:00
|
|
|
#include <boost/preprocessor/repetition/enum_shifted_binary_params.hpp>
|
2003-11-29 17:34:07 +01:00
|
|
|
|
2013-08-08 03:03:54 +02:00
|
|
|
// OVERVIEW
|
|
|
|
//
|
|
|
|
// The pop_alert() function on session is the interface for retrieving
|
|
|
|
// alerts, warnings, messages and errors from libtorrent. If no alerts have
|
|
|
|
// been posted by libtorrent pop_alert() will return a default initialized
|
|
|
|
// ``std::auto_ptr`` object. If there is an alert in libtorrent's queue, the alert
|
|
|
|
// from the front of the queue is popped and returned.
|
|
|
|
// You can then use the alert object and query
|
|
|
|
//
|
|
|
|
// By default, only errors are reported. set_alert_mask() can be
|
|
|
|
// used to specify which kinds of events should be reported. The alert mask
|
2013-08-08 04:57:07 +02:00
|
|
|
// is comprised by bits from the category_t enum.
|
2013-08-08 03:03:54 +02:00
|
|
|
//
|
|
|
|
// Every alert belongs to one or more category. There is a small cost involved in posting alerts. Only
|
|
|
|
// alerts that belong to an enabled category are posted. Setting the alert bitmask to 0 will disable
|
2013-08-08 04:57:07 +02:00
|
|
|
// all alerts (except those that are non-discardable).
|
2013-08-08 03:03:54 +02:00
|
|
|
//
|
2013-08-08 04:57:07 +02:00
|
|
|
// There are other alert base classes that some alerts derive from, all the
|
|
|
|
// alerts that are generated for a specific torrent are derived from torrent_alert,
|
|
|
|
// and tracker events derive from tracker_alert.
|
2013-08-08 03:03:54 +02:00
|
|
|
//
|
|
|
|
|
2004-01-25 19:18:36 +01:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
#pragma warning(pop)
|
|
|
|
#endif
|
|
|
|
|
2009-11-25 07:55:34 +01:00
|
|
|
#include "libtorrent/ptime.hpp"
|
2005-11-01 19:30:39 +01:00
|
|
|
#include "libtorrent/config.hpp"
|
2004-01-25 19:18:36 +01:00
|
|
|
|
2013-04-13 23:03:14 +02:00
|
|
|
#ifndef BOOST_NO_TYPEID
|
|
|
|
#include <typeinfo>
|
|
|
|
#endif
|
|
|
|
|
2007-04-17 07:56:43 +02:00
|
|
|
#ifndef TORRENT_MAX_ALERT_TYPES
|
|
|
|
#define TORRENT_MAX_ALERT_TYPES 15
|
|
|
|
#endif
|
2006-04-25 23:04:48 +02:00
|
|
|
|
2003-11-29 17:34:07 +01:00
|
|
|
namespace libtorrent {
|
|
|
|
|
2013-08-08 03:03:54 +02:00
|
|
|
// The ``alert`` class is the base class that specific messages are derived from.
|
2005-11-01 19:30:39 +01:00
|
|
|
class TORRENT_EXPORT alert
|
2003-11-29 17:34:07 +01:00
|
|
|
{
|
|
|
|
public:
|
2008-07-06 14:22:56 +02:00
|
|
|
|
2009-10-30 04:21:25 +01:00
|
|
|
#ifndef TORRENT_NO_DEPRECATE
|
2008-07-06 14:22:56 +02:00
|
|
|
// only here for backwards compatibility
|
2004-01-07 01:48:02 +01:00
|
|
|
enum severity_t { debug, info, warning, critical, fatal, none };
|
2009-10-30 04:21:25 +01:00
|
|
|
#endif
|
2003-11-29 17:34:07 +01:00
|
|
|
|
2013-08-16 07:07:09 +02:00
|
|
|
// these are bits for the alert_mask used by the session. See set_alert_mask().
|
2008-07-06 14:22:56 +02:00
|
|
|
enum category_t
|
|
|
|
{
|
2013-08-08 04:57:07 +02:00
|
|
|
// Enables alerts that report an error. This includes:
|
|
|
|
//
|
|
|
|
// * tracker errors
|
|
|
|
// * tracker warnings
|
|
|
|
// * file errors
|
|
|
|
// * resume data failures
|
|
|
|
// * web seed errors
|
|
|
|
// * .torrent files errors
|
|
|
|
// * listen socket errors
|
|
|
|
// * port mapping errors
|
2008-07-06 14:22:56 +02:00
|
|
|
error_notification = 0x1,
|
2013-08-08 04:57:07 +02:00
|
|
|
|
|
|
|
// Enables alerts when peers send invalid requests, get banned or
|
|
|
|
// snubbed.
|
2008-07-06 14:22:56 +02:00
|
|
|
peer_notification = 0x2,
|
2013-08-08 04:57:07 +02:00
|
|
|
|
|
|
|
// Enables alerts for port mapping events. For NAT-PMP and UPnP.
|
2008-07-06 14:22:56 +02:00
|
|
|
port_mapping_notification = 0x4,
|
2013-08-08 04:57:07 +02:00
|
|
|
|
|
|
|
// Enables alerts for events related to the storage. File errors and
|
|
|
|
// synchronization events for moving the storage, renaming files etc.
|
2008-07-06 14:22:56 +02:00
|
|
|
storage_notification = 0x8,
|
2013-08-08 04:57:07 +02:00
|
|
|
|
|
|
|
// Enables all tracker events. Includes announcing to trackers,
|
|
|
|
// receiving responses, warnings and errors.
|
2008-07-06 14:22:56 +02:00
|
|
|
tracker_notification = 0x10,
|
2013-08-08 04:57:07 +02:00
|
|
|
|
|
|
|
// Low level alerts for when peers are connected and disconnected.
|
2008-07-06 14:22:56 +02:00
|
|
|
debug_notification = 0x20,
|
2013-08-08 04:57:07 +02:00
|
|
|
|
|
|
|
// Enables alerts for when a torrent or the session changes state.
|
2008-07-06 14:22:56 +02:00
|
|
|
status_notification = 0x40,
|
2013-08-08 04:57:07 +02:00
|
|
|
|
|
|
|
// Alerts for when blocks are requested and completed. Also when
|
|
|
|
// pieces are completed.
|
2008-07-06 14:22:56 +02:00
|
|
|
progress_notification = 0x80,
|
2013-08-08 04:57:07 +02:00
|
|
|
|
|
|
|
// Alerts when a peer is blocked by the ip blocker or port blocker.
|
2008-07-06 14:22:56 +02:00
|
|
|
ip_block_notification = 0x100,
|
2013-08-08 04:57:07 +02:00
|
|
|
|
|
|
|
// Alerts when some limit is reached that might limit the download
|
|
|
|
// or upload rate.
|
2008-08-19 17:04:14 +02:00
|
|
|
performance_warning = 0x200,
|
2013-08-08 04:57:07 +02:00
|
|
|
|
|
|
|
// Alerts on events in the DHT node. For incoming searches or
|
|
|
|
// bootstrapping being done etc.
|
2008-09-20 19:42:25 +02:00
|
|
|
dht_notification = 0x400,
|
2013-08-08 04:57:07 +02:00
|
|
|
|
|
|
|
// If you enable these alerts, you will receive a stats_alert
|
|
|
|
// approximately once every second, for every active torrent.
|
|
|
|
// These alerts contain all statistics counters for the interval since
|
|
|
|
// the lasts stats alert.
|
2010-01-02 15:16:35 +01:00
|
|
|
stats_notification = 0x800,
|
2013-08-08 04:57:07 +02:00
|
|
|
|
|
|
|
// Alerts on RSS related events, like feeds being updated, feed error
|
|
|
|
// conditions and successful RSS feed updates. Enabling this categoty
|
|
|
|
// will make you receive rss_alert alerts.
|
2011-01-18 04:41:54 +01:00
|
|
|
rss_notification = 0x1000,
|
2008-07-06 14:22:56 +02:00
|
|
|
|
2013-08-08 04:57:07 +02:00
|
|
|
// The full bitmask, representing all available categories.
|
|
|
|
//
|
2013-02-03 01:07:42 +01:00
|
|
|
// since the enum is signed, make sure this isn't
|
|
|
|
// interpreted as -1. For instance, boost.python
|
|
|
|
// does that and fails when assigning it to an
|
|
|
|
// unsigned parameter.
|
|
|
|
all_categories = 0x7fffffff
|
2008-07-06 14:22:56 +02:00
|
|
|
};
|
|
|
|
|
2013-09-01 19:34:05 +02:00
|
|
|
// hidden
|
2008-07-06 14:22:56 +02:00
|
|
|
alert();
|
2013-09-01 19:34:05 +02:00
|
|
|
// hidden
|
2004-01-23 01:39:05 +01:00
|
|
|
virtual ~alert();
|
2003-11-29 17:34:07 +01:00
|
|
|
|
2004-01-23 01:39:05 +01:00
|
|
|
// a timestamp is automatically created in the constructor
|
2007-04-05 00:27:36 +02:00
|
|
|
ptime timestamp() const;
|
2003-11-29 17:34:07 +01:00
|
|
|
|
2013-08-08 03:03:54 +02:00
|
|
|
// returns an integer that is unique to this alert type. It can be
|
|
|
|
// compared against a specific alert by querying a static constant called ``alert_type``
|
|
|
|
// in the alert. It can be used to determine the run-time type of an alert* in
|
|
|
|
// order to cast to that alert type and access specific members.
|
|
|
|
//
|
|
|
|
// e.g::
|
|
|
|
//
|
|
|
|
// std::auto_ptr<alert> a = ses.pop_alert();
|
|
|
|
// switch (a->type())
|
|
|
|
// {
|
|
|
|
// case read_piece_alert::alert_type:
|
|
|
|
// {
|
|
|
|
// read_piece_alert* p = (read_piece_alert*)a.get();
|
|
|
|
// if (p->ec) {
|
|
|
|
// // read_piece failed
|
|
|
|
// break;
|
|
|
|
// }
|
|
|
|
// // use p
|
|
|
|
// break;
|
|
|
|
// }
|
|
|
|
// case file_renamed_alert::alert_type:
|
|
|
|
// {
|
|
|
|
// // etc...
|
|
|
|
// }
|
|
|
|
// }
|
2009-10-30 04:21:25 +01:00
|
|
|
virtual int type() const = 0;
|
2013-08-08 03:03:54 +02:00
|
|
|
|
|
|
|
// returns a string literal describing the type of the alert. It does
|
|
|
|
// not include any information that might be bundled with the alert.
|
2008-07-06 14:22:56 +02:00
|
|
|
virtual char const* what() const = 0;
|
2013-08-08 03:03:54 +02:00
|
|
|
|
|
|
|
// generate a string describing the alert and the information bundled
|
|
|
|
// with it. This is mainly intended for debug and development use. It is not suitable
|
|
|
|
// to use this for applications that may be localized. Instead, handle each alert
|
|
|
|
// type individually and extract and render the information from the alert depending
|
|
|
|
// on the locale.
|
2008-07-06 14:22:56 +02:00
|
|
|
virtual std::string message() const = 0;
|
2013-08-08 03:03:54 +02:00
|
|
|
|
|
|
|
// returns a bitmask specifying which categories this alert belong to.
|
2008-07-06 14:22:56 +02:00
|
|
|
virtual int category() const = 0;
|
2013-08-08 03:03:54 +02:00
|
|
|
|
|
|
|
// determines whether or not an alert is allowed to be discarded
|
|
|
|
// when the alert queue is full. There are a few alerts which may not be discared,
|
|
|
|
// since they would break the user contract, such as save_resume_data_alert.
|
2011-01-23 19:00:52 +01:00
|
|
|
virtual bool discardable() const { return true; }
|
2003-11-29 17:34:07 +01:00
|
|
|
|
2008-08-03 17:14:08 +02:00
|
|
|
#ifndef TORRENT_NO_DEPRECATE
|
2009-07-26 04:37:31 +02:00
|
|
|
TORRENT_DEPRECATED_PREFIX
|
2008-07-06 14:22:56 +02:00
|
|
|
severity_t severity() const TORRENT_DEPRECATED { return warning; }
|
2008-08-03 17:14:08 +02:00
|
|
|
#endif
|
2003-11-29 17:34:07 +01:00
|
|
|
|
2013-08-08 03:03:54 +02:00
|
|
|
// returns a pointer to a copy of the alert.
|
2003-11-29 17:34:07 +01:00
|
|
|
virtual std::auto_ptr<alert> clone() const = 0;
|
|
|
|
|
|
|
|
private:
|
2007-04-05 00:27:36 +02:00
|
|
|
ptime m_timestamp;
|
2003-11-29 17:34:07 +01:00
|
|
|
};
|
|
|
|
|
2013-09-01 19:34:05 +02:00
|
|
|
#ifndef TORRENT_NO_DEPRECATE
|
2005-11-01 19:30:39 +01:00
|
|
|
struct TORRENT_EXPORT unhandled_alert : std::exception
|
2003-11-29 17:34:07 +01:00
|
|
|
{
|
|
|
|
unhandled_alert() {}
|
|
|
|
};
|
|
|
|
|
2009-10-30 04:21:25 +01:00
|
|
|
#ifndef BOOST_NO_TYPEID
|
|
|
|
|
2003-11-29 17:34:07 +01:00
|
|
|
namespace detail {
|
|
|
|
|
|
|
|
struct void_;
|
|
|
|
|
2007-04-17 07:56:43 +02:00
|
|
|
template<class Handler
|
|
|
|
, BOOST_PP_ENUM_PARAMS(TORRENT_MAX_ALERT_TYPES, class T)>
|
2003-11-29 17:34:07 +01:00
|
|
|
void handle_alert_dispatch(
|
2007-04-17 07:56:43 +02:00
|
|
|
const std::auto_ptr<alert>& alert_, const Handler& handler
|
|
|
|
, const std::type_info& typeid_
|
2010-03-20 23:22:18 +01:00
|
|
|
, T0*, BOOST_PP_ENUM_SHIFTED_BINARY_PARAMS(TORRENT_MAX_ALERT_TYPES, T, *p))
|
2003-11-29 17:34:07 +01:00
|
|
|
{
|
|
|
|
if (typeid_ == typeid(T0))
|
|
|
|
handler(*static_cast<T0*>(alert_.get()));
|
|
|
|
else
|
2007-04-17 07:56:43 +02:00
|
|
|
handle_alert_dispatch(alert_, handler, typeid_
|
|
|
|
, BOOST_PP_ENUM_SHIFTED_PARAMS(
|
|
|
|
TORRENT_MAX_ALERT_TYPES, p), (void_*)0);
|
2003-11-29 17:34:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
template<class Handler>
|
|
|
|
void handle_alert_dispatch(
|
2010-03-20 23:22:18 +01:00
|
|
|
const std::auto_ptr<alert>&
|
|
|
|
, const Handler&
|
|
|
|
, const std::type_info&
|
2007-04-17 07:56:43 +02:00
|
|
|
, BOOST_PP_ENUM_PARAMS(TORRENT_MAX_ALERT_TYPES, void_* BOOST_PP_INTERCEPT))
|
2003-11-29 17:34:07 +01:00
|
|
|
{
|
|
|
|
throw unhandled_alert();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace detail
|
|
|
|
|
2007-04-17 07:56:43 +02:00
|
|
|
template<BOOST_PP_ENUM_PARAMS_WITH_A_DEFAULT(
|
|
|
|
TORRENT_MAX_ALERT_TYPES, class T, detail::void_)>
|
2005-11-01 19:30:39 +01:00
|
|
|
struct TORRENT_EXPORT handle_alert
|
2003-11-29 17:34:07 +01:00
|
|
|
{
|
|
|
|
template<class Handler>
|
2007-04-17 07:56:43 +02:00
|
|
|
handle_alert(const std::auto_ptr<alert>& alert_
|
|
|
|
, const Handler& handler)
|
2003-11-29 17:34:07 +01:00
|
|
|
{
|
|
|
|
#define ALERT_POINTER_TYPE(z, n, text) (BOOST_PP_CAT(T, n)*)0
|
|
|
|
|
2007-04-17 07:56:43 +02:00
|
|
|
detail::handle_alert_dispatch(alert_, handler, typeid(*alert_)
|
|
|
|
, BOOST_PP_ENUM(TORRENT_MAX_ALERT_TYPES, ALERT_POINTER_TYPE, _));
|
2003-11-29 17:34:07 +01:00
|
|
|
|
|
|
|
#undef ALERT_POINTER_TYPE
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2009-10-30 04:21:25 +01:00
|
|
|
#endif // BOOST_NO_TYPEID
|
2013-08-08 04:57:07 +02:00
|
|
|
#endif // TORRENT_NO_DEPRECATE
|
2009-10-30 04:21:25 +01:00
|
|
|
|
2013-08-08 03:03:54 +02:00
|
|
|
// When you get an alert, you can use ``alert_cast<>`` to attempt to cast the pointer to a
|
|
|
|
// more specific alert type, in order to query it for more information.
|
2009-11-02 04:34:16 +01:00
|
|
|
template <class T>
|
|
|
|
T* alert_cast(alert* a)
|
|
|
|
{
|
2011-01-24 19:20:41 +01:00
|
|
|
if (a == 0) return 0;
|
2009-11-02 04:34:16 +01:00
|
|
|
if (a->type() == T::alert_type) return static_cast<T*>(a);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
template <class T>
|
|
|
|
T const* alert_cast(alert const* a)
|
|
|
|
{
|
2011-01-24 19:20:41 +01:00
|
|
|
if (a == 0) return 0;
|
2009-11-02 04:34:16 +01:00
|
|
|
if (a->type() == T::alert_type) return static_cast<T const*>(a);
|
|
|
|
return 0;
|
|
|
|
}
|
2009-10-30 04:21:25 +01:00
|
|
|
|
2003-11-29 17:34:07 +01:00
|
|
|
} // namespace libtorrent
|
|
|
|
|
2004-01-25 19:18:36 +01:00
|
|
|
#endif // TORRENT_ALERT_HPP_INCLUDED
|
2003-11-29 17:34:07 +01:00
|
|
|
|