~ubuntu-branches/ubuntu/natty/kdenetwork/natty-proposed

« back to all changes in this revision

Viewing changes to kget/transfer-plugins/bittorrent/libbtcore/net/networkthread.h

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2011-01-21 16:28:50 UTC
  • mfrom: (1.1.55 upstream)
  • Revision ID: james.westby@ubuntu.com-20110121162850-5okl235t3l91cwx0
Tags: 4:4.6.0-0ubuntu1
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/***************************************************************************
2
 
 *   Copyright (C) 2005 by Joris Guisson                                   *
3
 
 *   joris.guisson@gmail.com                                               *
4
 
 *                                                                         *
5
 
 *   This program is free software; you can redistribute it and/or modify  *
6
 
 *   it under the terms of the GNU General Public License as published by  *
7
 
 *   the Free Software Foundation; either version 2 of the License, or     *
8
 
 *   (at your option) any later version.                                   *
9
 
 *                                                                         *
10
 
 *   This program 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         *
13
 
 *   GNU General Public License for more details.                          *
14
 
 *                                                                         *
15
 
 *   You should have received a copy of the GNU General Public License     *
16
 
 *   along with this program; if not, write to the                         *
17
 
 *   Free Software Foundation, Inc.,                                       *
18
 
 *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.          *
19
 
 ***************************************************************************/
20
 
#ifndef NETNETWORKTHREAD_H
21
 
#define NETNETWORKTHREAD_H
22
 
 
23
 
#include <qthread.h>
24
 
#include <util/constants.h>
25
 
#include <util/ptrmap.h>
26
 
#include <net/socketgroup.h>
27
 
                
28
 
using bt::Uint32;
29
 
 
30
 
namespace net
31
 
{
32
 
        class SocketMonitor;
33
 
        
34
 
        /**
35
 
                @author Joris Guisson <joris.guisson@gmail.com>
36
 
        
37
 
                Base class for the 2 networking threads. Handles the socket groups.
38
 
        */
39
 
        class NetworkThread : public QThread
40
 
        {
41
 
        protected:
42
 
                SocketMonitor* sm;
43
 
                bool running;
44
 
                bt::PtrMap<Uint32,SocketGroup> groups;
45
 
                bt::TimeStamp prev_run_time;
46
 
                
47
 
        public:
48
 
                NetworkThread(SocketMonitor* sm);
49
 
                virtual ~NetworkThread();
50
 
 
51
 
                
52
 
                /**
53
 
                 * Add a new group with a given limit
54
 
                 * @param gid The group ID (cannot be 0, 0 is the default group)
55
 
                 * @param limit The limit in bytes per sec
56
 
                 * @param assured_rate The assured rate for this group in bytes per second
57
 
                 */
58
 
                void addGroup(Uint32 gid,Uint32 limit,Uint32 assured_rate);
59
 
                
60
 
                /**
61
 
                 * Remove a group 
62
 
                 * @param gid The group ID
63
 
                 */
64
 
                void removeGroup(Uint32 gid);
65
 
                
66
 
                /**
67
 
                 * Set the limit for a group
68
 
                 * @param gid The group ID 
69
 
                 * @param limit The limit 
70
 
                 */
71
 
                void setGroupLimit(Uint32 gid,Uint32 limit);
72
 
                
73
 
                /**
74
 
                 * Set the assured rate for a group
75
 
                 * @param gid The group ID 
76
 
                 * @param as The assured rate
77
 
                 */
78
 
                void setGroupAssuredRate(Uint32 gid,Uint32 as);
79
 
                
80
 
                /**
81
 
                 * The main function of the thread
82
 
                 */
83
 
                void run();
84
 
                
85
 
                /**
86
 
                 * Subclasses must implement this function
87
 
                 */
88
 
                virtual void update() = 0;
89
 
                
90
 
                /**
91
 
                 * Do one SocketGroup
92
 
                 * @param g The group
93
 
                 * @param allowance The groups allowance
94
 
                 * @param now The current time
95
 
                 * @return true if the group can go again
96
 
                 */
97
 
                virtual bool doGroup(SocketGroup* g,Uint32 & allowance,bt::TimeStamp now) = 0;
98
 
                
99
 
                /// Stop before the next update
100
 
                void stop() {running = false;}
101
 
                
102
 
                /// Is the thread running
103
 
                bool isRunning() const {return running;}
104
 
                
105
 
        protected:
106
 
                /**
107
 
                 * Go over all groups and do them
108
 
                 * @param num_ready The number of ready sockets
109
 
                 * @param now The current time
110
 
                 * @param limit The global limit in bytes per sec
111
 
                 */
112
 
                void doGroups(Uint32 num_ready,bt::TimeStamp now,bt::Uint32 limit);
113
 
                
114
 
        private:
115
 
                Uint32 doGroupsLimited(Uint32 num_ready,bt::TimeStamp now,Uint32 & allowance);
116
 
        };
117
 
 
118
 
}
119
 
 
120
 
#endif