2003-11-29 17:34:07 +01:00
|
|
|
/*
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
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>
|
|
|
|
#include <queue>
|
|
|
|
#include <string>
|
|
|
|
#include <typeinfo>
|
|
|
|
|
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/thread/mutex.hpp>
|
2007-11-25 09:18:57 +01:00
|
|
|
#include <boost/thread/condition.hpp>
|
2009-01-17 20:45:14 +01:00
|
|
|
#include <boost/function.hpp>
|
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>
|
|
|
|
|
2004-01-25 19:18:36 +01:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
#pragma warning(pop)
|
|
|
|
#endif
|
|
|
|
|
2007-04-05 00:27:36 +02:00
|
|
|
#include "libtorrent/time.hpp"
|
2005-11-01 19:30:39 +01:00
|
|
|
#include "libtorrent/config.hpp"
|
2007-09-01 06:08:39 +02:00
|
|
|
#include "libtorrent/assert.hpp"
|
2009-05-23 23:36:09 +02:00
|
|
|
#include "libtorrent/socket.hpp" // for io_service
|
2004-01-25 19:18:36 +01:00
|
|
|
|
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 {
|
|
|
|
|
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
|
|
|
|
|
|
|
// only here for backwards compatibility
|
2004-01-07 01:48:02 +01:00
|
|
|
enum severity_t { debug, info, warning, critical, fatal, none };
|
2003-11-29 17:34:07 +01:00
|
|
|
|
2008-07-06 14:22:56 +02:00
|
|
|
enum category_t
|
|
|
|
{
|
|
|
|
error_notification = 0x1,
|
|
|
|
peer_notification = 0x2,
|
|
|
|
port_mapping_notification = 0x4,
|
|
|
|
storage_notification = 0x8,
|
|
|
|
tracker_notification = 0x10,
|
|
|
|
debug_notification = 0x20,
|
|
|
|
status_notification = 0x40,
|
|
|
|
progress_notification = 0x80,
|
|
|
|
ip_block_notification = 0x100,
|
2008-08-19 17:04:14 +02:00
|
|
|
performance_warning = 0x200,
|
2008-09-20 19:42:25 +02:00
|
|
|
dht_notification = 0x400,
|
2008-07-06 14:22:56 +02:00
|
|
|
|
|
|
|
all_categories = 0xffffffff
|
|
|
|
};
|
|
|
|
|
|
|
|
alert();
|
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
|
|
|
|
2008-07-06 14:22:56 +02:00
|
|
|
virtual char const* what() const = 0;
|
|
|
|
virtual std::string message() const = 0;
|
|
|
|
virtual int category() const = 0;
|
2003-11-29 17:34:07 +01:00
|
|
|
|
2008-08-03 17:14:08 +02:00
|
|
|
#ifndef TORRENT_NO_DEPRECATE
|
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
|
|
|
|
|
|
|
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
|
|
|
};
|
|
|
|
|
2005-11-01 19:30:39 +01:00
|
|
|
class TORRENT_EXPORT alert_manager
|
2003-11-29 17:34:07 +01:00
|
|
|
{
|
|
|
|
public:
|
2008-10-07 07:46:42 +02:00
|
|
|
enum { queue_size_limit_default = 1000 };
|
|
|
|
|
2009-05-23 23:36:09 +02:00
|
|
|
alert_manager(io_service& ios);
|
2003-11-29 17:34:07 +01:00
|
|
|
~alert_manager();
|
|
|
|
|
|
|
|
void post_alert(const alert& alert_);
|
|
|
|
bool pending() const;
|
|
|
|
std::auto_ptr<alert> get();
|
|
|
|
|
2008-07-06 14:22:56 +02:00
|
|
|
template <class T>
|
2009-02-21 11:21:28 +01:00
|
|
|
bool should_post() const
|
|
|
|
{
|
|
|
|
boost::mutex::scoped_lock lock(m_mutex);
|
|
|
|
if (m_alerts.size() >= m_queue_size_limit) return false;
|
|
|
|
return (m_alert_mask & T::static_category) != 0;
|
|
|
|
}
|
2003-11-29 17:34:07 +01:00
|
|
|
|
2007-11-25 09:18:57 +01:00
|
|
|
alert const* wait_for_alert(time_duration max_wait);
|
|
|
|
|
2009-02-21 11:21:28 +01:00
|
|
|
void set_alert_mask(int m)
|
|
|
|
{
|
|
|
|
boost::mutex::scoped_lock lock(m_mutex);
|
|
|
|
m_alert_mask = m;
|
|
|
|
}
|
2008-07-06 14:22:56 +02:00
|
|
|
|
2008-10-07 07:46:42 +02:00
|
|
|
size_t alert_queue_size_limit() const { return m_queue_size_limit; }
|
|
|
|
size_t set_alert_queue_size_limit(size_t queue_size_limit_);
|
|
|
|
|
2008-12-15 08:12:08 +01:00
|
|
|
void set_dispatch_function(boost::function<void(alert const&)> const&);
|
|
|
|
|
2003-11-29 17:34:07 +01:00
|
|
|
private:
|
|
|
|
std::queue<alert*> m_alerts;
|
|
|
|
mutable boost::mutex m_mutex;
|
2007-11-25 09:18:57 +01:00
|
|
|
boost::condition m_condition;
|
2008-07-06 14:22:56 +02:00
|
|
|
int m_alert_mask;
|
2008-10-07 07:46:42 +02:00
|
|
|
size_t m_queue_size_limit;
|
2008-12-15 08:12:08 +01:00
|
|
|
boost::function<void(alert const&)> m_dispatch;
|
2009-05-23 23:36:09 +02:00
|
|
|
io_service& m_ios;
|
2003-11-29 17:34:07 +01:00
|
|
|
};
|
|
|
|
|
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() {}
|
|
|
|
};
|
|
|
|
|
|
|
|
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_
|
|
|
|
, BOOST_PP_ENUM_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(
|
2007-04-17 07:56:43 +02:00
|
|
|
const std::auto_ptr<alert>& alert_
|
|
|
|
, const Handler& handler
|
|
|
|
, const std::type_info& typeid_
|
|
|
|
, 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
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace libtorrent
|
|
|
|
|
2004-01-25 19:18:36 +01:00
|
|
|
#endif // TORRENT_ALERT_HPP_INCLUDED
|
2003-11-29 17:34:07 +01:00
|
|
|
|