-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathincomingtransferthread.cpp
160 lines (127 loc) · 4.2 KB
/
incomingtransferthread.cpp
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
/*
This file is part of the Kapotah project.
Copyright (C) 2010 Shantanu Tushar <[email protected]>
Copyright (C) 2010 Sudhendu Kumar <[email protected]>
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 3 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, see <http://www.gnu.org/licenses/>.
*/
#include "incomingtransferthread.h"
#include "debug.h"
#include <QFile>
#include <QTcpSocket>
#include <QDir>
#include <qt4/QtCore/QDateTime>
static const int s_fileTransferPort = 45002;
IncomingTransferThread::IncomingTransferThread (QHostAddress ip, QString id, QString filename, quint64 size,
QObject* parent) : TransferThread (ip, parent), m_id (id), m_filename (filename), m_size (size)
{
doQuit = false;
}
IncomingTransferThread::~IncomingTransferThread()
{
m_mutex.lock();
doQuit = true;
m_mutex.unlock();
wait();
}
void IncomingTransferThread::stopTransfer()
{
deleteLater();
}
void IncomingTransferThread::run()
{
bool readyToReceive = false;
quint64 bytesCopied;
qint64 startTime;
QFile file;
//TODO: set enums in special case of size == 0
if (m_size == 0) { //Create an empty file
file.setFileName (m_filename);
file.open (QIODevice::WriteOnly);
emit done ();
file.close();
return;
}
QTcpSocket socket;
setStatus(Connecting);
socket.connectToHost (m_ip, s_fileTransferPort);
socket.waitForConnected();
setStatus(Requesting);
socket.write (m_id.toUtf8()); //ID
socket.waitForBytesWritten();
while (1) {
m_mutex.lock();
if (doQuit) {
m_mutex.unlock();
break;
}
m_mutex.unlock();
while (!socket.bytesAvailable() && !doQuit) {
socket.waitForReadyRead();
}
if (doQuit) {
setStatus(Canceled);
break;
}
if (readyToReceive) {
file.write (socket.readAll());
bytesCopied = file.pos();
emit progress (bytesCopied, m_size);
if (bytesCopied >= m_size) {
emit done ();
file.close();
socket.disconnectFromHost();
if (socket.state() != QTcpSocket::UnconnectedState)
socket.waitForDisconnected();
break;
}
} else {
QString data (socket.read (2));
if (data == "OK") {
setStatus(PreparingToReceive);
QDir dir(m_filename); //filename is a/b/c/d/blah.txt
if (!dir.exists()) {
dir.mkpath(".."); //create a/b/c/d/ if it doesn't exist
}
file.setFileName (m_filename);
if (!file.open (QIODevice::WriteOnly)) {
setStatus(ErrorCreatingFile);
kaDebug("Could not open file " + file.fileName());
break;
}
readyToReceive = true;
setStatus(Receiving);
startTime = QDateTime::currentMSecsSinceEpoch();
} else {
setStatus(ErrorTransferNotFound);
kaDebug(m_id + " not found on server: " + data);
break;
}
}
}
qDebug() << "Elapsed " << (QDateTime::currentMSecsSinceEpoch() - startTime) << " ms";
deleteLater();
}
void IncomingTransferThread::setStatus(IncomingTransferThread::Status status)
{
m_mutex.lock();
m_status = status;
m_mutex.unlock();
}
IncomingTransferThread::Status IncomingTransferThread::status()
{
Status status;
m_mutex.lock();
status = m_status;
m_mutex.unlock();
return status;
}
#include "incomingtransferthread.moc"