Add net-limit module for limiting network bandwidth.
[tairon.git] / src / net-limit / limitmanager.cpp
blob9ccbc67aafadd2bb912330741712ccc510b79e26
1 /***************************************************************************
2 * *
3 * Copyright (C) 2006 David Brodsky *
4 * *
5 * This library is free software; you can redistribute it and/or *
6 * modify it under the terms of the GNU Library General Public *
7 * License as published by the Free Software Foundation and appearing *
8 * in the file LICENSE.LGPL included in the packaging of this file. *
9 * *
10 * This library is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
13 * Library General Public License for more details. *
14 * *
15 ***************************************************************************/
17 #include <tairon/net/timer.h>
19 #include "limitmanager.h"
21 #include "limiter.h"
23 namespace Tairon
26 namespace Net
29 LimitManager *LimitManager::limitmanager = 0;
31 /* {{{ LimitManager::LimitManager() */
32 LimitManager::LimitManager()
34 timer = new Timer();
35 timer->timeoutSignal.connect(Tairon::Core::threadMethodDFunctor(Tairon::Core::Thread::current(), this, &LimitManager::timeout));
36 timer->start(1000); // every second
38 limitmanager = this;
40 /* }}} */
42 /* {{{ LimitManager::~LimitManager() */
43 LimitManager::~LimitManager()
45 timer->destroy();
47 /* }}} */
49 /* {{{ LimitManager::removeFromQueue(Limiter *) */
50 void LimitManager::removeFromQueue(Limiter *l)
52 toDelete.push_back(l);
54 /* }}} */
56 /* {{{ LimitManager::timeout() */
57 void LimitManager::timeout()
59 // A crash due to an invalid iterator may be caused if a limiter wants to
60 // be removed from the queue while the queue is traversed. This workaround
61 // should do the right thing.
62 for (std::list<Limiter *>::const_iterator it = toDelete.begin(); it != toDelete.end(); ++it)
63 waiting.erase(*it);
64 toDelete.clear();
66 for (std::set<Limiter *>::const_iterator it = waiting.begin(); it != waiting.end(); ++it)
67 (*it)->reset();
69 /* }}} */
71 /* {{{ LimitManager::wantMore(Limiter *) */
72 void LimitManager::wantMore(Limiter *l)
74 waiting.insert(l);
76 /* }}} */
78 }; // namespace Net
80 }; // namespace Tairon
82 // vim: ai sw=4 ts=4 noet fdm=marker