[prev in list] [next in list] [prev in thread] [next in thread]
List: kde-commits
Subject: extragear/network/kmldonkey
From: Gioacchino Mazzurco <gioxmx () libero ! it>
Date: 2009-06-04 16:07:02
Message-ID: 1244131622.916423.5298.nullmailer () svn ! kde ! org
[Download RAW message or body]
SVN commit 977553 by gmazzurco:
now we have local torrent full support!
thanks to Алексей Маркелов that submit a patch that make it work!
M +0 -1 kmldonkey/CMakeLists.txt
M +9 -4 kmldonkey/kmldonkey.cpp
M +3 -3 libkmldonkey/fileinfo.cpp
M +57 -35 libkmldonkey/torrenthost.cpp
M +10 -5 libkmldonkey/torrenthost.h
--- trunk/extragear/network/kmldonkey/kmldonkey/CMakeLists.txt #977552:977553
@@ -46,7 +46,6 @@
prefs/prefs.cpp
main.cpp
../libkmldonkey/emulecollection.cpp
- ../libkmldonkey/torrenthost.cpp
)
kde4_add_ui_files(kmldonkeyapp_KDEINIT_SRCS
--- trunk/extragear/network/kmldonkey/kmldonkey/kmldonkey.cpp #977552:977553
@@ -687,10 +687,15 @@
qDebug() << filePath;
if (filePath.isEmpty()) return;
- TorrentHost * torrentSender = new TorrentHost(filePath);
- //donkey->submitUrl("http://" + donkey->getLocalAddress().toString() +":"+ \
torrentSender->getPort() + "/ciccio.torrent"); //TODO: decomment this line when \
TorrentHost::sendTorrent() is fixed!
- qDebug() << "http://" + donkey->getLocalAddress().toString() +":"+ \
torrentSender->getPort();
-
+ TorrentHost * torrentSender = new TorrentHost(filePath, \
donkey->getLocalAddress(), QHostAddress(donkey->getHost()->address())); + qDebug() \
<< "submitting file: " << filePath + << "local address: " << \
donkey->getLocalAddress() + << "donkey host: " << \
donkey->getHost()->address(); + QString url = QString("http://%1:%2/%3")
+ .arg(donkey->getLocalAddress().toString())
+ .arg(torrentSender->serverPort())
+ .arg(QFileInfo(filePath).fileName());
+ donkey->submitUrl(url);
}
--- trunk/extragear/network/kmldonkey/libkmldonkey/fileinfo.cpp #977552:977553
@@ -273,14 +273,14 @@
QString FileInfo::fileUid() const
{
- return uids.first();
+ return uids.isEmpty() ? QString("NOT AVAILABLE") : uids.first();
}
QString FileInfo::fileUid(const QString& type) const
{
QRegExp match(QString("^urn:") + type + ":");
- QStringList results = uids.grep(match);
- if (!results.count()) return QString::null;
+ QStringList results = uids.filter(match);
+ if (!results.count()) return QString();
QString result(results.first());
result.replace(match, "");
return result;
--- trunk/extragear/network/kmldonkey/libkmldonkey/torrenthost.cpp #977552:977553
@@ -28,54 +28,76 @@
#include <QIODevice>
#include <QtDebug>
-TorrentHost::TorrentHost(QString filePath) : QTcpServer(){
+TorrentHost::TorrentHost(const QString &filePath, const QHostAddress &local, const \
QHostAddress &donkey) + : QTcpServer()
+ , m_donkey(donkey)
+{
qDebug() << "TorrentHost::TorrentHost(QString filePath) called \n";
torrentFile.setFileName(filePath);
connect(this, SIGNAL(newConnection()), this, SLOT(acceptConnection()));
- listen();
- qDebug() << "TorrentHost listening on port " << getPort();
+ listen(local);
+ qDebug() << "TorrentHost listening on port " << serverPort();
}
-TorrentHost::~TorrentHost(){
+TorrentHost::~TorrentHost(){}
- httpConnection->close();
- delete(httpConnection);
-}
-
-QString TorrentHost::getPort(){
- return QString::number(serverPort());
-}
-
void TorrentHost::acceptConnection(){
qDebug() << "TorrentHost::acceptConnection() called";
httpConnection = nextPendingConnection();
-
- connect(httpConnection, SIGNAL(readyRead()), this, SLOT(sendTorrent()));
+ if (!m_donkey.isNull() && httpConnection->peerAddress() != m_donkey) {
+ qDebug() << "Incorrect host connected: expecting" << m_donkey << "got: " << \
httpConnection->peerAddress(); + httpConnection->deleteLater();
+ return;
+ }
+ connect(httpConnection, SIGNAL(readyRead()), this, SLOT(dataAvailable()));
- close();
}
-void TorrentHost::sendTorrent(){
- qDebug() << "TorrentHost::sendTorrent() called";
- torrentFile.open(QIODevice::ReadOnly);
-
- while(httpConnection->canReadLine()){
- QString req(httpConnection->readLine(1000));
- qDebug() << "req == " << req;
- QStringList tokens = QStringList::split( QRegExp("[ \r\n][ \r\n]*"), req);
-//TODO: someone help me with http I don't know how to pass correctly a file trought \
http.
- if(!tokens.isEmpty() && (tokens[0] == "HEAD" || tokens[0] == "GET")){
- httpConnection->write("HTTP/1.0 200 Ok\r\n");
- //httpConnection->write("Content-Type: text/html; charset=ISO-8859-1\r\n");
- httpConnection->write("Content-Type: application/x-bittorrent; \
charset=ISO-8859-1\r\n"); // check if this line is correct
- httpConnection->write("Server: kmldonkey\r\n");
- httpConnection->write("Transfer-Encoding: chunked\r\n");
- httpConnection->write("\r\n");
+void TorrentHost::dataAvailable()
+{
+ qDebug() << "got data";
+ while (httpConnection->canReadLine()) {
+ qDebug() << "can read line";
+ const QByteArray rline = httpConnection->readLine();
+ qDebug() << "line : "<< rline;
+ if (rline.trimmed().isEmpty()) {
+ if (!m_currentRequest.isEmpty())
+ processRequest();
+ } else {
+ m_currentRequest << rline;
+ }
+ }
+}
- httpConnection->write(torrentFile.readAll());
- httpConnection->close();
+void TorrentHost::processRequest()
+{
+ qDebug() << "processing request" << m_currentRequest;
+ bool head = m_currentRequest.at(0).startsWith("HEAD");
+ bool get = !head && m_currentRequest.at(0).startsWith("GET");
+ Q_ASSERT(head || get);
+ QList<QByteArray> response;
+ response << "HTTP/1.0 200 Ok"
+ << "Content-Type: application/x-bittorrent"
+ << "Server: libkmldonkey/svn";
+ if (get)
+ response << QByteArray("Content-length: ") + \
QByteArray::number(torrentFile.size()); + response << "";
+ if (get) {
+ if (!torrentFile.open(QIODevice::ReadOnly))
+ Q_ASSERT(false);//form 404 response
+ QByteArray torrent = torrentFile.readAll();
+ response << torrent;
}
- }
-}
\ No newline at end of file
+ foreach (const QByteArray &line, response) {
+ qDebug() << "response : " << line;
+ httpConnection->write(line);
+ httpConnection->write("\r\n");
+ }
+ if (get) {
+ httpConnection->flush();
+ this->deleteLater();
+ }
+ m_currentRequest.clear();
+}
--- trunk/extragear/network/kmldonkey/libkmldonkey/torrenthost.h #977552:977553
@@ -29,23 +29,28 @@
#include <QString>
#include <QFile>
-class TorrentHost : public QTcpServer{
+#include "kmldonkey_export.h"
+class KMLDONKEY_EXPORT TorrentHost : public QTcpServer{
+
Q_OBJECT
public:
- TorrentHost(QString filePath);
+ TorrentHost(const QString &filePath, const QHostAddress &local, const \
QHostAddress &donkey); ~TorrentHost();
- QString getPort();
private:
QFile torrentFile;
QTcpSocket * httpConnection;
+ QHostAddress m_donkey;
+ QList<QByteArray> m_currentRequest;
+ void processRequest();
+
private slots:
void acceptConnection();
- void sendTorrent();
+ void dataAvailable();
};
-#endif /* __libkmldonkey_torrenthost_h__ */
\ No newline at end of file
+#endif /* __libkmldonkey_torrenthost_h__ */
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic