makes all tracker requests 'stopped' when aborting
[libtorrent.git] / include / libtorrent / alert.hpp
blobf3df277421535fd106fdd8f7773432b8cac46f8b
1 /*
3 Copyright (c) 2003, Arvid Norberg, Daniel Wallin
4 All rights reserved.
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions
8 are met:
10 * Redistributions of source code must retain the above copyright
11 notice, this list of conditions and the following disclaimer.
12 * Redistributions in binary form must reproduce the above copyright
13 notice, this list of conditions and the following disclaimer in
14 the documentation and/or other materials provided with the distribution.
15 * Neither the name of the author nor the names of its
16 contributors may be used to endorse or promote products derived
17 from this software without specific prior written permission.
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 POSSIBILITY OF SUCH DAMAGE.
33 #ifndef TORRENT_ALERT_HPP_INCLUDED
34 #define TORRENT_ALERT_HPP_INCLUDED
36 #include <memory>
37 #include <queue>
38 #include <string>
39 #include <typeinfo>
41 #ifdef _MSC_VER
42 #pragma warning(push, 1)
43 #endif
45 #include <boost/thread/mutex.hpp>
46 #include <boost/thread/condition.hpp>
48 #include <boost/preprocessor/repetition/enum_params_with_a_default.hpp>
49 #include <boost/preprocessor/repetition/enum.hpp>
50 #include <boost/preprocessor/repetition/enum_params.hpp>
51 #include <boost/preprocessor/repetition/enum_shifted_params.hpp>
53 #ifdef _MSC_VER
54 #pragma warning(pop)
55 #endif
57 #include "libtorrent/time.hpp"
58 #include "libtorrent/config.hpp"
59 #include "libtorrent/assert.hpp"
61 #ifndef TORRENT_MAX_ALERT_TYPES
62 #define TORRENT_MAX_ALERT_TYPES 15
63 #endif
65 namespace libtorrent {
67 class TORRENT_EXPORT alert
69 public:
71 // only here for backwards compatibility
72 enum severity_t { debug, info, warning, critical, fatal, none };
74 enum category_t
76 error_notification = 0x1,
77 peer_notification = 0x2,
78 port_mapping_notification = 0x4,
79 storage_notification = 0x8,
80 tracker_notification = 0x10,
81 debug_notification = 0x20,
82 status_notification = 0x40,
83 progress_notification = 0x80,
84 ip_block_notification = 0x100,
86 all_categories = 0xffffffff
89 alert();
90 virtual ~alert();
92 // a timestamp is automatically created in the constructor
93 ptime timestamp() const;
95 virtual char const* what() const = 0;
96 virtual std::string message() const = 0;
97 virtual int category() const = 0;
99 #ifndef TORRENT_NO_DEPRECATE
100 severity_t severity() const TORRENT_DEPRECATED { return warning; }
101 #endif
103 virtual std::auto_ptr<alert> clone() const = 0;
105 private:
106 ptime m_timestamp;
109 class TORRENT_EXPORT alert_manager
111 public:
112 alert_manager();
113 ~alert_manager();
115 void post_alert(const alert& alert_);
116 bool pending() const;
117 std::auto_ptr<alert> get();
119 template <class T>
120 bool should_post() const { return (m_alert_mask & T::static_category) != 0; }
122 alert const* wait_for_alert(time_duration max_wait);
124 void set_alert_mask(int m) { m_alert_mask = m; }
126 private:
127 std::queue<alert*> m_alerts;
128 mutable boost::mutex m_mutex;
129 boost::condition m_condition;
130 int m_alert_mask;
133 struct TORRENT_EXPORT unhandled_alert : std::exception
135 unhandled_alert() {}
138 namespace detail {
140 struct void_;
142 template<class Handler
143 , BOOST_PP_ENUM_PARAMS(TORRENT_MAX_ALERT_TYPES, class T)>
144 void handle_alert_dispatch(
145 const std::auto_ptr<alert>& alert_, const Handler& handler
146 , const std::type_info& typeid_
147 , BOOST_PP_ENUM_BINARY_PARAMS(TORRENT_MAX_ALERT_TYPES, T, *p))
149 if (typeid_ == typeid(T0))
150 handler(*static_cast<T0*>(alert_.get()));
151 else
152 handle_alert_dispatch(alert_, handler, typeid_
153 , BOOST_PP_ENUM_SHIFTED_PARAMS(
154 TORRENT_MAX_ALERT_TYPES, p), (void_*)0);
157 template<class Handler>
158 void handle_alert_dispatch(
159 const std::auto_ptr<alert>& alert_
160 , const Handler& handler
161 , const std::type_info& typeid_
162 , BOOST_PP_ENUM_PARAMS(TORRENT_MAX_ALERT_TYPES, void_* BOOST_PP_INTERCEPT))
164 throw unhandled_alert();
167 } // namespace detail
169 template<BOOST_PP_ENUM_PARAMS_WITH_A_DEFAULT(
170 TORRENT_MAX_ALERT_TYPES, class T, detail::void_)>
171 struct TORRENT_EXPORT handle_alert
173 template<class Handler>
174 handle_alert(const std::auto_ptr<alert>& alert_
175 , const Handler& handler)
177 #define ALERT_POINTER_TYPE(z, n, text) (BOOST_PP_CAT(T, n)*)0
179 detail::handle_alert_dispatch(alert_, handler, typeid(*alert_)
180 , BOOST_PP_ENUM(TORRENT_MAX_ALERT_TYPES, ALERT_POINTER_TYPE, _));
182 #undef ALERT_POINTER_TYPE
186 } // namespace libtorrent
188 #endif // TORRENT_ALERT_HPP_INCLUDED