//myserver.h
#ifndef MYSERVER_H
#define MYSERVER_H
#include <QTcpServer>
#include <fstream>
typedef std::vector<double> vect;
class MyServer : public QTcpServer
{
Q_OBJECT
public:
explicit MyServer(QObject *parent = 0);
void startServer();
void Generatemtx(std::vector<vect>* newmtx, int size);
signals:
public slots:
void JobIsDone();
protected:
void incomingConnection(qintptr socketDescriptor);
int *currentNum;
int size = 10;
std::vector<vect> fstmtx, scndmtx;
std::vector<QByteArray> finalmtx;
};
#endif // MYSERVER_H
//mythread.h
#ifndef MYTHREAD_H
#define MYTHREAD_H
#include <QThread>
#include <QTcpSocket>
#include <QDebug>
typedef std::vector<double> vect;
class MyThread : public QThread
{
Q_OBJECT
public:
explicit MyThread(std::vector<vect>* fst,std::vector<vect>* scnd,std::vector<QByteArray>* final,int *_currentNum,int, qintptr ID, QObject *parent = 0);
void run();
int* currentNum;
int size;
int* globalcounter;
std::vector<vect>* fstmtx;
std::vector<vect>* scndmtx;
std::vector<QByteArray>* finalmtx;
signals:
void error(QTcpSocket::SocketError socketerror);
void JobIsDone();
public slots:
void readyRead();
void disconnected();
private:
QTcpSocket *socket;
qintptr socketDescriptor;
};
#endif // MYTHREAD_H
//main.cpp
#include <QCoreApplication>
#include "myserver.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
// Make a server and starts it
MyServer server;
server.startServer();
return a.exec();
}
//myserver.cpp
#include "myserver.h"
#include "mythread.h"
MyServer::MyServer(QObject *parent) :
QTcpServer(parent)
{
currentNum = new int;
*currentNum = 0;
Generatemtx(&fstmtx,size);
Generatemtx(&scndmtx,size);
for(int i=0; i<size; i++)
finalmtx.push_back(*(new QByteArray));
//Generatemtx(&finalmtx,size);
}
void MyServer::startServer()
{
int port = 1234;
if(!this->listen(QHostAddress::Any, port))
{
qDebug() << "Could not start server";
}
else
{
qDebug() << "Listening to port " << port << "...";
}
}
// This function is called by QTcpServer when a new connection is available.
void MyServer::incomingConnection(qintptr socketDescriptor)
{
// We have a new connection
qDebug() << socketDescriptor << " Connecting...";
// Every new connection will be run in a newly created thread
MyThread *thread = new MyThread(&fstmtx,&scndmtx,&finalmtx,currentNum, size, socketDescriptor, this);
// connect signal/slot
// once a thread is not needed, it will be beleted later
connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
connect(thread,SIGNAL(JobIsDone()),this,SLOT(JobIsDone()));
thread->start();
}
void MyServer::Generatemtx(std::vector<vect>* newmtx, int size1)
{
double newnum = 0;
std::vector<double> newvec;
//newnum = rand()%100;
for(int i=0; i<size1; i++){
for(int j=0; j<size1; j++){
newnum = rand()%10;
newvec.push_back(newnum);
}
(*newmtx).push_back(newvec);
newvec.clear();
}
}
void MyServer::JobIsDone()
{
// for(unsigned int i; i<thread)
this->close();
//std::ofstream ofs ("test.txt",std::ios::out);
qDebug()<<"Job is done.Server stopped.";
for (unsigned int i=0;i<finalmtx.size();i++)
qDebug()<<finalmtx[i];
}
//mythread.cpp
#include "mythread.h"
MyThread::MyThread(std::vector<vect>* fst,std::vector<vect>* scnd,std::vector<QByteArray>* final,int* _currentNum, int _size, qintptr ID, QObject *parent ) :
QThread(parent)
{
this->socketDescriptor = ID;
currentNum = _currentNum;
fstmtx = fst;
scndmtx = scnd;
finalmtx = final;
size = _size;
globalcounter = new int;
*globalcounter = 0;
}
void MyThread::run()
{
// thread starts here
qDebug() << " Thread started";
socket = new QTcpSocket();
// set the ID
if(!socket->setSocketDescriptor(this->socketDescriptor))
{
// something's wrong, we just emit a signal
emit error(socket->error());
return;
}
// connect socket and signal
// note - Qt::DirectConnection is used because it's multithreaded
// This makes the slot to be invoked immediately, when the signal is emitted.
connect(socket, SIGNAL(readyRead()), this, SLOT(readyRead()), Qt::DirectConnection);
connect(socket, SIGNAL(disconnected()), this, SLOT(disconnected()));
// connect(this, SIGNAL(JobIsDone()), parent, SLOT(JobIsDone()), Qt::DirectConnection);
// We'll have multiple clients, we want to know which is which
qDebug() << socketDescriptor << " Client connected";
// make this thread a loop,
// thread will stay alive so that signal/slot to function properly
// not dropped out in the middle when thread dies
exec();
}
void MyThread::readyRead()
{
// get the information
//socket->waitForReadyRead(3000);
QByteArray Data = socket->readAll();
int Index = 0;
// QString Data;
if (Data.indexOf("Index:")!=-1)
{
bool ok;
Index = Data.mid(6,Data.indexOf("Vector:")-6).toInt(&ok, 10);
(*finalmtx)[Index] = Data.mid(Data.indexOf("Vector:")+7,Data.size()-Data.indexOf("Vector:")-16);
qDebug()<<(*finalmtx)[Index];
}
QString Data2;
// will write on server side window
qDebug() << socketDescriptor << " Data in: " << Data;
QTextStream os(socket);
os.setAutoDetectUnicode(true);
//os>>Data;
//os.setFieldAlignment(QTextStream::AlignRight);
if((*currentNum)<size){
os<<"Second:";
for(unsigned int i=0; i<(*scndmtx).size();i++)
{
for(unsigned int j=0; j<(*scndmtx).size();j++)
os<< ((*scndmtx)[i][j]);
os<<"_";
}
os<<"First:";
for(unsigned int i=0; i<(*fstmtx).size();i++)
os<<((*scndmtx)[*currentNum][i]);
os<<"Info: "<<(*currentNum);
socket->waitForBytesWritten(2000);
(*currentNum)++;
}
else{
os<<"Job is done!";
os.flush();
socket->waitForBytesWritten(1000);
socket->close();
//this->JobIsDone();
}
if((*globalcounter)==size-1)
this->JobIsDone();
(*globalcounter)++;
//qDebug<<"Counter "<<(*globalcounter);
//socket->write("Hello!!!");
}
void MyThread::disconnected()
{
qDebug() << socketDescriptor << " Disconnected";
socket->deleteLater();
exit(0);
}
//void MyThread::JobIsDone()
//{
//}