This repository has been archived by the owner on Feb 3, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
senderworker.cpp
executable file
·161 lines (141 loc) · 4.73 KB
/
senderworker.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
#include "senderworker.h"
SenderWorker::SenderWorker(QObject *parent) :
QThread(parent)
{
}
SenderWorker::SenderWorker(InetSockAddr & serverAddr,InetSockAddr& clientAddr,
QString& fileName, QString& mode)
{
this->localAddr = new InetSockAddr(serverAddr);
localAddr->setPort(0); // Se hará bind a un puerto aleatorio.
this->dstAddr = new InetSockAddr(clientAddr);
try{
socketCliente = new SocketUDP();
socketCliente->bind(*localAddr);
}catch(int e){
qDebug() << "SenderWorker::Error starting socket.";
}
this->fileName = fileName;
this->openMode = mode;
this->monitor = new SocketMonitor(); // Agregando socket al monitor.
monitor->set(*socketCliente, READ);
this->remintentAddr = new InetSockAddr(); // Dirección del remitente.
stop = false;
}
void SenderWorker::run()
{
if(wMode == CLIENT){
timeout.tv_sec=3;
timeout.tv_usec=0;
TFTPCommon::sendWRQ(*socketCliente, fileName.toLocal8Bit().data(),
OCTET, *dstAddr);
monitor->select(&timeout);
if(monitor->isSet(*socketCliente, READ)){
socketCliente->recvFrom(buff_in, 600, *dstAddr);
qDebug() << "new Server dst: " << dstAddr->toQString();
putFile();
}else{
qDebug() <<"No response for request...";
socketCliente->close();
monitor->zeroSet(READ);
return;
}
}else{
putFile();
}
emit clienteAtendido(RRQ);
return;
}
void SenderWorker::setWriteMode(writerMonede mode)
{
this->wMode = mode;
}
void SenderWorker::requesStop()
{
this->stop = true;
}
void SenderWorker::putFile()
{
int cBlockNum = 1, data_size = -1, data_sent, retransmit = 0;
timeval timeout;
bool last_sent = false;
std::fstream file;
if(openMode == "octet"){
file.open(fileName.toLocal8Bit().data(), std::ios_base::binary | std::ios_base::in);
}else if(openMode == "netascii"){
file.open(fileName.toLocal8Bit().data());
}
qDebug() << "Opened: " << fileName
<< " in: " << openMode << "mode.";
memset(buff_out, 0, 512);
while(1){
if(stop){
qDebug() << "Stopping transfer";
break;
}
timeout.tv_sec=3;
timeout.tv_usec=0;
if(retransmit == 0){
file.read((char*)buff_out,512);
data_size = file.gcount();
qDebug() << data_size << "bytes read from file.";
if(data_size < 512){
data_size -= (data_size==0)?0:1;
last_sent = true;
}
}
// Send block.
try{
data_sent = TFTPCommon::sendData(*socketCliente, cBlockNum, buff_out,
data_size, *dstAddr);
qDebug() << data_sent << "bytes sent to " << dstAddr->toQString();
}catch(int e){
qDebug() << "Error sending block " << cBlockNum
<< "to: " << dstAddr->toQString();
break;
}
// Listen for ACK.
try{
qDebug() << "Waiting for ACK #" << cBlockNum;
monitor->select(&timeout);
}catch(int e){
qDebug() << "Error waiting for ACK...";
break;
}
if(monitor->isSet(*socketCliente,READ)){
qDebug() << "==> sockCliente set for ACK";
socketCliente->recvFrom(buff_in, 600, *remintentAddr);
if(!(*remintentAddr == *dstAddr)){ // Verifica que sea el mismo cliente.
// send error.
qDebug() << "Source and dst address does not match."
<< remintentAddr->toQString() << "\n" << dstAddr->toQString();
break;
}
if(buff_in[0] == 0 && buff_in[1] == 4){ // ACK?
qDebug() << "ACK recieved, checking block num...";
if(cBlockNum == ntohs(*((uint16_t*)(buff_in+2)))){
qDebug() << "Block matches.." << ntohs(*((uint16_t*)(buff_in+2)));
cBlockNum += 1;
retransmit = 0;
if(last_sent){
qDebug() << "Archivo " << fileName
<< "enviado!.";
break;
}
continue;
}
}
}else{
retransmit += 1;
qDebug() << "retransmitiendo bloque:" << cBlockNum;
}
if(retransmit > 6){
qDebug() << "Número máximo de retransmisiones alcanzado";
break;
}
// Check if sender does not ACK,
}
socketCliente->close();
monitor->zeroSet(READ);
file.close();
}