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
/
writerworker.cpp
executable file
·186 lines (162 loc) · 5.58 KB
/
writerworker.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
#include "writerworker.h"
WriterWorker::WriterWorker(QObject *parent) :
QThread(parent)
{
fromAddr = new InetSockAddr();
stop = false;
}
WriterWorker::WriterWorker(InetSockAddr &serverAddr, InetSockAddr &clientAddr,
QString &fileName, QString & mode)
{
this->localAddr = new InetSockAddr(serverAddr);
localAddr->setPort(0);
this->dstAddr = new InetSockAddr(clientAddr);
try{
sockCliente = new SocketUDP();
sockCliente->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(*sockCliente, READ);
this->fromAddr = new InetSockAddr(); // Dirección del remitente.
stop = false;
}
void WriterWorker::run()
{
if(openMode == "octet"){
file.open(fileName.toLocal8Bit().data(), std::ios_base::binary | std::ios_base::out);
}else if(openMode == "netascii"){
file.open(fileName.toLocal8Bit().data());
}
if(file.is_open()){
qDebug() << "Opened: " << fileName
<< " in: " << openMode << "mode.";
if(writeMod == CLIENT){
timeout.tv_sec=6;
timeout.tv_usec=0;
TFTPCommon::sendRRQ(*sockCliente, fileName.toLocal8Bit().data(), OCTET, *dstAddr);
monitor->select(&timeout);
if(monitor->isSet(*sockCliente, READ)){
qDebug() << "Socket is set...";
int data_size = sockCliente->recvFrom(buff_in, 600, *dstAddr); // Overwrites 69 port and ip.
switch(ntohs(*(uint16_t*)(buff_in))){
// if(buff_in[0] == 0 && buff_in[1] == 3){ // DATA?
case 3: //DATA?
qDebug() << "DATA recieved, checking block num...";
if(1 == ntohs(*((uint16_t*)(buff_in+2)))){
qDebug() << "Block matches # " << ntohs(*((uint16_t*)(buff_in+2)));
file.write((char*)(buff_in+4), (data_size-4));
}else{
qDebug() << "Block does not match, recv # " << ntohs(*((uint16_t*)(buff_in+2)));
}
break;
case 5:
qDebug() << "Error recieved: " << QString(QLatin1String((char*)(buff_in+4)));
freeResources();
return;
break;
}
}else{
qDebug() <<"No response for request...";
freeResources();
return;
}
}
getFile();
}else{
qDebug() << "Could not open " << fileName << "for writting";
freeResources();
return;
}
emit clienteAtendido(WRQ);
return;
}
void WriterWorker::setMode(writerMonede mode)
{
this->writeMod = mode;
}
void WriterWorker::requestStop()
{
this->stop = true;
}
void WriterWorker::getFile()
{
int cBlockNum = (writeMod==SERVER)?0:1,
data_size, wait=0;
bool last_recv = false, error=false;
qDebug() << "Recibiendo paquetes... ";
while(1){
if(stop){
qDebug() << "Stopping transfer";
break;
}
timeout.tv_sec=3;
timeout.tv_usec=0;
if(error){
break;
}
//if(wait == 0){
qDebug() << "Sending ACK for block #:" << cBlockNum;
TFTPCommon::sendACK(*sockCliente, cBlockNum, *dstAddr);
if(last_recv){
qDebug() << "Archivo " << fileName
<< "recibido!.";
break;
}
//}
try{
qDebug() << "Waiting for DATA block #" << cBlockNum+1;
monitor->select(&timeout);
}catch(int e){
qDebug() << "Error waiting for ACK...";
return;
}
if(monitor->isSet(*sockCliente, READ)){
data_size = sockCliente->recvFrom(buff_in, 600, *fromAddr);
if(data_size < 516){
last_recv = true;
}
if(!(*fromAddr == *dstAddr)){
qDebug() << "WriterWorkerx =>Source and dst address does not match."
<< fromAddr->toQString() << "\n" << dstAddr->toQString();
break;
}
switch(ntohs(*(uint16_t*)(buff_in))){
// if(buff_in[0] == 0 && buff_in[1] == 3){ // DATA?
case 3: //DATA?
qDebug() << "DATA recieved, checking block num...";
if((cBlockNum +1) == ntohs(*((uint16_t*)(buff_in+2)))){
qDebug() << "Block matches # " << ntohs(*((uint16_t*)(buff_in+2)));
file.write((char*)(buff_in+4), (data_size-4));
cBlockNum += 1;
wait = 0;
continue;
}else{
qDebug() << "Block does not match, recv # " << ntohs(*((uint16_t*)(buff_in+2)));
}
break;
case 5:
qDebug() << "Error recieved: " << QString(QLatin1String((char*)(buff_in+4)));
error = true;
break;
}
}else{
wait += 1;
qDebug() << "=> Retransmisión: " << wait << " de ACK:" << cBlockNum;
}
if(wait > 10){
qDebug() << "x=> WriteWorker: Número máximo de timeout alcanzado";
break;
}
}
freeResources();
}
void WriterWorker::freeResources()
{
sockCliente->close();
monitor->zeroSet(READ);
file.close();
}