-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOutgoing.cpp
91 lines (70 loc) · 2.29 KB
/
Outgoing.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
#include "Outgoing.h"
Outgoing::Outgoing(QQueue <struct Cell>*pool,QSemaphore *geneblocker)
{
genepool = pool;
genepoolblocker = geneblocker;
hosts = new QList <struct s_host>();
//addHost("127.0.0.1",5443);
QTimer::singleShot(INTERVAL, this, SLOT(transfer()));
timeoutTimer = new QTimer();
connect(timeoutTimer,SIGNAL(timeout()),this,SLOT(socketTimeout()));
}
Outgoing::~Outgoing()
{
}
void Outgoing::transfer(){
socket = new QTcpSocket();
if(!hosts->size()){
QTimer::singleShot(INTERVAL, this, SLOT(transfer()));
return;
}
int target = qrand() % hosts->size();
connect(socket,SIGNAL(readyRead()),this,SLOT(socketReadyRead()));
socket->connectToHost(hosts->at(target).name, hosts->at(target).port, QIODevice::ReadOnly);
//use a timeout timer to know for your timeout limit
this->timeoutTimer->start(2000);
}
//a new slot for ready Read
void Outgoing::socketReadyRead()
{
timeoutTimer->stop();
qDebug() << "starting transfer";
quint32 number = 55;
QDataStream in(socket);
in >> number;
qDebug() << "receiving" << number << "cells";
for(uint i = 0; i < number; i++){
struct Cell cell;
int read = in.readRawData((char *)&cell,sizeof(cell));
if(read == sizeof(cell)){
qDebug() << "got cell" << cell.id << cell.generation;
genepoolblocker->acquire(1);
genepool->enqueue(cell);
genepoolblocker->release(1);
}
}
socket->close();
delete socket;
QTimer::singleShot(INTERVAL, this, SLOT(transfer()));
}
//a new slot for timeout
void Outgoing::socketTimeout ()
{
qDebug() << "could not connect to distant pond";
socket->close();
timeoutTimer->stop();
delete socket;
QTimer::singleShot(INTERVAL, this, SLOT(transfer()));
}
void Outgoing::deleteHost(int index){
hosts->removeAt(index);
}
void Outgoing::addHost(QString host, quint16 port){
struct s_host newHost;
newHost.name = host;
newHost.port = port;
hosts->append(newHost);
}
struct s_host Outgoing::getHost(int index){
return hosts->at(index);
}