/
CoreConnection.cpp
321 lines (259 loc) · 9.26 KB
/
CoreConnection.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
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/**
* D-LAN - A decentralized LAN file sharing software.
* Copyright (C) 2010-2012 Greg Burri <greg.burri@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 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 <priv/CoreConnection.h>
using namespace RCC;
#include <QHostAddress>
#include <QCoreApplication>
#include <LogManager/Builder.h>
#include <ProtoHelper.h>
#include <Constants.h>
#include <priv/CoreController.h>
#include <priv/Log.h>
#include <priv/BrowseResult.h>
#include <priv/SearchResult.h>
CoreConnection::CoreConnection(int socketTimeout) :
connection1(this->coreController),
connection2(this->coreController),
currentConnected(FIRST_CONNECTION),
connectingInProgress(false),
SOCKET_TIMEOUT(socketTimeout)
{
connect(&this->coreController, SIGNAL(statusChanged()), this, SIGNAL(localCoreStatusChanged()));
}
CoreConnection::~CoreConnection()
{
}
void CoreConnection::startLocalCore()
{
this->coreController.startCore();
}
void CoreConnection::stopLocalCore()
{
this->coreController.stopCore();
}
CoreStatus CoreConnection::getLocalCoreStatus() const
{
return this->coreController.getStatus();
}
void CoreConnection::connectToCore()
{
this->connectToCore(59485);
}
void CoreConnection::connectToCore(quint16 port)
{
this->connectToCore("localhost", port, Common::Hash());
}
void CoreConnection::connectToCore(const QString& address, quint16 port, Common::Hash password)
{
if (!this->connectToCorePrepare(address))
return;
this->temp().connectToCore(address, port, password);
}
void CoreConnection::connectToCore(const QString& address, quint16 port, const QString& password)
{
if (!this->connectToCorePrepare(address))
return;
this->temp().connectToCore(address, port, password);
}
Common::Hash CoreConnection::getRemoteID() const
{
return this->current().getRemoteID();
}
bool CoreConnection::isLocal() const
{
if (this->connectingInProgress)
return this->temp().isLocal();
else
return this->current().isLocal();
}
bool CoreConnection::isConnected() const
{
return this->current().isConnected();
}
bool CoreConnection::isConnecting() const
{
return this->connectingInProgress;
}
void CoreConnection::disconnectFromCore()
{
this->current().disconnectFromCore();
this->temp().disconnectFromCore();
}
QSharedPointer<ISendChatMessageResult> CoreConnection::sendChatMessage(const QString& message)
{
return this->current().sendChatMessage(this->SOCKET_TIMEOUT, message);
}
QSharedPointer<ISendChatMessageResult> CoreConnection::sendChatMessage(const QString& message, const QString& roomName)
{
return this->current().sendChatMessage(this->SOCKET_TIMEOUT, message, roomName);
}
QSharedPointer<ISendChatMessageResult> CoreConnection::sendChatMessage(const QString& message, const QString& roomName, const QList<Common::Hash>& peerIDsAnswered)
{
return this->current().sendChatMessage(this->SOCKET_TIMEOUT, message, roomName, peerIDsAnswered);
}
void CoreConnection::joinRoom(const QString& room)
{
this->current().joinRoom(room);
}
void CoreConnection::leaveRoom(const QString& room)
{
this->current().leaveRoom(room);
}
void CoreConnection::setCoreSettings(const Protos::GUI::CoreSettings settings)
{
this->current().setCoreSettings(settings);
}
void CoreConnection::setCoreLanguage(const QLocale& locale)
{
this->current().setCoreLanguage(locale);
this->temp().setCoreLanguage(locale);
}
bool CoreConnection::setCorePassword(const QString& newPassword, const QString& oldPassword)
{
return this->current().setCorePassword(newPassword, oldPassword);
}
void CoreConnection::resetCorePassword()
{
this->current().resetCorePassword();
}
QSharedPointer<IBrowseResult> CoreConnection::browse(const Common::Hash& peerID)
{
return this->current().browse(peerID, this->SOCKET_TIMEOUT);
}
QSharedPointer<IBrowseResult> CoreConnection::browse(const Common::Hash& peerID, const Protos::Common::Entry& entry)
{
return this->current().browse(peerID, entry, this->SOCKET_TIMEOUT);
}
QSharedPointer<IBrowseResult> CoreConnection::browse(const Common::Hash& peerID, const Protos::Common::Entries& entries, bool withRoots)
{
return this->current().browse(peerID, entries, withRoots, this->SOCKET_TIMEOUT);
}
QSharedPointer<ISearchResult> CoreConnection::search(const Protos::Common::FindPattern& findPattern, bool local)
{
return this->current().search(findPattern, local, this->SOCKET_TIMEOUT);
}
void CoreConnection::download(const Common::Hash& peerID, const Protos::Common::Entry& entry)
{
this->current().download(peerID, entry);
}
void CoreConnection::download(const Common::Hash& peerID, const Protos::Common::Entry& entry, const Common::Hash& sharedFolderID, const QString& path)
{
this->current().download(peerID, entry, sharedFolderID, path);
}
void CoreConnection::download(const Common::Hash& peerID, const Protos::Common::Entry& entry, const QString& absolutePath)
{
this->current().download(peerID, entry, Common::Hash(), absolutePath);
}
void CoreConnection::cancelDownloads(const QList<quint64>& downloadIDs, bool complete)
{
this->current().cancelDownloads(downloadIDs, complete);
}
void CoreConnection::pauseDownloads(const QList<quint64>& downloadIDs, bool pause)
{
this->current().pauseDownloads(downloadIDs, pause);
}
void CoreConnection::moveDownloads(quint64 downloadIDRef, const QList<quint64>& downloadIDs, Protos::GUI::MoveDownloads::Position position)
{
this->moveDownloads(QList<quint64>() << downloadIDRef, downloadIDs, position);
}
void CoreConnection::moveDownloads(const QList<quint64>& downloadIDRefs, const QList<quint64>& downloadIDs, Protos::GUI::MoveDownloads::Position position)
{
this->current().moveDownloads(downloadIDRefs, downloadIDs, position);
}
void CoreConnection::refresh()
{
this->current().refresh();
}
void CoreConnection::refreshNetworkInterfaces()
{
this->current().refreshNetworkInterfaces();
}
ICoreConnection::ConnectionInfo CoreConnection::getConnectionInfo() const
{
return this->current().getConnectionInfo();
}
ICoreConnection::ConnectionInfo CoreConnection::getConnectionInfoConnecting() const
{
return this->temp().getConnectionInfo();
}
void CoreConnection::tempConnectingError(RCC::ICoreConnection::ConnectionErrorCode code)
{
this->connectingInProgress = false;
emit connectingError(code);
this->temp().disconnect(this);
}
void CoreConnection::tempConnected()
{
this->current().disconnectFromCore(); // May throw a 'disconnected()' signal.
this->current().disconnect(this);
this->temp().disconnect(this);
this->connectingInProgress = false;
this->swap();
connect(&this->current(), SIGNAL(disconnected(bool)), this, SIGNAL(disconnected(bool)));
connect(&this->current(), SIGNAL(newState(const Protos::GUI::State&)), this, SIGNAL(newState(const Protos::GUI::State&)));
connect(&this->current(), SIGNAL(newChatMessages(const Protos::Common::ChatMessages&)), this, SIGNAL(newChatMessages(const Protos::Common::ChatMessages&)));
connect(&this->current(), SIGNAL(newLogMessages(QList<QSharedPointer<LM::IEntry>>)), this, SIGNAL(newLogMessages(QList<QSharedPointer<LM::IEntry>>)));
emit connected();
}
void CoreConnection::tempDisconnected()
{
if (this->connectingInProgress)
this->tempConnectingError(RCC_ERROR_HOST_TIMEOUT);
}
/**
* @return true is no error.
*/
bool CoreConnection::connectToCorePrepare(const QString& address)
{
emit connecting();
if (this->connectingInProgress)
{
this->tempConnectingError(RCC_ERROR_CONNECTING_IN_PROGRESS);
return false;
}
this->connectingInProgress = true;
if (address.isNull() || address.isEmpty())
{
this->tempConnectingError(RCC_ERROR_INVALID_ADDRESS);
return false;
}
connect(&this->temp(), SIGNAL(connectingError(RCC::ICoreConnection::ConnectionErrorCode)), this, SLOT(tempConnectingError(RCC::ICoreConnection::ConnectionErrorCode)));
connect(&this->temp(), SIGNAL(connected()), this, SLOT(tempConnected()));
connect(&this->temp(), SIGNAL(disconnected(bool)), this, SLOT(tempDisconnected()));
return true;
}
InternalCoreConnection& CoreConnection::current()
{
return this->currentConnected == FIRST_CONNECTION ? this->connection1 : this->connection2;
}
const InternalCoreConnection& CoreConnection::current() const
{
return this->currentConnected == FIRST_CONNECTION ? this->connection1 : this->connection2;
}
InternalCoreConnection& CoreConnection::temp()
{
return this->currentConnected == FIRST_CONNECTION ? this->connection2 : this->connection1;
}
const InternalCoreConnection& CoreConnection::temp() const
{
return this->currentConnected == FIRST_CONNECTION ? this->connection2 : this->connection1;
}
void CoreConnection::swap()
{
this->currentConnected = this->currentConnected == FIRST_CONNECTION ? SECOND_CONNECTION : FIRST_CONNECTION;
}