Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Brainstorming: Detecting network state #348

Open
jarolrod opened this issue Jun 6, 2023 · 4 comments
Open

Brainstorming: Detecting network state #348

jarolrod opened this issue Jun 6, 2023 · 4 comments
Labels
question Further information is requested

Comments

@jarolrod
Copy link
Member

jarolrod commented Jun 6, 2023

We need to have the ability to detect network state to allow for states such as the following (specified in the design file):

Paused: No Wifi Peers: No Internet
Mobile Node - Peers

We have two options to go down through, at least with platforms aside from android.

1. Enable the Qt Network API

This gives us access to the QNetworkConfigurationManager class. Here we have access to the following signals and functions that give us the information we need:

It should be noted that with these functions, Qt checks that the computer is connected to any signal, and not if that signal really has an internet connection.

2. Use system specific terminal commands in a Process

For example, on macOS, this could be something like the following:

#include <QProcess>

class NetworkManager : public QObject
{
    Q_OBJECT
    Q_PROPERTY(bool isOnline READ isOnline NOTIFY isOnlineChanged)

public:
    explicit NetworkManager(QObject *parent = nullptr)
        : QObject(parent), m_isOnline(false)
    {
        checkNetworkAccess();
    }

    bool isOnline() const { return m_isOnline; }

public slots:
    void checkNetworkAccess() {
        QProcess process;
        process.start("scutil --nwi");
        process.waitForFinished();

        QString output = process.readAllStandardOutput();

        // check if output contains 'No network connection'
        m_isOnline = !output.contains("No network");

        emit isOnlineChanged(m_isOnline);
    }

signals:
    void isOnlineChanged(bool isOnline);

private:
    bool m_isOnline;
};

There should be available commands for macOS, linux, and Windows; but this approach won't work for Android, where we'll just plug into some android api for this information.

@jarolrod
Copy link
Member Author

jarolrod commented Jun 6, 2023

cc @johnny9 @hebasto @promag

@jarolrod jarolrod added the question Further information is requested label Jun 6, 2023
@jarolrod
Copy link
Member Author

jarolrod commented Jun 6, 2023

related: #324

@johnny9
Copy link
Contributor

johnny9 commented Jun 10, 2023

We can try out the QNetworkInterface class https://doc.qt.io/qt-5/qnetworkinterface.html with the QNetwork module. Qt 6.1 has QNetworkInformation https://doc.qt.io/qt-6/qnetworkinformation.html. I think we should avoid trying to implement platform specific network tests ourselves.

@TheCompez
Copy link

Hi!

We have some limitations for QProcess in some platforms such as Android and iOS.
Have you tried the ping method?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants