~ubuntu-branches/ubuntu/maverick/libtorrent-rasterbar/maverick

« back to all changes in this revision

Viewing changes to src/alert.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Christophe Sauthier
  • Date: 2010-08-10 12:59:37 UTC
  • mfrom: (1.3.7 upstream)
  • Revision ID: james.westby@ubuntu.com-20100810125937-jbcmmf17y8yo9hgz
Tags: 0.15.0-0ubuntu1
* New upstream version.
* debian/patches/100_fix_html_docs.patch: refreshed.
* debian/control: bump up standards-version to 3.9.1 (no changes).

Show diffs side-by-side

added added

removed removed

Lines of Context:
33
33
#include "libtorrent/pch.hpp"
34
34
 
35
35
#include "libtorrent/alert.hpp"
 
36
#include "libtorrent/alert_types.hpp"
36
37
#include <boost/thread/xtime.hpp>
 
38
#include <boost/function.hpp>
 
39
#include <boost/bind.hpp>
37
40
 
38
41
namespace libtorrent {
39
42
 
41
44
        alert::~alert() {}
42
45
        ptime alert::timestamp() const { return m_timestamp; }
43
46
 
44
 
        alert_manager::alert_manager()
 
47
        alert_manager::alert_manager(io_service& ios)
45
48
                : m_alert_mask(alert::error_notification)
46
49
                , m_queue_size_limit(queue_size_limit_default)
 
50
                , m_ios(ios)
47
51
        {}
48
52
 
49
53
        alert_manager::~alert_manager()
75
79
                xt.nsec = boost::xtime::xtime_nsec_t(nsec);
76
80
                // apparently this call can be interrupted
77
81
                // prematurely if there are other signals
78
 
                if (!m_condition.timed_wait(lock, xt)) return 0;
79
 
                if (m_alerts.empty()) return 0;
80
 
                return m_alerts.front();
 
82
                while (m_condition.timed_wait(lock, xt))
 
83
                        if (!m_alerts.empty()) return m_alerts.front();
 
84
 
 
85
                return 0;
 
86
        }
 
87
 
 
88
        void alert_manager::set_dispatch_function(boost::function<void(alert const&)> const& fun)
 
89
        {
 
90
                boost::mutex::scoped_lock lock(m_mutex);
 
91
 
 
92
                m_dispatch = fun;
 
93
 
 
94
                std::queue<alert*> alerts = m_alerts;
 
95
                while (!m_alerts.empty()) m_alerts.pop();
 
96
                lock.unlock();
 
97
 
 
98
                while (!alerts.empty())
 
99
                {
 
100
                        m_dispatch(*alerts.front());
 
101
                        delete alerts.front();
 
102
                        alerts.pop();
 
103
                }
 
104
        }
 
105
 
 
106
        void dispatch_alert(boost::function<void(alert const&)> dispatcher
 
107
                , alert* alert_)
 
108
        {
 
109
                std::auto_ptr<alert> holder(alert_);
 
110
                dispatcher(*alert_);
81
111
        }
82
112
 
83
113
        void alert_manager::post_alert(const alert& alert_)
84
114
        {
85
115
                boost::mutex::scoped_lock lock(m_mutex);
86
116
 
 
117
                if (m_dispatch)
 
118
                {
 
119
                        TORRENT_ASSERT(m_alerts.empty());
 
120
                        m_ios.post(boost::bind(&dispatch_alert, m_dispatch, alert_.clone().release()));
 
121
                        return;
 
122
                }
 
123
 
87
124
                if (m_alerts.size() >= m_queue_size_limit) return;
88
125
                m_alerts.push(alert_.clone().release());
89
126
                m_condition.notify_all();
115
152
                return queue_size_limit_;
116
153
        }
117
154
 
 
155
        stats_alert::stats_alert(torrent_handle const& h, int in
 
156
                , stat const& s)
 
157
                : torrent_alert(h)
 
158
                , interval(in)
 
159
        {
 
160
                for (int i = 0; i < num_channels; ++i)
 
161
                        transferred[i] = s[i].counter();
 
162
        }
 
163
 
 
164
        std::string stats_alert::message() const
 
165
        {
 
166
                char msg[200];
 
167
                snprintf(msg, sizeof(msg), "%s: [%d] %d %d %d %d %d %d %d %d %d %d"
 
168
                        , torrent_alert::message().c_str()
 
169
                        , interval
 
170
                        , transferred[0]
 
171
                        , transferred[1]
 
172
                        , transferred[2]
 
173
                        , transferred[3]
 
174
                        , transferred[4]
 
175
                        , transferred[5]
 
176
                        , transferred[6]
 
177
                        , transferred[7]
 
178
                        , transferred[8]
 
179
                        , transferred[9]);
 
180
                return msg;
 
181
        }
 
182
 
 
183
        cache_flushed_alert::cache_flushed_alert(torrent_handle const& h): torrent_alert(h) {}
 
184
 
118
185
} // namespace libtorrent
119
186