#include "serverthread.h"
#include <QtNetwork>
#include <iostream>
#include <iomanip>
#include "messages.h"
#include "server.h"
#include <QList>
#include <fstream>
using namespace std;
ServerThread
::ServerThread(int socketDescriptor,
QObject *parent
) : QThread(parent
) { blockSize = 0;
tcpSocket->setSocketDescriptor(socketDescriptor);
this->remote_host_info.hostname = tcpSocket->peerName().toStdString();
this->remote_host_info.address = tcpSocket->peerAddress().toString().toStdString();
this->remote_host_info.port = tcpSocket->peerPort();
connect(tcpSocket, SIGNAL(connected()), this, SLOT(onConnected()));
connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(onReadyRead()));
connect(tcpSocket, SIGNAL(disconnected()), this, SLOT(onDisconnected()));
cout << "Host connected (" << this->remote_host_info.address << ":" << this->remote_host_info.port;
cout << (this->remote_host_info.hostname.empty() ? "" : "[" + this->remote_host_info.hostname + "]") << ")" << endl;
// emit sendMessage(GD_MSG_SERVER_VERSION, "Game Rule Server Version 1.0");
QMap<QString, QVariant> val;
val.insert(GD_MSG_SERVER_NAME, "File Transfer Server");
val.insert(GD_MSG_SERVER_VERSION, "1.0");
#if defined(Q_WS_X11)
val.insert(GD_MSG_SERVER_OPERATING_SYSTEM, "Linux");
#elif defined(Q_WS_WIN)
val.insert(GD_MSG_SERVER_OPERATING_SYSTEM, "Microsoft Windows");
#elif defined(Q_WS_MACX)
val.insert(GD_MSG_SERVER_OPERATING_SYSTEM, "Mac OS");
#endif
val.
insert(GD_TCP_LOCAL_HOSTNAME,
QHostInfo::localHostName());
val.insert(GD_TCP_LOCAL_ADDRESS, tcpSocket->localAddress().toString());
val.insert(GD_TCP_LOCAL_PORT, tcpSocket->localPort());
val.insert(GD_TCP_PEER_NAME, tcpSocket->peerName());
val.insert(GD_TCP_PEER_ADDRESS, tcpSocket->peerAddress().toString());
val.insert(GD_TCP_PEER_PORT, tcpSocket->peerPort());
emit sendMessage(GD_RETR_WELCOME_STRING, val);
}
ServerThread::~ServerThread() {
}
void ServerThread::run() {
while (tcpSocket
->state
() == QTcpSocket::ConnectedState) { if (tcpSocket->canReadLine()) {
cout << "readClient();" << endl;
}
sleep(1);
}
tcpSocket->disconnectFromHost();
cout << "Host diconnected (" << this->remote_host_info.address << ":" << this->remote_host_info.port;
cout << (this->remote_host_info.hostname.empty() ? "" : "[" + this->remote_host_info.hostname + "]") << ")" << endl;
tcpSocket->waitForDisconnected();
}
void ServerThread::onDisconnected() {
QList<QString> str_list = reinterpret_cast<Server*>(this->parent())->getData(tcpSocket->peerAddress());
cout << "FILE_LIST >> " << endl;
for(QList<QString>::iterator iter = str_list.begin();
iter != str_list.end(); ++iter) {
cout << (*iter).toStdString() << endl;
}
cout << "<< FILE_LIST " << endl;
QMap<QString, QList<QString> > file_list = reinterpret_cast<Server*>(this->parent())->getData();
cout << "FILE_LIST from Host>> " << endl;
for(QMap<QString, QList<QString> >::iterator hiter = file_list.begin(); hiter != file_list.end(); ++hiter) {
// QHostAddress ha = hiter.key();
QList<QString> list = hiter.value();
cout << "Host: " << addr.toStdString() << endl;//ha.toString().toStdString() << endl;
for(QList<QString>::iterator iter = list.begin(); iter != list.end(); ++iter) {
cout << (*iter).toStdString() << endl;
}
}
cout << "<< FILE_LIST " << endl;
}
void ServerThread::onConnected() {
}
cout << endl;
cout << std::setw(30) << std::setfill('-') << "MESSAGE BEGIN" << std::setw(30) << std::setfill('-') << "";
cout.flush();
cout << endl;
cout << "Received data......" << endl;
cout << msg.toStdString() << endl;;
cout << " MESSAGE: " << msg.toStdString() << endl;
cout << " DATA : " << data.toString().toStdString() << endl;
if(msg == GD_RETR_WELCOME_STRING) {
//}
}
else if(msg == GD_FILE) {
//mutex.lock();
QMap<QString, QVariant> val;
val = data.toMap();
qint64 chunk = 0;
qint64 size = 0;
char *data = NULL;
if(val.find("MD5SUM") != val.end())
md5sum = val.value("MD5SUM").toString();
if(val.find("CHUNK") != val.end())
chunk = val.value("CHUNK").toInt();
if(val.find("SIZE") != val.end())
size = val.value("SIZE").toInt();
if(val.find("FILENAME_NEW") != val.end())
filename = val.value("FILENAME_NEW").toString();
if(val.find("CONTENT") != val.end())
byteArray = val.value("CONTENT").toByteArray();
cout << "MD5SUM: " << md5sum.toStdString() << endl;
cout << "FILENAME: " << filename.toStdString() << endl;
cout << "CHUNK: " << chunk << endl;
cout << "SIZE: " << size << endl;
dir.mkdir(new_dir);
if(size > 0) {
//QMessageBox::information(this, "File1", new_dir);
//QMessageBox::information(this, "File", new_dir);
cout << "Can't write to file '" << filename.toStdString() << "'" << endl;
// QMessageBox::critical(this, "Can't write to file", new_dir);
file.seek(chunk);
file.write(byteArray.data(), size);
file.close();
}
}
}
}
if(tcpSocket == NULL) return;
//out.setVersion( out.version() ); // set to the current Qt version
//out << (quint16) 0;
out << (quint32) 0;
out << msg;
out << data;
out.device()->seek(0);
out << (quint32)(block.size() - sizeof(quint32));
//out << (quint16)(block.size() - sizeof(quint16));
tcpSocket->write(block);
tcpSocket->flush();
cout << "Message Sent: " << msg.toStdString() << endl;
}
void ServerThread::onReadyRead() {
if (tcpSocket == NULL) return;
//in.setVersion( in.version() ); // set to the current Qt version instead
if (blockSize == 0) {
if (tcpSocket->bytesAvailable() < (int)sizeof(quint32)) return;
in >> blockSize;
reading_message = true;
}
if (tcpSocket->bytesAvailable() < blockSize) return;
in >> msgString;
in >> msgData;
onMessage( msgString, msgData );
reading_message = false;
blockSize = 0;
if (tcpSocket->bytesAvailable() > 0) onReadyRead();
}
void ServerThread
::displayError(QAbstractSocket::SocketError socketError
) { switch(socketError) {
default:
cerr <<
QString("The following error occurred: %1.").
arg(tcpSocket->errorString()).toStdString() << endl;
}
}