summaryrefslogtreecommitdiffstats
path: root/libktorrent/net/socketmonitor.cpp
blob: 123536acfef3c4e462ecf27f2a0d82519d3987be (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
166
167
168
169
170
171
172
173
/***************************************************************************
 *   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 <unistd.h>
#include <util/functions.h>
#include <util/log.h>
#include <torrent/globals.h>
#include "socketmonitor.h"
#include "bufferedsocket.h"
#include "uploadthread.h"
#include "downloadthread.h"

using namespace bt;

namespace net
{
	SocketMonitor SocketMonitor::self;

	SocketMonitor::SocketMonitor() : ut(0),dt(0),next_group_id(1)
	{
		dt = new DownloadThread(this);
		ut = new UploadThread(this);
	}


	SocketMonitor::~SocketMonitor()
	{
		if (ut && ut->isRunning())
		{
			ut->stop();
			ut->signalDataReady(); // kick it in the nuts, if the thread is waiting for data
			if (!ut->wait(250))
			{
				ut->terminate();
				ut->wait();
			}
		}
			
		
		if (dt && dt->isRunning())
		{
			dt->stop();
			if (!dt->wait(250))
			{
				dt->terminate();
				dt->wait();
			}
		}
		
		delete ut;
		delete dt;
	}
	
	void SocketMonitor::lock()
	{
		mutex.lock();
	}
	
	void SocketMonitor::unlock()
	{
		mutex.unlock();
	}
	
	void SocketMonitor::setDownloadCap(Uint32 bytes_per_sec)
	{
		DownloadThread::setCap(bytes_per_sec);
	}
	
	void SocketMonitor::setUploadCap(Uint32 bytes_per_sec)
	{
		UploadThread::setCap(bytes_per_sec);
	}
	
	void SocketMonitor::setSleepTime(Uint32 sleep_time)
	{
		DownloadThread::setSleepTime(sleep_time);
		UploadThread::setSleepTime(sleep_time);
	}
	
	void SocketMonitor::add(BufferedSocket* sock)
	{
		TQMutexLocker lock(&mutex);
		
		bool start_threads = smap.count() == 0;
		smap.append(sock);
		
		if (start_threads)
		{
			Out(SYS_CON|LOG_DEBUG) << "Starting socketmonitor threads" << endl;
				
			if (!dt->isRunning())
				dt->start(TQThread::IdlePriority);
			if (!ut->isRunning())
				ut->start(TQThread::IdlePriority);
		}
	}
	
	void SocketMonitor::remove(BufferedSocket* sock)
	{
		TQMutexLocker lock(&mutex);
		if (smap.count() == 0)
			return;
		
		smap.remove(sock);
		if (smap.count() == 0)
		{
			Out(SYS_CON|LOG_DEBUG) << "Stopping socketmonitor threads" << endl;
			if (dt && dt->isRunning())
				dt->stop();
			if (ut && ut->isRunning())
			{
				ut->stop();
				ut->signalDataReady();
			}
		}
	}
	
	void SocketMonitor::signalPacketReady()
	{
		if (ut)
			ut->signalDataReady();
	}
	
	Uint32 SocketMonitor::newGroup(GroupType type,Uint32 limit)
	{
		lock();
		Uint32 gid = next_group_id++;
		if (type == UPLOAD_GROUP)
			ut->addGroup(gid,limit);
		else
			dt->addGroup(gid,limit);
		unlock();
		return gid;
	}
		
	void SocketMonitor::setGroupLimit(GroupType type,Uint32 gid,Uint32 limit)
	{
		lock();
		if (type == UPLOAD_GROUP)
			ut->setGroupLimit(gid,limit);
		else
			dt->setGroupLimit(gid,limit);
		unlock();
	}
		
	void SocketMonitor::removeGroup(GroupType type,Uint32 gid)
	{
		lock();
		if (type == UPLOAD_GROUP)
			ut->removeGroup(gid);
		else
			dt->removeGroup(gid);
		unlock();
	}

}