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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
|
#include <QtCore/QStringList>
#include "clientagent.h"
#include "serveragent.h"
#include "distfolder.h"
DistFolder::DistFolder(const QUuid& uuid, const QString& localPath, QObject *parent) :
QObject(parent), _uuid(uuid), _localPath(localPath),
_mtime(), _mtimeChanged(false),
_watcher(new Watcher(localPath, this)),
_server(new Server(this)),
_discoverer(new Discoverer(uuid, _server->serverPort(), QString("Files on %1").arg(_localPath.canonicalPath()), this)),
_syncFlags(Agent::SYNC_NORMAL),
_numAgents(0)
{
Q_ASSERT(_localPath.isReadable());
connect(_watcher, SIGNAL(pathAdded(QString)), SLOT(handlePathAdded(QString)));
connect(_watcher, SIGNAL(pathChanged(QString)), SLOT(handlePathChanged(QString)));
connect(_watcher, SIGNAL(pathRemoved(QString)), SLOT(handlePathRemoved(QString)));
updateLastModTime();
connect(_discoverer, SIGNAL(foundMoreRecentHost(QHostAddress,uint,QDateTime)),
SLOT(handleMoreRecentHost(QHostAddress,uint,QDateTime)));
connect(_server, SIGNAL(newConnection()), SLOT(handleNewConnection()));
qDebug() << "Ready Folder UUID:" << _uuid;
}
QString DistFolder::password() const
{
return _password;
}
void DistFolder::setPassword(const QString &passwd)
{
_password = passwd;
}
bool DistFolder::readOnlySync() const
{
return _syncFlags & Agent::SYNC_READ_ONLY;
}
void DistFolder::setReadOnlySync(bool read_only)
{
if (read_only) {
qDebug() << "Enabling read only sync";
_syncFlags |= Agent::SYNC_READ_ONLY;
} else {
_syncFlags &= ~Agent::SYNC_READ_ONLY;
}
}
bool DistFolder::pullMode() const
{
return _syncFlags & Agent::SYNC_PULL;
}
void DistFolder::setPullMode(bool pull_mode)
{
if (pull_mode) {
qDebug() << "Enabling pull sync";
_syncFlags |= Agent::SYNC_PULL;
} else {
_syncFlags &= ~Agent::SYNC_PULL;
}
updateLastModTime();
}
bool DistFolder::compress() const
{
return _syncFlags & Agent::SYNC_COMPRESS;
}
void DistFolder::setCompress(bool compress)
{
if (compress) {
qDebug() << "Enabling compression";
_syncFlags |= Agent::SYNC_COMPRESS;
} else {
_syncFlags &= ~Agent::SYNC_COMPRESS;
}
}
QDateTime DistFolder::scanLastModTime()
{
return scanLastModTime(_localPath);
}
void DistFolder::updateLastModTime(const QDateTime& dt)
{
QDateTime curMtime = _mtime;
if (pullMode()) {
_mtime = QDateTime::fromTime_t(0); // Force being the puller for the next sync
} else {
if (dt.isNull()) {
_mtime = scanLastModTime();
} else if (dt > _mtime) {
_mtime = dt;
}
}
if (_mtime != curMtime) {
if (_numAgents > 0) {
_mtimeChanged = true;
} else {
_discoverer->setLastModifiedTime(_mtime);
_mtimeChanged = false;
}
}
}
QDateTime DistFolder::scanLastModTime(const QDir& dir)
{
QFileInfoList list = dir.entryInfoList(QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot);
QDateTime max = QFileInfo(dir.absolutePath()).lastModified();
foreach (const QFileInfo &info, list) {
QDateTime mtime;
if (info.isDir()) {
mtime = scanLastModTime(QDir(info.absoluteFilePath()));
} else {
mtime = info.lastModified();
}
if (mtime > max) {
max = mtime;
}
}
return max;
}
void DistFolder::handleNewConnection()
{
qDebug() << "Incoming connection";
ServerAgent *agent =
new ServerAgent(static_cast<QSslSocket*>(_server->nextPendingConnection()),
_localPath, _password, _syncFlags, this);
connect(agent, SIGNAL(destroyed()), SLOT(handleDestroyedAgent()));
_numAgents++;
qDebug() << "Num agents" << _numAgents;
}
void DistFolder::handlePathAdded(const QString &path)
{
QFileInfo info(path);
qDebug() << "added: " << path;
if (info.lastModified() > _mtime) {
updateLastModTime(info.lastModified());
}
}
void DistFolder::handlePathChanged(const QString &path)
{
QFileInfo info(path);
qDebug() << "changed: " << path;
if (info.lastModified() > _mtime) {
updateLastModTime(info.lastModified());
}
}
void DistFolder::handlePathRemoved(const QString &path)
{
QFileInfo info(path);
qDebug() << "removed: " << path;
// Find a parent that has not been removed
const QString base_path = _localPath.absolutePath();
do {
info = QFileInfo(info.absolutePath());
} while (!info.exists() && // Until it exists and as long as we are under the base dir.
info.absoluteFilePath().startsWith(base_path));
if (info.lastModified() > _mtime) {
updateLastModTime(info.lastModified());
}
}
void DistFolder::handleMoreRecentHost(const QHostAddress &address, uint port, const QDateTime &dateTime)
{
Q_UNUSED(dateTime);
if (_numAgents == 0) {
qDebug() << "Trying to connect to" << address.toString() << port;
ClientAgent *agent = new ClientAgent(address, port, _localPath, _password, _syncFlags, this);
connect(agent, SIGNAL(destroyed()), SLOT(handleDestroyedAgent()));
connect(agent, SIGNAL(finished()), SLOT(handleClientFinished()));
_numAgents++;
qDebug() << "Num agents" << _numAgents;
} else {
qDebug() << "Busy but a more recent host was found";
// TODO
}
}
void DistFolder::handleClientFinished()
{
if (pullMode()) {
// Stop pulling once a succesful sync is done
qDebug() << "Stopping pull mode";
setPullMode(false);
}
}
void DistFolder::handleDestroyedAgent()
{
_numAgents--;
qDebug() << "Num agents" << _numAgents;
if (_numAgents == 0) {
// All sync agents have finished
if (_mtimeChanged) {
_discoverer->setLastModifiedTime(_mtime);
_mtimeChanged = false;
}
}
}
|