Browse Source

- Totally rewritten Web UI list refresh system (fixed memory leak)

adaptive-webui-19844
Christophe Dumez 16 years ago
parent
commit
0879f2c0ca
  1. 1
      Changelog
  2. 4
      src/bittorrent.cpp
  3. 4
      src/bittorrent.h
  4. 227
      src/eventmanager.cpp
  5. 15
      src/eventmanager.h
  6. 11
      src/httpconnection.cpp
  7. 10
      src/httpserver.cpp
  8. 250
      src/webui/scripts/client.js

1
Changelog

@ -6,6 +6,7 @@ @@ -6,6 +6,7 @@
- FEATURE: Can have different proxies for Bittorrent and search engine
- FEATURE: Allow multiple item selection in Web UI transfer list
- FEATURE: Moved uploads to a separate list in Web UI
- BUGFIX: Totally rewritten Web UI list refresh system (fixed memory leak)
- BUGFIX: Disable ETA calculation when ETA column is hidden
- BUGFIX: Removed "disconnected" connection state, detection was far from perfect
- COSMETIC: Transfer speed, ratio, connection status and DHT nodes are displayed in status bar

4
src/bittorrent.cpp

@ -721,7 +721,7 @@ void bittorrent::setUnfinishedTorrent(QString hash) { @@ -721,7 +721,7 @@ void bittorrent::setUnfinishedTorrent(QString hash) {
updateDownloadQueue();
}
}
emit torrentSwitchedtoUnfinished(hash);
//emit torrentSwitchedtoUnfinished(hash);
}
// Add the given hash to the list of finished torrents
@ -759,7 +759,7 @@ void bittorrent::setFinishedTorrent(QString hash) { @@ -759,7 +759,7 @@ void bittorrent::setFinishedTorrent(QString hash) {
}
// Save fast resume data
saveFastResumeAndRatioData(hash);
emit torrentSwitchedtoFinished(hash);
//emit torrentSwitchedtoFinished(hash);
}
// Pause a running torrent

4
src/bittorrent.h

