summaryrefslogtreecommitdiffstats
path: root/libktorrent/net/networkthread.cpp
blob: 0a48af99b998483522db8ccf4a45b773df29cf26 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/***************************************************************************
 *   Copyright (C) 2005 by Joris Guisson                                   *
 *   joris.guisson@gmail.com                                               *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.          *
 ***************************************************************************/
#include <math.h>
#include <util/functions.h>
#include <util/log.h>
#include "socketgroup.h"
#include "socketmonitor.h"
#include "networkthread.h"
		
using namespace bt;

namespace net
{

	NetworkThread::NetworkThread(SocketMonitor* sm)
		: sm(sm),running(false)
	{
		groups.setAutoDelete(true);
		groups.insert(0,new SocketGroup(0));
	}


	NetworkThread::~NetworkThread()
	{}

	void NetworkThread::run()
	{
		running = true;
		prev_run_time = bt::Now();
		while (running)
			update();
	}

	void NetworkThread::addGroup(Uint32 gid,Uint32 limit)
	{
		// if group already exists, just change the limit
		SocketGroup* g = groups.tqfind(gid);
		if (g)
		{
			g->setLimit(limit);
		}
		else
		{
			g = new SocketGroup(limit);
			groups.insert(gid,g);
		}
	}
		
	void NetworkThread::removeGroup(Uint32 gid)
	{
		// make sure the 0 group is never erased
		if (gid != 0)
			groups.erase(gid);
	}
		
	void NetworkThread::setGroupLimit(Uint32 gid,Uint32 limit)
	{
		SocketGroup* g = groups.tqfind(gid);
		if (g)
		{
			g->setLimit(limit);
		}
	}
	
	Uint32 NetworkThread::doGroupsLimited(Uint32 num_ready,bt::TimeStamp now,Uint32 & allowance)
	{
		Uint32 num_still_ready = 0;
		
		// this is one pass over all the groups
		bt::PtrMap<Uint32,SocketGroup>::iterator itr = groups.begin();
		while (itr != groups.end() && allowance > 0)
		{
			SocketGroup* g = itr->second;
			if (g->numSockets() > 0)
			{
				Uint32 group_allowance = (Uint32)ceil(((double)g->numSockets() / num_ready) * allowance);
				
				// lets not do to much and make sure we don't pass 0 to the socket group (0 is unlimited)
				if (group_allowance > allowance || group_allowance == 0)
					group_allowance = allowance;  
				
				Uint32 ga = group_allowance;
				
				if (!doGroup(g,ga,now))
					g->clear(); // group is done, so clear it
				else
					num_still_ready += g->numSockets(); // keep track of the number of sockets which are still ready
					
				Uint32 done = group_allowance - ga;
				if (allowance >= done)
					allowance -= done;
				else
					allowance = 0;
			}
			itr++;
		}
		
		return num_still_ready > 0;
	}
	
	void NetworkThread::doGroups(Uint32 num_ready,bt::TimeStamp now,bt::Uint32 limit)
	{
		if (limit == 0)
		{
			Uint32 allowance = 0;
			bt::PtrMap<Uint32,SocketGroup>::iterator itr = groups.begin();
			while (itr != groups.end())
			{
				SocketGroup* g = itr->second;
				if (g->numSockets() > 0)
				{
					g->calcAllowance(now);
					doGroup(g,allowance,now);
					g->clear();
				}
				itr++;
			}
		}
		else
		{
			// calculate group allowance for each group
			bt::PtrMap<Uint32,SocketGroup>::iterator itr = groups.begin();
			while (itr != groups.end())
			{
				SocketGroup* g = itr->second;
				g->calcAllowance(now);
				itr++;
			}
			
			Uint32 allowance = (Uint32)ceil(1.02 * limit * (now - prev_run_time) * 0.001);
			
			while (allowance > 0 && num_ready > 0)
			{
				// loop until nobody is ready anymore or the allowance is up
				num_ready = doGroupsLimited(num_ready,now,allowance);
			}
			
			// make sure all groups are cleared
			itr = groups.begin();
			while (itr != groups.end())
			{
				SocketGroup* g = itr->second;
				g->clear();
				itr++;
			}
		}
	}
}