SCXML FTP Client

使用状态机实现简单 FTP 客户端。

FTP Client uses Qt SCXML to implement a FTP client that can communicate with a FTP service by sending FTP control messages translated from state machine events and by translating server replies into state machine events. The data received from the FTP server is printed on the console.

RFC 959 specifies state charts for the command handling of the FTP client. They can be easily translated into SCXML to benefit from SCXML nested states. Connections between the client and server and data transfer are implemented by using C++. In addition, Qt signals and slots are used.

状态机拥有以下状态:

  • I as the initial state.
  • B for sending commands.
  • S for success.
  • F 对于故障。
  • W for waiting for a reply.
  • P for supplying a password upon server request.

The state machine is specified in the simpleftp.scxml file and compiled into the FtpClient class that implements the logic of the FTP protocol. It reacts to user input and to replies from the control channel by changing states and sending external events. In addition, we implement a FtpControlChannel class and a FtpDataChannel class that handle TCP sockets and servers and convert line endings.

运行范例

要运行范例从 Qt Creator ,打开 Welcome 模式,然后选择范例从 Examples 。更多信息,拜访 构建和运行范例 .

编译状态机

We link against the Qt SCXML module by adding the following line to the project build files.

With qmake, we add the following to ftpclient.pro

QT = core scxml network
					

接着,指定要编译的状态机:

STATECHARTS += simpleftp.scxml
					

With CMake, we add the following to CMakeLists.txt

find_package(Qt6 REQUIRED COMPONENTS Core Network Scxml)
target_link_libraries(ftpclient PRIVATE
    Qt6::Core
    Qt6::Network
    Qt6::Scxml
)
					

接着,指定要编译的状态机:

qt6_add_statecharts(ftpclient
    simpleftp.scxml
)
					

Qt SCXML 编译器 qscxmlc , is run automatically to generate simpleftp.h and simpleftp.cpp , and to add them appropriately to the project as headers and sources.

实例化状态机

We instantiate the generated FtpClient class, as well as the FtpDataChannel and FtpControlChannel 类在 main.cpp 文件:

#include "ftpcontrolchannel.h"
#include "ftpdatachannel.h"
#include "simpleftp.h"
    ...
int main(int argc, char *argv[])
{
    ...
    QCoreApplication app(argc, argv);
    FtpClient ftpClient;
    FtpDataChannel dataChannel;
    FtpControlChannel controlChannel;
    ...
					

与 FTP 服务器通信

We print all data retrieved from the server on the console:

    QObject::connect(&dataChannel, &FtpDataChannel::dataReceived,
                     [](const QByteArray &data) {
        std::cout << data.constData() << std::flush;
    });
					

We translate server replies into state machine events:

    QObject::connect(&controlChannel, &FtpControlChannel::reply, &ftpClient,
                     [&ftpClient](int code, const QString &parameters) {
        ftpClient.submitEvent(QString("reply.%1xx")
                              .arg(code / 100), parameters);
    });
					

We translate commands from the state machine into FTP control messages:

    ftpClient.connectToEvent("submit.cmd", &controlChannel,
                             [&controlChannel](const QScxmlEvent &event) {
        controlChannel.command(event.name().mid(11).toUtf8(),
                               event.data().toMap()["params"].toByteArray());
    });
					

We send commands to log into the FTP server as an anonymous user, to announce a port for the data connection, and to retrieve a file:

    QList<Command> commands({
        {"cmd.USER", "anonymous"},// login
        {"cmd.PORT", ""},         // announce port for data connection,
                                  // args added below.
        {"cmd.RETR", file}        // retrieve a file
    });
					

We specify that the FTP client should send the next command when entering the B state:

    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);
    }));
					

We specify that the FTP client should send an empty string as a password if the server asks for one:

    ftpClient.connectToState("P", QScxmlStateMachine::onEntry([&ftpClient]() {
        qDebug() << "Sending password";
        ftpClient.submitEvent("cmd.PASS", QString());
    }));
					

Finally, we connect to the FTP server specified as the first argument of the method and retrieve the file specified as the second argument:

    controlChannel.connectToServer(server);
    QObject::connect(&controlChannel, &FtpControlChannel::opened, &dataChannel,
                     [&](const QHostAddress &address, int) {
        dataChannel.listen(address);
        commands[1].args = dataChannel.portspec();
        ftpClient.start();
    });
					

For example, the following invocation prints the specified file from the specified server: ftpclient <server> <file> .

范例工程 @ code.qt.io