@ -224,8 +224,8 @@ class bittorrent : public QObject { @@ -224,8 +224,8 @@ class bittorrent : public QObject {
void updateUnfinishedTorrentNumber();
void forceUnfinishedListUpdate();
void forceFinishedListUpdate();
void torrentSwitchedtoFinished(QString hash);
void torrentSwitchedtoUnfinished(QString hash);
/*void torrentSwitchedtoFinished(QString hash);
void torrentSwitchedtoUnfinished(QString hash);*/
};
#endif

227
src/eventmanager.cpp

@ -27,49 +27,20 @@ @@ -27,49 +27,20 @@
EventManager::EventManager(QObject *parent, bittorrent *BTSession)
: QObject(parent), BTSession(BTSession)
{
revision = 0;
}
void EventManager::update(QVariantMap event)
{
++revision;
events << QPair<ulong, QVariantMap>(revision, event);
emit updated();
//qDebug("Added the following event");
//qDebug() << event;
/* QLinkedList<QPair<ulong, QVariantMap> >::iterator i;
for (i = events.begin(); i != events.end(); i++)
qDebug() << *i;*/
}
QVariant EventManager::querySince(ulong r) const
{
QVariant EventManager::getEventList() const {
QVariantList list;
QLinkedListIterator<QPair<ulong, QVariantMap> > i(events);
i.toBack();
while (i.hasPrevious())
{
QPair<ulong, QVariantMap> pair = i.previous();
if (pair.first <= r)
break;
list.prepend(QVariant(pair.second));
foreach(QVariantMap event, event_list.values()) {
list << QVariant(event);
}
QVariantMap map;
map["events"] = QVariant(list);
map["revision"] = QVariant((qulonglong) revision);
return QVariant(map);
}
bool EventManager::isUpdated(ulong r) const
{
return (r < revision);
return QVariant(list);
}
void EventManager::addedTorrent(QTorrentHandle& h)
{
QVariantMap event;
QString hash = h.hash();
event["type"] = QVariant("add");
event["hash"] = QVariant(hash);
event["name"] = QVariant(h.name());
event["seed"] = QVariant(h.is_seed());
@ -113,62 +84,19 @@ void EventManager::addedTorrent(QTorrentHandle& h) @@ -113,62 +84,19 @@ void EventManager::addedTorrent(QTorrentHandle& h)
event["state"] = QVariant();
}
}
update(event);
event_list[hash] = event;
}
void EventManager::torrentSwitchedtoUnfinished(QString hash) {
QVariantMap event;
QTorrentHandle h = BTSession->getTorrentHandle(hash);
event["type"] = QVariant("unfinish");
event["hash"] = QVariant(h.hash());
event["name"] = QVariant(h.name());
if(h.is_paused()) {
if(BTSession->isQueueingEnabled() && (BTSession->isDownloadQueued(hash) || BTSession->isUploadQueued(hash)))
event["state"] = QVariant("queued");
else
event["state"] = QVariant("paused");
} else {
switch(h.state())
{
case torrent_status::finished:
case torrent_status::seeding:
event["state"] = QVariant("seeding");
break;
case torrent_status::checking_files:
case torrent_status::queued_for_checking:
event["state"] = QVariant("checking");
break;
case torrent_status::connecting_to_tracker:
if(h.download_payload_rate() > 0)
event["state"] = QVariant("downloading");
else
event["state"] = QVariant("connecting");
break;
case torrent_status::downloading:
case torrent_status::downloading_metadata:
if(h.download_payload_rate() > 0)
event["state"] = QVariant("downloading");
else
event["state"] = QVariant("stalled");
break;
default:
qDebug("No status, should not happen!!! status is %d", h.state());
event["state"] = QVariant();
}
}
event["size"] = QVariant((qlonglong)h.actual_size());
event["progress"] = QVariant(h.progress());
event["dlspeed"] = QVariant(h.download_payload_rate());
event["upspeed"] = QVariant(h.upload_payload_rate());
update(event);
void EventManager::deletedTorrent(QString hash)
{
event_list.remove(hash);
}
void EventManager::torrentSwitchedtoFinished(QString hash) {
void EventManager::modifiedTorrent(QTorrentHandle h)
{
QString hash = h.hash();
QVariantMap event;
QTorrentHandle h = BTSession->getTorrentHandle(hash);
event["type"] = QVariant("finish");
event["hash"] = QVariant(h.hash());
event["name"] = QVariant(h.name());
if(h.is_paused()) {
if(BTSession->isQueueingEnabled() && (BTSession->isDownloadQueued(hash) || BTSession->isUploadQueued(hash)))
event["state"] = QVariant("queued");
@ -199,133 +127,18 @@ void EventManager::torrentSwitchedtoFinished(QString hash) { @@ -199,133 +127,18 @@ void EventManager::torrentSwitchedtoFinished(QString hash) {
event["state"] = QVariant("stalled");
break;
default:
qDebug("No status, should not happen!!! status is %d", h.state());
qDebug("No status, should not happen!!! status is %d", h.state());
event["state"] = QVariant();
}
}
event["name"] = QVariant(h.name());
event["size"] = QVariant((qlonglong)h.actual_size());
event["upspeed"] = QVariant(h.upload_payload_rate());
update(event);
}
void EventManager::deletedTorrent(QString hash)
{
QVariantMap event;
QTorrentHandle h = BTSession->getTorrentHandle(hash);
event["type"] = QVariant("delete");
event["hash"] = QVariant(hash);
event["seed"] = QVariant(h.is_seed());
QLinkedList<QPair<ulong, QVariantMap> >::iterator i = events.end();
bool loop = true;
while (loop && i != events.begin()) {
--i;
QVariantMap oldevent = i->second;
if(oldevent["hash"] == QVariant(hash))
{
if(oldevent["type"] == QVariant("add"))
loop = false;
i = events.erase(i);
}
}
update(event);
}
void EventManager::modifiedTorrent(QTorrentHandle h)
{
QString hash = h.hash();
QVariantMap event;
QVariant v;
if(h.is_paused()) {
if(BTSession->isQueueingEnabled() && (BTSession->isDownloadQueued(hash) || BTSession->isUploadQueued(hash)))
v = QVariant("queued");
else
v = QVariant("paused");
} else {
switch(h.state())
{
case torrent_status::finished:
case torrent_status::seeding:
v = QVariant("seeding");
break;
case torrent_status::checking_files:
case torrent_status::queued_for_checking:
v = QVariant("checking");
break;
case torrent_status::connecting_to_tracker:
if(h.download_payload_rate() > 0)
v = QVariant("downloading");
else
v = QVariant("connecting");
break;
case torrent_status::downloading:
case torrent_status::downloading_metadata:
if(h.download_payload_rate() > 0)
v = QVariant("downloading");
else
v = QVariant("stalled");
break;
default:
qDebug("No status, should not happen!!! status is %d", h.state());
v = QVariant();
}
}
if(modify(hash, "state", v))
event["state"] = v;
v = QVariant(h.name());
if(modify(hash, "name", v))
event["name"] = v;
v = QVariant((qlonglong)h.actual_size());
if(modify(hash, "size", v))
event["size"] = v;
if(!h.is_seed()) {
v = QVariant(h.progress());
if(modify(hash, "progress", v))
event["progress"] = v;
v = QVariant(h.download_payload_rate());
if(modify(hash, "dlspeed", v))
event["dlspeed"] = v;
}
v = QVariant(h.upload_payload_rate());
if(modify(hash, "upspeed", v))
event["upspeed"] = v;
v = QVariant(h.is_seed());
event["seed"] = v;
if(event.size() > 0)
{
event["type"] = QVariant("modify");
event["hash"] = QVariant(hash);
update(event);
}
}
bool EventManager::modify(QString hash, QString key, QVariant value)
{
QLinkedList<QPair<ulong, QVariantMap> >::iterator i = events.end();
while (i != events.begin()) {
--i;
QVariantMap event = i->second;
if(event["hash"] == QVariant(hash))
{
if(event["type"] == QVariant("add"))
return true;
if(event.contains(key))
{
if(event[key] == value)
return false;
else
{
if(event.size() <= 3)
i = events.erase(i);
else
i->second.remove(key);
return true;
}
}
}
event["progress"] = QVariant(h.progress());
event["dlspeed"] = QVariant(h.download_payload_rate());
}
return true;
event["upspeed"] = QVariant(h.upload_payload_rate());
event["seed"] = QVariant(h.is_seed());
event["hash"] = QVariant(hash);
event_list[hash] = event;
}

15
src/eventmanager.h

@ -23,8 +23,7 @@ @@ -23,8 +23,7 @@
#define EVENTMANAGER_H
#include "qtorrenthandle.h"
#include <QLinkedList>
#include <QPair>
#include <QHash>
#include <QVariant>
struct bittorrent;
@ -33,9 +32,7 @@ class EventManager : public QObject @@ -33,9 +32,7 @@ class EventManager : public QObject
{
Q_OBJECT
private:
ulong revision;
QLinkedList<QPair <ulong, QVariantMap> > events;
bool modify(QString hash, QString key, QVariant value);
QHash<QString, QVariantMap> event_list;
bittorrent* BTSession;
protected:
@ -43,18 +40,12 @@ class EventManager : public QObject @@ -43,18 +40,12 @@ class EventManager : public QObject
public:
EventManager(QObject *parent, bittorrent* BTSession);
QVariant querySince(ulong r) const;
bool isUpdated(ulong r) const;
signals:
void updated();
QVariant getEventList() const;
public slots:
void addedTorrent(QTorrentHandle& h);
void deletedTorrent(QString hash);
void modifiedTorrent(QTorrentHandle h);
void torrentSwitchedtoUnfinished(QString hash);
void torrentSwitchedtoFinished(QString hash);
};
#endif

11
src/httpconnection.cpp

@ -115,12 +115,7 @@ void HttpConnection::respond() @@ -115,12 +115,7 @@ void HttpConnection::respond()
{
if (list[1] == "events")
{
EventManager* manager = parent->eventManager();
uint r = parser.get("r").toUInt();
if(manager->isUpdated(r))
respondJson();
else
connect(manager, SIGNAL(updated()), this, SLOT(respondJson()));
respondJson();
return;
}
}
@ -166,9 +161,7 @@ void HttpConnection::respondNotFound() @@ -166,9 +161,7 @@ void HttpConnection::respondNotFound()
void HttpConnection::respondJson()
{
EventManager* manager = parent->eventManager();
QString temp = parser.get("r");
uint r = parser.get("r").toUInt();
QVariant data = manager->querySince(r);
QVariant data = manager->getEventList();
QString string = toJson(data);
generator.setStatusLine(200, "OK");
generator.setContentTypeByExt("js");

10
src/httpserver.cpp

@ -37,16 +37,14 @@ HttpServer::HttpServer(bittorrent *BTSession, int msec, QObject* parent) : QTcpS @@ -37,16 +37,14 @@ HttpServer::HttpServer(bittorrent *BTSession, int msec, QObject* parent) : QTcpS
QTorrentHandle h = BTSession->getTorrentHandle(hash);
if(h.is_valid()) manager->addedTorrent(h);
}
list = BTSession->getFinishedTorrents();
list = BTSession->getFinishedTorrents();
foreach(QString hash, list) {
QTorrentHandle h = BTSession->getTorrentHandle(hash);
if(h.is_valid()) manager->addedTorrent(h);
}
QTorrentHandle h = BTSession->getTorrentHandle(hash);
if(h.is_valid()) manager->addedTorrent(h);
}
//connect BTSession to manager
connect(BTSession, SIGNAL(addedTorrent(QTorrentHandle&)), manager, SLOT(addedTorrent(QTorrentHandle&)));
connect(BTSession, SIGNAL(deletedTorrent(QString)), manager, SLOT(deletedTorrent(QString)));
connect(BTSession, SIGNAL(torrentSwitchedtoUnfinished(QString)), manager, SLOT(torrentSwitchedtoUnfinished(QString)));
connect(BTSession, SIGNAL(torrentSwitchedtoFinished(QString)), manager, SLOT(torrentSwitchedtoFinished(QString)));
//set timer
timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(onTimer()));

250
src/webui/scripts/client.js

@ -32,6 +32,26 @@ window.addEvent('domready', function(){ @@ -32,6 +32,26 @@ window.addEvent('domready', function(){
myTableUP = new dynamicTable('myTableUP', {overCls: 'over', selectCls: 'selected', altCls: 'alt', type: 'UP'});
var r=0;
var waiting=false;
var stateToImg = function(state){
switch (state)
{
case 'paused':
return '<img src="images/skin/paused.png"/>';
case 'seeding':
return '<img src="images/skin/seeding.png"/>';
case 'checking':
return '<img src="images/time.png"/>';
case 'downloading':
return '<img src="images/skin/downloading.png"/>';
case 'connecting':
return '<img src="images/skin/connecting.png"/>';
case 'stalled':
return '<img src="images/skin/stalled.png"/>';
case 'queued':
return '<img src="images/skin/queued.png"/>';
}
return '';
};
var round1 = function(val){return Math.round(val*10)/10};
var fspeed = function(val){return round1(val/1024) + ' KiB/s';};
var fsize = function(val){
@ -45,197 +65,77 @@ window.addEvent('domready', function(){ @@ -45,197 +65,77 @@ window.addEvent('domready', function(){
return round1(val) + ' TiB';
};
var ajaxfn = function(){
var url = 'json/events?r='+r;
var url = 'json/events';
if (!waiting){
waiting=true;
var request = new Request.JSON({
url: url,
method: 'get',
onComplete: function(jsonObj) {
if(jsonObj){
r=jsonObj.revision;
var events=jsonObj.events;
events.each(function(event){
switch(event.type){
case 'add':
var row = new Array();
if(event.seed)
row.length = 4;
else
row.length = 6;
switch (event.state)
{
case 'paused':
row[0] = '<img src="images/skin/paused.png"/>';
break;
case 'seeding':
row[0] = '<img src="images/skin/seeding.png"/>';
break;
case 'checking':
row[0] = '<img src="images/time.png"/>';
break;
case 'downloading':
row[0] = '<img src="images/skin/downloading.png"/>';
break;
case 'connecting':
row[0] = '<img src="images/skin/connecting.png"/>';
break;
case 'stalled':
row[0] = '<img src="images/skin/stalled.png"/>';
break;
case 'queued':
row[0] = '<img src="images/skin/queued.png"/>';
break;
}
row[1] = event.name;
onComplete: function(events) {
if(events){
// Add new torrents or update them
unfinished_hashes = myTable.getRowIds();
finished_hashes = myTableUP.getRowIds();
events_hashes = new Array();
events.each(function(event){
events_hashes[events_hashes.length] = event.hash;
if(event.seed) {
var row = new Array();
row.length = 4;
row[0] = stateToImg(event.state);
row[1] = event.name;
row[2] = fsize(event.size);
if(!event.seed) {
if($defined(event.progress))
{
row[3] = round1(event.progress*100) + ' %';
}
if($defined(event.dlspeed))
row[4] = fspeed(event.dlspeed);
if($defined(event.upspeed))
row[5] = fspeed(event.upspeed);
} else {
if($defined(event.upspeed))
row[3] = fspeed(event.upspeed);
}
if(event.seed)
row[3] = fspeed(event.upspeed);
if(!finished_hashes.contains(event.hash)) {
// New finished torrent
finished_hashes[finished_hashes.length] = event.hash;
myTableUP.insertRow(event.hash, row);
else
myTable.insertRow(event.hash, row);
break;
case 'modify':
var row = new Array();
if($defined(event.state))
{
switch (event.state)
{
case 'paused':
row[0] = '<img src="images/skin/paused.png"/>';
break;
case 'seeding':
row[0] = '<img src="images/skin/seeding.png"/>';
break;
case 'checking':
row[0] = '<img src="images/time.png"/>';
break;
case 'downloading':
row[0] = '<img src="images/skin/downloading.png"/>';
break;
case 'connecting':
row[0] = '<img src="images/skin/connecting.png"/>';
break;
case 'stalled':
row[0] = '<img src="images/skin/stalled.png"/>';
break;
case 'queued':
row[0] = '<img src="images/skin/queued.png"/>';
break;
}
}
if($defined(event.name)) {
row[1] = event.name;
}
if($defined(event.size)){
row[2] = fsize(event.size);
}
if(!event.seed) {
if($defined(event.progress))
{
row[3] = round1(event.progress*100) + ' %';
if(unfinished_hashes.contains(event.hash)) {
// Torrent used to be in unfinished list
// Remove it
myTable.removeRow(event.hash);
unfinished_hashes.erase(event.hash);
}
if($defined(event.dlspeed))
row[4] = fspeed(event.dlspeed);
if($defined(event.upspeed))
row[5] = fspeed(event.upspeed);
} else {
if($defined(event.upspeed))
row[3] = fspeed(event.upspeed);
}
if(event.seed)
// Update torrent data
myTableUP.updateRow(event.hash, row);
else
myTable.updateRow(event.hash, row);
break;
case 'delete':
if(event.seed)
myTableUP.removeRow(event.hash);
else
myTable.removeRow(event.hash);
break;
case 'finish':
myTable.removeRow(event.hash);
var row = new Array();
row.length = 4;
switch (event.state)
{
case 'paused':
row[0] = '<img src="images/skin/paused.png"/>';
break;
case 'seeding':
row[0] = '<img src="images/skin/seeding.png"/>';
break;
case 'checking':
row[0] = '<img src="images/time.png"/>';
break;
case 'downloading':
row[0] = '<img src="images/skin/downloading.png"/>';
break;
case 'connecting':
row[0] = '<img src="images/skin/connecting.png"/>';
break;
case 'stalled':
row[0] = '<img src="images/skin/stalled.png"/>';
break;
case 'queued':
row[0] = '<img src="images/skin/queued.png"/>';
break;
}
row[1] = event.name;
row[2] = fsize(event.size);
row[3] = fspeed(event.upspeed);
myTableUP.insertRow(event.hash, row);
break;
case 'unfinish':
myTableUP.removeRow(event.hash);
} else {
var row = new Array();
row.length = 6;
switch (event.state)
{
case 'paused':
row[0] = '<img src="images/skin/paused.png"/>';
break;
case 'seeding':
row[0] = '<img src="images/skin/seeding.png"/>';
break;
case 'checking':
row[0] = '<img src="images/time.png"/>';
break;
case 'downloading':
row[0] = '<img src="images/skin/downloading.png"/>';
break;
case 'connecting':
row[0] = '<img src="images/skin/connecting.png"/>';
break;
case 'stalled':
row[0] = '<img src="images/skin/stalled.png"/>';
break;
case 'queued':
row[0] = '<img src="images/skin/queued.png"/>';
break;
}
row[1] = event.name;
row.length = 6;
row[0] = stateToImg(event.state);
row[1] = event.name;
row[2] = fsize(event.size);
row[3] = round1(event.progress*100) + ' %';
row[4] = fspeed(event.dlspeed);
row[5] = fspeed(event.upspeed);
myTable.insertRow(event.hash, row);
break;
}
});
if(!unfinished_hashes.contains(event.hash)) {
// New unfinished torrent
unfinished_hashes[unfinished_hashes.length] = event.hash;
myTable.insertRow(event.hash, row);
if(finished_hashes.contains(event.hash)) {
// Torrent used to be in unfinished list
// Remove it
myTableUP.removeRow(event.hash);
finished_hashes.erase(event.hash);
}
} else {
// Update torrent data
myTable.updateRow(event.hash, row);
}
}
});
// Remove deleted torrents
unfinished_hashes.each(function(hash){
if(!events_hashes.contains(hash)) {
myTable.removeRow(hash);
}
});
finished_hashes.each(function(hash){
if(!events_hashes.contains(hash)) {
myTableUP.removeRow(hash);
}
});
}
waiting=false;
ajaxfn.delay(1000);

Loading…
Cancel
Save