main.cpp Example File
ftpclient/main.cpp
#include "simpleftp.h"
#include "ftpcontrolchannel.h"
#include "ftpdatachannel.h"
#include <QCoreApplication>
#include <iostream>
struct Command {
QString cmd;
QString args;
};
int main(int argc, char *argv[])
{
if (argc != 3) {
qDebug() << "Usage: ftpclient <server> <file>";
return 1;
}
QString server = QString::fromLocal8Bit(argv[1]);
QString file = QString::fromLocal8Bit(argv[2]);
QCoreApplication app(argc, argv);
FtpClient ftpClient;
FtpDataChannel dataChannel;
FtpControlChannel controlChannel;
QObject::connect(&dataChannel, &FtpDataChannel::dataReceived, [](const QByteArray &data) {
std::cout << data.constData();
});
QObject::connect(&controlChannel, &FtpControlChannel::reply, &ftpClient,
[&ftpClient](int code, const QString ¶meters) {
ftpClient.submitEvent(QString("reply.%1xx").arg(code / 100), parameters);
});
ftpClient.connectToEvent("submit.cmd", &controlChannel,
[&controlChannel](const QScxmlEvent &event) {
controlChannel.command(event.name().mid(11).toUtf8(),
event.data().toMap()["params"].toByteArray());
});
QList<Command> commands({
{"cmd.USER", "anonymous"},
{"cmd.PORT", ""},
{"cmd.RETR", file}
});
ftpClient.connectToState("B", QScxmlStateMachine::onEntry([&]() {
if (commands.isEmpty()) {
app.quit();
return;
}
Command command = commands.takeFirst();
qDebug() << "Posting command" << command.cmd << command.args;
ftpClient.submitEvent(command.cmd, command.args);
}));
ftpClient.connectToState("P", QScxmlStateMachine::onEntry([&ftpClient]() {
qDebug() << "Sending password";
ftpClient.submitEvent("cmd.PASS", QString());
}));
controlChannel.connectToServer(server);
QObject::connect(&controlChannel, &FtpControlChannel::opened,
[&](const QHostAddress &address, int) {
dataChannel.listen(address);
commands[1].args = dataChannel.portspec();
ftpClient.start();
});
return app.exec();
}