-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathworkstation.cpp
809 lines (686 loc) · 26.2 KB
/
workstation.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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
#include "workstation.h"
#include "mainwindow.h"
#include "tcpsocket.h"
#include "udpsocket.h"
#include "ui_mainwindow.h"
#include "filedata.h"
#include "audiocomponent.h"
#define FILE_LIST 1
#define FILE_TRANSFER 2
#define VOICE_CHAT 3
Workstation::Workstation(MainWindow* mainWindow)
: socketThread_(new QThread()), voiceInThread_(new QThread()),
voiceOutThread_(new QThread()), mainWindowPointer_(mainWindow) {
int err = 0;
WSADATA wsaData;
WORD wVersionRequested = MAKEWORD(2,2);
if ((err = WSAStartup(wVersionRequested, &wsaData)) < 0) {
qDebug("Workstation::Workstation(): Missing WINSOCK2 DLL.");
throw "Workstation::Workstation(): Missing WINSOCK2 DLL.";
}
// Open TCP socket for reading and writing.
tcpSocket_ = new TCPSocket(mainWindow->winId());
// Connect signal and slot for WSA events
connect(mainWindow, SIGNAL(signalWMWSASyncTCPRx(int, int)),
tcpSocket_, SLOT(slotProcessWSAEvent(int, int)));
tcpSocket_->open(QIODevice::ReadWrite);
// Connections for receiving data on the tcp socket
connect(tcpSocket_, SIGNAL(signalClientConnected(Socket*)),
this, SLOT(processConnection(Socket*)));
connect(tcpSocket_, SIGNAL(signalDataReceived(Socket*)),
this, SLOT(decodeControlMessage(Socket*)));
// Connections for file list transfers
connect(mainWindow, SIGNAL(requestPlaylist(QString, short)),
this, SLOT(requestFileList(QString, short)));
connect(mainWindow, SIGNAL(requestFile(QString, short, QString)),
this, SLOT(requestFile(QString, short ,QString)));
// Connection for voice chat
connect(mainWindowPointer_, SIGNAL(initiateVoiceStream(short, QString, AudioComponent*)),
this, SLOT(initializeVoiceStream(short, QString, AudioComponent*)));
// Connections for multicast controls
connect(mainWindowPointer_, SIGNAL(startMulticast(QStringList*)),
this, SLOT(startMulticast(QStringList*)));
connect(mainWindowPointer_->getJoinMulticast(),
SIGNAL(play(QString,int)),this,SLOT(joinMulticast(QString, int)));
// Listen on the TCP socket for other client connections
if(!tcpSocket_->listen(7000)) {
tcpSocket_->listen(7001);
}
tcpSocket_->moveToThread(socketThread_);
socketThread_->start();
}
Workstation::~Workstation() {
delete tcpSocket_;
delete udpSocket_;
// Should delete the current transfers map here too?
}
void Workstation::initializeVoiceStream(short port, QString hostAddr, AudioComponent* player) {
qDebug("Workstation::startVoiceStream(); Starting voice chat...");
// Create the socket
voiceControlSocket_ = new TCPSocket(mainWindowPointer_->winId());
connect(mainWindowPointer_, SIGNAL(signalWMWSASyncTCPRx(int,int)),
voiceControlSocket_, SLOT(slotProcessWSAEvent(int,int)));
voiceControlSocket_->open(QIODevice::ReadWrite);
voiceControlSocket_->moveToThread(socketThread_);
// Connect to a remote host
if (!voiceControlSocket_->connectRemote(hostAddr, port)) {
qDebug("Workstation::startVoiceStream(); Failed to connect to remote.");
return;
}
// Create the control packet
QByteArray packet;
packet.insert(0, VOICE_CHAT);
packet += QByteArray::fromRawData((const char*)&port, sizeof(short));
// Send the file path to the client
voiceControlSocket_->write(packet);
// Connect the signals
connect(voiceControlSocket_, SIGNAL(signalSocketClosed()),
this, SLOT(endVoiceStream()));
connect(mainWindowPointer_, SIGNAL(disconnectVoiceStream()),
this, SLOT(endVoiceStreamUser()));
// Disconnect the button and the initial dialog
disconnect(mainWindowPointer_,
SIGNAL(initiateVoiceStream(short, QString, AudioComponent*)),
this,
SLOT(initializeVoiceStream(short, QString, AudioComponent*)));
// Connect signal and slot for WSA events.
udpSocket_ = new UDPSocket(mainWindowPointer_->winId());
connect(mainWindowPointer_, SIGNAL(signalWMWSASyncUDPRx(int, int)),
udpSocket_, SLOT(slotProcessWSAEvent(int, int)));
udpSocket_->open(QIODevice::ReadWrite);
udpSocket_->setDest(hostAddr, port);
udpSocket_->moveToThread(socketThread_);
// Listen on the TCP socket for other client connections
if(!udpSocket_->listen(port)) {
qDebug("Workstation::initializeVoiceChat(); Failed to listen to port: %d",
port);
return;
}
player->startMic(udpSocket_, voiceInThread_);
voiceInThread_->start();
player->playStream(udpSocket_, voiceOutThread_);
voiceOutThread_->start();
mainWindowPointer_->setVoiceCallActive(true);
}
void Workstation::endVoiceStream() {
qDebug("Workstation::endVoiceStream(); Ending voice chat.");
// Disconnect this slot and signal
disconnect(mainWindowPointer_, SIGNAL(disconnectVoiceStream()),
this, SLOT(endVoiceStreamUser()));
// Get the audio component
AudioComponent *player = mainWindowPointer_->getAudioPlayer();
// Stop the audio input and playback
player->stopMic();
player->stopStream();
// Make sure that the boolean flag is false
mainWindowPointer_->setVoiceCallActive(false);
udpSocket_->deleteLater();
udpSocket_ = NULL;
// Reconnect the button and the initial dialog
connect(mainWindowPointer_,
SIGNAL(initiateVoiceStream(short, QString, AudioComponent*)),
this,
SLOT(initializeVoiceStream(short, QString, AudioComponent*)));
}
void Workstation::endVoiceStreamUser()
{
qDebug("Workstation::endVoiceStreamUser(); Ending voice chat.");
// Disconnect this slot and signal
disconnect(voiceControlSocket_, SIGNAL(signalSocketClosed()),
this, SLOT(endVoiceStream()));
disconnect(mainWindowPointer_, SIGNAL(disconnectVoiceStream()),
this, SLOT(endVoiceStreamUser()));
// Get the audio component
AudioComponent *player = mainWindowPointer_->getAudioPlayer();
// Stop the audio input
player->stopMic();
player->stopStream();
// Make sure that the boolean flag is false
mainWindowPointer_->setVoiceCallActive(false);
voiceControlSocket_->closeConnection();
// Delete the socket, where the rest of the cleanup will be triggered from
voiceControlSocket_->deleteLater();
voiceControlSocket_ = NULL;
udpSocket_->deleteLater();
udpSocket_ = NULL;
// Reconnect the button and the initial dialog
connect(mainWindowPointer_,
SIGNAL(initiateVoiceStream(short, QString, AudioComponent*)),
this,
SLOT(initializeVoiceStream(short, QString, AudioComponent*)));
}
void Workstation::startMulticast(QStringList* list) {
qDebug("Workstation::startMulticast(); Starting multicast.");
udpSocket_ = new UDPSocket(mainWindowPointer_->winId());
connect(mainWindowPointer_, SIGNAL(signalWMWSASyncUDPRx(int, int)),
udpSocket_, SLOT(slotProcessWSAEvent(int, int)));
udpSocket_->open(QIODevice::WriteOnly);
multicastSession_ = new MulticastSession(udpSocket_, list);
connect(mainWindowPointer_->getHostMulticast(), SIGNAL(play()),
multicastSession_, SLOT(start()));
connect(mainWindowPointer_->getHostMulticast(), SIGNAL(pause()),
multicastSession_, SLOT(pause()));
connect(mainWindowPointer_->getHostMulticast(), SIGNAL(rejected()),
multicastSession_, SLOT(endSession()));
}
void Workstation::joinMulticast(QString address, int port) {
udpSocket_ = new UDPSocket(mainWindowPointer_->winId());
udpSocket_->listenMulticast(address,port);
connect(mainWindowPointer_, SIGNAL(signalWMWSASyncUDPRx(int, int)),
udpSocket_, SLOT(slotProcessWSAEvent(int, int)));
udpSocket_->open(QIODevice::ReadOnly);
udpSocket_->moveToThread(socketThread_);
connect(udpSocket_, SIGNAL(signalDataReceived(Socket*)),
mainWindowPointer_->getAudioPlayer(),
SLOT(addFromMulticast(Socket*)));
connect(mainWindowPointer_->getJoinMulticast(), SIGNAL(rejected()),
this, SLOT(leaveMulticast()));
}
void Workstation::leaveMulticast() {
qDebug("Workstation::leaveMulticast(); Leaving multicast session.");
udpSocket_->deleteLater();
udpSocket_ = NULL;
}
void Workstation::sendFile(Socket *socket, QByteArray *data)
{
// Create file name
QDataStream fileNameStream(&(*data), QIODevice::ReadOnly);
QString fileName;
fileNameStream >> fileName;
// Open the file
QFile file(fileName);
if(!file.open(QIODevice::ReadOnly)){
// Should probably do something better here...
return;
}
// Read file into a byte array for sending
QByteArray packet = file.readAll();
// Create the control packet
int length = packet.size();
QByteArray len((const char *)&length, sizeof(int));
packet.insert(0, len);
// Send our file to the other client
socket->write(packet);
}
void Workstation::acceptVoiceChat(Socket *socket)
{
// Make sure that we are not already connected to another
if (mainWindowPointer_->getVoiceCallActive())
{
return;
}
voiceControlSocket_ = (TCPSocket*)socket;
QString ip;
QByteArray data;
short port = 0;
// Read the packet
QByteArray packet = voiceControlSocket_->readAll();
// Get the port
data = packet.left(2);
memcpy(&port, data, sizeof(short));
//packet = packet.right((packet.size() - 2));
// Get the ip
ip = voiceControlSocket_->getIp();
// Get the user's response
if (mainWindowPointer_->requestVoiceChat(ip))
{
// Connect signal and slot for WSA events.
udpSocket_ = new UDPSocket(mainWindowPointer_->winId());
connect(mainWindowPointer_, SIGNAL(signalWMWSASyncUDPRx(int, int)),
udpSocket_, SLOT(slotProcessWSAEvent(int, int)));
// Listen on the TCP socket for other client connections
if(!udpSocket_->listen(7000)) {
udpSocket_->listen(7001);
}
udpSocket_->open(QIODevice::ReadWrite);
udpSocket_->setDest(ip, port);
udpSocket_->moveToThread(socketThread_);
// The user wants to voice chat
AudioComponent *audio = mainWindowPointer_->getAudioPlayer();
mainWindowPointer_->setVoiceCallActive(true);
audio->startMic(udpSocket_, voiceInThread_);
voiceInThread_->start();
audio->playStream(udpSocket_, voiceOutThread_);
voiceOutThread_->start();
// Connect signals
connect(voiceControlSocket_, SIGNAL(signalSocketClosed()),
this, SLOT(endVoiceStream()));
connect(mainWindowPointer_, SIGNAL(disconnectVoiceStream()),
this, SLOT(endVoiceStreamUser()));
}
else
{
// User does not want to voice chat
socket->deleteLater();
}
}
void Workstation::requestFile(QString ip, short port, QString songPath)
{
// Create the socket
TCPSocket *requestSocket = new TCPSocket(mainWindowPointer_->winId());
connect(mainWindowPointer_, SIGNAL(signalWMWSASyncTCPRx(int,int)),
requestSocket, SLOT(slotProcessWSAEvent(int,int)));
// Connect to a remote host
if (!requestSocket->connectRemote(ip, port)) {
qDebug("Workstation::requestFile(); Failed to connect to remote.");
return;
}
requestSocket->open(QIODevice::ReadWrite);
requestSocket->moveToThread(socketThread_);
// Convert the file path into a byte array via a stream.
QByteArray byteArray;
QDataStream stream(&byteArray, QIODevice::WriteOnly);
stream << songPath;
// Create the control packet
byteArray.insert(0, FILE_TRANSFER);
byteArray.append('\n');
// Send the file path to the client
requestSocket->write(byteArray);
// Put the transfer into the current transfer map
currentTransfers.insert(requestSocket, new FileData(this, songPath, port));
// Connect the signal for receiving the file
connect(requestSocket, SIGNAL(signalDataReceived(Socket*)),
this, SLOT(receiveFileController(Socket*)));
}
/*
-- FUNCTION: requestFileList
--
-- DATE: March 21, 2011
--
-- REVISIONS: (Date and Description)
--
-- DESIGNER: Luke Queenan
--
-- PROGRAMMER: Luke Queenan
--
-- INTERFACE: void Workstation::requestFileList();
--
-- RETURNS: void
--
-- NOTES:
-- This function is triggered when the user wishes to obtain a remote file list.
-- The function establishes a connection to the remote host and sends its file
-- list. After sending the file list, it connects the signal and slot for
-- receiving the other clients file list.
*/
void Workstation::requestFileList(QString ip, short port)
{
// Create the socket
TCPSocket *requestSocket = new TCPSocket(mainWindowPointer_->winId());
connect(mainWindowPointer_, SIGNAL(signalWMWSASyncTCPRx(int,int)),
requestSocket, SLOT(slotProcessWSAEvent(int,int)));
// Connect to a remote host
if (!requestSocket->connectRemote(ip, port)) {
qDebug("Workstation::requestFileList(); Failed to connect to remote.");
return;
}
requestSocket->open(QIODevice::ReadWrite);
requestSocket->moveToThread(socketThread_);
// Get our local file list and convert it to a data stream
QStringList fileList = mainWindowPointer_->getLocalFileList();
QByteArray byteArray;
QDataStream stream(&byteArray, QIODevice::WriteOnly);
stream << fileList.toSet();
// Create the control packet
byteArray.insert(0, FILE_LIST);
short myPort = tcpSocket_->getPort();
QByteArray portArray((const char *)&myPort, sizeof(short));
byteArray.insert(1, portArray);
byteArray.append('\n');
// Send our own file list to the other client
requestSocket->write(byteArray);
// Put the socket into the current transfers map
currentTransfers.insert(requestSocket, new FileData(this,port));
// Connect the signal for receiving the other client's file list
connect(requestSocket, SIGNAL(signalDataReceived(Socket*)),
this, SLOT(requestFileListController(Socket*)));
}
/*
-- FUNCTION: processConnection
--
-- DATE: March 21, 2011
--
-- REVISIONS: (Date and Description)
--
-- DESIGNER: Luke Queenan
--
-- PROGRAMMER: Luke Queenan
--
-- INTERFACE: void Workstation::processConnection();
--
-- RETURNS: void
--
-- NOTES:
-- The function gets called when a new connection is established. It connects
-- the socket's receive signal to the workstation's decode message function.
*/
void Workstation::processConnection(Socket* socket)
{
// Connect the socket to the window's message loop.
if (!connect(mainWindowPointer_, SIGNAL(signalWMWSASyncTCPRx(int, int)),
socket, SLOT(slotProcessWSAEvent(int, int)))) {
qDebug("Workstation::processConnection(); connect(signalWMWSASyncTCPRx()) failed.");
}
if (!socket->open(QIODevice::ReadWrite)) {
qDebug("Workstation::processConnection(); could not open client socket.");
}
}
/*
-- FUNCTION: decodeControlMessage
--
-- DATE: March 21, 2011
--
-- REVISIONS: (Date and Description)
--
-- DESIGNER: Luke Queenan
--
-- PROGRAMMER: Luke Queenan
--
-- INTERFACE: void Workstation::decodeControlMessage(TCPSocket *socket);
--
-- RETURNS: void
--
-- NOTES:
-- The function gets called when the first packet from a new connection is
-- received. The function will strip the first byte from the packet and call
-- the correct corresponding transfer function to deal with the rest of the
-- transfer.
*/
void Workstation::decodeControlMessage(Socket *socket)
{
// Disconnect from decode control message
disconnect(socket, SIGNAL(signalDataReceived(Socket*)),
tcpSocket_, SIGNAL(signalDataReceived(Socket*)));
// Read and store the first byte for the control message
QByteArray messageType = socket->read(1);
// TODO: For some reason we seem to receive a bunch of '\0' chars before the
// SIGNAL(signalDataReceived()) is disconnected. The sanity check
// and return; below shouldn't be necessary. It is just a bandaid fix
// and doesn't actually solve the problem. However, at the end of the
// day, sending extra NULL data is the least of our concerns.
if (messageType.size() == 0) {
return;
}
// Check for type of message
switch (messageType.at(0))
{
case FILE_LIST:
// Connect the signal in case we receive more data
connect(socket, SIGNAL(signalDataReceived(Socket*)),
this, SLOT(receiveFileListController(Socket*)));
// Create transfer object
currentTransfers.insert(socket, new FileData());
// Call function now to deal with rest of packet
receiveFileListController(&(*socket));
break;
case FILE_TRANSFER:
// Connect the signal in case we receive more data
connect(socket, SIGNAL(signalDataReceived(Socket*)),
this, SLOT(sendFileController(Socket*)));
currentTransfers.insert(socket, new FileData);
sendFileController(&(*socket));
break;
case VOICE_CHAT:
// Call the voice chat controller
acceptVoiceChat(socket);
break;
default:
// Since the message is not recognized, close the connection
//socket->deleteLater();
break;
}
}
void Workstation::sendFileController(Socket *socket)
{
// Read the packet from the socket
QByteArray packet = socket->readAll();
// If processing is finished
if (processReceivingFileRequest(&(*socket), &packet))
{
// Disconnect this slot from the received packet signal
disconnect(socket, SIGNAL(signalDataReceived(Socket*)),
this, SLOT(sendFileController(Socket*)));
}
}
bool Workstation::processReceivingFileRequest(Socket *socket, QByteArray *packet)
{
bool isReceivingFileRequestFinished = false;
if (packet->at(packet->length() - 1) == '\n')
{
// Get rid of the newline character
packet->truncate(packet->length() - 1);
FileData *fileData = currentTransfers.value(socket);
// Append any new data to any existing data
fileData->append(*packet);
// Send the file
sendFile(socket, &(fileData->getData()));
// Remove the file transfer
// Not sure if we can do this right after sending the data, this might
// destroy the socket?
currentTransfers.remove(socket);
// Since processing of the transfer is complete, return true
isReceivingFileRequestFinished = true;
}
else
{
// Append newly received data
FileData* fileData = currentTransfers.value(socket);
fileData->append(*packet);
// Since the transfer is not yet complete, return false
isReceivingFileRequestFinished = false;
}
return isReceivingFileRequestFinished;
}
void Workstation::receiveFileController(Socket *socket)
{
// TODO: PLEASE READ ME:
// Because we use readAll() to read data from the socket, it is quite
// possible for us to read two or more packets worth of data from the
// socket. This causes a problem as this method gets called once for
// EVERY packet received. This leads to the possibility that we have
// already read the entire file from the socket and quite likely deleted
// the socket already, leaving the *socket parameter passed into this
// method a segfaulting monster! Fortunately we track our sockets in the
// currentTransfers map and can very easily validate the *socket.
if (!currentTransfers.contains(socket)) {
return;
}
// Read the packet from the socket
QByteArray packet = socket->readAll();
// TODO: PLEASE READ ME:
// This is ensuring that we haven't read a bunch of NULLs off the socket.
// (This does actually seem to occur sometimes, please see the beginning of
// decodeControlMessage() for details.
if (packet.size() == 0) {
return;
}
// If processing is finished
if(processReceivingFile(&(*socket), &packet))
{
// Disconnect this slot from the received packet signal
if (!disconnect(socket, SIGNAL(signalDataReceived(Socket*)),
this, SLOT(receiveFileController(Socket*)))) {
}
// Close the socket
socket->deleteLater();
}
}
bool Workstation::processReceivingFile(Socket* socket, QByteArray* packet)
{
bool isFileListTransferComplete = false;
// Get the file data object for this transfer
FileData *fileData = currentTransfers.value(socket);
// Check to see if we have the size of the packet yet (first packet)
if (fileData->getTotalSize() == 0)
{
// Get the first eight bytes, convert them to a long long, remove them
// from the packet and set the length in the file data
QByteArray length = packet->left(4);
int packetLength;
memcpy(&packetLength, length, sizeof(int));
*packet = packet->right(packet->size() - 4);
fileData->setTotalSize(packetLength);
connect(this, SIGNAL(signalFileProgress(int,int,QString)),
mainWindowPointer_, SLOT(downloadStarted(int, int,QString)));
}
emit signalFileProgress(fileData->getTotalSize(), packet->size(), fileData->getPath());
// Append any new data to any existing data
fileData->append(*packet);
// Check to see if we have received all the data
if (fileData->transferComplete())
{
emit signalFileProgress(fileData->getTotalSize(), fileData->getTotalSize(), fileData->getPath());
// Write all the data to disk
fileData->writeToFile();
// Remove the file transfer
currentTransfers.remove(socket);
isFileListTransferComplete = true;
}
return isFileListTransferComplete;
}
/*
-- FUNCTION: processReceivingFileList
--
-- DATE: March 21, 2011
--
-- REVISIONS: (Date and Description)
--
-- DESIGNER: Luke Queenan
--
-- PROGRAMMER: Luke Queenan
--
-- INTERFACE: bool Workstation::processReceivingFileList(TCPSocket *socket,
-- QByteArray *packet);
--
-- RETURNS: true if the transfer is complete, false if more data is expected
--
-- NOTES:
-- This function receives a file list packet and assembles the entire list. Once
-- the list has been received the function returns true, or if there is more
-- data expected, false.
*/
bool Workstation::processReceivingFileList(Socket *socket, QByteArray *packet)
{
bool isFileListTransferComplete = false;
// Get the file transfer object
FileData *fileData = currentTransfers.value(socket);
// Check to see if this is the first packet
if (fileData->getPort() == 0)
{
QByteArray portArray = packet->left(2);
short port;
memcpy(&port, portArray, sizeof(short));
*packet = packet->right(packet->size() - 2);
fileData->setPort(port);
}
// Check to see if this is the last packet
if (packet->at(packet->length() - 1) == '\n')
{
// Get rid of the newline character
packet->truncate(packet->length() - 1);
QStringList fileList;
// Append any new data to any existing data
fileData->append(*packet);
// Get all the stored data from the current transfer
QByteArray data = fileData->getData();
// Load the data into the stream
QDataStream stream(&data, QIODevice::ReadOnly);
// Stream the packet back into a QStringList
stream >> fileList;
// Send the file list to the main window for procesing
QString ip = socket->getIp();
mainWindowPointer_->appendToRemote(fileList, ip, fileData->getPort());
// Remove the file transfer
currentTransfers.remove(socket);
// Since processing of the transfer is complete, return true
isFileListTransferComplete = true;
}
else
{
// Append newly received data
fileData->append(*packet);
// Since the transfer is not yet complete, return false
isFileListTransferComplete = false;
}
return isFileListTransferComplete;
}
/*
-- FUNCTION: receiveFileListController
--
-- DATE: March 21, 2011
--
-- REVISIONS: (Date and Description)
--
-- DESIGNER: Luke Queenan
--
-- PROGRAMMER: Luke Queenan
--
-- INTERFACE: void Workstation::receiveFileListController(TCPSocket *socket);
--
-- RETURNS: void
--
-- NOTES:
-- This function is the controller function for the server side file list
-- transfer. It receives a file list and then sends its own.
*/
void Workstation::receiveFileListController(Socket *socket)
{
// Read the packet from the socket
QByteArray packet = socket->readAll();
// If processing is finished
if(processReceivingFileList(&(*socket), &packet))
{
// Disconnect this slot from the received packet signal
disconnect(socket, SIGNAL(signalDataReceived(Socket*)),
this, SLOT(receiveFileListController(Socket*)));
// Send our own file list to the other client
QStringList fileList = mainWindowPointer_->getLocalFileList();
QByteArray byteArray;
QDataStream stream(&byteArray, QIODevice::WriteOnly);
stream << fileList.toSet();
// Create the control packet
byteArray.append('\n');
// Send our own file list to the other client
socket->write(byteArray);
}
}
/*
-- FUNCTION: requestFileListController
--
-- DATE: March 21, 2011
--
-- REVISIONS: (Date and Description)
--
-- DESIGNER: Luke Queenan
--
-- PROGRAMMER: Luke Queenan
--
-- INTERFACE: void Workstation::requestFileListController(TCPSocket *socket);
--
-- RETURNS: void
--
-- NOTES:
-- This function is the controller function for the client side file list
-- transfer. It receives a file list and then disconnects.
*/
void Workstation::requestFileListController(Socket *socket)
{
// TODO: There is a possiblility that for REALLY long filelists socket will
// have been deleted. This should be investigated further.
// See receiveFileController() for details.)
if (!currentTransfers.contains(socket)) {
return;
}
// Read the packet from the socket
QByteArray packet = socket->readAll();
// If processing is finished
if(processReceivingFileList(&(*socket), &packet))
{
// Disconnect this slot from the received packet signal
disconnect(socket, SIGNAL(signalDataReceived(Socket*)),
this, SLOT(requestFileListController(Socket*)));
// Close the socket
//delete socket;
socket->deleteLater();
}
}