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
/
tftpserver.cpp
executable file
·137 lines (126 loc) · 3.85 KB
/
tftpserver.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
#include "tftpserver.h"
TFTPServer::TFTPServer(QObject *parent):
TFTPCommon(parent)
{
this->currentDir = new QString("/Users/Lalo/Documents/Develop/Redes/TFTPTest/");
this->running = false;
this->cListener = NULL;
}
TFTPServer::TFTPServer(QString ip, qint16 port, QObject * parent):
TFTPCommon(parent)
{
this->currentDir = new QString("/Users/Lalo/Documents/Develop/Redes/TFTPTest/");
this->serverAddr = new InetSockAddr(ip, port);
this->running = false;
this->cListener = NULL;
}
TFTPServer::~TFTPServer()
{
this->stop();
delete cListener;
delete sockServer;
}
void TFTPServer::start()
{
try{
sockServer = new SocketUDP();
}catch(int e){
qDebug() << "TFTPServer::Error creating listener";
throw e;
}
try{
sockServer->bind(*serverAddr);
}catch(int e){
qDebug() << "TFTPServer::Error binding server socket";
this->stop();
throw e;
}
qDebug() << "TFTPServer::=>Starting server...";
try{
cListener = new Listener(sockServer);
connect(cListener, SIGNAL(nuevaPeticion(rqCode,QString,qint16,QString,QString)),
this, SLOT(atenderCliente(rqCode,QString,qint16,QString,QString)));
}catch(int e){
qDebug() << "TFTPServer::start(): Error: couldn't allocate Listener.";
this->stop();
throw e;
}
try{
cListener->start();
running = true;
}catch(int e){
qDebug() << "TFTPServer::start(): Error: couldn't start Listener.";
this->stop();
throw e;
}
}
void TFTPServer::stop()
{
qDebug() << "=>Stopping server...";
running = false;
if(cListener!=NULL){
disconnect(cListener,SIGNAL(nuevaPeticion(rqCode,QString,qint16,QString,QString)),
this, SLOT(atenderCliente(rqCode,QString,qint16,QString,QString)));
cListener->requestStop();
}
sockServer->close();
sockServer = NULL;
}
bool TFTPServer::isRunning()
{
return running;
}
void TFTPServer::setRunning(bool run)
{
this->running = run;
}
void TFTPServer::atenderCliente(rqCode code, QString ip,
qint16 port, QString fileName,
QString mode)
{
InetSockAddr * cAddr = new InetSockAddr(ip,port);
qDebug() << "TFTPServer::Atender: " << code << " en " << cAddr->toQString();
switch(code){
case RRQ:
try{
QString finalPath = (*currentDir + fileName);
sWorker = new SenderWorker(*serverAddr,*cAddr, finalPath, mode);
connect(sWorker, SIGNAL(clienteAtendido(rqCode)),
this, SLOT(clienteAtendido(rqCode)));
sWorker->setWriteMode(SERVER);
cListener->setPriority(QThread::LowestPriority);
sWorker->start();
}catch(int e){
qDebug() << "TFTP::Server Error starting Send worker";
}
break;
case WRQ:
try{
QString finalPath = (*currentDir + fileName);
wWorker = new WriterWorker(*serverAddr, *cAddr, finalPath, mode);
wWorker -> setMode(SERVER);
connect(wWorker, SIGNAL(clienteAtendido(rqCode)),
this, SLOT(clienteAtendido(rqCode)));
cListener->setPriority(QThread::LowestPriority);
wWorker->start();
}catch(int e){
qDebug() << "TFTP::Server Error starting Writer worker";
}
break;
}
}
void TFTPServer::clienteAtendido(rqCode code)
{
switch (code){
case RRQ:
disconnect(sWorker, SIGNAL(clienteAtendido(rqCode)),
this, SLOT(clienteAtendido(rqCode)));
break;
case WRQ:
disconnect(wWorker, SIGNAL(clienteAtendido(rqCode)),
this, SLOT(clienteAtendido(rqCode)));
break;
}
cListener->setPriority(QThread::InheritPriority);
cListener->setAtendido(true);
}